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/slab.h>
19 #include <linux/hash.h>
20 #include <linux/sysfs.h>
21 #include <linux/dcache.h>
22 #include <linux/percpu.h>
23 #include <linux/ptrace.h>
24 #include <linux/vmstat.h>
25 #include <linux/vmalloc.h>
26 #include <linux/hardirq.h>
27 #include <linux/rculist.h>
28 #include <linux/uaccess.h>
29 #include <linux/syscalls.h>
30 #include <linux/anon_inodes.h>
31 #include <linux/kernel_stat.h>
32 #include <linux/perf_event.h>
33 #include <linux/ftrace_event.h>
35 #include <asm/irq_regs.h>
37 atomic_t perf_task_events __read_mostly
;
38 static atomic_t nr_mmap_events __read_mostly
;
39 static atomic_t nr_comm_events __read_mostly
;
40 static atomic_t nr_task_events __read_mostly
;
42 static LIST_HEAD(pmus
);
43 static DEFINE_MUTEX(pmus_lock
);
44 static struct srcu_struct pmus_srcu
;
47 * perf event paranoia level:
48 * -1 - not paranoid at all
49 * 0 - disallow raw tracepoint access for unpriv
50 * 1 - disallow cpu events for unpriv
51 * 2 - disallow kernel profiling for unpriv
53 int sysctl_perf_event_paranoid __read_mostly
= 1;
55 int sysctl_perf_event_mlock __read_mostly
= 512; /* 'free' kb per user */
58 * max perf event sample rate
60 int sysctl_perf_event_sample_rate __read_mostly
= 100000;
62 static atomic64_t perf_event_id
;
64 void __weak
perf_event_print_debug(void) { }
66 extern __weak
const char *perf_pmu_name(void)
71 void perf_pmu_disable(struct pmu
*pmu
)
73 int *count
= this_cpu_ptr(pmu
->pmu_disable_count
);
75 pmu
->pmu_disable(pmu
);
78 void perf_pmu_enable(struct pmu
*pmu
)
80 int *count
= this_cpu_ptr(pmu
->pmu_disable_count
);
85 static DEFINE_PER_CPU(struct list_head
, rotation_list
);
88 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
89 * because they're strictly cpu affine and rotate_start is called with IRQs
90 * disabled, while rotate_context is called from IRQ context.
92 static void perf_pmu_rotate_start(struct pmu
*pmu
)
94 struct perf_cpu_context
*cpuctx
= this_cpu_ptr(pmu
->pmu_cpu_context
);
95 struct list_head
*head
= &__get_cpu_var(rotation_list
);
97 WARN_ON(!irqs_disabled());
99 if (list_empty(&cpuctx
->rotation_list
))
100 list_add(&cpuctx
->rotation_list
, head
);
103 static void get_ctx(struct perf_event_context
*ctx
)
105 WARN_ON(!atomic_inc_not_zero(&ctx
->refcount
));
108 static void free_ctx(struct rcu_head
*head
)
110 struct perf_event_context
*ctx
;
112 ctx
= container_of(head
, struct perf_event_context
, rcu_head
);
116 static void put_ctx(struct perf_event_context
*ctx
)
118 if (atomic_dec_and_test(&ctx
->refcount
)) {
120 put_ctx(ctx
->parent_ctx
);
122 put_task_struct(ctx
->task
);
123 call_rcu(&ctx
->rcu_head
, free_ctx
);
127 static void unclone_ctx(struct perf_event_context
*ctx
)
129 if (ctx
->parent_ctx
) {
130 put_ctx(ctx
->parent_ctx
);
131 ctx
->parent_ctx
= NULL
;
136 * If we inherit events we want to return the parent event id
139 static u64
primary_event_id(struct perf_event
*event
)
144 id
= event
->parent
->id
;
150 * Get the perf_event_context for a task and lock it.
151 * This has to cope with with the fact that until it is locked,
152 * the context could get moved to another task.
154 static struct perf_event_context
*
155 perf_lock_task_context(struct task_struct
*task
, int ctxn
, unsigned long *flags
)
157 struct perf_event_context
*ctx
;
161 ctx
= rcu_dereference(task
->perf_event_ctxp
[ctxn
]);
164 * If this context is a clone of another, it might
165 * get swapped for another underneath us by
166 * perf_event_task_sched_out, though the
167 * rcu_read_lock() protects us from any context
168 * getting freed. Lock the context and check if it
169 * got swapped before we could get the lock, and retry
170 * if so. If we locked the right context, then it
171 * can't get swapped on us any more.
173 raw_spin_lock_irqsave(&ctx
->lock
, *flags
);
174 if (ctx
!= rcu_dereference(task
->perf_event_ctxp
[ctxn
])) {
175 raw_spin_unlock_irqrestore(&ctx
->lock
, *flags
);
179 if (!atomic_inc_not_zero(&ctx
->refcount
)) {
180 raw_spin_unlock_irqrestore(&ctx
->lock
, *flags
);
189 * Get the context for a task and increment its pin_count so it
190 * can't get swapped to another task. This also increments its
191 * reference count so that the context can't get freed.
193 static struct perf_event_context
*
194 perf_pin_task_context(struct task_struct
*task
, int ctxn
)
196 struct perf_event_context
*ctx
;
199 ctx
= perf_lock_task_context(task
, ctxn
, &flags
);
202 raw_spin_unlock_irqrestore(&ctx
->lock
, flags
);
207 static void perf_unpin_context(struct perf_event_context
*ctx
)
211 raw_spin_lock_irqsave(&ctx
->lock
, flags
);
213 raw_spin_unlock_irqrestore(&ctx
->lock
, flags
);
217 static inline u64
perf_clock(void)
219 return local_clock();
223 * Update the record of the current time in a context.
225 static void update_context_time(struct perf_event_context
*ctx
)
227 u64 now
= perf_clock();
229 ctx
->time
+= now
- ctx
->timestamp
;
230 ctx
->timestamp
= now
;
234 * Update the total_time_enabled and total_time_running fields for a event.
236 static void update_event_times(struct perf_event
*event
)
238 struct perf_event_context
*ctx
= event
->ctx
;
241 if (event
->state
< PERF_EVENT_STATE_INACTIVE
||
242 event
->group_leader
->state
< PERF_EVENT_STATE_INACTIVE
)
248 run_end
= event
->tstamp_stopped
;
250 event
->total_time_enabled
= run_end
- event
->tstamp_enabled
;
252 if (event
->state
== PERF_EVENT_STATE_INACTIVE
)
253 run_end
= event
->tstamp_stopped
;
257 event
->total_time_running
= run_end
- event
->tstamp_running
;
261 * Update total_time_enabled and total_time_running for all events in a group.
263 static void update_group_times(struct perf_event
*leader
)
265 struct perf_event
*event
;
267 update_event_times(leader
);
268 list_for_each_entry(event
, &leader
->sibling_list
, group_entry
)
269 update_event_times(event
);
272 static struct list_head
*
273 ctx_group_list(struct perf_event
*event
, struct perf_event_context
*ctx
)
275 if (event
->attr
.pinned
)
276 return &ctx
->pinned_groups
;
278 return &ctx
->flexible_groups
;
282 * Add a event from the lists for its context.
283 * Must be called with ctx->mutex and ctx->lock held.
286 list_add_event(struct perf_event
*event
, struct perf_event_context
*ctx
)
288 WARN_ON_ONCE(event
->attach_state
& PERF_ATTACH_CONTEXT
);
289 event
->attach_state
|= PERF_ATTACH_CONTEXT
;
292 * If we're a stand alone event or group leader, we go to the context
293 * list, group events are kept attached to the group so that
294 * perf_group_detach can, at all times, locate all siblings.
296 if (event
->group_leader
== event
) {
297 struct list_head
*list
;
299 if (is_software_event(event
))
300 event
->group_flags
|= PERF_GROUP_SOFTWARE
;
302 list
= ctx_group_list(event
, ctx
);
303 list_add_tail(&event
->group_entry
, list
);
306 list_add_rcu(&event
->event_entry
, &ctx
->event_list
);
308 perf_pmu_rotate_start(ctx
->pmu
);
310 if (event
->attr
.inherit_stat
)
314 static void perf_group_attach(struct perf_event
*event
)
316 struct perf_event
*group_leader
= event
->group_leader
;
319 * We can have double attach due to group movement in perf_event_open.
321 if (event
->attach_state
& PERF_ATTACH_GROUP
)
324 event
->attach_state
|= PERF_ATTACH_GROUP
;
326 if (group_leader
== event
)
329 if (group_leader
->group_flags
& PERF_GROUP_SOFTWARE
&&
330 !is_software_event(event
))
331 group_leader
->group_flags
&= ~PERF_GROUP_SOFTWARE
;
333 list_add_tail(&event
->group_entry
, &group_leader
->sibling_list
);
334 group_leader
->nr_siblings
++;
338 * Remove a event from the lists for its context.
339 * Must be called with ctx->mutex and ctx->lock held.
342 list_del_event(struct perf_event
*event
, struct perf_event_context
*ctx
)
345 * We can have double detach due to exit/hot-unplug + close.
347 if (!(event
->attach_state
& PERF_ATTACH_CONTEXT
))
350 event
->attach_state
&= ~PERF_ATTACH_CONTEXT
;
353 if (event
->attr
.inherit_stat
)
356 list_del_rcu(&event
->event_entry
);
358 if (event
->group_leader
== event
)
359 list_del_init(&event
->group_entry
);
361 update_group_times(event
);
364 * If event was in error state, then keep it
365 * that way, otherwise bogus counts will be
366 * returned on read(). The only way to get out
367 * of error state is by explicit re-enabling
370 if (event
->state
> PERF_EVENT_STATE_OFF
)
371 event
->state
= PERF_EVENT_STATE_OFF
;
374 static void perf_group_detach(struct perf_event
*event
)
376 struct perf_event
*sibling
, *tmp
;
377 struct list_head
*list
= NULL
;
380 * We can have double detach due to exit/hot-unplug + close.
382 if (!(event
->attach_state
& PERF_ATTACH_GROUP
))
385 event
->attach_state
&= ~PERF_ATTACH_GROUP
;
388 * If this is a sibling, remove it from its group.
390 if (event
->group_leader
!= event
) {
391 list_del_init(&event
->group_entry
);
392 event
->group_leader
->nr_siblings
--;
396 if (!list_empty(&event
->group_entry
))
397 list
= &event
->group_entry
;
400 * If this was a group event with sibling events then
401 * upgrade the siblings to singleton events by adding them
402 * to whatever list we are on.
404 list_for_each_entry_safe(sibling
, tmp
, &event
->sibling_list
, group_entry
) {
406 list_move_tail(&sibling
->group_entry
, list
);
407 sibling
->group_leader
= sibling
;
409 /* Inherit group flags from the previous leader */
410 sibling
->group_flags
= event
->group_flags
;
415 event_filter_match(struct perf_event
*event
)
417 return event
->cpu
== -1 || event
->cpu
== smp_processor_id();
421 event_sched_out(struct perf_event
*event
,
422 struct perf_cpu_context
*cpuctx
,
423 struct perf_event_context
*ctx
)
427 * An event which could not be activated because of
428 * filter mismatch still needs to have its timings
429 * maintained, otherwise bogus information is return
430 * via read() for time_enabled, time_running:
432 if (event
->state
== PERF_EVENT_STATE_INACTIVE
433 && !event_filter_match(event
)) {
434 delta
= ctx
->time
- event
->tstamp_stopped
;
435 event
->tstamp_running
+= delta
;
436 event
->tstamp_stopped
= ctx
->time
;
439 if (event
->state
!= PERF_EVENT_STATE_ACTIVE
)
442 event
->state
= PERF_EVENT_STATE_INACTIVE
;
443 if (event
->pending_disable
) {
444 event
->pending_disable
= 0;
445 event
->state
= PERF_EVENT_STATE_OFF
;
447 event
->tstamp_stopped
= ctx
->time
;
448 event
->pmu
->del(event
, 0);
451 if (!is_software_event(event
))
452 cpuctx
->active_oncpu
--;
454 if (event
->attr
.exclusive
|| !cpuctx
->active_oncpu
)
455 cpuctx
->exclusive
= 0;
459 group_sched_out(struct perf_event
*group_event
,
460 struct perf_cpu_context
*cpuctx
,
461 struct perf_event_context
*ctx
)
463 struct perf_event
*event
;
464 int state
= group_event
->state
;
466 event_sched_out(group_event
, cpuctx
, ctx
);
469 * Schedule out siblings (if any):
471 list_for_each_entry(event
, &group_event
->sibling_list
, group_entry
)
472 event_sched_out(event
, cpuctx
, ctx
);
474 if (state
== PERF_EVENT_STATE_ACTIVE
&& group_event
->attr
.exclusive
)
475 cpuctx
->exclusive
= 0;
478 static inline struct perf_cpu_context
*
479 __get_cpu_context(struct perf_event_context
*ctx
)
481 return this_cpu_ptr(ctx
->pmu
->pmu_cpu_context
);
485 * Cross CPU call to remove a performance event
487 * We disable the event on the hardware level first. After that we
488 * remove it from the context list.
490 static void __perf_event_remove_from_context(void *info
)
492 struct perf_event
*event
= info
;
493 struct perf_event_context
*ctx
= event
->ctx
;
494 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
497 * If this is a task context, we need to check whether it is
498 * the current task context of this cpu. If not it has been
499 * scheduled out before the smp call arrived.
501 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
504 raw_spin_lock(&ctx
->lock
);
506 event_sched_out(event
, cpuctx
, ctx
);
508 list_del_event(event
, ctx
);
510 raw_spin_unlock(&ctx
->lock
);
515 * Remove the event from a task's (or a CPU's) list of events.
517 * Must be called with ctx->mutex held.
519 * CPU events are removed with a smp call. For task events we only
520 * call when the task is on a CPU.
522 * If event->ctx is a cloned context, callers must make sure that
523 * every task struct that event->ctx->task could possibly point to
524 * remains valid. This is OK when called from perf_release since
525 * that only calls us on the top-level context, which can't be a clone.
526 * When called from perf_event_exit_task, it's OK because the
527 * context has been detached from its task.
529 static void perf_event_remove_from_context(struct perf_event
*event
)
531 struct perf_event_context
*ctx
= event
->ctx
;
532 struct task_struct
*task
= ctx
->task
;
536 * Per cpu events are removed via an smp call and
537 * the removal is always successful.
539 smp_call_function_single(event
->cpu
,
540 __perf_event_remove_from_context
,
546 task_oncpu_function_call(task
, __perf_event_remove_from_context
,
549 raw_spin_lock_irq(&ctx
->lock
);
551 * If the context is active we need to retry the smp call.
553 if (ctx
->nr_active
&& !list_empty(&event
->group_entry
)) {
554 raw_spin_unlock_irq(&ctx
->lock
);
559 * The lock prevents that this context is scheduled in so we
560 * can remove the event safely, if the call above did not
563 if (!list_empty(&event
->group_entry
))
564 list_del_event(event
, ctx
);
565 raw_spin_unlock_irq(&ctx
->lock
);
569 * Cross CPU call to disable a performance event
571 static void __perf_event_disable(void *info
)
573 struct perf_event
*event
= info
;
574 struct perf_event_context
*ctx
= event
->ctx
;
575 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
578 * If this is a per-task event, need to check whether this
579 * event's task is the current task on this cpu.
581 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
584 raw_spin_lock(&ctx
->lock
);
587 * If the event is on, turn it off.
588 * If it is in error state, leave it in error state.
590 if (event
->state
>= PERF_EVENT_STATE_INACTIVE
) {
591 update_context_time(ctx
);
592 update_group_times(event
);
593 if (event
== event
->group_leader
)
594 group_sched_out(event
, cpuctx
, ctx
);
596 event_sched_out(event
, cpuctx
, ctx
);
597 event
->state
= PERF_EVENT_STATE_OFF
;
600 raw_spin_unlock(&ctx
->lock
);
606 * If event->ctx is a cloned context, callers must make sure that
607 * every task struct that event->ctx->task could possibly point to
608 * remains valid. This condition is satisifed when called through
609 * perf_event_for_each_child or perf_event_for_each because they
610 * hold the top-level event's child_mutex, so any descendant that
611 * goes to exit will block in sync_child_event.
612 * When called from perf_pending_event it's OK because event->ctx
613 * is the current context on this CPU and preemption is disabled,
614 * hence we can't get into perf_event_task_sched_out for this context.
616 void perf_event_disable(struct perf_event
*event
)
618 struct perf_event_context
*ctx
= event
->ctx
;
619 struct task_struct
*task
= ctx
->task
;
623 * Disable the event on the cpu that it's on
625 smp_call_function_single(event
->cpu
, __perf_event_disable
,
631 task_oncpu_function_call(task
, __perf_event_disable
, event
);
633 raw_spin_lock_irq(&ctx
->lock
);
635 * If the event is still active, we need to retry the cross-call.
637 if (event
->state
== PERF_EVENT_STATE_ACTIVE
) {
638 raw_spin_unlock_irq(&ctx
->lock
);
643 * Since we have the lock this context can't be scheduled
644 * in, so we can change the state safely.
646 if (event
->state
== PERF_EVENT_STATE_INACTIVE
) {
647 update_group_times(event
);
648 event
->state
= PERF_EVENT_STATE_OFF
;
651 raw_spin_unlock_irq(&ctx
->lock
);
655 event_sched_in(struct perf_event
*event
,
656 struct perf_cpu_context
*cpuctx
,
657 struct perf_event_context
*ctx
)
659 if (event
->state
<= PERF_EVENT_STATE_OFF
)
662 event
->state
= PERF_EVENT_STATE_ACTIVE
;
663 event
->oncpu
= smp_processor_id();
665 * The new state must be visible before we turn it on in the hardware:
669 if (event
->pmu
->add(event
, PERF_EF_START
)) {
670 event
->state
= PERF_EVENT_STATE_INACTIVE
;
675 event
->tstamp_running
+= ctx
->time
- event
->tstamp_stopped
;
677 if (!is_software_event(event
))
678 cpuctx
->active_oncpu
++;
681 if (event
->attr
.exclusive
)
682 cpuctx
->exclusive
= 1;
688 group_sched_in(struct perf_event
*group_event
,
689 struct perf_cpu_context
*cpuctx
,
690 struct perf_event_context
*ctx
)
692 struct perf_event
*event
, *partial_group
= NULL
;
693 struct pmu
*pmu
= group_event
->pmu
;
695 if (group_event
->state
== PERF_EVENT_STATE_OFF
)
700 if (event_sched_in(group_event
, cpuctx
, ctx
)) {
701 pmu
->cancel_txn(pmu
);
706 * Schedule in siblings as one group (if any):
708 list_for_each_entry(event
, &group_event
->sibling_list
, group_entry
) {
709 if (event_sched_in(event
, cpuctx
, ctx
)) {
710 partial_group
= event
;
715 if (!pmu
->commit_txn(pmu
))
720 * Groups can be scheduled in as one unit only, so undo any
721 * partial group before returning:
723 list_for_each_entry(event
, &group_event
->sibling_list
, group_entry
) {
724 if (event
== partial_group
)
726 event_sched_out(event
, cpuctx
, ctx
);
728 event_sched_out(group_event
, cpuctx
, ctx
);
730 pmu
->cancel_txn(pmu
);
736 * Work out whether we can put this event group on the CPU now.
738 static int group_can_go_on(struct perf_event
*event
,
739 struct perf_cpu_context
*cpuctx
,
743 * Groups consisting entirely of software events can always go on.
745 if (event
->group_flags
& PERF_GROUP_SOFTWARE
)
748 * If an exclusive group is already on, no other hardware
751 if (cpuctx
->exclusive
)
754 * If this group is exclusive and there are already
755 * events on the CPU, it can't go on.
757 if (event
->attr
.exclusive
&& cpuctx
->active_oncpu
)
760 * Otherwise, try to add it if all previous groups were able
766 static void add_event_to_ctx(struct perf_event
*event
,
767 struct perf_event_context
*ctx
)
769 list_add_event(event
, ctx
);
770 perf_group_attach(event
);
771 event
->tstamp_enabled
= ctx
->time
;
772 event
->tstamp_running
= ctx
->time
;
773 event
->tstamp_stopped
= ctx
->time
;
777 * Cross CPU call to install and enable a performance event
779 * Must be called with ctx->mutex held
781 static void __perf_install_in_context(void *info
)
783 struct perf_event
*event
= info
;
784 struct perf_event_context
*ctx
= event
->ctx
;
785 struct perf_event
*leader
= event
->group_leader
;
786 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
790 * If this is a task context, we need to check whether it is
791 * the current task context of this cpu. If not it has been
792 * scheduled out before the smp call arrived.
793 * Or possibly this is the right context but it isn't
794 * on this cpu because it had no events.
796 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
) {
797 if (cpuctx
->task_ctx
|| ctx
->task
!= current
)
799 cpuctx
->task_ctx
= ctx
;
802 raw_spin_lock(&ctx
->lock
);
804 update_context_time(ctx
);
806 add_event_to_ctx(event
, ctx
);
808 if (event
->cpu
!= -1 && event
->cpu
!= smp_processor_id())
812 * Don't put the event on if it is disabled or if
813 * it is in a group and the group isn't on.
815 if (event
->state
!= PERF_EVENT_STATE_INACTIVE
||
816 (leader
!= event
&& leader
->state
!= PERF_EVENT_STATE_ACTIVE
))
820 * An exclusive event can't go on if there are already active
821 * hardware events, and no hardware event can go on if there
822 * is already an exclusive event on.
824 if (!group_can_go_on(event
, cpuctx
, 1))
827 err
= event_sched_in(event
, cpuctx
, ctx
);
831 * This event couldn't go on. If it is in a group
832 * then we have to pull the whole group off.
833 * If the event group is pinned then put it in error state.
836 group_sched_out(leader
, cpuctx
, ctx
);
837 if (leader
->attr
.pinned
) {
838 update_group_times(leader
);
839 leader
->state
= PERF_EVENT_STATE_ERROR
;
844 raw_spin_unlock(&ctx
->lock
);
848 * Attach a performance event to a context
850 * First we add the event to the list with the hardware enable bit
851 * in event->hw_config cleared.
853 * If the event is attached to a task which is on a CPU we use a smp
854 * call to enable it in the task context. The task might have been
855 * scheduled away, but we check this in the smp call again.
857 * Must be called with ctx->mutex held.
860 perf_install_in_context(struct perf_event_context
*ctx
,
861 struct perf_event
*event
,
864 struct task_struct
*task
= ctx
->task
;
870 * Per cpu events are installed via an smp call and
871 * the install is always successful.
873 smp_call_function_single(cpu
, __perf_install_in_context
,
879 task_oncpu_function_call(task
, __perf_install_in_context
,
882 raw_spin_lock_irq(&ctx
->lock
);
884 * we need to retry the smp call.
886 if (ctx
->is_active
&& list_empty(&event
->group_entry
)) {
887 raw_spin_unlock_irq(&ctx
->lock
);
892 * The lock prevents that this context is scheduled in so we
893 * can add the event safely, if it the call above did not
896 if (list_empty(&event
->group_entry
))
897 add_event_to_ctx(event
, ctx
);
898 raw_spin_unlock_irq(&ctx
->lock
);
902 * Put a event into inactive state and update time fields.
903 * Enabling the leader of a group effectively enables all
904 * the group members that aren't explicitly disabled, so we
905 * have to update their ->tstamp_enabled also.
906 * Note: this works for group members as well as group leaders
907 * since the non-leader members' sibling_lists will be empty.
909 static void __perf_event_mark_enabled(struct perf_event
*event
,
910 struct perf_event_context
*ctx
)
912 struct perf_event
*sub
;
914 event
->state
= PERF_EVENT_STATE_INACTIVE
;
915 event
->tstamp_enabled
= ctx
->time
- event
->total_time_enabled
;
916 list_for_each_entry(sub
, &event
->sibling_list
, group_entry
) {
917 if (sub
->state
>= PERF_EVENT_STATE_INACTIVE
) {
918 sub
->tstamp_enabled
=
919 ctx
->time
- sub
->total_time_enabled
;
925 * Cross CPU call to enable a performance event
927 static void __perf_event_enable(void *info
)
929 struct perf_event
*event
= info
;
930 struct perf_event_context
*ctx
= event
->ctx
;
931 struct perf_event
*leader
= event
->group_leader
;
932 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
936 * If this is a per-task event, need to check whether this
937 * event's task is the current task on this cpu.
939 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
) {
940 if (cpuctx
->task_ctx
|| ctx
->task
!= current
)
942 cpuctx
->task_ctx
= ctx
;
945 raw_spin_lock(&ctx
->lock
);
947 update_context_time(ctx
);
949 if (event
->state
>= PERF_EVENT_STATE_INACTIVE
)
951 __perf_event_mark_enabled(event
, ctx
);
953 if (event
->cpu
!= -1 && event
->cpu
!= smp_processor_id())
957 * If the event is in a group and isn't the group leader,
958 * then don't put it on unless the group is on.
960 if (leader
!= event
&& leader
->state
!= PERF_EVENT_STATE_ACTIVE
)
963 if (!group_can_go_on(event
, cpuctx
, 1)) {
967 err
= group_sched_in(event
, cpuctx
, ctx
);
969 err
= event_sched_in(event
, cpuctx
, ctx
);
974 * If this event can't go on and it's part of a
975 * group, then the whole group has to come off.
978 group_sched_out(leader
, cpuctx
, ctx
);
979 if (leader
->attr
.pinned
) {
980 update_group_times(leader
);
981 leader
->state
= PERF_EVENT_STATE_ERROR
;
986 raw_spin_unlock(&ctx
->lock
);
992 * If event->ctx is a cloned context, callers must make sure that
993 * every task struct that event->ctx->task could possibly point to
994 * remains valid. This condition is satisfied when called through
995 * perf_event_for_each_child or perf_event_for_each as described
996 * for perf_event_disable.
998 void perf_event_enable(struct perf_event
*event
)
1000 struct perf_event_context
*ctx
= event
->ctx
;
1001 struct task_struct
*task
= ctx
->task
;
1005 * Enable the event on the cpu that it's on
1007 smp_call_function_single(event
->cpu
, __perf_event_enable
,
1012 raw_spin_lock_irq(&ctx
->lock
);
1013 if (event
->state
>= PERF_EVENT_STATE_INACTIVE
)
1017 * If the event is in error state, clear that first.
1018 * That way, if we see the event in error state below, we
1019 * know that it has gone back into error state, as distinct
1020 * from the task having been scheduled away before the
1021 * cross-call arrived.
1023 if (event
->state
== PERF_EVENT_STATE_ERROR
)
1024 event
->state
= PERF_EVENT_STATE_OFF
;
1027 raw_spin_unlock_irq(&ctx
->lock
);
1028 task_oncpu_function_call(task
, __perf_event_enable
, event
);
1030 raw_spin_lock_irq(&ctx
->lock
);
1033 * If the context is active and the event is still off,
1034 * we need to retry the cross-call.
1036 if (ctx
->is_active
&& event
->state
== PERF_EVENT_STATE_OFF
)
1040 * Since we have the lock this context can't be scheduled
1041 * in, so we can change the state safely.
1043 if (event
->state
== PERF_EVENT_STATE_OFF
)
1044 __perf_event_mark_enabled(event
, ctx
);
1047 raw_spin_unlock_irq(&ctx
->lock
);
1050 static int perf_event_refresh(struct perf_event
*event
, int refresh
)
1053 * not supported on inherited events
1055 if (event
->attr
.inherit
)
1058 atomic_add(refresh
, &event
->event_limit
);
1059 perf_event_enable(event
);
1065 EVENT_FLEXIBLE
= 0x1,
1067 EVENT_ALL
= EVENT_FLEXIBLE
| EVENT_PINNED
,
1070 static void ctx_sched_out(struct perf_event_context
*ctx
,
1071 struct perf_cpu_context
*cpuctx
,
1072 enum event_type_t event_type
)
1074 struct perf_event
*event
;
1076 raw_spin_lock(&ctx
->lock
);
1077 perf_pmu_disable(ctx
->pmu
);
1079 if (likely(!ctx
->nr_events
))
1081 update_context_time(ctx
);
1083 if (!ctx
->nr_active
)
1086 if (event_type
& EVENT_PINNED
) {
1087 list_for_each_entry(event
, &ctx
->pinned_groups
, group_entry
)
1088 group_sched_out(event
, cpuctx
, ctx
);
1091 if (event_type
& EVENT_FLEXIBLE
) {
1092 list_for_each_entry(event
, &ctx
->flexible_groups
, group_entry
)
1093 group_sched_out(event
, cpuctx
, ctx
);
1096 perf_pmu_enable(ctx
->pmu
);
1097 raw_spin_unlock(&ctx
->lock
);
1101 * Test whether two contexts are equivalent, i.e. whether they
1102 * have both been cloned from the same version of the same context
1103 * and they both have the same number of enabled events.
1104 * If the number of enabled events is the same, then the set
1105 * of enabled events should be the same, because these are both
1106 * inherited contexts, therefore we can't access individual events
1107 * in them directly with an fd; we can only enable/disable all
1108 * events via prctl, or enable/disable all events in a family
1109 * via ioctl, which will have the same effect on both contexts.
1111 static int context_equiv(struct perf_event_context
*ctx1
,
1112 struct perf_event_context
*ctx2
)
1114 return ctx1
->parent_ctx
&& ctx1
->parent_ctx
== ctx2
->parent_ctx
1115 && ctx1
->parent_gen
== ctx2
->parent_gen
1116 && !ctx1
->pin_count
&& !ctx2
->pin_count
;
1119 static void __perf_event_sync_stat(struct perf_event
*event
,
1120 struct perf_event
*next_event
)
1124 if (!event
->attr
.inherit_stat
)
1128 * Update the event value, we cannot use perf_event_read()
1129 * because we're in the middle of a context switch and have IRQs
1130 * disabled, which upsets smp_call_function_single(), however
1131 * we know the event must be on the current CPU, therefore we
1132 * don't need to use it.
1134 switch (event
->state
) {
1135 case PERF_EVENT_STATE_ACTIVE
:
1136 event
->pmu
->read(event
);
1139 case PERF_EVENT_STATE_INACTIVE
:
1140 update_event_times(event
);
1148 * In order to keep per-task stats reliable we need to flip the event
1149 * values when we flip the contexts.
1151 value
= local64_read(&next_event
->count
);
1152 value
= local64_xchg(&event
->count
, value
);
1153 local64_set(&next_event
->count
, value
);
1155 swap(event
->total_time_enabled
, next_event
->total_time_enabled
);
1156 swap(event
->total_time_running
, next_event
->total_time_running
);
1159 * Since we swizzled the values, update the user visible data too.
1161 perf_event_update_userpage(event
);
1162 perf_event_update_userpage(next_event
);
1165 #define list_next_entry(pos, member) \
1166 list_entry(pos->member.next, typeof(*pos), member)
1168 static void perf_event_sync_stat(struct perf_event_context
*ctx
,
1169 struct perf_event_context
*next_ctx
)
1171 struct perf_event
*event
, *next_event
;
1176 update_context_time(ctx
);
1178 event
= list_first_entry(&ctx
->event_list
,
1179 struct perf_event
, event_entry
);
1181 next_event
= list_first_entry(&next_ctx
->event_list
,
1182 struct perf_event
, event_entry
);
1184 while (&event
->event_entry
!= &ctx
->event_list
&&
1185 &next_event
->event_entry
!= &next_ctx
->event_list
) {
1187 __perf_event_sync_stat(event
, next_event
);
1189 event
= list_next_entry(event
, event_entry
);
1190 next_event
= list_next_entry(next_event
, event_entry
);
1194 void perf_event_context_sched_out(struct task_struct
*task
, int ctxn
,
1195 struct task_struct
*next
)
1197 struct perf_event_context
*ctx
= task
->perf_event_ctxp
[ctxn
];
1198 struct perf_event_context
*next_ctx
;
1199 struct perf_event_context
*parent
;
1200 struct perf_cpu_context
*cpuctx
;
1206 cpuctx
= __get_cpu_context(ctx
);
1207 if (!cpuctx
->task_ctx
)
1211 parent
= rcu_dereference(ctx
->parent_ctx
);
1212 next_ctx
= next
->perf_event_ctxp
[ctxn
];
1213 if (parent
&& next_ctx
&&
1214 rcu_dereference(next_ctx
->parent_ctx
) == parent
) {
1216 * Looks like the two contexts are clones, so we might be
1217 * able to optimize the context switch. We lock both
1218 * contexts and check that they are clones under the
1219 * lock (including re-checking that neither has been
1220 * uncloned in the meantime). It doesn't matter which
1221 * order we take the locks because no other cpu could
1222 * be trying to lock both of these tasks.
1224 raw_spin_lock(&ctx
->lock
);
1225 raw_spin_lock_nested(&next_ctx
->lock
, SINGLE_DEPTH_NESTING
);
1226 if (context_equiv(ctx
, next_ctx
)) {
1228 * XXX do we need a memory barrier of sorts
1229 * wrt to rcu_dereference() of perf_event_ctxp
1231 task
->perf_event_ctxp
[ctxn
] = next_ctx
;
1232 next
->perf_event_ctxp
[ctxn
] = ctx
;
1234 next_ctx
->task
= task
;
1237 perf_event_sync_stat(ctx
, next_ctx
);
1239 raw_spin_unlock(&next_ctx
->lock
);
1240 raw_spin_unlock(&ctx
->lock
);
1245 ctx_sched_out(ctx
, cpuctx
, EVENT_ALL
);
1246 cpuctx
->task_ctx
= NULL
;
1250 #define for_each_task_context_nr(ctxn) \
1251 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
1254 * Called from scheduler to remove the events of the current task,
1255 * with interrupts disabled.
1257 * We stop each event and update the event value in event->count.
1259 * This does not protect us against NMI, but disable()
1260 * sets the disabled bit in the control field of event _before_
1261 * accessing the event control register. If a NMI hits, then it will
1262 * not restart the event.
1264 void __perf_event_task_sched_out(struct task_struct
*task
,
1265 struct task_struct
*next
)
1269 perf_sw_event(PERF_COUNT_SW_CONTEXT_SWITCHES
, 1, 1, NULL
, 0);
1271 for_each_task_context_nr(ctxn
)
1272 perf_event_context_sched_out(task
, ctxn
, next
);
1275 static void task_ctx_sched_out(struct perf_event_context
*ctx
,
1276 enum event_type_t event_type
)
1278 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
1280 if (!cpuctx
->task_ctx
)
1283 if (WARN_ON_ONCE(ctx
!= cpuctx
->task_ctx
))
1286 ctx_sched_out(ctx
, cpuctx
, event_type
);
1287 cpuctx
->task_ctx
= NULL
;
1291 * Called with IRQs disabled
1293 static void cpu_ctx_sched_out(struct perf_cpu_context
*cpuctx
,
1294 enum event_type_t event_type
)
1296 ctx_sched_out(&cpuctx
->ctx
, cpuctx
, event_type
);
1300 ctx_pinned_sched_in(struct perf_event_context
*ctx
,
1301 struct perf_cpu_context
*cpuctx
)
1303 struct perf_event
*event
;
1305 list_for_each_entry(event
, &ctx
->pinned_groups
, group_entry
) {
1306 if (event
->state
<= PERF_EVENT_STATE_OFF
)
1308 if (event
->cpu
!= -1 && event
->cpu
!= smp_processor_id())
1311 if (group_can_go_on(event
, cpuctx
, 1))
1312 group_sched_in(event
, cpuctx
, ctx
);
1315 * If this pinned group hasn't been scheduled,
1316 * put it in error state.
1318 if (event
->state
== PERF_EVENT_STATE_INACTIVE
) {
1319 update_group_times(event
);
1320 event
->state
= PERF_EVENT_STATE_ERROR
;
1326 ctx_flexible_sched_in(struct perf_event_context
*ctx
,
1327 struct perf_cpu_context
*cpuctx
)
1329 struct perf_event
*event
;
1332 list_for_each_entry(event
, &ctx
->flexible_groups
, group_entry
) {
1333 /* Ignore events in OFF or ERROR state */
1334 if (event
->state
<= PERF_EVENT_STATE_OFF
)
1337 * Listen to the 'cpu' scheduling filter constraint
1340 if (event
->cpu
!= -1 && event
->cpu
!= smp_processor_id())
1343 if (group_can_go_on(event
, cpuctx
, can_add_hw
)) {
1344 if (group_sched_in(event
, cpuctx
, ctx
))
1351 ctx_sched_in(struct perf_event_context
*ctx
,
1352 struct perf_cpu_context
*cpuctx
,
1353 enum event_type_t event_type
)
1355 raw_spin_lock(&ctx
->lock
);
1357 if (likely(!ctx
->nr_events
))
1360 ctx
->timestamp
= perf_clock();
1363 * First go through the list and put on any pinned groups
1364 * in order to give them the best chance of going on.
1366 if (event_type
& EVENT_PINNED
)
1367 ctx_pinned_sched_in(ctx
, cpuctx
);
1369 /* Then walk through the lower prio flexible groups */
1370 if (event_type
& EVENT_FLEXIBLE
)
1371 ctx_flexible_sched_in(ctx
, cpuctx
);
1374 raw_spin_unlock(&ctx
->lock
);
1377 static void cpu_ctx_sched_in(struct perf_cpu_context
*cpuctx
,
1378 enum event_type_t event_type
)
1380 struct perf_event_context
*ctx
= &cpuctx
->ctx
;
1382 ctx_sched_in(ctx
, cpuctx
, event_type
);
1385 static void task_ctx_sched_in(struct perf_event_context
*ctx
,
1386 enum event_type_t event_type
)
1388 struct perf_cpu_context
*cpuctx
;
1390 cpuctx
= __get_cpu_context(ctx
);
1391 if (cpuctx
->task_ctx
== ctx
)
1394 ctx_sched_in(ctx
, cpuctx
, event_type
);
1395 cpuctx
->task_ctx
= ctx
;
1398 void perf_event_context_sched_in(struct perf_event_context
*ctx
)
1400 struct perf_cpu_context
*cpuctx
;
1402 cpuctx
= __get_cpu_context(ctx
);
1403 if (cpuctx
->task_ctx
== ctx
)
1406 perf_pmu_disable(ctx
->pmu
);
1408 * We want to keep the following priority order:
1409 * cpu pinned (that don't need to move), task pinned,
1410 * cpu flexible, task flexible.
1412 cpu_ctx_sched_out(cpuctx
, EVENT_FLEXIBLE
);
1414 ctx_sched_in(ctx
, cpuctx
, EVENT_PINNED
);
1415 cpu_ctx_sched_in(cpuctx
, EVENT_FLEXIBLE
);
1416 ctx_sched_in(ctx
, cpuctx
, EVENT_FLEXIBLE
);
1418 cpuctx
->task_ctx
= ctx
;
1421 * Since these rotations are per-cpu, we need to ensure the
1422 * cpu-context we got scheduled on is actually rotating.
1424 perf_pmu_rotate_start(ctx
->pmu
);
1425 perf_pmu_enable(ctx
->pmu
);
1429 * Called from scheduler to add the events of the current task
1430 * with interrupts disabled.
1432 * We restore the event value and then enable it.
1434 * This does not protect us against NMI, but enable()
1435 * sets the enabled bit in the control field of event _before_
1436 * accessing the event control register. If a NMI hits, then it will
1437 * keep the event running.
1439 void __perf_event_task_sched_in(struct task_struct
*task
)
1441 struct perf_event_context
*ctx
;
1444 for_each_task_context_nr(ctxn
) {
1445 ctx
= task
->perf_event_ctxp
[ctxn
];
1449 perf_event_context_sched_in(ctx
);
1453 #define MAX_INTERRUPTS (~0ULL)
1455 static void perf_log_throttle(struct perf_event
*event
, int enable
);
1457 static u64
perf_calculate_period(struct perf_event
*event
, u64 nsec
, u64 count
)
1459 u64 frequency
= event
->attr
.sample_freq
;
1460 u64 sec
= NSEC_PER_SEC
;
1461 u64 divisor
, dividend
;
1463 int count_fls
, nsec_fls
, frequency_fls
, sec_fls
;
1465 count_fls
= fls64(count
);
1466 nsec_fls
= fls64(nsec
);
1467 frequency_fls
= fls64(frequency
);
1471 * We got @count in @nsec, with a target of sample_freq HZ
1472 * the target period becomes:
1475 * period = -------------------
1476 * @nsec * sample_freq
1481 * Reduce accuracy by one bit such that @a and @b converge
1482 * to a similar magnitude.
1484 #define REDUCE_FLS(a, b) \
1486 if (a##_fls > b##_fls) { \
1496 * Reduce accuracy until either term fits in a u64, then proceed with
1497 * the other, so that finally we can do a u64/u64 division.
1499 while (count_fls
+ sec_fls
> 64 && nsec_fls
+ frequency_fls
> 64) {
1500 REDUCE_FLS(nsec
, frequency
);
1501 REDUCE_FLS(sec
, count
);
1504 if (count_fls
+ sec_fls
> 64) {
1505 divisor
= nsec
* frequency
;
1507 while (count_fls
+ sec_fls
> 64) {
1508 REDUCE_FLS(count
, sec
);
1512 dividend
= count
* sec
;
1514 dividend
= count
* sec
;
1516 while (nsec_fls
+ frequency_fls
> 64) {
1517 REDUCE_FLS(nsec
, frequency
);
1521 divisor
= nsec
* frequency
;
1527 return div64_u64(dividend
, divisor
);
1530 static void perf_adjust_period(struct perf_event
*event
, u64 nsec
, u64 count
)
1532 struct hw_perf_event
*hwc
= &event
->hw
;
1533 s64 period
, sample_period
;
1536 period
= perf_calculate_period(event
, nsec
, count
);
1538 delta
= (s64
)(period
- hwc
->sample_period
);
1539 delta
= (delta
+ 7) / 8; /* low pass filter */
1541 sample_period
= hwc
->sample_period
+ delta
;
1546 hwc
->sample_period
= sample_period
;
1548 if (local64_read(&hwc
->period_left
) > 8*sample_period
) {
1549 event
->pmu
->stop(event
, PERF_EF_UPDATE
);
1550 local64_set(&hwc
->period_left
, 0);
1551 event
->pmu
->start(event
, PERF_EF_RELOAD
);
1555 static void perf_ctx_adjust_freq(struct perf_event_context
*ctx
, u64 period
)
1557 struct perf_event
*event
;
1558 struct hw_perf_event
*hwc
;
1559 u64 interrupts
, now
;
1562 raw_spin_lock(&ctx
->lock
);
1563 list_for_each_entry_rcu(event
, &ctx
->event_list
, event_entry
) {
1564 if (event
->state
!= PERF_EVENT_STATE_ACTIVE
)
1567 if (event
->cpu
!= -1 && event
->cpu
!= smp_processor_id())
1572 interrupts
= hwc
->interrupts
;
1573 hwc
->interrupts
= 0;
1576 * unthrottle events on the tick
1578 if (interrupts
== MAX_INTERRUPTS
) {
1579 perf_log_throttle(event
, 1);
1580 event
->pmu
->start(event
, 0);
1583 if (!event
->attr
.freq
|| !event
->attr
.sample_freq
)
1586 event
->pmu
->read(event
);
1587 now
= local64_read(&event
->count
);
1588 delta
= now
- hwc
->freq_count_stamp
;
1589 hwc
->freq_count_stamp
= now
;
1592 perf_adjust_period(event
, period
, delta
);
1594 raw_spin_unlock(&ctx
->lock
);
1598 * Round-robin a context's events:
1600 static void rotate_ctx(struct perf_event_context
*ctx
)
1602 raw_spin_lock(&ctx
->lock
);
1604 /* Rotate the first entry last of non-pinned groups */
1605 list_rotate_left(&ctx
->flexible_groups
);
1607 raw_spin_unlock(&ctx
->lock
);
1611 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
1612 * because they're strictly cpu affine and rotate_start is called with IRQs
1613 * disabled, while rotate_context is called from IRQ context.
1615 static void perf_rotate_context(struct perf_cpu_context
*cpuctx
)
1617 u64 interval
= (u64
)cpuctx
->jiffies_interval
* TICK_NSEC
;
1618 struct perf_event_context
*ctx
= NULL
;
1619 int rotate
= 0, remove
= 1;
1621 if (cpuctx
->ctx
.nr_events
) {
1623 if (cpuctx
->ctx
.nr_events
!= cpuctx
->ctx
.nr_active
)
1627 ctx
= cpuctx
->task_ctx
;
1628 if (ctx
&& ctx
->nr_events
) {
1630 if (ctx
->nr_events
!= ctx
->nr_active
)
1634 perf_pmu_disable(cpuctx
->ctx
.pmu
);
1635 perf_ctx_adjust_freq(&cpuctx
->ctx
, interval
);
1637 perf_ctx_adjust_freq(ctx
, interval
);
1642 cpu_ctx_sched_out(cpuctx
, EVENT_FLEXIBLE
);
1644 task_ctx_sched_out(ctx
, EVENT_FLEXIBLE
);
1646 rotate_ctx(&cpuctx
->ctx
);
1650 cpu_ctx_sched_in(cpuctx
, EVENT_FLEXIBLE
);
1652 task_ctx_sched_in(ctx
, EVENT_FLEXIBLE
);
1656 list_del_init(&cpuctx
->rotation_list
);
1658 perf_pmu_enable(cpuctx
->ctx
.pmu
);
1661 void perf_event_task_tick(void)
1663 struct list_head
*head
= &__get_cpu_var(rotation_list
);
1664 struct perf_cpu_context
*cpuctx
, *tmp
;
1666 WARN_ON(!irqs_disabled());
1668 list_for_each_entry_safe(cpuctx
, tmp
, head
, rotation_list
) {
1669 if (cpuctx
->jiffies_interval
== 1 ||
1670 !(jiffies
% cpuctx
->jiffies_interval
))
1671 perf_rotate_context(cpuctx
);
1675 static int event_enable_on_exec(struct perf_event
*event
,
1676 struct perf_event_context
*ctx
)
1678 if (!event
->attr
.enable_on_exec
)
1681 event
->attr
.enable_on_exec
= 0;
1682 if (event
->state
>= PERF_EVENT_STATE_INACTIVE
)
1685 __perf_event_mark_enabled(event
, ctx
);
1691 * Enable all of a task's events that have been marked enable-on-exec.
1692 * This expects task == current.
1694 static void perf_event_enable_on_exec(struct perf_event_context
*ctx
)
1696 struct perf_event
*event
;
1697 unsigned long flags
;
1701 local_irq_save(flags
);
1702 if (!ctx
|| !ctx
->nr_events
)
1705 task_ctx_sched_out(ctx
, EVENT_ALL
);
1707 raw_spin_lock(&ctx
->lock
);
1709 list_for_each_entry(event
, &ctx
->pinned_groups
, group_entry
) {
1710 ret
= event_enable_on_exec(event
, ctx
);
1715 list_for_each_entry(event
, &ctx
->flexible_groups
, group_entry
) {
1716 ret
= event_enable_on_exec(event
, ctx
);
1722 * Unclone this context if we enabled any event.
1727 raw_spin_unlock(&ctx
->lock
);
1729 perf_event_context_sched_in(ctx
);
1731 local_irq_restore(flags
);
1735 * Cross CPU call to read the hardware event
1737 static void __perf_event_read(void *info
)
1739 struct perf_event
*event
= info
;
1740 struct perf_event_context
*ctx
= event
->ctx
;
1741 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
1744 * If this is a task context, we need to check whether it is
1745 * the current task context of this cpu. If not it has been
1746 * scheduled out before the smp call arrived. In that case
1747 * event->count would have been updated to a recent sample
1748 * when the event was scheduled out.
1750 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
1753 raw_spin_lock(&ctx
->lock
);
1754 update_context_time(ctx
);
1755 update_event_times(event
);
1756 raw_spin_unlock(&ctx
->lock
);
1758 event
->pmu
->read(event
);
1761 static inline u64
perf_event_count(struct perf_event
*event
)
1763 return local64_read(&event
->count
) + atomic64_read(&event
->child_count
);
1766 static u64
perf_event_read(struct perf_event
*event
)
1769 * If event is enabled and currently active on a CPU, update the
1770 * value in the event structure:
1772 if (event
->state
== PERF_EVENT_STATE_ACTIVE
) {
1773 smp_call_function_single(event
->oncpu
,
1774 __perf_event_read
, event
, 1);
1775 } else if (event
->state
== PERF_EVENT_STATE_INACTIVE
) {
1776 struct perf_event_context
*ctx
= event
->ctx
;
1777 unsigned long flags
;
1779 raw_spin_lock_irqsave(&ctx
->lock
, flags
);
1781 * may read while context is not active
1782 * (e.g., thread is blocked), in that case
1783 * we cannot update context time
1786 update_context_time(ctx
);
1787 update_event_times(event
);
1788 raw_spin_unlock_irqrestore(&ctx
->lock
, flags
);
1791 return perf_event_count(event
);
1798 struct callchain_cpus_entries
{
1799 struct rcu_head rcu_head
;
1800 struct perf_callchain_entry
*cpu_entries
[0];
1803 static DEFINE_PER_CPU(int, callchain_recursion
[PERF_NR_CONTEXTS
]);
1804 static atomic_t nr_callchain_events
;
1805 static DEFINE_MUTEX(callchain_mutex
);
1806 struct callchain_cpus_entries
*callchain_cpus_entries
;
1809 __weak
void perf_callchain_kernel(struct perf_callchain_entry
*entry
,
1810 struct pt_regs
*regs
)
1814 __weak
void perf_callchain_user(struct perf_callchain_entry
*entry
,
1815 struct pt_regs
*regs
)
1819 static void release_callchain_buffers_rcu(struct rcu_head
*head
)
1821 struct callchain_cpus_entries
*entries
;
1824 entries
= container_of(head
, struct callchain_cpus_entries
, rcu_head
);
1826 for_each_possible_cpu(cpu
)
1827 kfree(entries
->cpu_entries
[cpu
]);
1832 static void release_callchain_buffers(void)
1834 struct callchain_cpus_entries
*entries
;
1836 entries
= callchain_cpus_entries
;
1837 rcu_assign_pointer(callchain_cpus_entries
, NULL
);
1838 call_rcu(&entries
->rcu_head
, release_callchain_buffers_rcu
);
1841 static int alloc_callchain_buffers(void)
1845 struct callchain_cpus_entries
*entries
;
1848 * We can't use the percpu allocation API for data that can be
1849 * accessed from NMI. Use a temporary manual per cpu allocation
1850 * until that gets sorted out.
1852 size
= sizeof(*entries
) + sizeof(struct perf_callchain_entry
*) *
1853 num_possible_cpus();
1855 entries
= kzalloc(size
, GFP_KERNEL
);
1859 size
= sizeof(struct perf_callchain_entry
) * PERF_NR_CONTEXTS
;
1861 for_each_possible_cpu(cpu
) {
1862 entries
->cpu_entries
[cpu
] = kmalloc_node(size
, GFP_KERNEL
,
1864 if (!entries
->cpu_entries
[cpu
])
1868 rcu_assign_pointer(callchain_cpus_entries
, entries
);
1873 for_each_possible_cpu(cpu
)
1874 kfree(entries
->cpu_entries
[cpu
]);
1880 static int get_callchain_buffers(void)
1885 mutex_lock(&callchain_mutex
);
1887 count
= atomic_inc_return(&nr_callchain_events
);
1888 if (WARN_ON_ONCE(count
< 1)) {
1894 /* If the allocation failed, give up */
1895 if (!callchain_cpus_entries
)
1900 err
= alloc_callchain_buffers();
1902 release_callchain_buffers();
1904 mutex_unlock(&callchain_mutex
);
1909 static void put_callchain_buffers(void)
1911 if (atomic_dec_and_mutex_lock(&nr_callchain_events
, &callchain_mutex
)) {
1912 release_callchain_buffers();
1913 mutex_unlock(&callchain_mutex
);
1917 static int get_recursion_context(int *recursion
)
1925 else if (in_softirq())
1930 if (recursion
[rctx
])
1939 static inline void put_recursion_context(int *recursion
, int rctx
)
1945 static struct perf_callchain_entry
*get_callchain_entry(int *rctx
)
1948 struct callchain_cpus_entries
*entries
;
1950 *rctx
= get_recursion_context(__get_cpu_var(callchain_recursion
));
1954 entries
= rcu_dereference(callchain_cpus_entries
);
1958 cpu
= smp_processor_id();
1960 return &entries
->cpu_entries
[cpu
][*rctx
];
1964 put_callchain_entry(int rctx
)
1966 put_recursion_context(__get_cpu_var(callchain_recursion
), rctx
);
1969 static struct perf_callchain_entry
*perf_callchain(struct pt_regs
*regs
)
1972 struct perf_callchain_entry
*entry
;
1975 entry
= get_callchain_entry(&rctx
);
1984 if (!user_mode(regs
)) {
1985 perf_callchain_store(entry
, PERF_CONTEXT_KERNEL
);
1986 perf_callchain_kernel(entry
, regs
);
1988 regs
= task_pt_regs(current
);
1994 perf_callchain_store(entry
, PERF_CONTEXT_USER
);
1995 perf_callchain_user(entry
, regs
);
1999 put_callchain_entry(rctx
);
2005 * Initialize the perf_event context in a task_struct:
2007 static void __perf_event_init_context(struct perf_event_context
*ctx
)
2009 raw_spin_lock_init(&ctx
->lock
);
2010 mutex_init(&ctx
->mutex
);
2011 INIT_LIST_HEAD(&ctx
->pinned_groups
);
2012 INIT_LIST_HEAD(&ctx
->flexible_groups
);
2013 INIT_LIST_HEAD(&ctx
->event_list
);
2014 atomic_set(&ctx
->refcount
, 1);
2017 static struct perf_event_context
*
2018 alloc_perf_context(struct pmu
*pmu
, struct task_struct
*task
)
2020 struct perf_event_context
*ctx
;
2022 ctx
= kzalloc(sizeof(struct perf_event_context
), GFP_KERNEL
);
2026 __perf_event_init_context(ctx
);
2029 get_task_struct(task
);
2036 static struct task_struct
*
2037 find_lively_task_by_vpid(pid_t vpid
)
2039 struct task_struct
*task
;
2046 task
= find_task_by_vpid(vpid
);
2048 get_task_struct(task
);
2052 return ERR_PTR(-ESRCH
);
2055 * Can't attach events to a dying task.
2058 if (task
->flags
& PF_EXITING
)
2061 /* Reuse ptrace permission checks for now. */
2063 if (!ptrace_may_access(task
, PTRACE_MODE_READ
))
2068 put_task_struct(task
);
2069 return ERR_PTR(err
);
2073 static struct perf_event_context
*
2074 find_get_context(struct pmu
*pmu
, struct task_struct
*task
, int cpu
)
2076 struct perf_event_context
*ctx
;
2077 struct perf_cpu_context
*cpuctx
;
2078 unsigned long flags
;
2081 if (!task
&& cpu
!= -1) {
2082 /* Must be root to operate on a CPU event: */
2083 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN
))
2084 return ERR_PTR(-EACCES
);
2086 if (cpu
< 0 || cpu
>= nr_cpumask_bits
)
2087 return ERR_PTR(-EINVAL
);
2090 * We could be clever and allow to attach a event to an
2091 * offline CPU and activate it when the CPU comes up, but
2094 if (!cpu_online(cpu
))
2095 return ERR_PTR(-ENODEV
);
2097 cpuctx
= per_cpu_ptr(pmu
->pmu_cpu_context
, cpu
);
2105 ctxn
= pmu
->task_ctx_nr
;
2110 ctx
= perf_lock_task_context(task
, ctxn
, &flags
);
2113 raw_spin_unlock_irqrestore(&ctx
->lock
, flags
);
2117 ctx
= alloc_perf_context(pmu
, task
);
2124 if (cmpxchg(&task
->perf_event_ctxp
[ctxn
], NULL
, ctx
)) {
2126 * We raced with some other task; use
2127 * the context they set.
2129 put_task_struct(task
);
2138 return ERR_PTR(err
);
2141 static void perf_event_free_filter(struct perf_event
*event
);
2143 static void free_event_rcu(struct rcu_head
*head
)
2145 struct perf_event
*event
;
2147 event
= container_of(head
, struct perf_event
, rcu_head
);
2149 put_pid_ns(event
->ns
);
2150 perf_event_free_filter(event
);
2154 static void perf_buffer_put(struct perf_buffer
*buffer
);
2156 static void free_event(struct perf_event
*event
)
2158 irq_work_sync(&event
->pending
);
2160 if (!event
->parent
) {
2161 if (event
->attach_state
& PERF_ATTACH_TASK
)
2162 jump_label_dec(&perf_task_events
);
2163 if (event
->attr
.mmap
|| event
->attr
.mmap_data
)
2164 atomic_dec(&nr_mmap_events
);
2165 if (event
->attr
.comm
)
2166 atomic_dec(&nr_comm_events
);
2167 if (event
->attr
.task
)
2168 atomic_dec(&nr_task_events
);
2169 if (event
->attr
.sample_type
& PERF_SAMPLE_CALLCHAIN
)
2170 put_callchain_buffers();
2173 if (event
->buffer
) {
2174 perf_buffer_put(event
->buffer
);
2175 event
->buffer
= NULL
;
2179 event
->destroy(event
);
2182 put_ctx(event
->ctx
);
2184 call_rcu(&event
->rcu_head
, free_event_rcu
);
2187 int perf_event_release_kernel(struct perf_event
*event
)
2189 struct perf_event_context
*ctx
= event
->ctx
;
2192 * Remove from the PMU, can't get re-enabled since we got
2193 * here because the last ref went.
2195 perf_event_disable(event
);
2197 WARN_ON_ONCE(ctx
->parent_ctx
);
2199 * There are two ways this annotation is useful:
2201 * 1) there is a lock recursion from perf_event_exit_task
2202 * see the comment there.
2204 * 2) there is a lock-inversion with mmap_sem through
2205 * perf_event_read_group(), which takes faults while
2206 * holding ctx->mutex, however this is called after
2207 * the last filedesc died, so there is no possibility
2208 * to trigger the AB-BA case.
2210 mutex_lock_nested(&ctx
->mutex
, SINGLE_DEPTH_NESTING
);
2211 raw_spin_lock_irq(&ctx
->lock
);
2212 perf_group_detach(event
);
2213 list_del_event(event
, ctx
);
2214 raw_spin_unlock_irq(&ctx
->lock
);
2215 mutex_unlock(&ctx
->mutex
);
2217 mutex_lock(&event
->owner
->perf_event_mutex
);
2218 list_del_init(&event
->owner_entry
);
2219 mutex_unlock(&event
->owner
->perf_event_mutex
);
2220 put_task_struct(event
->owner
);
2226 EXPORT_SYMBOL_GPL(perf_event_release_kernel
);
2229 * Called when the last reference to the file is gone.
2231 static int perf_release(struct inode
*inode
, struct file
*file
)
2233 struct perf_event
*event
= file
->private_data
;
2235 file
->private_data
= NULL
;
2237 return perf_event_release_kernel(event
);
2240 static int perf_event_read_size(struct perf_event
*event
)
2242 int entry
= sizeof(u64
); /* value */
2246 if (event
->attr
.read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
2247 size
+= sizeof(u64
);
2249 if (event
->attr
.read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
2250 size
+= sizeof(u64
);
2252 if (event
->attr
.read_format
& PERF_FORMAT_ID
)
2253 entry
+= sizeof(u64
);
2255 if (event
->attr
.read_format
& PERF_FORMAT_GROUP
) {
2256 nr
+= event
->group_leader
->nr_siblings
;
2257 size
+= sizeof(u64
);
2265 u64
perf_event_read_value(struct perf_event
*event
, u64
*enabled
, u64
*running
)
2267 struct perf_event
*child
;
2273 mutex_lock(&event
->child_mutex
);
2274 total
+= perf_event_read(event
);
2275 *enabled
+= event
->total_time_enabled
+
2276 atomic64_read(&event
->child_total_time_enabled
);
2277 *running
+= event
->total_time_running
+
2278 atomic64_read(&event
->child_total_time_running
);
2280 list_for_each_entry(child
, &event
->child_list
, child_list
) {
2281 total
+= perf_event_read(child
);
2282 *enabled
+= child
->total_time_enabled
;
2283 *running
+= child
->total_time_running
;
2285 mutex_unlock(&event
->child_mutex
);
2289 EXPORT_SYMBOL_GPL(perf_event_read_value
);
2291 static int perf_event_read_group(struct perf_event
*event
,
2292 u64 read_format
, char __user
*buf
)
2294 struct perf_event
*leader
= event
->group_leader
, *sub
;
2295 int n
= 0, size
= 0, ret
= -EFAULT
;
2296 struct perf_event_context
*ctx
= leader
->ctx
;
2298 u64 count
, enabled
, running
;
2300 mutex_lock(&ctx
->mutex
);
2301 count
= perf_event_read_value(leader
, &enabled
, &running
);
2303 values
[n
++] = 1 + leader
->nr_siblings
;
2304 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
2305 values
[n
++] = enabled
;
2306 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
2307 values
[n
++] = running
;
2308 values
[n
++] = count
;
2309 if (read_format
& PERF_FORMAT_ID
)
2310 values
[n
++] = primary_event_id(leader
);
2312 size
= n
* sizeof(u64
);
2314 if (copy_to_user(buf
, values
, size
))
2319 list_for_each_entry(sub
, &leader
->sibling_list
, group_entry
) {
2322 values
[n
++] = perf_event_read_value(sub
, &enabled
, &running
);
2323 if (read_format
& PERF_FORMAT_ID
)
2324 values
[n
++] = primary_event_id(sub
);
2326 size
= n
* sizeof(u64
);
2328 if (copy_to_user(buf
+ ret
, values
, size
)) {
2336 mutex_unlock(&ctx
->mutex
);
2341 static int perf_event_read_one(struct perf_event
*event
,
2342 u64 read_format
, char __user
*buf
)
2344 u64 enabled
, running
;
2348 values
[n
++] = perf_event_read_value(event
, &enabled
, &running
);
2349 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
2350 values
[n
++] = enabled
;
2351 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
2352 values
[n
++] = running
;
2353 if (read_format
& PERF_FORMAT_ID
)
2354 values
[n
++] = primary_event_id(event
);
2356 if (copy_to_user(buf
, values
, n
* sizeof(u64
)))
2359 return n
* sizeof(u64
);
2363 * Read the performance event - simple non blocking version for now
2366 perf_read_hw(struct perf_event
*event
, char __user
*buf
, size_t count
)
2368 u64 read_format
= event
->attr
.read_format
;
2372 * Return end-of-file for a read on a event that is in
2373 * error state (i.e. because it was pinned but it couldn't be
2374 * scheduled on to the CPU at some point).
2376 if (event
->state
== PERF_EVENT_STATE_ERROR
)
2379 if (count
< perf_event_read_size(event
))
2382 WARN_ON_ONCE(event
->ctx
->parent_ctx
);
2383 if (read_format
& PERF_FORMAT_GROUP
)
2384 ret
= perf_event_read_group(event
, read_format
, buf
);
2386 ret
= perf_event_read_one(event
, read_format
, buf
);
2392 perf_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
2394 struct perf_event
*event
= file
->private_data
;
2396 return perf_read_hw(event
, buf
, count
);
2399 static unsigned int perf_poll(struct file
*file
, poll_table
*wait
)
2401 struct perf_event
*event
= file
->private_data
;
2402 struct perf_buffer
*buffer
;
2403 unsigned int events
= POLL_HUP
;
2406 buffer
= rcu_dereference(event
->buffer
);
2408 events
= atomic_xchg(&buffer
->poll
, 0);
2411 poll_wait(file
, &event
->waitq
, wait
);
2416 static void perf_event_reset(struct perf_event
*event
)
2418 (void)perf_event_read(event
);
2419 local64_set(&event
->count
, 0);
2420 perf_event_update_userpage(event
);
2424 * Holding the top-level event's child_mutex means that any
2425 * descendant process that has inherited this event will block
2426 * in sync_child_event if it goes to exit, thus satisfying the
2427 * task existence requirements of perf_event_enable/disable.
2429 static void perf_event_for_each_child(struct perf_event
*event
,
2430 void (*func
)(struct perf_event
*))
2432 struct perf_event
*child
;
2434 WARN_ON_ONCE(event
->ctx
->parent_ctx
);
2435 mutex_lock(&event
->child_mutex
);
2437 list_for_each_entry(child
, &event
->child_list
, child_list
)
2439 mutex_unlock(&event
->child_mutex
);
2442 static void perf_event_for_each(struct perf_event
*event
,
2443 void (*func
)(struct perf_event
*))
2445 struct perf_event_context
*ctx
= event
->ctx
;
2446 struct perf_event
*sibling
;
2448 WARN_ON_ONCE(ctx
->parent_ctx
);
2449 mutex_lock(&ctx
->mutex
);
2450 event
= event
->group_leader
;
2452 perf_event_for_each_child(event
, func
);
2454 list_for_each_entry(sibling
, &event
->sibling_list
, group_entry
)
2455 perf_event_for_each_child(event
, func
);
2456 mutex_unlock(&ctx
->mutex
);
2459 static int perf_event_period(struct perf_event
*event
, u64 __user
*arg
)
2461 struct perf_event_context
*ctx
= event
->ctx
;
2465 if (!event
->attr
.sample_period
)
2468 if (copy_from_user(&value
, arg
, sizeof(value
)))
2474 raw_spin_lock_irq(&ctx
->lock
);
2475 if (event
->attr
.freq
) {
2476 if (value
> sysctl_perf_event_sample_rate
) {
2481 event
->attr
.sample_freq
= value
;
2483 event
->attr
.sample_period
= value
;
2484 event
->hw
.sample_period
= value
;
2487 raw_spin_unlock_irq(&ctx
->lock
);
2492 static const struct file_operations perf_fops
;
2494 static struct perf_event
*perf_fget_light(int fd
, int *fput_needed
)
2498 file
= fget_light(fd
, fput_needed
);
2500 return ERR_PTR(-EBADF
);
2502 if (file
->f_op
!= &perf_fops
) {
2503 fput_light(file
, *fput_needed
);
2505 return ERR_PTR(-EBADF
);
2508 return file
->private_data
;
2511 static int perf_event_set_output(struct perf_event
*event
,
2512 struct perf_event
*output_event
);
2513 static int perf_event_set_filter(struct perf_event
*event
, void __user
*arg
);
2515 static long perf_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
2517 struct perf_event
*event
= file
->private_data
;
2518 void (*func
)(struct perf_event
*);
2522 case PERF_EVENT_IOC_ENABLE
:
2523 func
= perf_event_enable
;
2525 case PERF_EVENT_IOC_DISABLE
:
2526 func
= perf_event_disable
;
2528 case PERF_EVENT_IOC_RESET
:
2529 func
= perf_event_reset
;
2532 case PERF_EVENT_IOC_REFRESH
:
2533 return perf_event_refresh(event
, arg
);
2535 case PERF_EVENT_IOC_PERIOD
:
2536 return perf_event_period(event
, (u64 __user
*)arg
);
2538 case PERF_EVENT_IOC_SET_OUTPUT
:
2540 struct perf_event
*output_event
= NULL
;
2541 int fput_needed
= 0;
2545 output_event
= perf_fget_light(arg
, &fput_needed
);
2546 if (IS_ERR(output_event
))
2547 return PTR_ERR(output_event
);
2550 ret
= perf_event_set_output(event
, output_event
);
2552 fput_light(output_event
->filp
, fput_needed
);
2557 case PERF_EVENT_IOC_SET_FILTER
:
2558 return perf_event_set_filter(event
, (void __user
*)arg
);
2564 if (flags
& PERF_IOC_FLAG_GROUP
)
2565 perf_event_for_each(event
, func
);
2567 perf_event_for_each_child(event
, func
);
2572 int perf_event_task_enable(void)
2574 struct perf_event
*event
;
2576 mutex_lock(¤t
->perf_event_mutex
);
2577 list_for_each_entry(event
, ¤t
->perf_event_list
, owner_entry
)
2578 perf_event_for_each_child(event
, perf_event_enable
);
2579 mutex_unlock(¤t
->perf_event_mutex
);
2584 int perf_event_task_disable(void)
2586 struct perf_event
*event
;
2588 mutex_lock(¤t
->perf_event_mutex
);
2589 list_for_each_entry(event
, ¤t
->perf_event_list
, owner_entry
)
2590 perf_event_for_each_child(event
, perf_event_disable
);
2591 mutex_unlock(¤t
->perf_event_mutex
);
2596 #ifndef PERF_EVENT_INDEX_OFFSET
2597 # define PERF_EVENT_INDEX_OFFSET 0
2600 static int perf_event_index(struct perf_event
*event
)
2602 if (event
->hw
.state
& PERF_HES_STOPPED
)
2605 if (event
->state
!= PERF_EVENT_STATE_ACTIVE
)
2608 return event
->hw
.idx
+ 1 - PERF_EVENT_INDEX_OFFSET
;
2612 * Callers need to ensure there can be no nesting of this function, otherwise
2613 * the seqlock logic goes bad. We can not serialize this because the arch
2614 * code calls this from NMI context.
2616 void perf_event_update_userpage(struct perf_event
*event
)
2618 struct perf_event_mmap_page
*userpg
;
2619 struct perf_buffer
*buffer
;
2622 buffer
= rcu_dereference(event
->buffer
);
2626 userpg
= buffer
->user_page
;
2629 * Disable preemption so as to not let the corresponding user-space
2630 * spin too long if we get preempted.
2635 userpg
->index
= perf_event_index(event
);
2636 userpg
->offset
= perf_event_count(event
);
2637 if (event
->state
== PERF_EVENT_STATE_ACTIVE
)
2638 userpg
->offset
-= local64_read(&event
->hw
.prev_count
);
2640 userpg
->time_enabled
= event
->total_time_enabled
+
2641 atomic64_read(&event
->child_total_time_enabled
);
2643 userpg
->time_running
= event
->total_time_running
+
2644 atomic64_read(&event
->child_total_time_running
);
2653 static unsigned long perf_data_size(struct perf_buffer
*buffer
);
2656 perf_buffer_init(struct perf_buffer
*buffer
, long watermark
, int flags
)
2658 long max_size
= perf_data_size(buffer
);
2661 buffer
->watermark
= min(max_size
, watermark
);
2663 if (!buffer
->watermark
)
2664 buffer
->watermark
= max_size
/ 2;
2666 if (flags
& PERF_BUFFER_WRITABLE
)
2667 buffer
->writable
= 1;
2669 atomic_set(&buffer
->refcount
, 1);
2672 #ifndef CONFIG_PERF_USE_VMALLOC
2675 * Back perf_mmap() with regular GFP_KERNEL-0 pages.
2678 static struct page
*
2679 perf_mmap_to_page(struct perf_buffer
*buffer
, unsigned long pgoff
)
2681 if (pgoff
> buffer
->nr_pages
)
2685 return virt_to_page(buffer
->user_page
);
2687 return virt_to_page(buffer
->data_pages
[pgoff
- 1]);
2690 static void *perf_mmap_alloc_page(int cpu
)
2695 node
= (cpu
== -1) ? cpu
: cpu_to_node(cpu
);
2696 page
= alloc_pages_node(node
, GFP_KERNEL
| __GFP_ZERO
, 0);
2700 return page_address(page
);
2703 static struct perf_buffer
*
2704 perf_buffer_alloc(int nr_pages
, long watermark
, int cpu
, int flags
)
2706 struct perf_buffer
*buffer
;
2710 size
= sizeof(struct perf_buffer
);
2711 size
+= nr_pages
* sizeof(void *);
2713 buffer
= kzalloc(size
, GFP_KERNEL
);
2717 buffer
->user_page
= perf_mmap_alloc_page(cpu
);
2718 if (!buffer
->user_page
)
2719 goto fail_user_page
;
2721 for (i
= 0; i
< nr_pages
; i
++) {
2722 buffer
->data_pages
[i
] = perf_mmap_alloc_page(cpu
);
2723 if (!buffer
->data_pages
[i
])
2724 goto fail_data_pages
;
2727 buffer
->nr_pages
= nr_pages
;
2729 perf_buffer_init(buffer
, watermark
, flags
);
2734 for (i
--; i
>= 0; i
--)
2735 free_page((unsigned long)buffer
->data_pages
[i
]);
2737 free_page((unsigned long)buffer
->user_page
);
2746 static void perf_mmap_free_page(unsigned long addr
)
2748 struct page
*page
= virt_to_page((void *)addr
);
2750 page
->mapping
= NULL
;
2754 static void perf_buffer_free(struct perf_buffer
*buffer
)
2758 perf_mmap_free_page((unsigned long)buffer
->user_page
);
2759 for (i
= 0; i
< buffer
->nr_pages
; i
++)
2760 perf_mmap_free_page((unsigned long)buffer
->data_pages
[i
]);
2764 static inline int page_order(struct perf_buffer
*buffer
)
2772 * Back perf_mmap() with vmalloc memory.
2774 * Required for architectures that have d-cache aliasing issues.
2777 static inline int page_order(struct perf_buffer
*buffer
)
2779 return buffer
->page_order
;
2782 static struct page
*
2783 perf_mmap_to_page(struct perf_buffer
*buffer
, unsigned long pgoff
)
2785 if (pgoff
> (1UL << page_order(buffer
)))
2788 return vmalloc_to_page((void *)buffer
->user_page
+ pgoff
* PAGE_SIZE
);
2791 static void perf_mmap_unmark_page(void *addr
)
2793 struct page
*page
= vmalloc_to_page(addr
);
2795 page
->mapping
= NULL
;
2798 static void perf_buffer_free_work(struct work_struct
*work
)
2800 struct perf_buffer
*buffer
;
2804 buffer
= container_of(work
, struct perf_buffer
, work
);
2805 nr
= 1 << page_order(buffer
);
2807 base
= buffer
->user_page
;
2808 for (i
= 0; i
< nr
+ 1; i
++)
2809 perf_mmap_unmark_page(base
+ (i
* PAGE_SIZE
));
2815 static void perf_buffer_free(struct perf_buffer
*buffer
)
2817 schedule_work(&buffer
->work
);
2820 static struct perf_buffer
*
2821 perf_buffer_alloc(int nr_pages
, long watermark
, int cpu
, int flags
)
2823 struct perf_buffer
*buffer
;
2827 size
= sizeof(struct perf_buffer
);
2828 size
+= sizeof(void *);
2830 buffer
= kzalloc(size
, GFP_KERNEL
);
2834 INIT_WORK(&buffer
->work
, perf_buffer_free_work
);
2836 all_buf
= vmalloc_user((nr_pages
+ 1) * PAGE_SIZE
);
2840 buffer
->user_page
= all_buf
;
2841 buffer
->data_pages
[0] = all_buf
+ PAGE_SIZE
;
2842 buffer
->page_order
= ilog2(nr_pages
);
2843 buffer
->nr_pages
= 1;
2845 perf_buffer_init(buffer
, watermark
, flags
);
2858 static unsigned long perf_data_size(struct perf_buffer
*buffer
)
2860 return buffer
->nr_pages
<< (PAGE_SHIFT
+ page_order(buffer
));
2863 static int perf_mmap_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
2865 struct perf_event
*event
= vma
->vm_file
->private_data
;
2866 struct perf_buffer
*buffer
;
2867 int ret
= VM_FAULT_SIGBUS
;
2869 if (vmf
->flags
& FAULT_FLAG_MKWRITE
) {
2870 if (vmf
->pgoff
== 0)
2876 buffer
= rcu_dereference(event
->buffer
);
2880 if (vmf
->pgoff
&& (vmf
->flags
& FAULT_FLAG_WRITE
))
2883 vmf
->page
= perf_mmap_to_page(buffer
, vmf
->pgoff
);
2887 get_page(vmf
->page
);
2888 vmf
->page
->mapping
= vma
->vm_file
->f_mapping
;
2889 vmf
->page
->index
= vmf
->pgoff
;
2898 static void perf_buffer_free_rcu(struct rcu_head
*rcu_head
)
2900 struct perf_buffer
*buffer
;
2902 buffer
= container_of(rcu_head
, struct perf_buffer
, rcu_head
);
2903 perf_buffer_free(buffer
);
2906 static struct perf_buffer
*perf_buffer_get(struct perf_event
*event
)
2908 struct perf_buffer
*buffer
;
2911 buffer
= rcu_dereference(event
->buffer
);
2913 if (!atomic_inc_not_zero(&buffer
->refcount
))
2921 static void perf_buffer_put(struct perf_buffer
*buffer
)
2923 if (!atomic_dec_and_test(&buffer
->refcount
))
2926 call_rcu(&buffer
->rcu_head
, perf_buffer_free_rcu
);
2929 static void perf_mmap_open(struct vm_area_struct
*vma
)
2931 struct perf_event
*event
= vma
->vm_file
->private_data
;
2933 atomic_inc(&event
->mmap_count
);
2936 static void perf_mmap_close(struct vm_area_struct
*vma
)
2938 struct perf_event
*event
= vma
->vm_file
->private_data
;
2940 if (atomic_dec_and_mutex_lock(&event
->mmap_count
, &event
->mmap_mutex
)) {
2941 unsigned long size
= perf_data_size(event
->buffer
);
2942 struct user_struct
*user
= event
->mmap_user
;
2943 struct perf_buffer
*buffer
= event
->buffer
;
2945 atomic_long_sub((size
>> PAGE_SHIFT
) + 1, &user
->locked_vm
);
2946 vma
->vm_mm
->locked_vm
-= event
->mmap_locked
;
2947 rcu_assign_pointer(event
->buffer
, NULL
);
2948 mutex_unlock(&event
->mmap_mutex
);
2950 perf_buffer_put(buffer
);
2955 static const struct vm_operations_struct perf_mmap_vmops
= {
2956 .open
= perf_mmap_open
,
2957 .close
= perf_mmap_close
,
2958 .fault
= perf_mmap_fault
,
2959 .page_mkwrite
= perf_mmap_fault
,
2962 static int perf_mmap(struct file
*file
, struct vm_area_struct
*vma
)
2964 struct perf_event
*event
= file
->private_data
;
2965 unsigned long user_locked
, user_lock_limit
;
2966 struct user_struct
*user
= current_user();
2967 unsigned long locked
, lock_limit
;
2968 struct perf_buffer
*buffer
;
2969 unsigned long vma_size
;
2970 unsigned long nr_pages
;
2971 long user_extra
, extra
;
2972 int ret
= 0, flags
= 0;
2975 * Don't allow mmap() of inherited per-task counters. This would
2976 * create a performance issue due to all children writing to the
2979 if (event
->cpu
== -1 && event
->attr
.inherit
)
2982 if (!(vma
->vm_flags
& VM_SHARED
))
2985 vma_size
= vma
->vm_end
- vma
->vm_start
;
2986 nr_pages
= (vma_size
/ PAGE_SIZE
) - 1;
2989 * If we have buffer pages ensure they're a power-of-two number, so we
2990 * can do bitmasks instead of modulo.
2992 if (nr_pages
!= 0 && !is_power_of_2(nr_pages
))
2995 if (vma_size
!= PAGE_SIZE
* (1 + nr_pages
))
2998 if (vma
->vm_pgoff
!= 0)
3001 WARN_ON_ONCE(event
->ctx
->parent_ctx
);
3002 mutex_lock(&event
->mmap_mutex
);
3003 if (event
->buffer
) {
3004 if (event
->buffer
->nr_pages
== nr_pages
)
3005 atomic_inc(&event
->buffer
->refcount
);
3011 user_extra
= nr_pages
+ 1;
3012 user_lock_limit
= sysctl_perf_event_mlock
>> (PAGE_SHIFT
- 10);
3015 * Increase the limit linearly with more CPUs:
3017 user_lock_limit
*= num_online_cpus();
3019 user_locked
= atomic_long_read(&user
->locked_vm
) + user_extra
;
3022 if (user_locked
> user_lock_limit
)
3023 extra
= user_locked
- user_lock_limit
;
3025 lock_limit
= rlimit(RLIMIT_MEMLOCK
);
3026 lock_limit
>>= PAGE_SHIFT
;
3027 locked
= vma
->vm_mm
->locked_vm
+ extra
;
3029 if ((locked
> lock_limit
) && perf_paranoid_tracepoint_raw() &&
3030 !capable(CAP_IPC_LOCK
)) {
3035 WARN_ON(event
->buffer
);
3037 if (vma
->vm_flags
& VM_WRITE
)
3038 flags
|= PERF_BUFFER_WRITABLE
;
3040 buffer
= perf_buffer_alloc(nr_pages
, event
->attr
.wakeup_watermark
,
3046 rcu_assign_pointer(event
->buffer
, buffer
);
3048 atomic_long_add(user_extra
, &user
->locked_vm
);
3049 event
->mmap_locked
= extra
;
3050 event
->mmap_user
= get_current_user();
3051 vma
->vm_mm
->locked_vm
+= event
->mmap_locked
;
3055 atomic_inc(&event
->mmap_count
);
3056 mutex_unlock(&event
->mmap_mutex
);
3058 vma
->vm_flags
|= VM_RESERVED
;
3059 vma
->vm_ops
= &perf_mmap_vmops
;
3064 static int perf_fasync(int fd
, struct file
*filp
, int on
)
3066 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
3067 struct perf_event
*event
= filp
->private_data
;
3070 mutex_lock(&inode
->i_mutex
);
3071 retval
= fasync_helper(fd
, filp
, on
, &event
->fasync
);
3072 mutex_unlock(&inode
->i_mutex
);
3080 static const struct file_operations perf_fops
= {
3081 .llseek
= no_llseek
,
3082 .release
= perf_release
,
3085 .unlocked_ioctl
= perf_ioctl
,
3086 .compat_ioctl
= perf_ioctl
,
3088 .fasync
= perf_fasync
,
3094 * If there's data, ensure we set the poll() state and publish everything
3095 * to user-space before waking everybody up.
3098 void perf_event_wakeup(struct perf_event
*event
)
3100 wake_up_all(&event
->waitq
);
3102 if (event
->pending_kill
) {
3103 kill_fasync(&event
->fasync
, SIGIO
, event
->pending_kill
);
3104 event
->pending_kill
= 0;
3108 static void perf_pending_event(struct irq_work
*entry
)
3110 struct perf_event
*event
= container_of(entry
,
3111 struct perf_event
, pending
);
3113 if (event
->pending_disable
) {
3114 event
->pending_disable
= 0;
3115 __perf_event_disable(event
);
3118 if (event
->pending_wakeup
) {
3119 event
->pending_wakeup
= 0;
3120 perf_event_wakeup(event
);
3125 * We assume there is only KVM supporting the callbacks.
3126 * Later on, we might change it to a list if there is
3127 * another virtualization implementation supporting the callbacks.
3129 struct perf_guest_info_callbacks
*perf_guest_cbs
;
3131 int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks
*cbs
)
3133 perf_guest_cbs
= cbs
;
3136 EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks
);
3138 int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks
*cbs
)
3140 perf_guest_cbs
= NULL
;
3143 EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks
);
3148 static bool perf_output_space(struct perf_buffer
*buffer
, unsigned long tail
,
3149 unsigned long offset
, unsigned long head
)
3153 if (!buffer
->writable
)
3156 mask
= perf_data_size(buffer
) - 1;
3158 offset
= (offset
- tail
) & mask
;
3159 head
= (head
- tail
) & mask
;
3161 if ((int)(head
- offset
) < 0)
3167 static void perf_output_wakeup(struct perf_output_handle
*handle
)
3169 atomic_set(&handle
->buffer
->poll
, POLL_IN
);
3172 handle
->event
->pending_wakeup
= 1;
3173 irq_work_queue(&handle
->event
->pending
);
3175 perf_event_wakeup(handle
->event
);
3179 * We need to ensure a later event_id doesn't publish a head when a former
3180 * event isn't done writing. However since we need to deal with NMIs we
3181 * cannot fully serialize things.
3183 * We only publish the head (and generate a wakeup) when the outer-most
3186 static void perf_output_get_handle(struct perf_output_handle
*handle
)
3188 struct perf_buffer
*buffer
= handle
->buffer
;
3191 local_inc(&buffer
->nest
);
3192 handle
->wakeup
= local_read(&buffer
->wakeup
);
3195 static void perf_output_put_handle(struct perf_output_handle
*handle
)
3197 struct perf_buffer
*buffer
= handle
->buffer
;
3201 head
= local_read(&buffer
->head
);
3204 * IRQ/NMI can happen here, which means we can miss a head update.
3207 if (!local_dec_and_test(&buffer
->nest
))
3211 * Publish the known good head. Rely on the full barrier implied
3212 * by atomic_dec_and_test() order the buffer->head read and this
3215 buffer
->user_page
->data_head
= head
;
3218 * Now check if we missed an update, rely on the (compiler)
3219 * barrier in atomic_dec_and_test() to re-read buffer->head.
3221 if (unlikely(head
!= local_read(&buffer
->head
))) {
3222 local_inc(&buffer
->nest
);
3226 if (handle
->wakeup
!= local_read(&buffer
->wakeup
))
3227 perf_output_wakeup(handle
);
3233 __always_inline
void perf_output_copy(struct perf_output_handle
*handle
,
3234 const void *buf
, unsigned int len
)
3237 unsigned long size
= min_t(unsigned long, handle
->size
, len
);
3239 memcpy(handle
->addr
, buf
, size
);
3242 handle
->addr
+= size
;
3244 handle
->size
-= size
;
3245 if (!handle
->size
) {
3246 struct perf_buffer
*buffer
= handle
->buffer
;
3249 handle
->page
&= buffer
->nr_pages
- 1;
3250 handle
->addr
= buffer
->data_pages
[handle
->page
];
3251 handle
->size
= PAGE_SIZE
<< page_order(buffer
);
3256 int perf_output_begin(struct perf_output_handle
*handle
,
3257 struct perf_event
*event
, unsigned int size
,
3258 int nmi
, int sample
)
3260 struct perf_buffer
*buffer
;
3261 unsigned long tail
, offset
, head
;
3264 struct perf_event_header header
;
3271 * For inherited events we send all the output towards the parent.
3274 event
= event
->parent
;
3276 buffer
= rcu_dereference(event
->buffer
);
3280 handle
->buffer
= buffer
;
3281 handle
->event
= event
;
3283 handle
->sample
= sample
;
3285 if (!buffer
->nr_pages
)
3288 have_lost
= local_read(&buffer
->lost
);
3290 size
+= sizeof(lost_event
);
3292 perf_output_get_handle(handle
);
3296 * Userspace could choose to issue a mb() before updating the
3297 * tail pointer. So that all reads will be completed before the
3300 tail
= ACCESS_ONCE(buffer
->user_page
->data_tail
);
3302 offset
= head
= local_read(&buffer
->head
);
3304 if (unlikely(!perf_output_space(buffer
, tail
, offset
, head
)))
3306 } while (local_cmpxchg(&buffer
->head
, offset
, head
) != offset
);
3308 if (head
- local_read(&buffer
->wakeup
) > buffer
->watermark
)
3309 local_add(buffer
->watermark
, &buffer
->wakeup
);
3311 handle
->page
= offset
>> (PAGE_SHIFT
+ page_order(buffer
));
3312 handle
->page
&= buffer
->nr_pages
- 1;
3313 handle
->size
= offset
& ((PAGE_SIZE
<< page_order(buffer
)) - 1);
3314 handle
->addr
= buffer
->data_pages
[handle
->page
];
3315 handle
->addr
+= handle
->size
;
3316 handle
->size
= (PAGE_SIZE
<< page_order(buffer
)) - handle
->size
;
3319 lost_event
.header
.type
= PERF_RECORD_LOST
;
3320 lost_event
.header
.misc
= 0;
3321 lost_event
.header
.size
= sizeof(lost_event
);
3322 lost_event
.id
= event
->id
;
3323 lost_event
.lost
= local_xchg(&buffer
->lost
, 0);
3325 perf_output_put(handle
, lost_event
);
3331 local_inc(&buffer
->lost
);
3332 perf_output_put_handle(handle
);
3339 void perf_output_end(struct perf_output_handle
*handle
)
3341 struct perf_event
*event
= handle
->event
;
3342 struct perf_buffer
*buffer
= handle
->buffer
;
3344 int wakeup_events
= event
->attr
.wakeup_events
;
3346 if (handle
->sample
&& wakeup_events
) {
3347 int events
= local_inc_return(&buffer
->events
);
3348 if (events
>= wakeup_events
) {
3349 local_sub(wakeup_events
, &buffer
->events
);
3350 local_inc(&buffer
->wakeup
);
3354 perf_output_put_handle(handle
);
3358 static u32
perf_event_pid(struct perf_event
*event
, struct task_struct
*p
)
3361 * only top level events have the pid namespace they were created in
3364 event
= event
->parent
;
3366 return task_tgid_nr_ns(p
, event
->ns
);
3369 static u32
perf_event_tid(struct perf_event
*event
, struct task_struct
*p
)
3372 * only top level events have the pid namespace they were created in
3375 event
= event
->parent
;
3377 return task_pid_nr_ns(p
, event
->ns
);
3380 static void perf_output_read_one(struct perf_output_handle
*handle
,
3381 struct perf_event
*event
)
3383 u64 read_format
= event
->attr
.read_format
;
3387 values
[n
++] = perf_event_count(event
);
3388 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
) {
3389 values
[n
++] = event
->total_time_enabled
+
3390 atomic64_read(&event
->child_total_time_enabled
);
3392 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
) {
3393 values
[n
++] = event
->total_time_running
+
3394 atomic64_read(&event
->child_total_time_running
);
3396 if (read_format
& PERF_FORMAT_ID
)
3397 values
[n
++] = primary_event_id(event
);
3399 perf_output_copy(handle
, values
, n
* sizeof(u64
));
3403 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
3405 static void perf_output_read_group(struct perf_output_handle
*handle
,
3406 struct perf_event
*event
)
3408 struct perf_event
*leader
= event
->group_leader
, *sub
;
3409 u64 read_format
= event
->attr
.read_format
;
3413 values
[n
++] = 1 + leader
->nr_siblings
;
3415 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
3416 values
[n
++] = leader
->total_time_enabled
;
3418 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
3419 values
[n
++] = leader
->total_time_running
;
3421 if (leader
!= event
)
3422 leader
->pmu
->read(leader
);
3424 values
[n
++] = perf_event_count(leader
);
3425 if (read_format
& PERF_FORMAT_ID
)
3426 values
[n
++] = primary_event_id(leader
);
3428 perf_output_copy(handle
, values
, n
* sizeof(u64
));
3430 list_for_each_entry(sub
, &leader
->sibling_list
, group_entry
) {
3434 sub
->pmu
->read(sub
);
3436 values
[n
++] = perf_event_count(sub
);
3437 if (read_format
& PERF_FORMAT_ID
)
3438 values
[n
++] = primary_event_id(sub
);
3440 perf_output_copy(handle
, values
, n
* sizeof(u64
));
3444 static void perf_output_read(struct perf_output_handle
*handle
,
3445 struct perf_event
*event
)
3447 if (event
->attr
.read_format
& PERF_FORMAT_GROUP
)
3448 perf_output_read_group(handle
, event
);
3450 perf_output_read_one(handle
, event
);
3453 void perf_output_sample(struct perf_output_handle
*handle
,
3454 struct perf_event_header
*header
,
3455 struct perf_sample_data
*data
,
3456 struct perf_event
*event
)
3458 u64 sample_type
= data
->type
;
3460 perf_output_put(handle
, *header
);
3462 if (sample_type
& PERF_SAMPLE_IP
)
3463 perf_output_put(handle
, data
->ip
);
3465 if (sample_type
& PERF_SAMPLE_TID
)
3466 perf_output_put(handle
, data
->tid_entry
);
3468 if (sample_type
& PERF_SAMPLE_TIME
)
3469 perf_output_put(handle
, data
->time
);
3471 if (sample_type
& PERF_SAMPLE_ADDR
)
3472 perf_output_put(handle
, data
->addr
);
3474 if (sample_type
& PERF_SAMPLE_ID
)
3475 perf_output_put(handle
, data
->id
);
3477 if (sample_type
& PERF_SAMPLE_STREAM_ID
)
3478 perf_output_put(handle
, data
->stream_id
);
3480 if (sample_type
& PERF_SAMPLE_CPU
)
3481 perf_output_put(handle
, data
->cpu_entry
);
3483 if (sample_type
& PERF_SAMPLE_PERIOD
)
3484 perf_output_put(handle
, data
->period
);
3486 if (sample_type
& PERF_SAMPLE_READ
)
3487 perf_output_read(handle
, event
);
3489 if (sample_type
& PERF_SAMPLE_CALLCHAIN
) {
3490 if (data
->callchain
) {
3493 if (data
->callchain
)
3494 size
+= data
->callchain
->nr
;
3496 size
*= sizeof(u64
);
3498 perf_output_copy(handle
, data
->callchain
, size
);
3501 perf_output_put(handle
, nr
);
3505 if (sample_type
& PERF_SAMPLE_RAW
) {
3507 perf_output_put(handle
, data
->raw
->size
);
3508 perf_output_copy(handle
, data
->raw
->data
,
3515 .size
= sizeof(u32
),
3518 perf_output_put(handle
, raw
);
3523 void perf_prepare_sample(struct perf_event_header
*header
,
3524 struct perf_sample_data
*data
,
3525 struct perf_event
*event
,
3526 struct pt_regs
*regs
)
3528 u64 sample_type
= event
->attr
.sample_type
;
3530 data
->type
= sample_type
;
3532 header
->type
= PERF_RECORD_SAMPLE
;
3533 header
->size
= sizeof(*header
);
3536 header
->misc
|= perf_misc_flags(regs
);
3538 if (sample_type
& PERF_SAMPLE_IP
) {
3539 data
->ip
= perf_instruction_pointer(regs
);
3541 header
->size
+= sizeof(data
->ip
);
3544 if (sample_type
& PERF_SAMPLE_TID
) {
3545 /* namespace issues */
3546 data
->tid_entry
.pid
= perf_event_pid(event
, current
);
3547 data
->tid_entry
.tid
= perf_event_tid(event
, current
);
3549 header
->size
+= sizeof(data
->tid_entry
);
3552 if (sample_type
& PERF_SAMPLE_TIME
) {
3553 data
->time
= perf_clock();
3555 header
->size
+= sizeof(data
->time
);
3558 if (sample_type
& PERF_SAMPLE_ADDR
)
3559 header
->size
+= sizeof(data
->addr
);
3561 if (sample_type
& PERF_SAMPLE_ID
) {
3562 data
->id
= primary_event_id(event
);
3564 header
->size
+= sizeof(data
->id
);
3567 if (sample_type
& PERF_SAMPLE_STREAM_ID
) {
3568 data
->stream_id
= event
->id
;
3570 header
->size
+= sizeof(data
->stream_id
);
3573 if (sample_type
& PERF_SAMPLE_CPU
) {
3574 data
->cpu_entry
.cpu
= raw_smp_processor_id();
3575 data
->cpu_entry
.reserved
= 0;
3577 header
->size
+= sizeof(data
->cpu_entry
);
3580 if (sample_type
& PERF_SAMPLE_PERIOD
)
3581 header
->size
+= sizeof(data
->period
);
3583 if (sample_type
& PERF_SAMPLE_READ
)
3584 header
->size
+= perf_event_read_size(event
);
3586 if (sample_type
& PERF_SAMPLE_CALLCHAIN
) {
3589 data
->callchain
= perf_callchain(regs
);
3591 if (data
->callchain
)
3592 size
+= data
->callchain
->nr
;
3594 header
->size
+= size
* sizeof(u64
);
3597 if (sample_type
& PERF_SAMPLE_RAW
) {
3598 int size
= sizeof(u32
);
3601 size
+= data
->raw
->size
;
3603 size
+= sizeof(u32
);
3605 WARN_ON_ONCE(size
& (sizeof(u64
)-1));
3606 header
->size
+= size
;
3610 static void perf_event_output(struct perf_event
*event
, int nmi
,
3611 struct perf_sample_data
*data
,
3612 struct pt_regs
*regs
)
3614 struct perf_output_handle handle
;
3615 struct perf_event_header header
;
3617 /* protect the callchain buffers */
3620 perf_prepare_sample(&header
, data
, event
, regs
);
3622 if (perf_output_begin(&handle
, event
, header
.size
, nmi
, 1))
3625 perf_output_sample(&handle
, &header
, data
, event
);
3627 perf_output_end(&handle
);
3637 struct perf_read_event
{
3638 struct perf_event_header header
;
3645 perf_event_read_event(struct perf_event
*event
,
3646 struct task_struct
*task
)
3648 struct perf_output_handle handle
;
3649 struct perf_read_event read_event
= {
3651 .type
= PERF_RECORD_READ
,
3653 .size
= sizeof(read_event
) + perf_event_read_size(event
),
3655 .pid
= perf_event_pid(event
, task
),
3656 .tid
= perf_event_tid(event
, task
),
3660 ret
= perf_output_begin(&handle
, event
, read_event
.header
.size
, 0, 0);
3664 perf_output_put(&handle
, read_event
);
3665 perf_output_read(&handle
, event
);
3667 perf_output_end(&handle
);
3671 * task tracking -- fork/exit
3673 * enabled by: attr.comm | attr.mmap | attr.mmap_data | attr.task
3676 struct perf_task_event
{
3677 struct task_struct
*task
;
3678 struct perf_event_context
*task_ctx
;
3681 struct perf_event_header header
;
3691 static void perf_event_task_output(struct perf_event
*event
,
3692 struct perf_task_event
*task_event
)
3694 struct perf_output_handle handle
;
3695 struct task_struct
*task
= task_event
->task
;
3698 size
= task_event
->event_id
.header
.size
;
3699 ret
= perf_output_begin(&handle
, event
, size
, 0, 0);
3704 task_event
->event_id
.pid
= perf_event_pid(event
, task
);
3705 task_event
->event_id
.ppid
= perf_event_pid(event
, current
);
3707 task_event
->event_id
.tid
= perf_event_tid(event
, task
);
3708 task_event
->event_id
.ptid
= perf_event_tid(event
, current
);
3710 perf_output_put(&handle
, task_event
->event_id
);
3712 perf_output_end(&handle
);
3715 static int perf_event_task_match(struct perf_event
*event
)
3717 if (event
->state
< PERF_EVENT_STATE_INACTIVE
)
3720 if (event
->cpu
!= -1 && event
->cpu
!= smp_processor_id())
3723 if (event
->attr
.comm
|| event
->attr
.mmap
||
3724 event
->attr
.mmap_data
|| event
->attr
.task
)
3730 static void perf_event_task_ctx(struct perf_event_context
*ctx
,
3731 struct perf_task_event
*task_event
)
3733 struct perf_event
*event
;
3735 list_for_each_entry_rcu(event
, &ctx
->event_list
, event_entry
) {
3736 if (perf_event_task_match(event
))
3737 perf_event_task_output(event
, task_event
);
3741 static void perf_event_task_event(struct perf_task_event
*task_event
)
3743 struct perf_cpu_context
*cpuctx
;
3744 struct perf_event_context
*ctx
;
3749 list_for_each_entry_rcu(pmu
, &pmus
, entry
) {
3750 cpuctx
= get_cpu_ptr(pmu
->pmu_cpu_context
);
3751 perf_event_task_ctx(&cpuctx
->ctx
, task_event
);
3753 ctx
= task_event
->task_ctx
;
3755 ctxn
= pmu
->task_ctx_nr
;
3758 ctx
= rcu_dereference(current
->perf_event_ctxp
[ctxn
]);
3761 perf_event_task_ctx(ctx
, task_event
);
3763 put_cpu_ptr(pmu
->pmu_cpu_context
);
3768 static void perf_event_task(struct task_struct
*task
,
3769 struct perf_event_context
*task_ctx
,
3772 struct perf_task_event task_event
;
3774 if (!atomic_read(&nr_comm_events
) &&
3775 !atomic_read(&nr_mmap_events
) &&
3776 !atomic_read(&nr_task_events
))
3779 task_event
= (struct perf_task_event
){
3781 .task_ctx
= task_ctx
,
3784 .type
= new ? PERF_RECORD_FORK
: PERF_RECORD_EXIT
,
3786 .size
= sizeof(task_event
.event_id
),
3792 .time
= perf_clock(),
3796 perf_event_task_event(&task_event
);
3799 void perf_event_fork(struct task_struct
*task
)
3801 perf_event_task(task
, NULL
, 1);
3808 struct perf_comm_event
{
3809 struct task_struct
*task
;
3814 struct perf_event_header header
;
3821 static void perf_event_comm_output(struct perf_event
*event
,
3822 struct perf_comm_event
*comm_event
)
3824 struct perf_output_handle handle
;
3825 int size
= comm_event
->event_id
.header
.size
;
3826 int ret
= perf_output_begin(&handle
, event
, size
, 0, 0);
3831 comm_event
->event_id
.pid
= perf_event_pid(event
, comm_event
->task
);
3832 comm_event
->event_id
.tid
= perf_event_tid(event
, comm_event
->task
);
3834 perf_output_put(&handle
, comm_event
->event_id
);
3835 perf_output_copy(&handle
, comm_event
->comm
,
3836 comm_event
->comm_size
);
3837 perf_output_end(&handle
);
3840 static int perf_event_comm_match(struct perf_event
*event
)
3842 if (event
->state
< PERF_EVENT_STATE_INACTIVE
)
3845 if (event
->cpu
!= -1 && event
->cpu
!= smp_processor_id())
3848 if (event
->attr
.comm
)
3854 static void perf_event_comm_ctx(struct perf_event_context
*ctx
,
3855 struct perf_comm_event
*comm_event
)
3857 struct perf_event
*event
;
3859 list_for_each_entry_rcu(event
, &ctx
->event_list
, event_entry
) {
3860 if (perf_event_comm_match(event
))
3861 perf_event_comm_output(event
, comm_event
);
3865 static void perf_event_comm_event(struct perf_comm_event
*comm_event
)
3867 struct perf_cpu_context
*cpuctx
;
3868 struct perf_event_context
*ctx
;
3869 char comm
[TASK_COMM_LEN
];
3874 memset(comm
, 0, sizeof(comm
));
3875 strlcpy(comm
, comm_event
->task
->comm
, sizeof(comm
));
3876 size
= ALIGN(strlen(comm
)+1, sizeof(u64
));
3878 comm_event
->comm
= comm
;
3879 comm_event
->comm_size
= size
;
3881 comm_event
->event_id
.header
.size
= sizeof(comm_event
->event_id
) + size
;
3884 list_for_each_entry_rcu(pmu
, &pmus
, entry
) {
3885 cpuctx
= get_cpu_ptr(pmu
->pmu_cpu_context
);
3886 perf_event_comm_ctx(&cpuctx
->ctx
, comm_event
);
3888 ctxn
= pmu
->task_ctx_nr
;
3892 ctx
= rcu_dereference(current
->perf_event_ctxp
[ctxn
]);
3894 perf_event_comm_ctx(ctx
, comm_event
);
3896 put_cpu_ptr(pmu
->pmu_cpu_context
);
3901 void perf_event_comm(struct task_struct
*task
)
3903 struct perf_comm_event comm_event
;
3904 struct perf_event_context
*ctx
;
3907 for_each_task_context_nr(ctxn
) {
3908 ctx
= task
->perf_event_ctxp
[ctxn
];
3912 perf_event_enable_on_exec(ctx
);
3915 if (!atomic_read(&nr_comm_events
))
3918 comm_event
= (struct perf_comm_event
){
3924 .type
= PERF_RECORD_COMM
,
3933 perf_event_comm_event(&comm_event
);
3940 struct perf_mmap_event
{
3941 struct vm_area_struct
*vma
;
3943 const char *file_name
;
3947 struct perf_event_header header
;
3957 static void perf_event_mmap_output(struct perf_event
*event
,
3958 struct perf_mmap_event
*mmap_event
)
3960 struct perf_output_handle handle
;
3961 int size
= mmap_event
->event_id
.header
.size
;
3962 int ret
= perf_output_begin(&handle
, event
, size
, 0, 0);
3967 mmap_event
->event_id
.pid
= perf_event_pid(event
, current
);
3968 mmap_event
->event_id
.tid
= perf_event_tid(event
, current
);
3970 perf_output_put(&handle
, mmap_event
->event_id
);
3971 perf_output_copy(&handle
, mmap_event
->file_name
,
3972 mmap_event
->file_size
);
3973 perf_output_end(&handle
);
3976 static int perf_event_mmap_match(struct perf_event
*event
,
3977 struct perf_mmap_event
*mmap_event
,
3980 if (event
->state
< PERF_EVENT_STATE_INACTIVE
)
3983 if (event
->cpu
!= -1 && event
->cpu
!= smp_processor_id())
3986 if ((!executable
&& event
->attr
.mmap_data
) ||
3987 (executable
&& event
->attr
.mmap
))
3993 static void perf_event_mmap_ctx(struct perf_event_context
*ctx
,
3994 struct perf_mmap_event
*mmap_event
,
3997 struct perf_event
*event
;
3999 list_for_each_entry_rcu(event
, &ctx
->event_list
, event_entry
) {
4000 if (perf_event_mmap_match(event
, mmap_event
, executable
))
4001 perf_event_mmap_output(event
, mmap_event
);
4005 static void perf_event_mmap_event(struct perf_mmap_event
*mmap_event
)
4007 struct perf_cpu_context
*cpuctx
;
4008 struct perf_event_context
*ctx
;
4009 struct vm_area_struct
*vma
= mmap_event
->vma
;
4010 struct file
*file
= vma
->vm_file
;
4018 memset(tmp
, 0, sizeof(tmp
));
4022 * d_path works from the end of the buffer backwards, so we
4023 * need to add enough zero bytes after the string to handle
4024 * the 64bit alignment we do later.
4026 buf
= kzalloc(PATH_MAX
+ sizeof(u64
), GFP_KERNEL
);
4028 name
= strncpy(tmp
, "//enomem", sizeof(tmp
));
4031 name
= d_path(&file
->f_path
, buf
, PATH_MAX
);
4033 name
= strncpy(tmp
, "//toolong", sizeof(tmp
));
4037 if (arch_vma_name(mmap_event
->vma
)) {
4038 name
= strncpy(tmp
, arch_vma_name(mmap_event
->vma
),
4044 name
= strncpy(tmp
, "[vdso]", sizeof(tmp
));
4046 } else if (vma
->vm_start
<= vma
->vm_mm
->start_brk
&&
4047 vma
->vm_end
>= vma
->vm_mm
->brk
) {
4048 name
= strncpy(tmp
, "[heap]", sizeof(tmp
));
4050 } else if (vma
->vm_start
<= vma
->vm_mm
->start_stack
&&
4051 vma
->vm_end
>= vma
->vm_mm
->start_stack
) {
4052 name
= strncpy(tmp
, "[stack]", sizeof(tmp
));
4056 name
= strncpy(tmp
, "//anon", sizeof(tmp
));
4061 size
= ALIGN(strlen(name
)+1, sizeof(u64
));
4063 mmap_event
->file_name
= name
;
4064 mmap_event
->file_size
= size
;
4066 mmap_event
->event_id
.header
.size
= sizeof(mmap_event
->event_id
) + size
;
4069 list_for_each_entry_rcu(pmu
, &pmus
, entry
) {
4070 cpuctx
= get_cpu_ptr(pmu
->pmu_cpu_context
);
4071 perf_event_mmap_ctx(&cpuctx
->ctx
, mmap_event
,
4072 vma
->vm_flags
& VM_EXEC
);
4074 ctxn
= pmu
->task_ctx_nr
;
4078 ctx
= rcu_dereference(current
->perf_event_ctxp
[ctxn
]);
4080 perf_event_mmap_ctx(ctx
, mmap_event
,
4081 vma
->vm_flags
& VM_EXEC
);
4084 put_cpu_ptr(pmu
->pmu_cpu_context
);
4091 void perf_event_mmap(struct vm_area_struct
*vma
)
4093 struct perf_mmap_event mmap_event
;
4095 if (!atomic_read(&nr_mmap_events
))
4098 mmap_event
= (struct perf_mmap_event
){
4104 .type
= PERF_RECORD_MMAP
,
4105 .misc
= PERF_RECORD_MISC_USER
,
4110 .start
= vma
->vm_start
,
4111 .len
= vma
->vm_end
- vma
->vm_start
,
4112 .pgoff
= (u64
)vma
->vm_pgoff
<< PAGE_SHIFT
,
4116 perf_event_mmap_event(&mmap_event
);
4120 * IRQ throttle logging
4123 static void perf_log_throttle(struct perf_event
*event
, int enable
)
4125 struct perf_output_handle handle
;
4129 struct perf_event_header header
;
4133 } throttle_event
= {
4135 .type
= PERF_RECORD_THROTTLE
,
4137 .size
= sizeof(throttle_event
),
4139 .time
= perf_clock(),
4140 .id
= primary_event_id(event
),
4141 .stream_id
= event
->id
,
4145 throttle_event
.header
.type
= PERF_RECORD_UNTHROTTLE
;
4147 ret
= perf_output_begin(&handle
, event
, sizeof(throttle_event
), 1, 0);
4151 perf_output_put(&handle
, throttle_event
);
4152 perf_output_end(&handle
);
4156 * Generic event overflow handling, sampling.
4159 static int __perf_event_overflow(struct perf_event
*event
, int nmi
,
4160 int throttle
, struct perf_sample_data
*data
,
4161 struct pt_regs
*regs
)
4163 int events
= atomic_read(&event
->event_limit
);
4164 struct hw_perf_event
*hwc
= &event
->hw
;
4170 if (hwc
->interrupts
!= MAX_INTERRUPTS
) {
4172 if (HZ
* hwc
->interrupts
>
4173 (u64
)sysctl_perf_event_sample_rate
) {
4174 hwc
->interrupts
= MAX_INTERRUPTS
;
4175 perf_log_throttle(event
, 0);
4180 * Keep re-disabling events even though on the previous
4181 * pass we disabled it - just in case we raced with a
4182 * sched-in and the event got enabled again:
4188 if (event
->attr
.freq
) {
4189 u64 now
= perf_clock();
4190 s64 delta
= now
- hwc
->freq_time_stamp
;
4192 hwc
->freq_time_stamp
= now
;
4194 if (delta
> 0 && delta
< 2*TICK_NSEC
)
4195 perf_adjust_period(event
, delta
, hwc
->last_period
);
4199 * XXX event_limit might not quite work as expected on inherited
4203 event
->pending_kill
= POLL_IN
;
4204 if (events
&& atomic_dec_and_test(&event
->event_limit
)) {
4206 event
->pending_kill
= POLL_HUP
;
4208 event
->pending_disable
= 1;
4209 irq_work_queue(&event
->pending
);
4211 perf_event_disable(event
);
4214 if (event
->overflow_handler
)
4215 event
->overflow_handler(event
, nmi
, data
, regs
);
4217 perf_event_output(event
, nmi
, data
, regs
);
4222 int perf_event_overflow(struct perf_event
*event
, int nmi
,
4223 struct perf_sample_data
*data
,
4224 struct pt_regs
*regs
)
4226 return __perf_event_overflow(event
, nmi
, 1, data
, regs
);
4230 * Generic software event infrastructure
4233 struct swevent_htable
{
4234 struct swevent_hlist
*swevent_hlist
;
4235 struct mutex hlist_mutex
;
4238 /* Recursion avoidance in each contexts */
4239 int recursion
[PERF_NR_CONTEXTS
];
4242 static DEFINE_PER_CPU(struct swevent_htable
, swevent_htable
);
4245 * We directly increment event->count and keep a second value in
4246 * event->hw.period_left to count intervals. This period event
4247 * is kept in the range [-sample_period, 0] so that we can use the
4251 static u64
perf_swevent_set_period(struct perf_event
*event
)
4253 struct hw_perf_event
*hwc
= &event
->hw
;
4254 u64 period
= hwc
->last_period
;
4258 hwc
->last_period
= hwc
->sample_period
;
4261 old
= val
= local64_read(&hwc
->period_left
);
4265 nr
= div64_u64(period
+ val
, period
);
4266 offset
= nr
* period
;
4268 if (local64_cmpxchg(&hwc
->period_left
, old
, val
) != old
)
4274 static void perf_swevent_overflow(struct perf_event
*event
, u64 overflow
,
4275 int nmi
, struct perf_sample_data
*data
,
4276 struct pt_regs
*regs
)
4278 struct hw_perf_event
*hwc
= &event
->hw
;
4281 data
->period
= event
->hw
.last_period
;
4283 overflow
= perf_swevent_set_period(event
);
4285 if (hwc
->interrupts
== MAX_INTERRUPTS
)
4288 for (; overflow
; overflow
--) {
4289 if (__perf_event_overflow(event
, nmi
, throttle
,
4292 * We inhibit the overflow from happening when
4293 * hwc->interrupts == MAX_INTERRUPTS.
4301 static void perf_swevent_event(struct perf_event
*event
, u64 nr
,
4302 int nmi
, struct perf_sample_data
*data
,
4303 struct pt_regs
*regs
)
4305 struct hw_perf_event
*hwc
= &event
->hw
;
4307 local64_add(nr
, &event
->count
);
4312 if (!hwc
->sample_period
)
4315 if (nr
== 1 && hwc
->sample_period
== 1 && !event
->attr
.freq
)
4316 return perf_swevent_overflow(event
, 1, nmi
, data
, regs
);
4318 if (local64_add_negative(nr
, &hwc
->period_left
))
4321 perf_swevent_overflow(event
, 0, nmi
, data
, regs
);
4324 static int perf_exclude_event(struct perf_event
*event
,
4325 struct pt_regs
*regs
)
4327 if (event
->hw
.state
& PERF_HES_STOPPED
)
4331 if (event
->attr
.exclude_user
&& user_mode(regs
))
4334 if (event
->attr
.exclude_kernel
&& !user_mode(regs
))
4341 static int perf_swevent_match(struct perf_event
*event
,
4342 enum perf_type_id type
,
4344 struct perf_sample_data
*data
,
4345 struct pt_regs
*regs
)
4347 if (event
->attr
.type
!= type
)
4350 if (event
->attr
.config
!= event_id
)
4353 if (perf_exclude_event(event
, regs
))
4359 static inline u64
swevent_hash(u64 type
, u32 event_id
)
4361 u64 val
= event_id
| (type
<< 32);
4363 return hash_64(val
, SWEVENT_HLIST_BITS
);
4366 static inline struct hlist_head
*
4367 __find_swevent_head(struct swevent_hlist
*hlist
, u64 type
, u32 event_id
)
4369 u64 hash
= swevent_hash(type
, event_id
);
4371 return &hlist
->heads
[hash
];
4374 /* For the read side: events when they trigger */
4375 static inline struct hlist_head
*
4376 find_swevent_head_rcu(struct swevent_htable
*swhash
, u64 type
, u32 event_id
)
4378 struct swevent_hlist
*hlist
;
4380 hlist
= rcu_dereference(swhash
->swevent_hlist
);
4384 return __find_swevent_head(hlist
, type
, event_id
);
4387 /* For the event head insertion and removal in the hlist */
4388 static inline struct hlist_head
*
4389 find_swevent_head(struct swevent_htable
*swhash
, struct perf_event
*event
)
4391 struct swevent_hlist
*hlist
;
4392 u32 event_id
= event
->attr
.config
;
4393 u64 type
= event
->attr
.type
;
4396 * Event scheduling is always serialized against hlist allocation
4397 * and release. Which makes the protected version suitable here.
4398 * The context lock guarantees that.
4400 hlist
= rcu_dereference_protected(swhash
->swevent_hlist
,
4401 lockdep_is_held(&event
->ctx
->lock
));
4405 return __find_swevent_head(hlist
, type
, event_id
);
4408 static void do_perf_sw_event(enum perf_type_id type
, u32 event_id
,
4410 struct perf_sample_data
*data
,
4411 struct pt_regs
*regs
)
4413 struct swevent_htable
*swhash
= &__get_cpu_var(swevent_htable
);
4414 struct perf_event
*event
;
4415 struct hlist_node
*node
;
4416 struct hlist_head
*head
;
4419 head
= find_swevent_head_rcu(swhash
, type
, event_id
);
4423 hlist_for_each_entry_rcu(event
, node
, head
, hlist_entry
) {
4424 if (perf_swevent_match(event
, type
, event_id
, data
, regs
))
4425 perf_swevent_event(event
, nr
, nmi
, data
, regs
);
4431 int perf_swevent_get_recursion_context(void)
4433 struct swevent_htable
*swhash
= &__get_cpu_var(swevent_htable
);
4435 return get_recursion_context(swhash
->recursion
);
4437 EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context
);
4439 void inline perf_swevent_put_recursion_context(int rctx
)
4441 struct swevent_htable
*swhash
= &__get_cpu_var(swevent_htable
);
4443 put_recursion_context(swhash
->recursion
, rctx
);
4446 void __perf_sw_event(u32 event_id
, u64 nr
, int nmi
,
4447 struct pt_regs
*regs
, u64 addr
)
4449 struct perf_sample_data data
;
4452 preempt_disable_notrace();
4453 rctx
= perf_swevent_get_recursion_context();
4457 perf_sample_data_init(&data
, addr
);
4459 do_perf_sw_event(PERF_TYPE_SOFTWARE
, event_id
, nr
, nmi
, &data
, regs
);
4461 perf_swevent_put_recursion_context(rctx
);
4462 preempt_enable_notrace();
4465 static void perf_swevent_read(struct perf_event
*event
)
4469 static int perf_swevent_add(struct perf_event
*event
, int flags
)
4471 struct swevent_htable
*swhash
= &__get_cpu_var(swevent_htable
);
4472 struct hw_perf_event
*hwc
= &event
->hw
;
4473 struct hlist_head
*head
;
4475 if (hwc
->sample_period
) {
4476 hwc
->last_period
= hwc
->sample_period
;
4477 perf_swevent_set_period(event
);
4480 hwc
->state
= !(flags
& PERF_EF_START
);
4482 head
= find_swevent_head(swhash
, event
);
4483 if (WARN_ON_ONCE(!head
))
4486 hlist_add_head_rcu(&event
->hlist_entry
, head
);
4491 static void perf_swevent_del(struct perf_event
*event
, int flags
)
4493 hlist_del_rcu(&event
->hlist_entry
);
4496 static void perf_swevent_start(struct perf_event
*event
, int flags
)
4498 event
->hw
.state
= 0;
4501 static void perf_swevent_stop(struct perf_event
*event
, int flags
)
4503 event
->hw
.state
= PERF_HES_STOPPED
;
4506 /* Deref the hlist from the update side */
4507 static inline struct swevent_hlist
*
4508 swevent_hlist_deref(struct swevent_htable
*swhash
)
4510 return rcu_dereference_protected(swhash
->swevent_hlist
,
4511 lockdep_is_held(&swhash
->hlist_mutex
));
4514 static void swevent_hlist_release_rcu(struct rcu_head
*rcu_head
)
4516 struct swevent_hlist
*hlist
;
4518 hlist
= container_of(rcu_head
, struct swevent_hlist
, rcu_head
);
4522 static void swevent_hlist_release(struct swevent_htable
*swhash
)
4524 struct swevent_hlist
*hlist
= swevent_hlist_deref(swhash
);
4529 rcu_assign_pointer(swhash
->swevent_hlist
, NULL
);
4530 call_rcu(&hlist
->rcu_head
, swevent_hlist_release_rcu
);
4533 static void swevent_hlist_put_cpu(struct perf_event
*event
, int cpu
)
4535 struct swevent_htable
*swhash
= &per_cpu(swevent_htable
, cpu
);
4537 mutex_lock(&swhash
->hlist_mutex
);
4539 if (!--swhash
->hlist_refcount
)
4540 swevent_hlist_release(swhash
);
4542 mutex_unlock(&swhash
->hlist_mutex
);
4545 static void swevent_hlist_put(struct perf_event
*event
)
4549 if (event
->cpu
!= -1) {
4550 swevent_hlist_put_cpu(event
, event
->cpu
);
4554 for_each_possible_cpu(cpu
)
4555 swevent_hlist_put_cpu(event
, cpu
);
4558 static int swevent_hlist_get_cpu(struct perf_event
*event
, int cpu
)
4560 struct swevent_htable
*swhash
= &per_cpu(swevent_htable
, cpu
);
4563 mutex_lock(&swhash
->hlist_mutex
);
4565 if (!swevent_hlist_deref(swhash
) && cpu_online(cpu
)) {
4566 struct swevent_hlist
*hlist
;
4568 hlist
= kzalloc(sizeof(*hlist
), GFP_KERNEL
);
4573 rcu_assign_pointer(swhash
->swevent_hlist
, hlist
);
4575 swhash
->hlist_refcount
++;
4577 mutex_unlock(&swhash
->hlist_mutex
);
4582 static int swevent_hlist_get(struct perf_event
*event
)
4585 int cpu
, failed_cpu
;
4587 if (event
->cpu
!= -1)
4588 return swevent_hlist_get_cpu(event
, event
->cpu
);
4591 for_each_possible_cpu(cpu
) {
4592 err
= swevent_hlist_get_cpu(event
, cpu
);
4602 for_each_possible_cpu(cpu
) {
4603 if (cpu
== failed_cpu
)
4605 swevent_hlist_put_cpu(event
, cpu
);
4612 atomic_t perf_swevent_enabled
[PERF_COUNT_SW_MAX
];
4614 static void sw_perf_event_destroy(struct perf_event
*event
)
4616 u64 event_id
= event
->attr
.config
;
4618 WARN_ON(event
->parent
);
4620 jump_label_dec(&perf_swevent_enabled
[event_id
]);
4621 swevent_hlist_put(event
);
4624 static int perf_swevent_init(struct perf_event
*event
)
4626 int event_id
= event
->attr
.config
;
4628 if (event
->attr
.type
!= PERF_TYPE_SOFTWARE
)
4632 case PERF_COUNT_SW_CPU_CLOCK
:
4633 case PERF_COUNT_SW_TASK_CLOCK
:
4640 if (event_id
> PERF_COUNT_SW_MAX
)
4643 if (!event
->parent
) {
4646 err
= swevent_hlist_get(event
);
4650 jump_label_inc(&perf_swevent_enabled
[event_id
]);
4651 event
->destroy
= sw_perf_event_destroy
;
4657 static struct pmu perf_swevent
= {
4658 .task_ctx_nr
= perf_sw_context
,
4660 .event_init
= perf_swevent_init
,
4661 .add
= perf_swevent_add
,
4662 .del
= perf_swevent_del
,
4663 .start
= perf_swevent_start
,
4664 .stop
= perf_swevent_stop
,
4665 .read
= perf_swevent_read
,
4668 #ifdef CONFIG_EVENT_TRACING
4670 static int perf_tp_filter_match(struct perf_event
*event
,
4671 struct perf_sample_data
*data
)
4673 void *record
= data
->raw
->data
;
4675 if (likely(!event
->filter
) || filter_match_preds(event
->filter
, record
))
4680 static int perf_tp_event_match(struct perf_event
*event
,
4681 struct perf_sample_data
*data
,
4682 struct pt_regs
*regs
)
4685 * All tracepoints are from kernel-space.
4687 if (event
->attr
.exclude_kernel
)
4690 if (!perf_tp_filter_match(event
, data
))
4696 void perf_tp_event(u64 addr
, u64 count
, void *record
, int entry_size
,
4697 struct pt_regs
*regs
, struct hlist_head
*head
, int rctx
)
4699 struct perf_sample_data data
;
4700 struct perf_event
*event
;
4701 struct hlist_node
*node
;
4703 struct perf_raw_record raw
= {
4708 perf_sample_data_init(&data
, addr
);
4711 hlist_for_each_entry_rcu(event
, node
, head
, hlist_entry
) {
4712 if (perf_tp_event_match(event
, &data
, regs
))
4713 perf_swevent_event(event
, count
, 1, &data
, regs
);
4716 perf_swevent_put_recursion_context(rctx
);
4718 EXPORT_SYMBOL_GPL(perf_tp_event
);
4720 static void tp_perf_event_destroy(struct perf_event
*event
)
4722 perf_trace_destroy(event
);
4725 static int perf_tp_event_init(struct perf_event
*event
)
4729 if (event
->attr
.type
!= PERF_TYPE_TRACEPOINT
)
4733 * Raw tracepoint data is a severe data leak, only allow root to
4736 if ((event
->attr
.sample_type
& PERF_SAMPLE_RAW
) &&
4737 perf_paranoid_tracepoint_raw() &&
4738 !capable(CAP_SYS_ADMIN
))
4741 err
= perf_trace_init(event
);
4745 event
->destroy
= tp_perf_event_destroy
;
4750 static struct pmu perf_tracepoint
= {
4751 .task_ctx_nr
= perf_sw_context
,
4753 .event_init
= perf_tp_event_init
,
4754 .add
= perf_trace_add
,
4755 .del
= perf_trace_del
,
4756 .start
= perf_swevent_start
,
4757 .stop
= perf_swevent_stop
,
4758 .read
= perf_swevent_read
,
4761 static inline void perf_tp_register(void)
4763 perf_pmu_register(&perf_tracepoint
);
4766 static int perf_event_set_filter(struct perf_event
*event
, void __user
*arg
)
4771 if (event
->attr
.type
!= PERF_TYPE_TRACEPOINT
)
4774 filter_str
= strndup_user(arg
, PAGE_SIZE
);
4775 if (IS_ERR(filter_str
))
4776 return PTR_ERR(filter_str
);
4778 ret
= ftrace_profile_set_filter(event
, event
->attr
.config
, filter_str
);
4784 static void perf_event_free_filter(struct perf_event
*event
)
4786 ftrace_profile_free_filter(event
);
4791 static inline void perf_tp_register(void)
4795 static int perf_event_set_filter(struct perf_event
*event
, void __user
*arg
)
4800 static void perf_event_free_filter(struct perf_event
*event
)
4804 #endif /* CONFIG_EVENT_TRACING */
4806 #ifdef CONFIG_HAVE_HW_BREAKPOINT
4807 void perf_bp_event(struct perf_event
*bp
, void *data
)
4809 struct perf_sample_data sample
;
4810 struct pt_regs
*regs
= data
;
4812 perf_sample_data_init(&sample
, bp
->attr
.bp_addr
);
4814 if (!bp
->hw
.state
&& !perf_exclude_event(bp
, regs
))
4815 perf_swevent_event(bp
, 1, 1, &sample
, regs
);
4820 * hrtimer based swevent callback
4823 static enum hrtimer_restart
perf_swevent_hrtimer(struct hrtimer
*hrtimer
)
4825 enum hrtimer_restart ret
= HRTIMER_RESTART
;
4826 struct perf_sample_data data
;
4827 struct pt_regs
*regs
;
4828 struct perf_event
*event
;
4831 event
= container_of(hrtimer
, struct perf_event
, hw
.hrtimer
);
4832 event
->pmu
->read(event
);
4834 perf_sample_data_init(&data
, 0);
4835 data
.period
= event
->hw
.last_period
;
4836 regs
= get_irq_regs();
4838 if (regs
&& !perf_exclude_event(event
, regs
)) {
4839 if (!(event
->attr
.exclude_idle
&& current
->pid
== 0))
4840 if (perf_event_overflow(event
, 0, &data
, regs
))
4841 ret
= HRTIMER_NORESTART
;
4844 period
= max_t(u64
, 10000, event
->hw
.sample_period
);
4845 hrtimer_forward_now(hrtimer
, ns_to_ktime(period
));
4850 static void perf_swevent_start_hrtimer(struct perf_event
*event
)
4852 struct hw_perf_event
*hwc
= &event
->hw
;
4854 hrtimer_init(&hwc
->hrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
4855 hwc
->hrtimer
.function
= perf_swevent_hrtimer
;
4856 if (hwc
->sample_period
) {
4857 s64 period
= local64_read(&hwc
->period_left
);
4863 local64_set(&hwc
->period_left
, 0);
4865 period
= max_t(u64
, 10000, hwc
->sample_period
);
4867 __hrtimer_start_range_ns(&hwc
->hrtimer
,
4868 ns_to_ktime(period
), 0,
4869 HRTIMER_MODE_REL_PINNED
, 0);
4873 static void perf_swevent_cancel_hrtimer(struct perf_event
*event
)
4875 struct hw_perf_event
*hwc
= &event
->hw
;
4877 if (hwc
->sample_period
) {
4878 ktime_t remaining
= hrtimer_get_remaining(&hwc
->hrtimer
);
4879 local64_set(&hwc
->period_left
, ktime_to_ns(remaining
));
4881 hrtimer_cancel(&hwc
->hrtimer
);
4886 * Software event: cpu wall time clock
4889 static void cpu_clock_event_update(struct perf_event
*event
)
4894 now
= local_clock();
4895 prev
= local64_xchg(&event
->hw
.prev_count
, now
);
4896 local64_add(now
- prev
, &event
->count
);
4899 static void cpu_clock_event_start(struct perf_event
*event
, int flags
)
4901 local64_set(&event
->hw
.prev_count
, local_clock());
4902 perf_swevent_start_hrtimer(event
);
4905 static void cpu_clock_event_stop(struct perf_event
*event
, int flags
)
4907 perf_swevent_cancel_hrtimer(event
);
4908 cpu_clock_event_update(event
);
4911 static int cpu_clock_event_add(struct perf_event
*event
, int flags
)
4913 if (flags
& PERF_EF_START
)
4914 cpu_clock_event_start(event
, flags
);
4919 static void cpu_clock_event_del(struct perf_event
*event
, int flags
)
4921 cpu_clock_event_stop(event
, flags
);
4924 static void cpu_clock_event_read(struct perf_event
*event
)
4926 cpu_clock_event_update(event
);
4929 static int cpu_clock_event_init(struct perf_event
*event
)
4931 if (event
->attr
.type
!= PERF_TYPE_SOFTWARE
)
4934 if (event
->attr
.config
!= PERF_COUNT_SW_CPU_CLOCK
)
4940 static struct pmu perf_cpu_clock
= {
4941 .task_ctx_nr
= perf_sw_context
,
4943 .event_init
= cpu_clock_event_init
,
4944 .add
= cpu_clock_event_add
,
4945 .del
= cpu_clock_event_del
,
4946 .start
= cpu_clock_event_start
,
4947 .stop
= cpu_clock_event_stop
,
4948 .read
= cpu_clock_event_read
,
4952 * Software event: task time clock
4955 static void task_clock_event_update(struct perf_event
*event
, u64 now
)
4960 prev
= local64_xchg(&event
->hw
.prev_count
, now
);
4962 local64_add(delta
, &event
->count
);
4965 static void task_clock_event_start(struct perf_event
*event
, int flags
)
4967 local64_set(&event
->hw
.prev_count
, event
->ctx
->time
);
4968 perf_swevent_start_hrtimer(event
);
4971 static void task_clock_event_stop(struct perf_event
*event
, int flags
)
4973 perf_swevent_cancel_hrtimer(event
);
4974 task_clock_event_update(event
, event
->ctx
->time
);
4977 static int task_clock_event_add(struct perf_event
*event
, int flags
)
4979 if (flags
& PERF_EF_START
)
4980 task_clock_event_start(event
, flags
);
4985 static void task_clock_event_del(struct perf_event
*event
, int flags
)
4987 task_clock_event_stop(event
, PERF_EF_UPDATE
);
4990 static void task_clock_event_read(struct perf_event
*event
)
4995 update_context_time(event
->ctx
);
4996 time
= event
->ctx
->time
;
4998 u64 now
= perf_clock();
4999 u64 delta
= now
- event
->ctx
->timestamp
;
5000 time
= event
->ctx
->time
+ delta
;
5003 task_clock_event_update(event
, time
);
5006 static int task_clock_event_init(struct perf_event
*event
)
5008 if (event
->attr
.type
!= PERF_TYPE_SOFTWARE
)
5011 if (event
->attr
.config
!= PERF_COUNT_SW_TASK_CLOCK
)
5017 static struct pmu perf_task_clock
= {
5018 .task_ctx_nr
= perf_sw_context
,
5020 .event_init
= task_clock_event_init
,
5021 .add
= task_clock_event_add
,
5022 .del
= task_clock_event_del
,
5023 .start
= task_clock_event_start
,
5024 .stop
= task_clock_event_stop
,
5025 .read
= task_clock_event_read
,
5028 static void perf_pmu_nop_void(struct pmu
*pmu
)
5032 static int perf_pmu_nop_int(struct pmu
*pmu
)
5037 static void perf_pmu_start_txn(struct pmu
*pmu
)
5039 perf_pmu_disable(pmu
);
5042 static int perf_pmu_commit_txn(struct pmu
*pmu
)
5044 perf_pmu_enable(pmu
);
5048 static void perf_pmu_cancel_txn(struct pmu
*pmu
)
5050 perf_pmu_enable(pmu
);
5054 * Ensures all contexts with the same task_ctx_nr have the same
5055 * pmu_cpu_context too.
5057 static void *find_pmu_context(int ctxn
)
5064 list_for_each_entry(pmu
, &pmus
, entry
) {
5065 if (pmu
->task_ctx_nr
== ctxn
)
5066 return pmu
->pmu_cpu_context
;
5072 static void free_pmu_context(void * __percpu cpu_context
)
5076 mutex_lock(&pmus_lock
);
5078 * Like a real lame refcount.
5080 list_for_each_entry(pmu
, &pmus
, entry
) {
5081 if (pmu
->pmu_cpu_context
== cpu_context
)
5085 free_percpu(cpu_context
);
5087 mutex_unlock(&pmus_lock
);
5090 int perf_pmu_register(struct pmu
*pmu
)
5094 mutex_lock(&pmus_lock
);
5096 pmu
->pmu_disable_count
= alloc_percpu(int);
5097 if (!pmu
->pmu_disable_count
)
5100 pmu
->pmu_cpu_context
= find_pmu_context(pmu
->task_ctx_nr
);
5101 if (pmu
->pmu_cpu_context
)
5102 goto got_cpu_context
;
5104 pmu
->pmu_cpu_context
= alloc_percpu(struct perf_cpu_context
);
5105 if (!pmu
->pmu_cpu_context
)
5108 for_each_possible_cpu(cpu
) {
5109 struct perf_cpu_context
*cpuctx
;
5111 cpuctx
= per_cpu_ptr(pmu
->pmu_cpu_context
, cpu
);
5112 __perf_event_init_context(&cpuctx
->ctx
);
5113 cpuctx
->ctx
.type
= cpu_context
;
5114 cpuctx
->ctx
.pmu
= pmu
;
5115 cpuctx
->jiffies_interval
= 1;
5116 INIT_LIST_HEAD(&cpuctx
->rotation_list
);
5120 if (!pmu
->start_txn
) {
5121 if (pmu
->pmu_enable
) {
5123 * If we have pmu_enable/pmu_disable calls, install
5124 * transaction stubs that use that to try and batch
5125 * hardware accesses.
5127 pmu
->start_txn
= perf_pmu_start_txn
;
5128 pmu
->commit_txn
= perf_pmu_commit_txn
;
5129 pmu
->cancel_txn
= perf_pmu_cancel_txn
;
5131 pmu
->start_txn
= perf_pmu_nop_void
;
5132 pmu
->commit_txn
= perf_pmu_nop_int
;
5133 pmu
->cancel_txn
= perf_pmu_nop_void
;
5137 if (!pmu
->pmu_enable
) {
5138 pmu
->pmu_enable
= perf_pmu_nop_void
;
5139 pmu
->pmu_disable
= perf_pmu_nop_void
;
5142 list_add_rcu(&pmu
->entry
, &pmus
);
5145 mutex_unlock(&pmus_lock
);
5150 free_percpu(pmu
->pmu_disable_count
);
5154 void perf_pmu_unregister(struct pmu
*pmu
)
5156 mutex_lock(&pmus_lock
);
5157 list_del_rcu(&pmu
->entry
);
5158 mutex_unlock(&pmus_lock
);
5161 * We dereference the pmu list under both SRCU and regular RCU, so
5162 * synchronize against both of those.
5164 synchronize_srcu(&pmus_srcu
);
5167 free_percpu(pmu
->pmu_disable_count
);
5168 free_pmu_context(pmu
->pmu_cpu_context
);
5171 struct pmu
*perf_init_event(struct perf_event
*event
)
5173 struct pmu
*pmu
= NULL
;
5176 idx
= srcu_read_lock(&pmus_srcu
);
5177 list_for_each_entry_rcu(pmu
, &pmus
, entry
) {
5178 int ret
= pmu
->event_init(event
);
5182 if (ret
!= -ENOENT
) {
5187 pmu
= ERR_PTR(-ENOENT
);
5189 srcu_read_unlock(&pmus_srcu
, idx
);
5195 * Allocate and initialize a event structure
5197 static struct perf_event
*
5198 perf_event_alloc(struct perf_event_attr
*attr
, int cpu
,
5199 struct task_struct
*task
,
5200 struct perf_event
*group_leader
,
5201 struct perf_event
*parent_event
,
5202 perf_overflow_handler_t overflow_handler
)
5205 struct perf_event
*event
;
5206 struct hw_perf_event
*hwc
;
5209 event
= kzalloc(sizeof(*event
), GFP_KERNEL
);
5211 return ERR_PTR(-ENOMEM
);
5214 * Single events are their own group leaders, with an
5215 * empty sibling list:
5218 group_leader
= event
;
5220 mutex_init(&event
->child_mutex
);
5221 INIT_LIST_HEAD(&event
->child_list
);
5223 INIT_LIST_HEAD(&event
->group_entry
);
5224 INIT_LIST_HEAD(&event
->event_entry
);
5225 INIT_LIST_HEAD(&event
->sibling_list
);
5226 init_waitqueue_head(&event
->waitq
);
5227 init_irq_work(&event
->pending
, perf_pending_event
);
5229 mutex_init(&event
->mmap_mutex
);
5232 event
->attr
= *attr
;
5233 event
->group_leader
= group_leader
;
5237 event
->parent
= parent_event
;
5239 event
->ns
= get_pid_ns(current
->nsproxy
->pid_ns
);
5240 event
->id
= atomic64_inc_return(&perf_event_id
);
5242 event
->state
= PERF_EVENT_STATE_INACTIVE
;
5245 event
->attach_state
= PERF_ATTACH_TASK
;
5246 #ifdef CONFIG_HAVE_HW_BREAKPOINT
5248 * hw_breakpoint is a bit difficult here..
5250 if (attr
->type
== PERF_TYPE_BREAKPOINT
)
5251 event
->hw
.bp_target
= task
;
5255 if (!overflow_handler
&& parent_event
)
5256 overflow_handler
= parent_event
->overflow_handler
;
5258 event
->overflow_handler
= overflow_handler
;
5261 event
->state
= PERF_EVENT_STATE_OFF
;
5266 hwc
->sample_period
= attr
->sample_period
;
5267 if (attr
->freq
&& attr
->sample_freq
)
5268 hwc
->sample_period
= 1;
5269 hwc
->last_period
= hwc
->sample_period
;
5271 local64_set(&hwc
->period_left
, hwc
->sample_period
);
5274 * we currently do not support PERF_FORMAT_GROUP on inherited events
5276 if (attr
->inherit
&& (attr
->read_format
& PERF_FORMAT_GROUP
))
5279 pmu
= perf_init_event(event
);
5285 else if (IS_ERR(pmu
))
5290 put_pid_ns(event
->ns
);
5292 return ERR_PTR(err
);
5297 if (!event
->parent
) {
5298 if (event
->attach_state
& PERF_ATTACH_TASK
)
5299 jump_label_inc(&perf_task_events
);
5300 if (event
->attr
.mmap
|| event
->attr
.mmap_data
)
5301 atomic_inc(&nr_mmap_events
);
5302 if (event
->attr
.comm
)
5303 atomic_inc(&nr_comm_events
);
5304 if (event
->attr
.task
)
5305 atomic_inc(&nr_task_events
);
5306 if (event
->attr
.sample_type
& PERF_SAMPLE_CALLCHAIN
) {
5307 err
= get_callchain_buffers();
5310 return ERR_PTR(err
);
5318 static int perf_copy_attr(struct perf_event_attr __user
*uattr
,
5319 struct perf_event_attr
*attr
)
5324 if (!access_ok(VERIFY_WRITE
, uattr
, PERF_ATTR_SIZE_VER0
))
5328 * zero the full structure, so that a short copy will be nice.
5330 memset(attr
, 0, sizeof(*attr
));
5332 ret
= get_user(size
, &uattr
->size
);
5336 if (size
> PAGE_SIZE
) /* silly large */
5339 if (!size
) /* abi compat */
5340 size
= PERF_ATTR_SIZE_VER0
;
5342 if (size
< PERF_ATTR_SIZE_VER0
)
5346 * If we're handed a bigger struct than we know of,
5347 * ensure all the unknown bits are 0 - i.e. new
5348 * user-space does not rely on any kernel feature
5349 * extensions we dont know about yet.
5351 if (size
> sizeof(*attr
)) {
5352 unsigned char __user
*addr
;
5353 unsigned char __user
*end
;
5356 addr
= (void __user
*)uattr
+ sizeof(*attr
);
5357 end
= (void __user
*)uattr
+ size
;
5359 for (; addr
< end
; addr
++) {
5360 ret
= get_user(val
, addr
);
5366 size
= sizeof(*attr
);
5369 ret
= copy_from_user(attr
, uattr
, size
);
5374 * If the type exists, the corresponding creation will verify
5377 if (attr
->type
>= PERF_TYPE_MAX
)
5380 if (attr
->__reserved_1
)
5383 if (attr
->sample_type
& ~(PERF_SAMPLE_MAX
-1))
5386 if (attr
->read_format
& ~(PERF_FORMAT_MAX
-1))
5393 put_user(sizeof(*attr
), &uattr
->size
);
5399 perf_event_set_output(struct perf_event
*event
, struct perf_event
*output_event
)
5401 struct perf_buffer
*buffer
= NULL
, *old_buffer
= NULL
;
5407 /* don't allow circular references */
5408 if (event
== output_event
)
5412 * Don't allow cross-cpu buffers
5414 if (output_event
->cpu
!= event
->cpu
)
5418 * If its not a per-cpu buffer, it must be the same task.
5420 if (output_event
->cpu
== -1 && output_event
->ctx
!= event
->ctx
)
5424 mutex_lock(&event
->mmap_mutex
);
5425 /* Can't redirect output if we've got an active mmap() */
5426 if (atomic_read(&event
->mmap_count
))
5430 /* get the buffer we want to redirect to */
5431 buffer
= perf_buffer_get(output_event
);
5436 old_buffer
= event
->buffer
;
5437 rcu_assign_pointer(event
->buffer
, buffer
);
5440 mutex_unlock(&event
->mmap_mutex
);
5443 perf_buffer_put(old_buffer
);
5449 * sys_perf_event_open - open a performance event, associate it to a task/cpu
5451 * @attr_uptr: event_id type attributes for monitoring/sampling
5454 * @group_fd: group leader event fd
5456 SYSCALL_DEFINE5(perf_event_open
,
5457 struct perf_event_attr __user
*, attr_uptr
,
5458 pid_t
, pid
, int, cpu
, int, group_fd
, unsigned long, flags
)
5460 struct perf_event
*group_leader
= NULL
, *output_event
= NULL
;
5461 struct perf_event
*event
, *sibling
;
5462 struct perf_event_attr attr
;
5463 struct perf_event_context
*ctx
;
5464 struct file
*event_file
= NULL
;
5465 struct file
*group_file
= NULL
;
5466 struct task_struct
*task
= NULL
;
5470 int fput_needed
= 0;
5473 /* for future expandability... */
5474 if (flags
& ~(PERF_FLAG_FD_NO_GROUP
| PERF_FLAG_FD_OUTPUT
))
5477 err
= perf_copy_attr(attr_uptr
, &attr
);
5481 if (!attr
.exclude_kernel
) {
5482 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN
))
5487 if (attr
.sample_freq
> sysctl_perf_event_sample_rate
)
5491 event_fd
= get_unused_fd_flags(O_RDWR
);
5495 if (group_fd
!= -1) {
5496 group_leader
= perf_fget_light(group_fd
, &fput_needed
);
5497 if (IS_ERR(group_leader
)) {
5498 err
= PTR_ERR(group_leader
);
5501 group_file
= group_leader
->filp
;
5502 if (flags
& PERF_FLAG_FD_OUTPUT
)
5503 output_event
= group_leader
;
5504 if (flags
& PERF_FLAG_FD_NO_GROUP
)
5505 group_leader
= NULL
;
5509 task
= find_lively_task_by_vpid(pid
);
5511 err
= PTR_ERR(task
);
5516 event
= perf_event_alloc(&attr
, cpu
, task
, group_leader
, NULL
, NULL
);
5517 if (IS_ERR(event
)) {
5518 err
= PTR_ERR(event
);
5523 * Special case software events and allow them to be part of
5524 * any hardware group.
5529 (is_software_event(event
) != is_software_event(group_leader
))) {
5530 if (is_software_event(event
)) {
5532 * If event and group_leader are not both a software
5533 * event, and event is, then group leader is not.
5535 * Allow the addition of software events to !software
5536 * groups, this is safe because software events never
5539 pmu
= group_leader
->pmu
;
5540 } else if (is_software_event(group_leader
) &&
5541 (group_leader
->group_flags
& PERF_GROUP_SOFTWARE
)) {
5543 * In case the group is a pure software group, and we
5544 * try to add a hardware event, move the whole group to
5545 * the hardware context.
5552 * Get the target context (task or percpu):
5554 ctx
= find_get_context(pmu
, task
, cpu
);
5561 * Look up the group leader (we will attach this event to it):
5567 * Do not allow a recursive hierarchy (this new sibling
5568 * becoming part of another group-sibling):
5570 if (group_leader
->group_leader
!= group_leader
)
5573 * Do not allow to attach to a group in a different
5574 * task or CPU context:
5577 if (group_leader
->ctx
->type
!= ctx
->type
)
5580 if (group_leader
->ctx
!= ctx
)
5585 * Only a group leader can be exclusive or pinned
5587 if (attr
.exclusive
|| attr
.pinned
)
5592 err
= perf_event_set_output(event
, output_event
);
5597 event_file
= anon_inode_getfile("[perf_event]", &perf_fops
, event
, O_RDWR
);
5598 if (IS_ERR(event_file
)) {
5599 err
= PTR_ERR(event_file
);
5604 struct perf_event_context
*gctx
= group_leader
->ctx
;
5606 mutex_lock(&gctx
->mutex
);
5607 perf_event_remove_from_context(group_leader
);
5608 list_for_each_entry(sibling
, &group_leader
->sibling_list
,
5610 perf_event_remove_from_context(sibling
);
5613 mutex_unlock(&gctx
->mutex
);
5617 event
->filp
= event_file
;
5618 WARN_ON_ONCE(ctx
->parent_ctx
);
5619 mutex_lock(&ctx
->mutex
);
5622 perf_install_in_context(ctx
, group_leader
, cpu
);
5624 list_for_each_entry(sibling
, &group_leader
->sibling_list
,
5626 perf_install_in_context(ctx
, sibling
, cpu
);
5631 perf_install_in_context(ctx
, event
, cpu
);
5633 mutex_unlock(&ctx
->mutex
);
5635 event
->owner
= current
;
5636 get_task_struct(current
);
5637 mutex_lock(¤t
->perf_event_mutex
);
5638 list_add_tail(&event
->owner_entry
, ¤t
->perf_event_list
);
5639 mutex_unlock(¤t
->perf_event_mutex
);
5642 * Drop the reference on the group_event after placing the
5643 * new event on the sibling_list. This ensures destruction
5644 * of the group leader will find the pointer to itself in
5645 * perf_group_detach().
5647 fput_light(group_file
, fput_needed
);
5648 fd_install(event_fd
, event_file
);
5657 put_task_struct(task
);
5659 fput_light(group_file
, fput_needed
);
5661 put_unused_fd(event_fd
);
5666 * perf_event_create_kernel_counter
5668 * @attr: attributes of the counter to create
5669 * @cpu: cpu in which the counter is bound
5670 * @task: task to profile (NULL for percpu)
5673 perf_event_create_kernel_counter(struct perf_event_attr
*attr
, int cpu
,
5674 struct task_struct
*task
,
5675 perf_overflow_handler_t overflow_handler
)
5677 struct perf_event_context
*ctx
;
5678 struct perf_event
*event
;
5682 * Get the target context (task or percpu):
5685 event
= perf_event_alloc(attr
, cpu
, task
, NULL
, NULL
, overflow_handler
);
5686 if (IS_ERR(event
)) {
5687 err
= PTR_ERR(event
);
5691 ctx
= find_get_context(event
->pmu
, task
, cpu
);
5698 WARN_ON_ONCE(ctx
->parent_ctx
);
5699 mutex_lock(&ctx
->mutex
);
5700 perf_install_in_context(ctx
, event
, cpu
);
5702 mutex_unlock(&ctx
->mutex
);
5704 event
->owner
= current
;
5705 get_task_struct(current
);
5706 mutex_lock(¤t
->perf_event_mutex
);
5707 list_add_tail(&event
->owner_entry
, ¤t
->perf_event_list
);
5708 mutex_unlock(¤t
->perf_event_mutex
);
5715 return ERR_PTR(err
);
5717 EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter
);
5719 static void sync_child_event(struct perf_event
*child_event
,
5720 struct task_struct
*child
)
5722 struct perf_event
*parent_event
= child_event
->parent
;
5725 if (child_event
->attr
.inherit_stat
)
5726 perf_event_read_event(child_event
, child
);
5728 child_val
= perf_event_count(child_event
);
5731 * Add back the child's count to the parent's count:
5733 atomic64_add(child_val
, &parent_event
->child_count
);
5734 atomic64_add(child_event
->total_time_enabled
,
5735 &parent_event
->child_total_time_enabled
);
5736 atomic64_add(child_event
->total_time_running
,
5737 &parent_event
->child_total_time_running
);
5740 * Remove this event from the parent's list
5742 WARN_ON_ONCE(parent_event
->ctx
->parent_ctx
);
5743 mutex_lock(&parent_event
->child_mutex
);
5744 list_del_init(&child_event
->child_list
);
5745 mutex_unlock(&parent_event
->child_mutex
);
5748 * Release the parent event, if this was the last
5751 fput(parent_event
->filp
);
5755 __perf_event_exit_task(struct perf_event
*child_event
,
5756 struct perf_event_context
*child_ctx
,
5757 struct task_struct
*child
)
5759 struct perf_event
*parent_event
;
5761 perf_event_remove_from_context(child_event
);
5763 parent_event
= child_event
->parent
;
5765 * It can happen that parent exits first, and has events
5766 * that are still around due to the child reference. These
5767 * events need to be zapped - but otherwise linger.
5770 sync_child_event(child_event
, child
);
5771 free_event(child_event
);
5775 static void perf_event_exit_task_context(struct task_struct
*child
, int ctxn
)
5777 struct perf_event
*child_event
, *tmp
;
5778 struct perf_event_context
*child_ctx
;
5779 unsigned long flags
;
5781 if (likely(!child
->perf_event_ctxp
[ctxn
])) {
5782 perf_event_task(child
, NULL
, 0);
5786 local_irq_save(flags
);
5788 * We can't reschedule here because interrupts are disabled,
5789 * and either child is current or it is a task that can't be
5790 * scheduled, so we are now safe from rescheduling changing
5793 child_ctx
= child
->perf_event_ctxp
[ctxn
];
5794 task_ctx_sched_out(child_ctx
, EVENT_ALL
);
5797 * Take the context lock here so that if find_get_context is
5798 * reading child->perf_event_ctxp, we wait until it has
5799 * incremented the context's refcount before we do put_ctx below.
5801 raw_spin_lock(&child_ctx
->lock
);
5802 child
->perf_event_ctxp
[ctxn
] = NULL
;
5804 * If this context is a clone; unclone it so it can't get
5805 * swapped to another process while we're removing all
5806 * the events from it.
5808 unclone_ctx(child_ctx
);
5809 update_context_time(child_ctx
);
5810 raw_spin_unlock_irqrestore(&child_ctx
->lock
, flags
);
5813 * Report the task dead after unscheduling the events so that we
5814 * won't get any samples after PERF_RECORD_EXIT. We can however still
5815 * get a few PERF_RECORD_READ events.
5817 perf_event_task(child
, child_ctx
, 0);
5820 * We can recurse on the same lock type through:
5822 * __perf_event_exit_task()
5823 * sync_child_event()
5824 * fput(parent_event->filp)
5826 * mutex_lock(&ctx->mutex)
5828 * But since its the parent context it won't be the same instance.
5830 mutex_lock(&child_ctx
->mutex
);
5833 list_for_each_entry_safe(child_event
, tmp
, &child_ctx
->pinned_groups
,
5835 __perf_event_exit_task(child_event
, child_ctx
, child
);
5837 list_for_each_entry_safe(child_event
, tmp
, &child_ctx
->flexible_groups
,
5839 __perf_event_exit_task(child_event
, child_ctx
, child
);
5842 * If the last event was a group event, it will have appended all
5843 * its siblings to the list, but we obtained 'tmp' before that which
5844 * will still point to the list head terminating the iteration.
5846 if (!list_empty(&child_ctx
->pinned_groups
) ||
5847 !list_empty(&child_ctx
->flexible_groups
))
5850 mutex_unlock(&child_ctx
->mutex
);
5856 * When a child task exits, feed back event values to parent events.
5858 void perf_event_exit_task(struct task_struct
*child
)
5862 for_each_task_context_nr(ctxn
)
5863 perf_event_exit_task_context(child
, ctxn
);
5866 static void perf_free_event(struct perf_event
*event
,
5867 struct perf_event_context
*ctx
)
5869 struct perf_event
*parent
= event
->parent
;
5871 if (WARN_ON_ONCE(!parent
))
5874 mutex_lock(&parent
->child_mutex
);
5875 list_del_init(&event
->child_list
);
5876 mutex_unlock(&parent
->child_mutex
);
5880 perf_group_detach(event
);
5881 list_del_event(event
, ctx
);
5886 * free an unexposed, unused context as created by inheritance by
5887 * perf_event_init_task below, used by fork() in case of fail.
5889 void perf_event_free_task(struct task_struct
*task
)
5891 struct perf_event_context
*ctx
;
5892 struct perf_event
*event
, *tmp
;
5895 for_each_task_context_nr(ctxn
) {
5896 ctx
= task
->perf_event_ctxp
[ctxn
];
5900 mutex_lock(&ctx
->mutex
);
5902 list_for_each_entry_safe(event
, tmp
, &ctx
->pinned_groups
,
5904 perf_free_event(event
, ctx
);
5906 list_for_each_entry_safe(event
, tmp
, &ctx
->flexible_groups
,
5908 perf_free_event(event
, ctx
);
5910 if (!list_empty(&ctx
->pinned_groups
) ||
5911 !list_empty(&ctx
->flexible_groups
))
5914 mutex_unlock(&ctx
->mutex
);
5920 void perf_event_delayed_put(struct task_struct
*task
)
5924 for_each_task_context_nr(ctxn
)
5925 WARN_ON_ONCE(task
->perf_event_ctxp
[ctxn
]);
5929 * inherit a event from parent task to child task:
5931 static struct perf_event
*
5932 inherit_event(struct perf_event
*parent_event
,
5933 struct task_struct
*parent
,
5934 struct perf_event_context
*parent_ctx
,
5935 struct task_struct
*child
,
5936 struct perf_event
*group_leader
,
5937 struct perf_event_context
*child_ctx
)
5939 struct perf_event
*child_event
;
5940 unsigned long flags
;
5943 * Instead of creating recursive hierarchies of events,
5944 * we link inherited events back to the original parent,
5945 * which has a filp for sure, which we use as the reference
5948 if (parent_event
->parent
)
5949 parent_event
= parent_event
->parent
;
5951 child_event
= perf_event_alloc(&parent_event
->attr
,
5954 group_leader
, parent_event
,
5956 if (IS_ERR(child_event
))
5961 * Make the child state follow the state of the parent event,
5962 * not its attr.disabled bit. We hold the parent's mutex,
5963 * so we won't race with perf_event_{en, dis}able_family.
5965 if (parent_event
->state
>= PERF_EVENT_STATE_INACTIVE
)
5966 child_event
->state
= PERF_EVENT_STATE_INACTIVE
;
5968 child_event
->state
= PERF_EVENT_STATE_OFF
;
5970 if (parent_event
->attr
.freq
) {
5971 u64 sample_period
= parent_event
->hw
.sample_period
;
5972 struct hw_perf_event
*hwc
= &child_event
->hw
;
5974 hwc
->sample_period
= sample_period
;
5975 hwc
->last_period
= sample_period
;
5977 local64_set(&hwc
->period_left
, sample_period
);
5980 child_event
->ctx
= child_ctx
;
5981 child_event
->overflow_handler
= parent_event
->overflow_handler
;
5984 * Link it up in the child's context:
5986 raw_spin_lock_irqsave(&child_ctx
->lock
, flags
);
5987 add_event_to_ctx(child_event
, child_ctx
);
5988 raw_spin_unlock_irqrestore(&child_ctx
->lock
, flags
);
5991 * Get a reference to the parent filp - we will fput it
5992 * when the child event exits. This is safe to do because
5993 * we are in the parent and we know that the filp still
5994 * exists and has a nonzero count:
5996 atomic_long_inc(&parent_event
->filp
->f_count
);
5999 * Link this into the parent event's child list
6001 WARN_ON_ONCE(parent_event
->ctx
->parent_ctx
);
6002 mutex_lock(&parent_event
->child_mutex
);
6003 list_add_tail(&child_event
->child_list
, &parent_event
->child_list
);
6004 mutex_unlock(&parent_event
->child_mutex
);
6009 static int inherit_group(struct perf_event
*parent_event
,
6010 struct task_struct
*parent
,
6011 struct perf_event_context
*parent_ctx
,
6012 struct task_struct
*child
,
6013 struct perf_event_context
*child_ctx
)
6015 struct perf_event
*leader
;
6016 struct perf_event
*sub
;
6017 struct perf_event
*child_ctr
;
6019 leader
= inherit_event(parent_event
, parent
, parent_ctx
,
6020 child
, NULL
, child_ctx
);
6022 return PTR_ERR(leader
);
6023 list_for_each_entry(sub
, &parent_event
->sibling_list
, group_entry
) {
6024 child_ctr
= inherit_event(sub
, parent
, parent_ctx
,
6025 child
, leader
, child_ctx
);
6026 if (IS_ERR(child_ctr
))
6027 return PTR_ERR(child_ctr
);
6033 inherit_task_group(struct perf_event
*event
, struct task_struct
*parent
,
6034 struct perf_event_context
*parent_ctx
,
6035 struct task_struct
*child
, int ctxn
,
6039 struct perf_event_context
*child_ctx
;
6041 if (!event
->attr
.inherit
) {
6046 child_ctx
= child
->perf_event_ctxp
[ctxn
];
6049 * This is executed from the parent task context, so
6050 * inherit events that have been marked for cloning.
6051 * First allocate and initialize a context for the
6055 child_ctx
= alloc_perf_context(event
->pmu
, child
);
6059 child
->perf_event_ctxp
[ctxn
] = child_ctx
;
6062 ret
= inherit_group(event
, parent
, parent_ctx
,
6072 * Initialize the perf_event context in task_struct
6074 int perf_event_init_context(struct task_struct
*child
, int ctxn
)
6076 struct perf_event_context
*child_ctx
, *parent_ctx
;
6077 struct perf_event_context
*cloned_ctx
;
6078 struct perf_event
*event
;
6079 struct task_struct
*parent
= current
;
6080 int inherited_all
= 1;
6083 child
->perf_event_ctxp
[ctxn
] = NULL
;
6085 mutex_init(&child
->perf_event_mutex
);
6086 INIT_LIST_HEAD(&child
->perf_event_list
);
6088 if (likely(!parent
->perf_event_ctxp
[ctxn
]))
6092 * If the parent's context is a clone, pin it so it won't get
6095 parent_ctx
= perf_pin_task_context(parent
, ctxn
);
6098 * No need to check if parent_ctx != NULL here; since we saw
6099 * it non-NULL earlier, the only reason for it to become NULL
6100 * is if we exit, and since we're currently in the middle of
6101 * a fork we can't be exiting at the same time.
6105 * Lock the parent list. No need to lock the child - not PID
6106 * hashed yet and not running, so nobody can access it.
6108 mutex_lock(&parent_ctx
->mutex
);
6111 * We dont have to disable NMIs - we are only looking at
6112 * the list, not manipulating it:
6114 list_for_each_entry(event
, &parent_ctx
->pinned_groups
, group_entry
) {
6115 ret
= inherit_task_group(event
, parent
, parent_ctx
,
6116 child
, ctxn
, &inherited_all
);
6121 list_for_each_entry(event
, &parent_ctx
->flexible_groups
, group_entry
) {
6122 ret
= inherit_task_group(event
, parent
, parent_ctx
,
6123 child
, ctxn
, &inherited_all
);
6128 child_ctx
= child
->perf_event_ctxp
[ctxn
];
6130 if (child_ctx
&& inherited_all
) {
6132 * Mark the child context as a clone of the parent
6133 * context, or of whatever the parent is a clone of.
6134 * Note that if the parent is a clone, it could get
6135 * uncloned at any point, but that doesn't matter
6136 * because the list of events and the generation
6137 * count can't have changed since we took the mutex.
6139 cloned_ctx
= rcu_dereference(parent_ctx
->parent_ctx
);
6141 child_ctx
->parent_ctx
= cloned_ctx
;
6142 child_ctx
->parent_gen
= parent_ctx
->parent_gen
;
6144 child_ctx
->parent_ctx
= parent_ctx
;
6145 child_ctx
->parent_gen
= parent_ctx
->generation
;
6147 get_ctx(child_ctx
->parent_ctx
);
6150 mutex_unlock(&parent_ctx
->mutex
);
6152 perf_unpin_context(parent_ctx
);
6158 * Initialize the perf_event context in task_struct
6160 int perf_event_init_task(struct task_struct
*child
)
6164 for_each_task_context_nr(ctxn
) {
6165 ret
= perf_event_init_context(child
, ctxn
);
6173 static void __init
perf_event_init_all_cpus(void)
6175 struct swevent_htable
*swhash
;
6178 for_each_possible_cpu(cpu
) {
6179 swhash
= &per_cpu(swevent_htable
, cpu
);
6180 mutex_init(&swhash
->hlist_mutex
);
6181 INIT_LIST_HEAD(&per_cpu(rotation_list
, cpu
));
6185 static void __cpuinit
perf_event_init_cpu(int cpu
)
6187 struct swevent_htable
*swhash
= &per_cpu(swevent_htable
, cpu
);
6189 mutex_lock(&swhash
->hlist_mutex
);
6190 if (swhash
->hlist_refcount
> 0) {
6191 struct swevent_hlist
*hlist
;
6193 hlist
= kzalloc_node(sizeof(*hlist
), GFP_KERNEL
, cpu_to_node(cpu
));
6195 rcu_assign_pointer(swhash
->swevent_hlist
, hlist
);
6197 mutex_unlock(&swhash
->hlist_mutex
);
6200 #ifdef CONFIG_HOTPLUG_CPU
6201 static void perf_pmu_rotate_stop(struct pmu
*pmu
)
6203 struct perf_cpu_context
*cpuctx
= this_cpu_ptr(pmu
->pmu_cpu_context
);
6205 WARN_ON(!irqs_disabled());
6207 list_del_init(&cpuctx
->rotation_list
);
6210 static void __perf_event_exit_context(void *__info
)
6212 struct perf_event_context
*ctx
= __info
;
6213 struct perf_event
*event
, *tmp
;
6215 perf_pmu_rotate_stop(ctx
->pmu
);
6217 list_for_each_entry_safe(event
, tmp
, &ctx
->pinned_groups
, group_entry
)
6218 __perf_event_remove_from_context(event
);
6219 list_for_each_entry_safe(event
, tmp
, &ctx
->flexible_groups
, group_entry
)
6220 __perf_event_remove_from_context(event
);
6223 static void perf_event_exit_cpu_context(int cpu
)
6225 struct perf_event_context
*ctx
;
6229 idx
= srcu_read_lock(&pmus_srcu
);
6230 list_for_each_entry_rcu(pmu
, &pmus
, entry
) {
6231 ctx
= &per_cpu_ptr(pmu
->pmu_cpu_context
, cpu
)->ctx
;
6233 mutex_lock(&ctx
->mutex
);
6234 smp_call_function_single(cpu
, __perf_event_exit_context
, ctx
, 1);
6235 mutex_unlock(&ctx
->mutex
);
6237 srcu_read_unlock(&pmus_srcu
, idx
);
6240 static void perf_event_exit_cpu(int cpu
)
6242 struct swevent_htable
*swhash
= &per_cpu(swevent_htable
, cpu
);
6244 mutex_lock(&swhash
->hlist_mutex
);
6245 swevent_hlist_release(swhash
);
6246 mutex_unlock(&swhash
->hlist_mutex
);
6248 perf_event_exit_cpu_context(cpu
);
6251 static inline void perf_event_exit_cpu(int cpu
) { }
6254 static int __cpuinit
6255 perf_cpu_notify(struct notifier_block
*self
, unsigned long action
, void *hcpu
)
6257 unsigned int cpu
= (long)hcpu
;
6259 switch (action
& ~CPU_TASKS_FROZEN
) {
6261 case CPU_UP_PREPARE
:
6262 case CPU_DOWN_FAILED
:
6263 perf_event_init_cpu(cpu
);
6266 case CPU_UP_CANCELED
:
6267 case CPU_DOWN_PREPARE
:
6268 perf_event_exit_cpu(cpu
);
6278 void __init
perf_event_init(void)
6280 perf_event_init_all_cpus();
6281 init_srcu_struct(&pmus_srcu
);
6282 perf_pmu_register(&perf_swevent
);
6283 perf_pmu_register(&perf_cpu_clock
);
6284 perf_pmu_register(&perf_task_clock
);
6286 perf_cpu_notifier(perf_cpu_notify
);