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>
38 * Each CPU has a list of per CPU events:
40 static DEFINE_PER_CPU(struct perf_cpu_context
, perf_cpu_context
);
42 int perf_max_events __read_mostly
= 1;
43 static int perf_reserved_percpu __read_mostly
;
44 static int perf_overcommit __read_mostly
= 1;
46 static atomic_t nr_events __read_mostly
;
47 static atomic_t nr_mmap_events __read_mostly
;
48 static atomic_t nr_comm_events __read_mostly
;
49 static atomic_t nr_task_events __read_mostly
;
52 * perf event paranoia level:
53 * -1 - not paranoid at all
54 * 0 - disallow raw tracepoint access for unpriv
55 * 1 - disallow cpu events for unpriv
56 * 2 - disallow kernel profiling for unpriv
58 int sysctl_perf_event_paranoid __read_mostly
= 1;
60 int sysctl_perf_event_mlock __read_mostly
= 512; /* 'free' kb per user */
63 * max perf event sample rate
65 int sysctl_perf_event_sample_rate __read_mostly
= 100000;
67 static atomic64_t perf_event_id
;
70 * Lock for (sysadmin-configurable) event reservations:
72 static DEFINE_SPINLOCK(perf_resource_lock
);
74 void __weak
hw_perf_disable(void) { barrier(); }
75 void __weak
hw_perf_enable(void) { barrier(); }
77 void __weak
perf_event_print_debug(void) { }
79 static DEFINE_PER_CPU(int, perf_disable_count
);
81 void perf_disable(void)
83 if (!__get_cpu_var(perf_disable_count
)++)
87 void perf_enable(void)
89 if (!--__get_cpu_var(perf_disable_count
))
93 static void get_ctx(struct perf_event_context
*ctx
)
95 WARN_ON(!atomic_inc_not_zero(&ctx
->refcount
));
98 static void free_ctx(struct rcu_head
*head
)
100 struct perf_event_context
*ctx
;
102 ctx
= container_of(head
, struct perf_event_context
, rcu_head
);
106 static void put_ctx(struct perf_event_context
*ctx
)
108 if (atomic_dec_and_test(&ctx
->refcount
)) {
110 put_ctx(ctx
->parent_ctx
);
112 put_task_struct(ctx
->task
);
113 call_rcu(&ctx
->rcu_head
, free_ctx
);
117 static void unclone_ctx(struct perf_event_context
*ctx
)
119 if (ctx
->parent_ctx
) {
120 put_ctx(ctx
->parent_ctx
);
121 ctx
->parent_ctx
= NULL
;
126 * If we inherit events we want to return the parent event id
129 static u64
primary_event_id(struct perf_event
*event
)
134 id
= event
->parent
->id
;
140 * Get the perf_event_context for a task and lock it.
141 * This has to cope with with the fact that until it is locked,
142 * the context could get moved to another task.
144 static struct perf_event_context
*
145 perf_lock_task_context(struct task_struct
*task
, unsigned long *flags
)
147 struct perf_event_context
*ctx
;
151 ctx
= rcu_dereference(task
->perf_event_ctxp
);
154 * If this context is a clone of another, it might
155 * get swapped for another underneath us by
156 * perf_event_task_sched_out, though the
157 * rcu_read_lock() protects us from any context
158 * getting freed. Lock the context and check if it
159 * got swapped before we could get the lock, and retry
160 * if so. If we locked the right context, then it
161 * can't get swapped on us any more.
163 raw_spin_lock_irqsave(&ctx
->lock
, *flags
);
164 if (ctx
!= rcu_dereference(task
->perf_event_ctxp
)) {
165 raw_spin_unlock_irqrestore(&ctx
->lock
, *flags
);
169 if (!atomic_inc_not_zero(&ctx
->refcount
)) {
170 raw_spin_unlock_irqrestore(&ctx
->lock
, *flags
);
179 * Get the context for a task and increment its pin_count so it
180 * can't get swapped to another task. This also increments its
181 * reference count so that the context can't get freed.
183 static struct perf_event_context
*perf_pin_task_context(struct task_struct
*task
)
185 struct perf_event_context
*ctx
;
188 ctx
= perf_lock_task_context(task
, &flags
);
191 raw_spin_unlock_irqrestore(&ctx
->lock
, flags
);
196 static void perf_unpin_context(struct perf_event_context
*ctx
)
200 raw_spin_lock_irqsave(&ctx
->lock
, flags
);
202 raw_spin_unlock_irqrestore(&ctx
->lock
, flags
);
206 static inline u64
perf_clock(void)
208 return local_clock();
212 * Update the record of the current time in a context.
214 static void update_context_time(struct perf_event_context
*ctx
)
216 u64 now
= perf_clock();
218 ctx
->time
+= now
- ctx
->timestamp
;
219 ctx
->timestamp
= now
;
223 * Update the total_time_enabled and total_time_running fields for a event.
225 static void update_event_times(struct perf_event
*event
)
227 struct perf_event_context
*ctx
= event
->ctx
;
230 if (event
->state
< PERF_EVENT_STATE_INACTIVE
||
231 event
->group_leader
->state
< PERF_EVENT_STATE_INACTIVE
)
237 run_end
= event
->tstamp_stopped
;
239 event
->total_time_enabled
= run_end
- event
->tstamp_enabled
;
241 if (event
->state
== PERF_EVENT_STATE_INACTIVE
)
242 run_end
= event
->tstamp_stopped
;
246 event
->total_time_running
= run_end
- event
->tstamp_running
;
250 * Update total_time_enabled and total_time_running for all events in a group.
252 static void update_group_times(struct perf_event
*leader
)
254 struct perf_event
*event
;
256 update_event_times(leader
);
257 list_for_each_entry(event
, &leader
->sibling_list
, group_entry
)
258 update_event_times(event
);
261 static struct list_head
*
262 ctx_group_list(struct perf_event
*event
, struct perf_event_context
*ctx
)
264 if (event
->attr
.pinned
)
265 return &ctx
->pinned_groups
;
267 return &ctx
->flexible_groups
;
271 * Add a event from the lists for its context.
272 * Must be called with ctx->mutex and ctx->lock held.
275 list_add_event(struct perf_event
*event
, struct perf_event_context
*ctx
)
277 WARN_ON_ONCE(event
->attach_state
& PERF_ATTACH_CONTEXT
);
278 event
->attach_state
|= PERF_ATTACH_CONTEXT
;
281 * If we're a stand alone event or group leader, we go to the context
282 * list, group events are kept attached to the group so that
283 * perf_group_detach can, at all times, locate all siblings.
285 if (event
->group_leader
== event
) {
286 struct list_head
*list
;
288 if (is_software_event(event
))
289 event
->group_flags
|= PERF_GROUP_SOFTWARE
;
291 list
= ctx_group_list(event
, ctx
);
292 list_add_tail(&event
->group_entry
, list
);
295 list_add_rcu(&event
->event_entry
, &ctx
->event_list
);
297 if (event
->attr
.inherit_stat
)
301 static void perf_group_attach(struct perf_event
*event
)
303 struct perf_event
*group_leader
= event
->group_leader
;
305 WARN_ON_ONCE(event
->attach_state
& PERF_ATTACH_GROUP
);
306 event
->attach_state
|= PERF_ATTACH_GROUP
;
308 if (group_leader
== event
)
311 if (group_leader
->group_flags
& PERF_GROUP_SOFTWARE
&&
312 !is_software_event(event
))
313 group_leader
->group_flags
&= ~PERF_GROUP_SOFTWARE
;
315 list_add_tail(&event
->group_entry
, &group_leader
->sibling_list
);
316 group_leader
->nr_siblings
++;
320 * Remove a event from the lists for its context.
321 * Must be called with ctx->mutex and ctx->lock held.
324 list_del_event(struct perf_event
*event
, struct perf_event_context
*ctx
)
327 * We can have double detach due to exit/hot-unplug + close.
329 if (!(event
->attach_state
& PERF_ATTACH_CONTEXT
))
332 event
->attach_state
&= ~PERF_ATTACH_CONTEXT
;
335 if (event
->attr
.inherit_stat
)
338 list_del_rcu(&event
->event_entry
);
340 if (event
->group_leader
== event
)
341 list_del_init(&event
->group_entry
);
343 update_group_times(event
);
346 * If event was in error state, then keep it
347 * that way, otherwise bogus counts will be
348 * returned on read(). The only way to get out
349 * of error state is by explicit re-enabling
352 if (event
->state
> PERF_EVENT_STATE_OFF
)
353 event
->state
= PERF_EVENT_STATE_OFF
;
356 static void perf_group_detach(struct perf_event
*event
)
358 struct perf_event
*sibling
, *tmp
;
359 struct list_head
*list
= NULL
;
362 * We can have double detach due to exit/hot-unplug + close.
364 if (!(event
->attach_state
& PERF_ATTACH_GROUP
))
367 event
->attach_state
&= ~PERF_ATTACH_GROUP
;
370 * If this is a sibling, remove it from its group.
372 if (event
->group_leader
!= event
) {
373 list_del_init(&event
->group_entry
);
374 event
->group_leader
->nr_siblings
--;
378 if (!list_empty(&event
->group_entry
))
379 list
= &event
->group_entry
;
382 * If this was a group event with sibling events then
383 * upgrade the siblings to singleton events by adding them
384 * to whatever list we are on.
386 list_for_each_entry_safe(sibling
, tmp
, &event
->sibling_list
, group_entry
) {
388 list_move_tail(&sibling
->group_entry
, list
);
389 sibling
->group_leader
= sibling
;
391 /* Inherit group flags from the previous leader */
392 sibling
->group_flags
= event
->group_flags
;
397 event_filter_match(struct perf_event
*event
)
399 return event
->cpu
== -1 || event
->cpu
== smp_processor_id();
403 event_sched_out(struct perf_event
*event
,
404 struct perf_cpu_context
*cpuctx
,
405 struct perf_event_context
*ctx
)
409 * An event which could not be activated because of
410 * filter mismatch still needs to have its timings
411 * maintained, otherwise bogus information is return
412 * via read() for time_enabled, time_running:
414 if (event
->state
== PERF_EVENT_STATE_INACTIVE
415 && !event_filter_match(event
)) {
416 delta
= ctx
->time
- event
->tstamp_stopped
;
417 event
->tstamp_running
+= delta
;
418 event
->tstamp_stopped
= ctx
->time
;
421 if (event
->state
!= PERF_EVENT_STATE_ACTIVE
)
424 event
->state
= PERF_EVENT_STATE_INACTIVE
;
425 if (event
->pending_disable
) {
426 event
->pending_disable
= 0;
427 event
->state
= PERF_EVENT_STATE_OFF
;
429 event
->tstamp_stopped
= ctx
->time
;
430 event
->pmu
->disable(event
);
433 if (!is_software_event(event
))
434 cpuctx
->active_oncpu
--;
436 if (event
->attr
.exclusive
|| !cpuctx
->active_oncpu
)
437 cpuctx
->exclusive
= 0;
441 group_sched_out(struct perf_event
*group_event
,
442 struct perf_cpu_context
*cpuctx
,
443 struct perf_event_context
*ctx
)
445 struct perf_event
*event
;
446 int state
= group_event
->state
;
448 event_sched_out(group_event
, cpuctx
, ctx
);
451 * Schedule out siblings (if any):
453 list_for_each_entry(event
, &group_event
->sibling_list
, group_entry
)
454 event_sched_out(event
, cpuctx
, ctx
);
456 if (state
== PERF_EVENT_STATE_ACTIVE
&& group_event
->attr
.exclusive
)
457 cpuctx
->exclusive
= 0;
461 * Cross CPU call to remove a performance event
463 * We disable the event on the hardware level first. After that we
464 * remove it from the context list.
466 static void __perf_event_remove_from_context(void *info
)
468 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
469 struct perf_event
*event
= info
;
470 struct perf_event_context
*ctx
= event
->ctx
;
473 * If this is a task context, we need to check whether it is
474 * the current task context of this cpu. If not it has been
475 * scheduled out before the smp call arrived.
477 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
480 raw_spin_lock(&ctx
->lock
);
482 event_sched_out(event
, cpuctx
, ctx
);
484 list_del_event(event
, ctx
);
488 * Allow more per task events with respect to the
491 cpuctx
->max_pertask
=
492 min(perf_max_events
- ctx
->nr_events
,
493 perf_max_events
- perf_reserved_percpu
);
496 raw_spin_unlock(&ctx
->lock
);
501 * Remove the event from a task's (or a CPU's) list of events.
503 * Must be called with ctx->mutex held.
505 * CPU events are removed with a smp call. For task events we only
506 * call when the task is on a CPU.
508 * If event->ctx is a cloned context, callers must make sure that
509 * every task struct that event->ctx->task could possibly point to
510 * remains valid. This is OK when called from perf_release since
511 * that only calls us on the top-level context, which can't be a clone.
512 * When called from perf_event_exit_task, it's OK because the
513 * context has been detached from its task.
515 static void perf_event_remove_from_context(struct perf_event
*event
)
517 struct perf_event_context
*ctx
= event
->ctx
;
518 struct task_struct
*task
= ctx
->task
;
522 * Per cpu events are removed via an smp call and
523 * the removal is always successful.
525 smp_call_function_single(event
->cpu
,
526 __perf_event_remove_from_context
,
532 task_oncpu_function_call(task
, __perf_event_remove_from_context
,
535 raw_spin_lock_irq(&ctx
->lock
);
537 * If the context is active we need to retry the smp call.
539 if (ctx
->nr_active
&& !list_empty(&event
->group_entry
)) {
540 raw_spin_unlock_irq(&ctx
->lock
);
545 * The lock prevents that this context is scheduled in so we
546 * can remove the event safely, if the call above did not
549 if (!list_empty(&event
->group_entry
))
550 list_del_event(event
, ctx
);
551 raw_spin_unlock_irq(&ctx
->lock
);
555 * Cross CPU call to disable a performance event
557 static void __perf_event_disable(void *info
)
559 struct perf_event
*event
= info
;
560 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
561 struct perf_event_context
*ctx
= event
->ctx
;
564 * If this is a per-task event, need to check whether this
565 * event's task is the current task on this cpu.
567 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
570 raw_spin_lock(&ctx
->lock
);
573 * If the event is on, turn it off.
574 * If it is in error state, leave it in error state.
576 if (event
->state
>= PERF_EVENT_STATE_INACTIVE
) {
577 update_context_time(ctx
);
578 update_group_times(event
);
579 if (event
== event
->group_leader
)
580 group_sched_out(event
, cpuctx
, ctx
);
582 event_sched_out(event
, cpuctx
, ctx
);
583 event
->state
= PERF_EVENT_STATE_OFF
;
586 raw_spin_unlock(&ctx
->lock
);
592 * If event->ctx is a cloned context, callers must make sure that
593 * every task struct that event->ctx->task could possibly point to
594 * remains valid. This condition is satisifed when called through
595 * perf_event_for_each_child or perf_event_for_each because they
596 * hold the top-level event's child_mutex, so any descendant that
597 * goes to exit will block in sync_child_event.
598 * When called from perf_pending_event it's OK because event->ctx
599 * is the current context on this CPU and preemption is disabled,
600 * hence we can't get into perf_event_task_sched_out for this context.
602 void perf_event_disable(struct perf_event
*event
)
604 struct perf_event_context
*ctx
= event
->ctx
;
605 struct task_struct
*task
= ctx
->task
;
609 * Disable the event on the cpu that it's on
611 smp_call_function_single(event
->cpu
, __perf_event_disable
,
617 task_oncpu_function_call(task
, __perf_event_disable
, event
);
619 raw_spin_lock_irq(&ctx
->lock
);
621 * If the event is still active, we need to retry the cross-call.
623 if (event
->state
== PERF_EVENT_STATE_ACTIVE
) {
624 raw_spin_unlock_irq(&ctx
->lock
);
629 * Since we have the lock this context can't be scheduled
630 * in, so we can change the state safely.
632 if (event
->state
== PERF_EVENT_STATE_INACTIVE
) {
633 update_group_times(event
);
634 event
->state
= PERF_EVENT_STATE_OFF
;
637 raw_spin_unlock_irq(&ctx
->lock
);
641 event_sched_in(struct perf_event
*event
,
642 struct perf_cpu_context
*cpuctx
,
643 struct perf_event_context
*ctx
)
645 if (event
->state
<= PERF_EVENT_STATE_OFF
)
648 event
->state
= PERF_EVENT_STATE_ACTIVE
;
649 event
->oncpu
= smp_processor_id();
651 * The new state must be visible before we turn it on in the hardware:
655 if (event
->pmu
->enable(event
)) {
656 event
->state
= PERF_EVENT_STATE_INACTIVE
;
661 event
->tstamp_running
+= ctx
->time
- event
->tstamp_stopped
;
663 if (!is_software_event(event
))
664 cpuctx
->active_oncpu
++;
667 if (event
->attr
.exclusive
)
668 cpuctx
->exclusive
= 1;
674 group_sched_in(struct perf_event
*group_event
,
675 struct perf_cpu_context
*cpuctx
,
676 struct perf_event_context
*ctx
)
678 struct perf_event
*event
, *partial_group
= NULL
;
679 struct pmu
*pmu
= group_event
->pmu
;
682 if (group_event
->state
== PERF_EVENT_STATE_OFF
)
685 /* Check if group transaction availabe */
692 if (event_sched_in(group_event
, cpuctx
, ctx
)) {
694 pmu
->cancel_txn(pmu
);
699 * Schedule in siblings as one group (if any):
701 list_for_each_entry(event
, &group_event
->sibling_list
, group_entry
) {
702 if (event_sched_in(event
, cpuctx
, ctx
)) {
703 partial_group
= event
;
708 if (!txn
|| !pmu
->commit_txn(pmu
))
713 * Groups can be scheduled in as one unit only, so undo any
714 * partial group before returning:
716 list_for_each_entry(event
, &group_event
->sibling_list
, group_entry
) {
717 if (event
== partial_group
)
719 event_sched_out(event
, cpuctx
, ctx
);
721 event_sched_out(group_event
, cpuctx
, ctx
);
724 pmu
->cancel_txn(pmu
);
730 * Work out whether we can put this event group on the CPU now.
732 static int group_can_go_on(struct perf_event
*event
,
733 struct perf_cpu_context
*cpuctx
,
737 * Groups consisting entirely of software events can always go on.
739 if (event
->group_flags
& PERF_GROUP_SOFTWARE
)
742 * If an exclusive group is already on, no other hardware
745 if (cpuctx
->exclusive
)
748 * If this group is exclusive and there are already
749 * events on the CPU, it can't go on.
751 if (event
->attr
.exclusive
&& cpuctx
->active_oncpu
)
754 * Otherwise, try to add it if all previous groups were able
760 static void add_event_to_ctx(struct perf_event
*event
,
761 struct perf_event_context
*ctx
)
763 list_add_event(event
, ctx
);
764 perf_group_attach(event
);
765 event
->tstamp_enabled
= ctx
->time
;
766 event
->tstamp_running
= ctx
->time
;
767 event
->tstamp_stopped
= ctx
->time
;
771 * Cross CPU call to install and enable a performance event
773 * Must be called with ctx->mutex held
775 static void __perf_install_in_context(void *info
)
777 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
778 struct perf_event
*event
= info
;
779 struct perf_event_context
*ctx
= event
->ctx
;
780 struct perf_event
*leader
= event
->group_leader
;
784 * If this is a task context, we need to check whether it is
785 * the current task context of this cpu. If not it has been
786 * scheduled out before the smp call arrived.
787 * Or possibly this is the right context but it isn't
788 * on this cpu because it had no events.
790 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
) {
791 if (cpuctx
->task_ctx
|| ctx
->task
!= current
)
793 cpuctx
->task_ctx
= ctx
;
796 raw_spin_lock(&ctx
->lock
);
798 update_context_time(ctx
);
800 add_event_to_ctx(event
, ctx
);
802 if (event
->cpu
!= -1 && event
->cpu
!= smp_processor_id())
806 * Don't put the event on if it is disabled or if
807 * it is in a group and the group isn't on.
809 if (event
->state
!= PERF_EVENT_STATE_INACTIVE
||
810 (leader
!= event
&& leader
->state
!= PERF_EVENT_STATE_ACTIVE
))
814 * An exclusive event can't go on if there are already active
815 * hardware events, and no hardware event can go on if there
816 * is already an exclusive event on.
818 if (!group_can_go_on(event
, cpuctx
, 1))
821 err
= event_sched_in(event
, cpuctx
, ctx
);
825 * This event couldn't go on. If it is in a group
826 * then we have to pull the whole group off.
827 * If the event group is pinned then put it in error state.
830 group_sched_out(leader
, cpuctx
, ctx
);
831 if (leader
->attr
.pinned
) {
832 update_group_times(leader
);
833 leader
->state
= PERF_EVENT_STATE_ERROR
;
837 if (!err
&& !ctx
->task
&& cpuctx
->max_pertask
)
838 cpuctx
->max_pertask
--;
841 raw_spin_unlock(&ctx
->lock
);
845 * Attach a performance event to a context
847 * First we add the event to the list with the hardware enable bit
848 * in event->hw_config cleared.
850 * If the event is attached to a task which is on a CPU we use a smp
851 * call to enable it in the task context. The task might have been
852 * scheduled away, but we check this in the smp call again.
854 * Must be called with ctx->mutex held.
857 perf_install_in_context(struct perf_event_context
*ctx
,
858 struct perf_event
*event
,
861 struct task_struct
*task
= ctx
->task
;
865 * Per cpu events are installed via an smp call and
866 * the install is always successful.
868 smp_call_function_single(cpu
, __perf_install_in_context
,
874 task_oncpu_function_call(task
, __perf_install_in_context
,
877 raw_spin_lock_irq(&ctx
->lock
);
879 * we need to retry the smp call.
881 if (ctx
->is_active
&& list_empty(&event
->group_entry
)) {
882 raw_spin_unlock_irq(&ctx
->lock
);
887 * The lock prevents that this context is scheduled in so we
888 * can add the event safely, if it the call above did not
891 if (list_empty(&event
->group_entry
))
892 add_event_to_ctx(event
, ctx
);
893 raw_spin_unlock_irq(&ctx
->lock
);
897 * Put a event into inactive state and update time fields.
898 * Enabling the leader of a group effectively enables all
899 * the group members that aren't explicitly disabled, so we
900 * have to update their ->tstamp_enabled also.
901 * Note: this works for group members as well as group leaders
902 * since the non-leader members' sibling_lists will be empty.
904 static void __perf_event_mark_enabled(struct perf_event
*event
,
905 struct perf_event_context
*ctx
)
907 struct perf_event
*sub
;
909 event
->state
= PERF_EVENT_STATE_INACTIVE
;
910 event
->tstamp_enabled
= ctx
->time
- event
->total_time_enabled
;
911 list_for_each_entry(sub
, &event
->sibling_list
, group_entry
) {
912 if (sub
->state
>= PERF_EVENT_STATE_INACTIVE
) {
913 sub
->tstamp_enabled
=
914 ctx
->time
- sub
->total_time_enabled
;
920 * Cross CPU call to enable a performance event
922 static void __perf_event_enable(void *info
)
924 struct perf_event
*event
= info
;
925 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
926 struct perf_event_context
*ctx
= event
->ctx
;
927 struct perf_event
*leader
= event
->group_leader
;
931 * If this is a per-task event, need to check whether this
932 * event's task is the current task on this cpu.
934 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
) {
935 if (cpuctx
->task_ctx
|| ctx
->task
!= current
)
937 cpuctx
->task_ctx
= ctx
;
940 raw_spin_lock(&ctx
->lock
);
942 update_context_time(ctx
);
944 if (event
->state
>= PERF_EVENT_STATE_INACTIVE
)
946 __perf_event_mark_enabled(event
, ctx
);
948 if (event
->cpu
!= -1 && event
->cpu
!= smp_processor_id())
952 * If the event is in a group and isn't the group leader,
953 * then don't put it on unless the group is on.
955 if (leader
!= event
&& leader
->state
!= PERF_EVENT_STATE_ACTIVE
)
958 if (!group_can_go_on(event
, cpuctx
, 1)) {
962 err
= group_sched_in(event
, cpuctx
, ctx
);
964 err
= event_sched_in(event
, cpuctx
, ctx
);
969 * If this event can't go on and it's part of a
970 * group, then the whole group has to come off.
973 group_sched_out(leader
, cpuctx
, ctx
);
974 if (leader
->attr
.pinned
) {
975 update_group_times(leader
);
976 leader
->state
= PERF_EVENT_STATE_ERROR
;
981 raw_spin_unlock(&ctx
->lock
);
987 * If event->ctx is a cloned context, callers must make sure that
988 * every task struct that event->ctx->task could possibly point to
989 * remains valid. This condition is satisfied when called through
990 * perf_event_for_each_child or perf_event_for_each as described
991 * for perf_event_disable.
993 void perf_event_enable(struct perf_event
*event
)
995 struct perf_event_context
*ctx
= event
->ctx
;
996 struct task_struct
*task
= ctx
->task
;
1000 * Enable the event on the cpu that it's on
1002 smp_call_function_single(event
->cpu
, __perf_event_enable
,
1007 raw_spin_lock_irq(&ctx
->lock
);
1008 if (event
->state
>= PERF_EVENT_STATE_INACTIVE
)
1012 * If the event is in error state, clear that first.
1013 * That way, if we see the event in error state below, we
1014 * know that it has gone back into error state, as distinct
1015 * from the task having been scheduled away before the
1016 * cross-call arrived.
1018 if (event
->state
== PERF_EVENT_STATE_ERROR
)
1019 event
->state
= PERF_EVENT_STATE_OFF
;
1022 raw_spin_unlock_irq(&ctx
->lock
);
1023 task_oncpu_function_call(task
, __perf_event_enable
, event
);
1025 raw_spin_lock_irq(&ctx
->lock
);
1028 * If the context is active and the event is still off,
1029 * we need to retry the cross-call.
1031 if (ctx
->is_active
&& event
->state
== PERF_EVENT_STATE_OFF
)
1035 * Since we have the lock this context can't be scheduled
1036 * in, so we can change the state safely.
1038 if (event
->state
== PERF_EVENT_STATE_OFF
)
1039 __perf_event_mark_enabled(event
, ctx
);
1042 raw_spin_unlock_irq(&ctx
->lock
);
1045 static int perf_event_refresh(struct perf_event
*event
, int refresh
)
1048 * not supported on inherited events
1050 if (event
->attr
.inherit
)
1053 atomic_add(refresh
, &event
->event_limit
);
1054 perf_event_enable(event
);
1060 EVENT_FLEXIBLE
= 0x1,
1062 EVENT_ALL
= EVENT_FLEXIBLE
| EVENT_PINNED
,
1065 static void ctx_sched_out(struct perf_event_context
*ctx
,
1066 struct perf_cpu_context
*cpuctx
,
1067 enum event_type_t event_type
)
1069 struct perf_event
*event
;
1071 raw_spin_lock(&ctx
->lock
);
1073 if (likely(!ctx
->nr_events
))
1075 update_context_time(ctx
);
1077 if (!ctx
->nr_active
)
1080 if (event_type
& EVENT_PINNED
) {
1081 list_for_each_entry(event
, &ctx
->pinned_groups
, group_entry
)
1082 group_sched_out(event
, cpuctx
, ctx
);
1085 if (event_type
& EVENT_FLEXIBLE
) {
1086 list_for_each_entry(event
, &ctx
->flexible_groups
, group_entry
)
1087 group_sched_out(event
, cpuctx
, ctx
);
1090 raw_spin_unlock(&ctx
->lock
);
1094 * Test whether two contexts are equivalent, i.e. whether they
1095 * have both been cloned from the same version of the same context
1096 * and they both have the same number of enabled events.
1097 * If the number of enabled events is the same, then the set
1098 * of enabled events should be the same, because these are both
1099 * inherited contexts, therefore we can't access individual events
1100 * in them directly with an fd; we can only enable/disable all
1101 * events via prctl, or enable/disable all events in a family
1102 * via ioctl, which will have the same effect on both contexts.
1104 static int context_equiv(struct perf_event_context
*ctx1
,
1105 struct perf_event_context
*ctx2
)
1107 return ctx1
->parent_ctx
&& ctx1
->parent_ctx
== ctx2
->parent_ctx
1108 && ctx1
->parent_gen
== ctx2
->parent_gen
1109 && !ctx1
->pin_count
&& !ctx2
->pin_count
;
1112 static void __perf_event_sync_stat(struct perf_event
*event
,
1113 struct perf_event
*next_event
)
1117 if (!event
->attr
.inherit_stat
)
1121 * Update the event value, we cannot use perf_event_read()
1122 * because we're in the middle of a context switch and have IRQs
1123 * disabled, which upsets smp_call_function_single(), however
1124 * we know the event must be on the current CPU, therefore we
1125 * don't need to use it.
1127 switch (event
->state
) {
1128 case PERF_EVENT_STATE_ACTIVE
:
1129 event
->pmu
->read(event
);
1132 case PERF_EVENT_STATE_INACTIVE
:
1133 update_event_times(event
);
1141 * In order to keep per-task stats reliable we need to flip the event
1142 * values when we flip the contexts.
1144 value
= local64_read(&next_event
->count
);
1145 value
= local64_xchg(&event
->count
, value
);
1146 local64_set(&next_event
->count
, value
);
1148 swap(event
->total_time_enabled
, next_event
->total_time_enabled
);
1149 swap(event
->total_time_running
, next_event
->total_time_running
);
1152 * Since we swizzled the values, update the user visible data too.
1154 perf_event_update_userpage(event
);
1155 perf_event_update_userpage(next_event
);
1158 #define list_next_entry(pos, member) \
1159 list_entry(pos->member.next, typeof(*pos), member)
1161 static void perf_event_sync_stat(struct perf_event_context
*ctx
,
1162 struct perf_event_context
*next_ctx
)
1164 struct perf_event
*event
, *next_event
;
1169 update_context_time(ctx
);
1171 event
= list_first_entry(&ctx
->event_list
,
1172 struct perf_event
, event_entry
);
1174 next_event
= list_first_entry(&next_ctx
->event_list
,
1175 struct perf_event
, event_entry
);
1177 while (&event
->event_entry
!= &ctx
->event_list
&&
1178 &next_event
->event_entry
!= &next_ctx
->event_list
) {
1180 __perf_event_sync_stat(event
, next_event
);
1182 event
= list_next_entry(event
, event_entry
);
1183 next_event
= list_next_entry(next_event
, event_entry
);
1188 * Called from scheduler to remove the events of the current task,
1189 * with interrupts disabled.
1191 * We stop each event and update the event value in event->count.
1193 * This does not protect us against NMI, but disable()
1194 * sets the disabled bit in the control field of event _before_
1195 * accessing the event control register. If a NMI hits, then it will
1196 * not restart the event.
1198 void perf_event_task_sched_out(struct task_struct
*task
,
1199 struct task_struct
*next
)
1201 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
1202 struct perf_event_context
*ctx
= task
->perf_event_ctxp
;
1203 struct perf_event_context
*next_ctx
;
1204 struct perf_event_context
*parent
;
1207 perf_sw_event(PERF_COUNT_SW_CONTEXT_SWITCHES
, 1, 1, NULL
, 0);
1209 if (likely(!ctx
|| !cpuctx
->task_ctx
))
1213 parent
= rcu_dereference(ctx
->parent_ctx
);
1214 next_ctx
= next
->perf_event_ctxp
;
1215 if (parent
&& next_ctx
&&
1216 rcu_dereference(next_ctx
->parent_ctx
) == parent
) {
1218 * Looks like the two contexts are clones, so we might be
1219 * able to optimize the context switch. We lock both
1220 * contexts and check that they are clones under the
1221 * lock (including re-checking that neither has been
1222 * uncloned in the meantime). It doesn't matter which
1223 * order we take the locks because no other cpu could
1224 * be trying to lock both of these tasks.
1226 raw_spin_lock(&ctx
->lock
);
1227 raw_spin_lock_nested(&next_ctx
->lock
, SINGLE_DEPTH_NESTING
);
1228 if (context_equiv(ctx
, next_ctx
)) {
1230 * XXX do we need a memory barrier of sorts
1231 * wrt to rcu_dereference() of perf_event_ctxp
1233 task
->perf_event_ctxp
= next_ctx
;
1234 next
->perf_event_ctxp
= ctx
;
1236 next_ctx
->task
= task
;
1239 perf_event_sync_stat(ctx
, next_ctx
);
1241 raw_spin_unlock(&next_ctx
->lock
);
1242 raw_spin_unlock(&ctx
->lock
);
1247 ctx_sched_out(ctx
, cpuctx
, EVENT_ALL
);
1248 cpuctx
->task_ctx
= NULL
;
1252 static void task_ctx_sched_out(struct perf_event_context
*ctx
,
1253 enum event_type_t event_type
)
1255 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
1257 if (!cpuctx
->task_ctx
)
1260 if (WARN_ON_ONCE(ctx
!= cpuctx
->task_ctx
))
1263 ctx_sched_out(ctx
, cpuctx
, event_type
);
1264 cpuctx
->task_ctx
= NULL
;
1268 * Called with IRQs disabled
1270 static void __perf_event_task_sched_out(struct perf_event_context
*ctx
)
1272 task_ctx_sched_out(ctx
, EVENT_ALL
);
1276 * Called with IRQs disabled
1278 static void cpu_ctx_sched_out(struct perf_cpu_context
*cpuctx
,
1279 enum event_type_t event_type
)
1281 ctx_sched_out(&cpuctx
->ctx
, cpuctx
, event_type
);
1285 ctx_pinned_sched_in(struct perf_event_context
*ctx
,
1286 struct perf_cpu_context
*cpuctx
)
1288 struct perf_event
*event
;
1290 list_for_each_entry(event
, &ctx
->pinned_groups
, group_entry
) {
1291 if (event
->state
<= PERF_EVENT_STATE_OFF
)
1293 if (event
->cpu
!= -1 && event
->cpu
!= smp_processor_id())
1296 if (group_can_go_on(event
, cpuctx
, 1))
1297 group_sched_in(event
, cpuctx
, ctx
);
1300 * If this pinned group hasn't been scheduled,
1301 * put it in error state.
1303 if (event
->state
== PERF_EVENT_STATE_INACTIVE
) {
1304 update_group_times(event
);
1305 event
->state
= PERF_EVENT_STATE_ERROR
;
1311 ctx_flexible_sched_in(struct perf_event_context
*ctx
,
1312 struct perf_cpu_context
*cpuctx
)
1314 struct perf_event
*event
;
1317 list_for_each_entry(event
, &ctx
->flexible_groups
, group_entry
) {
1318 /* Ignore events in OFF or ERROR state */
1319 if (event
->state
<= PERF_EVENT_STATE_OFF
)
1322 * Listen to the 'cpu' scheduling filter constraint
1325 if (event
->cpu
!= -1 && event
->cpu
!= smp_processor_id())
1328 if (group_can_go_on(event
, cpuctx
, can_add_hw
)) {
1329 if (group_sched_in(event
, cpuctx
, ctx
))
1336 ctx_sched_in(struct perf_event_context
*ctx
,
1337 struct perf_cpu_context
*cpuctx
,
1338 enum event_type_t event_type
)
1340 raw_spin_lock(&ctx
->lock
);
1342 if (likely(!ctx
->nr_events
))
1345 ctx
->timestamp
= perf_clock();
1348 * First go through the list and put on any pinned groups
1349 * in order to give them the best chance of going on.
1351 if (event_type
& EVENT_PINNED
)
1352 ctx_pinned_sched_in(ctx
, cpuctx
);
1354 /* Then walk through the lower prio flexible groups */
1355 if (event_type
& EVENT_FLEXIBLE
)
1356 ctx_flexible_sched_in(ctx
, cpuctx
);
1359 raw_spin_unlock(&ctx
->lock
);
1362 static void cpu_ctx_sched_in(struct perf_cpu_context
*cpuctx
,
1363 enum event_type_t event_type
)
1365 struct perf_event_context
*ctx
= &cpuctx
->ctx
;
1367 ctx_sched_in(ctx
, cpuctx
, event_type
);
1370 static void task_ctx_sched_in(struct task_struct
*task
,
1371 enum event_type_t event_type
)
1373 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
1374 struct perf_event_context
*ctx
= task
->perf_event_ctxp
;
1378 if (cpuctx
->task_ctx
== ctx
)
1380 ctx_sched_in(ctx
, cpuctx
, event_type
);
1381 cpuctx
->task_ctx
= ctx
;
1384 * Called from scheduler to add the events of the current task
1385 * with interrupts disabled.
1387 * We restore the event value and then enable it.
1389 * This does not protect us against NMI, but enable()
1390 * sets the enabled bit in the control field of event _before_
1391 * accessing the event control register. If a NMI hits, then it will
1392 * keep the event running.
1394 void perf_event_task_sched_in(struct task_struct
*task
)
1396 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
1397 struct perf_event_context
*ctx
= task
->perf_event_ctxp
;
1402 if (cpuctx
->task_ctx
== ctx
)
1406 * We want to keep the following priority order:
1407 * cpu pinned (that don't need to move), task pinned,
1408 * cpu flexible, task flexible.
1410 cpu_ctx_sched_out(cpuctx
, EVENT_FLEXIBLE
);
1412 ctx_sched_in(ctx
, cpuctx
, EVENT_PINNED
);
1413 cpu_ctx_sched_in(cpuctx
, EVENT_FLEXIBLE
);
1414 ctx_sched_in(ctx
, cpuctx
, EVENT_FLEXIBLE
);
1416 cpuctx
->task_ctx
= ctx
;
1419 #define MAX_INTERRUPTS (~0ULL)
1421 static void perf_log_throttle(struct perf_event
*event
, int enable
);
1423 static u64
perf_calculate_period(struct perf_event
*event
, u64 nsec
, u64 count
)
1425 u64 frequency
= event
->attr
.sample_freq
;
1426 u64 sec
= NSEC_PER_SEC
;
1427 u64 divisor
, dividend
;
1429 int count_fls
, nsec_fls
, frequency_fls
, sec_fls
;
1431 count_fls
= fls64(count
);
1432 nsec_fls
= fls64(nsec
);
1433 frequency_fls
= fls64(frequency
);
1437 * We got @count in @nsec, with a target of sample_freq HZ
1438 * the target period becomes:
1441 * period = -------------------
1442 * @nsec * sample_freq
1447 * Reduce accuracy by one bit such that @a and @b converge
1448 * to a similar magnitude.
1450 #define REDUCE_FLS(a, b) \
1452 if (a##_fls > b##_fls) { \
1462 * Reduce accuracy until either term fits in a u64, then proceed with
1463 * the other, so that finally we can do a u64/u64 division.
1465 while (count_fls
+ sec_fls
> 64 && nsec_fls
+ frequency_fls
> 64) {
1466 REDUCE_FLS(nsec
, frequency
);
1467 REDUCE_FLS(sec
, count
);
1470 if (count_fls
+ sec_fls
> 64) {
1471 divisor
= nsec
* frequency
;
1473 while (count_fls
+ sec_fls
> 64) {
1474 REDUCE_FLS(count
, sec
);
1478 dividend
= count
* sec
;
1480 dividend
= count
* sec
;
1482 while (nsec_fls
+ frequency_fls
> 64) {
1483 REDUCE_FLS(nsec
, frequency
);
1487 divisor
= nsec
* frequency
;
1493 return div64_u64(dividend
, divisor
);
1496 static void perf_event_stop(struct perf_event
*event
)
1498 if (!event
->pmu
->stop
)
1499 return event
->pmu
->disable(event
);
1501 return event
->pmu
->stop(event
);
1504 static int perf_event_start(struct perf_event
*event
)
1506 if (!event
->pmu
->start
)
1507 return event
->pmu
->enable(event
);
1509 return event
->pmu
->start(event
);
1512 static void perf_adjust_period(struct perf_event
*event
, u64 nsec
, u64 count
)
1514 struct hw_perf_event
*hwc
= &event
->hw
;
1515 s64 period
, sample_period
;
1518 period
= perf_calculate_period(event
, nsec
, count
);
1520 delta
= (s64
)(period
- hwc
->sample_period
);
1521 delta
= (delta
+ 7) / 8; /* low pass filter */
1523 sample_period
= hwc
->sample_period
+ delta
;
1528 hwc
->sample_period
= sample_period
;
1530 if (local64_read(&hwc
->period_left
) > 8*sample_period
) {
1531 perf_event_stop(event
);
1532 local64_set(&hwc
->period_left
, 0);
1533 perf_event_start(event
);
1537 static void perf_ctx_adjust_freq(struct perf_event_context
*ctx
)
1539 struct perf_event
*event
;
1540 struct hw_perf_event
*hwc
;
1541 u64 interrupts
, now
;
1544 raw_spin_lock(&ctx
->lock
);
1545 list_for_each_entry_rcu(event
, &ctx
->event_list
, event_entry
) {
1546 if (event
->state
!= PERF_EVENT_STATE_ACTIVE
)
1549 if (event
->cpu
!= -1 && event
->cpu
!= smp_processor_id())
1554 interrupts
= hwc
->interrupts
;
1555 hwc
->interrupts
= 0;
1558 * unthrottle events on the tick
1560 if (interrupts
== MAX_INTERRUPTS
) {
1561 perf_log_throttle(event
, 1);
1562 event
->pmu
->unthrottle(event
);
1565 if (!event
->attr
.freq
|| !event
->attr
.sample_freq
)
1568 event
->pmu
->read(event
);
1569 now
= local64_read(&event
->count
);
1570 delta
= now
- hwc
->freq_count_stamp
;
1571 hwc
->freq_count_stamp
= now
;
1574 perf_adjust_period(event
, TICK_NSEC
, delta
);
1576 raw_spin_unlock(&ctx
->lock
);
1580 * Round-robin a context's events:
1582 static void rotate_ctx(struct perf_event_context
*ctx
)
1584 raw_spin_lock(&ctx
->lock
);
1586 /* Rotate the first entry last of non-pinned groups */
1587 list_rotate_left(&ctx
->flexible_groups
);
1589 raw_spin_unlock(&ctx
->lock
);
1592 void perf_event_task_tick(struct task_struct
*curr
)
1594 struct perf_cpu_context
*cpuctx
;
1595 struct perf_event_context
*ctx
;
1598 if (!atomic_read(&nr_events
))
1601 cpuctx
= &__get_cpu_var(perf_cpu_context
);
1602 if (cpuctx
->ctx
.nr_events
&&
1603 cpuctx
->ctx
.nr_events
!= cpuctx
->ctx
.nr_active
)
1606 ctx
= curr
->perf_event_ctxp
;
1607 if (ctx
&& ctx
->nr_events
&& ctx
->nr_events
!= ctx
->nr_active
)
1610 perf_ctx_adjust_freq(&cpuctx
->ctx
);
1612 perf_ctx_adjust_freq(ctx
);
1617 cpu_ctx_sched_out(cpuctx
, EVENT_FLEXIBLE
);
1619 task_ctx_sched_out(ctx
, EVENT_FLEXIBLE
);
1621 rotate_ctx(&cpuctx
->ctx
);
1625 cpu_ctx_sched_in(cpuctx
, EVENT_FLEXIBLE
);
1627 task_ctx_sched_in(curr
, EVENT_FLEXIBLE
);
1630 static int event_enable_on_exec(struct perf_event
*event
,
1631 struct perf_event_context
*ctx
)
1633 if (!event
->attr
.enable_on_exec
)
1636 event
->attr
.enable_on_exec
= 0;
1637 if (event
->state
>= PERF_EVENT_STATE_INACTIVE
)
1640 __perf_event_mark_enabled(event
, ctx
);
1646 * Enable all of a task's events that have been marked enable-on-exec.
1647 * This expects task == current.
1649 static void perf_event_enable_on_exec(struct task_struct
*task
)
1651 struct perf_event_context
*ctx
;
1652 struct perf_event
*event
;
1653 unsigned long flags
;
1657 local_irq_save(flags
);
1658 ctx
= task
->perf_event_ctxp
;
1659 if (!ctx
|| !ctx
->nr_events
)
1662 __perf_event_task_sched_out(ctx
);
1664 raw_spin_lock(&ctx
->lock
);
1666 list_for_each_entry(event
, &ctx
->pinned_groups
, group_entry
) {
1667 ret
= event_enable_on_exec(event
, ctx
);
1672 list_for_each_entry(event
, &ctx
->flexible_groups
, group_entry
) {
1673 ret
= event_enable_on_exec(event
, ctx
);
1679 * Unclone this context if we enabled any event.
1684 raw_spin_unlock(&ctx
->lock
);
1686 perf_event_task_sched_in(task
);
1688 local_irq_restore(flags
);
1692 * Cross CPU call to read the hardware event
1694 static void __perf_event_read(void *info
)
1696 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
1697 struct perf_event
*event
= info
;
1698 struct perf_event_context
*ctx
= event
->ctx
;
1701 * If this is a task context, we need to check whether it is
1702 * the current task context of this cpu. If not it has been
1703 * scheduled out before the smp call arrived. In that case
1704 * event->count would have been updated to a recent sample
1705 * when the event was scheduled out.
1707 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
1710 raw_spin_lock(&ctx
->lock
);
1711 update_context_time(ctx
);
1712 update_event_times(event
);
1713 raw_spin_unlock(&ctx
->lock
);
1715 event
->pmu
->read(event
);
1718 static inline u64
perf_event_count(struct perf_event
*event
)
1720 return local64_read(&event
->count
) + atomic64_read(&event
->child_count
);
1723 static u64
perf_event_read(struct perf_event
*event
)
1726 * If event is enabled and currently active on a CPU, update the
1727 * value in the event structure:
1729 if (event
->state
== PERF_EVENT_STATE_ACTIVE
) {
1730 smp_call_function_single(event
->oncpu
,
1731 __perf_event_read
, event
, 1);
1732 } else if (event
->state
== PERF_EVENT_STATE_INACTIVE
) {
1733 struct perf_event_context
*ctx
= event
->ctx
;
1734 unsigned long flags
;
1736 raw_spin_lock_irqsave(&ctx
->lock
, flags
);
1737 update_context_time(ctx
);
1738 update_event_times(event
);
1739 raw_spin_unlock_irqrestore(&ctx
->lock
, flags
);
1742 return perf_event_count(event
);
1749 struct callchain_cpus_entries
{
1750 struct rcu_head rcu_head
;
1751 struct perf_callchain_entry
*cpu_entries
[0];
1754 static DEFINE_PER_CPU(int, callchain_recursion
[PERF_NR_CONTEXTS
]);
1755 static atomic_t nr_callchain_events
;
1756 static DEFINE_MUTEX(callchain_mutex
);
1757 struct callchain_cpus_entries
*callchain_cpus_entries
;
1760 __weak
void perf_callchain_kernel(struct perf_callchain_entry
*entry
,
1761 struct pt_regs
*regs
)
1765 __weak
void perf_callchain_user(struct perf_callchain_entry
*entry
,
1766 struct pt_regs
*regs
)
1770 static void release_callchain_buffers_rcu(struct rcu_head
*head
)
1772 struct callchain_cpus_entries
*entries
;
1775 entries
= container_of(head
, struct callchain_cpus_entries
, rcu_head
);
1777 for_each_possible_cpu(cpu
)
1778 kfree(entries
->cpu_entries
[cpu
]);
1783 static void release_callchain_buffers(void)
1785 struct callchain_cpus_entries
*entries
;
1787 entries
= callchain_cpus_entries
;
1788 rcu_assign_pointer(callchain_cpus_entries
, NULL
);
1789 call_rcu(&entries
->rcu_head
, release_callchain_buffers_rcu
);
1792 static int alloc_callchain_buffers(void)
1796 struct callchain_cpus_entries
*entries
;
1799 * We can't use the percpu allocation API for data that can be
1800 * accessed from NMI. Use a temporary manual per cpu allocation
1801 * until that gets sorted out.
1803 size
= sizeof(*entries
) + sizeof(struct perf_callchain_entry
*) *
1804 num_possible_cpus();
1806 entries
= kzalloc(size
, GFP_KERNEL
);
1810 size
= sizeof(struct perf_callchain_entry
) * PERF_NR_CONTEXTS
;
1812 for_each_possible_cpu(cpu
) {
1813 entries
->cpu_entries
[cpu
] = kmalloc_node(size
, GFP_KERNEL
,
1815 if (!entries
->cpu_entries
[cpu
])
1819 rcu_assign_pointer(callchain_cpus_entries
, entries
);
1824 for_each_possible_cpu(cpu
)
1825 kfree(entries
->cpu_entries
[cpu
]);
1831 static int get_callchain_buffers(void)
1836 mutex_lock(&callchain_mutex
);
1838 count
= atomic_inc_return(&nr_callchain_events
);
1839 if (WARN_ON_ONCE(count
< 1)) {
1845 /* If the allocation failed, give up */
1846 if (!callchain_cpus_entries
)
1851 err
= alloc_callchain_buffers();
1853 release_callchain_buffers();
1855 mutex_unlock(&callchain_mutex
);
1860 static void put_callchain_buffers(void)
1862 if (atomic_dec_and_mutex_lock(&nr_callchain_events
, &callchain_mutex
)) {
1863 release_callchain_buffers();
1864 mutex_unlock(&callchain_mutex
);
1868 static int get_recursion_context(int *recursion
)
1876 else if (in_softirq())
1881 if (recursion
[rctx
])
1890 static inline void put_recursion_context(int *recursion
, int rctx
)
1896 static struct perf_callchain_entry
*get_callchain_entry(int *rctx
)
1899 struct callchain_cpus_entries
*entries
;
1901 *rctx
= get_recursion_context(__get_cpu_var(callchain_recursion
));
1905 entries
= rcu_dereference(callchain_cpus_entries
);
1909 cpu
= smp_processor_id();
1911 return &entries
->cpu_entries
[cpu
][*rctx
];
1915 put_callchain_entry(int rctx
)
1917 put_recursion_context(__get_cpu_var(callchain_recursion
), rctx
);
1920 static struct perf_callchain_entry
*perf_callchain(struct pt_regs
*regs
)
1923 struct perf_callchain_entry
*entry
;
1926 entry
= get_callchain_entry(&rctx
);
1935 if (!user_mode(regs
)) {
1936 perf_callchain_store(entry
, PERF_CONTEXT_KERNEL
);
1937 perf_callchain_kernel(entry
, regs
);
1939 regs
= task_pt_regs(current
);
1945 perf_callchain_store(entry
, PERF_CONTEXT_USER
);
1946 perf_callchain_user(entry
, regs
);
1950 put_callchain_entry(rctx
);
1956 * Initialize the perf_event context in a task_struct:
1959 __perf_event_init_context(struct perf_event_context
*ctx
,
1960 struct task_struct
*task
)
1962 raw_spin_lock_init(&ctx
->lock
);
1963 mutex_init(&ctx
->mutex
);
1964 INIT_LIST_HEAD(&ctx
->pinned_groups
);
1965 INIT_LIST_HEAD(&ctx
->flexible_groups
);
1966 INIT_LIST_HEAD(&ctx
->event_list
);
1967 atomic_set(&ctx
->refcount
, 1);
1971 static struct perf_event_context
*find_get_context(pid_t pid
, int cpu
)
1973 struct perf_event_context
*ctx
;
1974 struct perf_cpu_context
*cpuctx
;
1975 struct task_struct
*task
;
1976 unsigned long flags
;
1979 if (pid
== -1 && cpu
!= -1) {
1980 /* Must be root to operate on a CPU event: */
1981 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN
))
1982 return ERR_PTR(-EACCES
);
1984 if (cpu
< 0 || cpu
>= nr_cpumask_bits
)
1985 return ERR_PTR(-EINVAL
);
1988 * We could be clever and allow to attach a event to an
1989 * offline CPU and activate it when the CPU comes up, but
1992 if (!cpu_online(cpu
))
1993 return ERR_PTR(-ENODEV
);
1995 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
2006 task
= find_task_by_vpid(pid
);
2008 get_task_struct(task
);
2012 return ERR_PTR(-ESRCH
);
2015 * Can't attach events to a dying task.
2018 if (task
->flags
& PF_EXITING
)
2021 /* Reuse ptrace permission checks for now. */
2023 if (!ptrace_may_access(task
, PTRACE_MODE_READ
))
2027 ctx
= perf_lock_task_context(task
, &flags
);
2030 raw_spin_unlock_irqrestore(&ctx
->lock
, flags
);
2034 ctx
= kzalloc(sizeof(struct perf_event_context
), GFP_KERNEL
);
2038 __perf_event_init_context(ctx
, task
);
2040 if (cmpxchg(&task
->perf_event_ctxp
, NULL
, ctx
)) {
2042 * We raced with some other task; use
2043 * the context they set.
2048 get_task_struct(task
);
2051 put_task_struct(task
);
2055 put_task_struct(task
);
2056 return ERR_PTR(err
);
2059 static void perf_event_free_filter(struct perf_event
*event
);
2061 static void free_event_rcu(struct rcu_head
*head
)
2063 struct perf_event
*event
;
2065 event
= container_of(head
, struct perf_event
, rcu_head
);
2067 put_pid_ns(event
->ns
);
2068 perf_event_free_filter(event
);
2072 static void perf_pending_sync(struct perf_event
*event
);
2073 static void perf_buffer_put(struct perf_buffer
*buffer
);
2075 static void free_event(struct perf_event
*event
)
2077 perf_pending_sync(event
);
2079 if (!event
->parent
) {
2080 atomic_dec(&nr_events
);
2081 if (event
->attr
.mmap
|| event
->attr
.mmap_data
)
2082 atomic_dec(&nr_mmap_events
);
2083 if (event
->attr
.comm
)
2084 atomic_dec(&nr_comm_events
);
2085 if (event
->attr
.task
)
2086 atomic_dec(&nr_task_events
);
2087 if (event
->attr
.sample_type
& PERF_SAMPLE_CALLCHAIN
)
2088 put_callchain_buffers();
2091 if (event
->buffer
) {
2092 perf_buffer_put(event
->buffer
);
2093 event
->buffer
= NULL
;
2097 event
->destroy(event
);
2099 put_ctx(event
->ctx
);
2100 call_rcu(&event
->rcu_head
, free_event_rcu
);
2103 int perf_event_release_kernel(struct perf_event
*event
)
2105 struct perf_event_context
*ctx
= event
->ctx
;
2108 * Remove from the PMU, can't get re-enabled since we got
2109 * here because the last ref went.
2111 perf_event_disable(event
);
2113 WARN_ON_ONCE(ctx
->parent_ctx
);
2115 * There are two ways this annotation is useful:
2117 * 1) there is a lock recursion from perf_event_exit_task
2118 * see the comment there.
2120 * 2) there is a lock-inversion with mmap_sem through
2121 * perf_event_read_group(), which takes faults while
2122 * holding ctx->mutex, however this is called after
2123 * the last filedesc died, so there is no possibility
2124 * to trigger the AB-BA case.
2126 mutex_lock_nested(&ctx
->mutex
, SINGLE_DEPTH_NESTING
);
2127 raw_spin_lock_irq(&ctx
->lock
);
2128 perf_group_detach(event
);
2129 list_del_event(event
, ctx
);
2130 raw_spin_unlock_irq(&ctx
->lock
);
2131 mutex_unlock(&ctx
->mutex
);
2133 mutex_lock(&event
->owner
->perf_event_mutex
);
2134 list_del_init(&event
->owner_entry
);
2135 mutex_unlock(&event
->owner
->perf_event_mutex
);
2136 put_task_struct(event
->owner
);
2142 EXPORT_SYMBOL_GPL(perf_event_release_kernel
);
2145 * Called when the last reference to the file is gone.
2147 static int perf_release(struct inode
*inode
, struct file
*file
)
2149 struct perf_event
*event
= file
->private_data
;
2151 file
->private_data
= NULL
;
2153 return perf_event_release_kernel(event
);
2156 static int perf_event_read_size(struct perf_event
*event
)
2158 int entry
= sizeof(u64
); /* value */
2162 if (event
->attr
.read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
2163 size
+= sizeof(u64
);
2165 if (event
->attr
.read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
2166 size
+= sizeof(u64
);
2168 if (event
->attr
.read_format
& PERF_FORMAT_ID
)
2169 entry
+= sizeof(u64
);
2171 if (event
->attr
.read_format
& PERF_FORMAT_GROUP
) {
2172 nr
+= event
->group_leader
->nr_siblings
;
2173 size
+= sizeof(u64
);
2181 u64
perf_event_read_value(struct perf_event
*event
, u64
*enabled
, u64
*running
)
2183 struct perf_event
*child
;
2189 mutex_lock(&event
->child_mutex
);
2190 total
+= perf_event_read(event
);
2191 *enabled
+= event
->total_time_enabled
+
2192 atomic64_read(&event
->child_total_time_enabled
);
2193 *running
+= event
->total_time_running
+
2194 atomic64_read(&event
->child_total_time_running
);
2196 list_for_each_entry(child
, &event
->child_list
, child_list
) {
2197 total
+= perf_event_read(child
);
2198 *enabled
+= child
->total_time_enabled
;
2199 *running
+= child
->total_time_running
;
2201 mutex_unlock(&event
->child_mutex
);
2205 EXPORT_SYMBOL_GPL(perf_event_read_value
);
2207 static int perf_event_read_group(struct perf_event
*event
,
2208 u64 read_format
, char __user
*buf
)
2210 struct perf_event
*leader
= event
->group_leader
, *sub
;
2211 int n
= 0, size
= 0, ret
= -EFAULT
;
2212 struct perf_event_context
*ctx
= leader
->ctx
;
2214 u64 count
, enabled
, running
;
2216 mutex_lock(&ctx
->mutex
);
2217 count
= perf_event_read_value(leader
, &enabled
, &running
);
2219 values
[n
++] = 1 + leader
->nr_siblings
;
2220 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
2221 values
[n
++] = enabled
;
2222 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
2223 values
[n
++] = running
;
2224 values
[n
++] = count
;
2225 if (read_format
& PERF_FORMAT_ID
)
2226 values
[n
++] = primary_event_id(leader
);
2228 size
= n
* sizeof(u64
);
2230 if (copy_to_user(buf
, values
, size
))
2235 list_for_each_entry(sub
, &leader
->sibling_list
, group_entry
) {
2238 values
[n
++] = perf_event_read_value(sub
, &enabled
, &running
);
2239 if (read_format
& PERF_FORMAT_ID
)
2240 values
[n
++] = primary_event_id(sub
);
2242 size
= n
* sizeof(u64
);
2244 if (copy_to_user(buf
+ ret
, values
, size
)) {
2252 mutex_unlock(&ctx
->mutex
);
2257 static int perf_event_read_one(struct perf_event
*event
,
2258 u64 read_format
, char __user
*buf
)
2260 u64 enabled
, running
;
2264 values
[n
++] = perf_event_read_value(event
, &enabled
, &running
);
2265 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
2266 values
[n
++] = enabled
;
2267 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
2268 values
[n
++] = running
;
2269 if (read_format
& PERF_FORMAT_ID
)
2270 values
[n
++] = primary_event_id(event
);
2272 if (copy_to_user(buf
, values
, n
* sizeof(u64
)))
2275 return n
* sizeof(u64
);
2279 * Read the performance event - simple non blocking version for now
2282 perf_read_hw(struct perf_event
*event
, char __user
*buf
, size_t count
)
2284 u64 read_format
= event
->attr
.read_format
;
2288 * Return end-of-file for a read on a event that is in
2289 * error state (i.e. because it was pinned but it couldn't be
2290 * scheduled on to the CPU at some point).
2292 if (event
->state
== PERF_EVENT_STATE_ERROR
)
2295 if (count
< perf_event_read_size(event
))
2298 WARN_ON_ONCE(event
->ctx
->parent_ctx
);
2299 if (read_format
& PERF_FORMAT_GROUP
)
2300 ret
= perf_event_read_group(event
, read_format
, buf
);
2302 ret
= perf_event_read_one(event
, read_format
, buf
);
2308 perf_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
2310 struct perf_event
*event
= file
->private_data
;
2312 return perf_read_hw(event
, buf
, count
);
2315 static unsigned int perf_poll(struct file
*file
, poll_table
*wait
)
2317 struct perf_event
*event
= file
->private_data
;
2318 struct perf_buffer
*buffer
;
2319 unsigned int events
= POLL_HUP
;
2322 buffer
= rcu_dereference(event
->buffer
);
2324 events
= atomic_xchg(&buffer
->poll
, 0);
2327 poll_wait(file
, &event
->waitq
, wait
);
2332 static void perf_event_reset(struct perf_event
*event
)
2334 (void)perf_event_read(event
);
2335 local64_set(&event
->count
, 0);
2336 perf_event_update_userpage(event
);
2340 * Holding the top-level event's child_mutex means that any
2341 * descendant process that has inherited this event will block
2342 * in sync_child_event if it goes to exit, thus satisfying the
2343 * task existence requirements of perf_event_enable/disable.
2345 static void perf_event_for_each_child(struct perf_event
*event
,
2346 void (*func
)(struct perf_event
*))
2348 struct perf_event
*child
;
2350 WARN_ON_ONCE(event
->ctx
->parent_ctx
);
2351 mutex_lock(&event
->child_mutex
);
2353 list_for_each_entry(child
, &event
->child_list
, child_list
)
2355 mutex_unlock(&event
->child_mutex
);
2358 static void perf_event_for_each(struct perf_event
*event
,
2359 void (*func
)(struct perf_event
*))
2361 struct perf_event_context
*ctx
= event
->ctx
;
2362 struct perf_event
*sibling
;
2364 WARN_ON_ONCE(ctx
->parent_ctx
);
2365 mutex_lock(&ctx
->mutex
);
2366 event
= event
->group_leader
;
2368 perf_event_for_each_child(event
, func
);
2370 list_for_each_entry(sibling
, &event
->sibling_list
, group_entry
)
2371 perf_event_for_each_child(event
, func
);
2372 mutex_unlock(&ctx
->mutex
);
2375 static int perf_event_period(struct perf_event
*event
, u64 __user
*arg
)
2377 struct perf_event_context
*ctx
= event
->ctx
;
2382 if (!event
->attr
.sample_period
)
2385 size
= copy_from_user(&value
, arg
, sizeof(value
));
2386 if (size
!= sizeof(value
))
2392 raw_spin_lock_irq(&ctx
->lock
);
2393 if (event
->attr
.freq
) {
2394 if (value
> sysctl_perf_event_sample_rate
) {
2399 event
->attr
.sample_freq
= value
;
2401 event
->attr
.sample_period
= value
;
2402 event
->hw
.sample_period
= value
;
2405 raw_spin_unlock_irq(&ctx
->lock
);
2410 static const struct file_operations perf_fops
;
2412 static struct perf_event
*perf_fget_light(int fd
, int *fput_needed
)
2416 file
= fget_light(fd
, fput_needed
);
2418 return ERR_PTR(-EBADF
);
2420 if (file
->f_op
!= &perf_fops
) {
2421 fput_light(file
, *fput_needed
);
2423 return ERR_PTR(-EBADF
);
2426 return file
->private_data
;
2429 static int perf_event_set_output(struct perf_event
*event
,
2430 struct perf_event
*output_event
);
2431 static int perf_event_set_filter(struct perf_event
*event
, void __user
*arg
);
2433 static long perf_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
2435 struct perf_event
*event
= file
->private_data
;
2436 void (*func
)(struct perf_event
*);
2440 case PERF_EVENT_IOC_ENABLE
:
2441 func
= perf_event_enable
;
2443 case PERF_EVENT_IOC_DISABLE
:
2444 func
= perf_event_disable
;
2446 case PERF_EVENT_IOC_RESET
:
2447 func
= perf_event_reset
;
2450 case PERF_EVENT_IOC_REFRESH
:
2451 return perf_event_refresh(event
, arg
);
2453 case PERF_EVENT_IOC_PERIOD
:
2454 return perf_event_period(event
, (u64 __user
*)arg
);
2456 case PERF_EVENT_IOC_SET_OUTPUT
:
2458 struct perf_event
*output_event
= NULL
;
2459 int fput_needed
= 0;
2463 output_event
= perf_fget_light(arg
, &fput_needed
);
2464 if (IS_ERR(output_event
))
2465 return PTR_ERR(output_event
);
2468 ret
= perf_event_set_output(event
, output_event
);
2470 fput_light(output_event
->filp
, fput_needed
);
2475 case PERF_EVENT_IOC_SET_FILTER
:
2476 return perf_event_set_filter(event
, (void __user
*)arg
);
2482 if (flags
& PERF_IOC_FLAG_GROUP
)
2483 perf_event_for_each(event
, func
);
2485 perf_event_for_each_child(event
, func
);
2490 int perf_event_task_enable(void)
2492 struct perf_event
*event
;
2494 mutex_lock(¤t
->perf_event_mutex
);
2495 list_for_each_entry(event
, ¤t
->perf_event_list
, owner_entry
)
2496 perf_event_for_each_child(event
, perf_event_enable
);
2497 mutex_unlock(¤t
->perf_event_mutex
);
2502 int perf_event_task_disable(void)
2504 struct perf_event
*event
;
2506 mutex_lock(¤t
->perf_event_mutex
);
2507 list_for_each_entry(event
, ¤t
->perf_event_list
, owner_entry
)
2508 perf_event_for_each_child(event
, perf_event_disable
);
2509 mutex_unlock(¤t
->perf_event_mutex
);
2514 #ifndef PERF_EVENT_INDEX_OFFSET
2515 # define PERF_EVENT_INDEX_OFFSET 0
2518 static int perf_event_index(struct perf_event
*event
)
2520 if (event
->state
!= PERF_EVENT_STATE_ACTIVE
)
2523 return event
->hw
.idx
+ 1 - PERF_EVENT_INDEX_OFFSET
;
2527 * Callers need to ensure there can be no nesting of this function, otherwise
2528 * the seqlock logic goes bad. We can not serialize this because the arch
2529 * code calls this from NMI context.
2531 void perf_event_update_userpage(struct perf_event
*event
)
2533 struct perf_event_mmap_page
*userpg
;
2534 struct perf_buffer
*buffer
;
2537 buffer
= rcu_dereference(event
->buffer
);
2541 userpg
= buffer
->user_page
;
2544 * Disable preemption so as to not let the corresponding user-space
2545 * spin too long if we get preempted.
2550 userpg
->index
= perf_event_index(event
);
2551 userpg
->offset
= perf_event_count(event
);
2552 if (event
->state
== PERF_EVENT_STATE_ACTIVE
)
2553 userpg
->offset
-= local64_read(&event
->hw
.prev_count
);
2555 userpg
->time_enabled
= event
->total_time_enabled
+
2556 atomic64_read(&event
->child_total_time_enabled
);
2558 userpg
->time_running
= event
->total_time_running
+
2559 atomic64_read(&event
->child_total_time_running
);
2568 static unsigned long perf_data_size(struct perf_buffer
*buffer
);
2571 perf_buffer_init(struct perf_buffer
*buffer
, long watermark
, int flags
)
2573 long max_size
= perf_data_size(buffer
);
2576 buffer
->watermark
= min(max_size
, watermark
);
2578 if (!buffer
->watermark
)
2579 buffer
->watermark
= max_size
/ 2;
2581 if (flags
& PERF_BUFFER_WRITABLE
)
2582 buffer
->writable
= 1;
2584 atomic_set(&buffer
->refcount
, 1);
2587 #ifndef CONFIG_PERF_USE_VMALLOC
2590 * Back perf_mmap() with regular GFP_KERNEL-0 pages.
2593 static struct page
*
2594 perf_mmap_to_page(struct perf_buffer
*buffer
, unsigned long pgoff
)
2596 if (pgoff
> buffer
->nr_pages
)
2600 return virt_to_page(buffer
->user_page
);
2602 return virt_to_page(buffer
->data_pages
[pgoff
- 1]);
2605 static void *perf_mmap_alloc_page(int cpu
)
2610 node
= (cpu
== -1) ? cpu
: cpu_to_node(cpu
);
2611 page
= alloc_pages_node(node
, GFP_KERNEL
| __GFP_ZERO
, 0);
2615 return page_address(page
);
2618 static struct perf_buffer
*
2619 perf_buffer_alloc(int nr_pages
, long watermark
, int cpu
, int flags
)
2621 struct perf_buffer
*buffer
;
2625 size
= sizeof(struct perf_buffer
);
2626 size
+= nr_pages
* sizeof(void *);
2628 buffer
= kzalloc(size
, GFP_KERNEL
);
2632 buffer
->user_page
= perf_mmap_alloc_page(cpu
);
2633 if (!buffer
->user_page
)
2634 goto fail_user_page
;
2636 for (i
= 0; i
< nr_pages
; i
++) {
2637 buffer
->data_pages
[i
] = perf_mmap_alloc_page(cpu
);
2638 if (!buffer
->data_pages
[i
])
2639 goto fail_data_pages
;
2642 buffer
->nr_pages
= nr_pages
;
2644 perf_buffer_init(buffer
, watermark
, flags
);
2649 for (i
--; i
>= 0; i
--)
2650 free_page((unsigned long)buffer
->data_pages
[i
]);
2652 free_page((unsigned long)buffer
->user_page
);
2661 static void perf_mmap_free_page(unsigned long addr
)
2663 struct page
*page
= virt_to_page((void *)addr
);
2665 page
->mapping
= NULL
;
2669 static void perf_buffer_free(struct perf_buffer
*buffer
)
2673 perf_mmap_free_page((unsigned long)buffer
->user_page
);
2674 for (i
= 0; i
< buffer
->nr_pages
; i
++)
2675 perf_mmap_free_page((unsigned long)buffer
->data_pages
[i
]);
2679 static inline int page_order(struct perf_buffer
*buffer
)
2687 * Back perf_mmap() with vmalloc memory.
2689 * Required for architectures that have d-cache aliasing issues.
2692 static inline int page_order(struct perf_buffer
*buffer
)
2694 return buffer
->page_order
;
2697 static struct page
*
2698 perf_mmap_to_page(struct perf_buffer
*buffer
, unsigned long pgoff
)
2700 if (pgoff
> (1UL << page_order(buffer
)))
2703 return vmalloc_to_page((void *)buffer
->user_page
+ pgoff
* PAGE_SIZE
);
2706 static void perf_mmap_unmark_page(void *addr
)
2708 struct page
*page
= vmalloc_to_page(addr
);
2710 page
->mapping
= NULL
;
2713 static void perf_buffer_free_work(struct work_struct
*work
)
2715 struct perf_buffer
*buffer
;
2719 buffer
= container_of(work
, struct perf_buffer
, work
);
2720 nr
= 1 << page_order(buffer
);
2722 base
= buffer
->user_page
;
2723 for (i
= 0; i
< nr
+ 1; i
++)
2724 perf_mmap_unmark_page(base
+ (i
* PAGE_SIZE
));
2730 static void perf_buffer_free(struct perf_buffer
*buffer
)
2732 schedule_work(&buffer
->work
);
2735 static struct perf_buffer
*
2736 perf_buffer_alloc(int nr_pages
, long watermark
, int cpu
, int flags
)
2738 struct perf_buffer
*buffer
;
2742 size
= sizeof(struct perf_buffer
);
2743 size
+= sizeof(void *);
2745 buffer
= kzalloc(size
, GFP_KERNEL
);
2749 INIT_WORK(&buffer
->work
, perf_buffer_free_work
);
2751 all_buf
= vmalloc_user((nr_pages
+ 1) * PAGE_SIZE
);
2755 buffer
->user_page
= all_buf
;
2756 buffer
->data_pages
[0] = all_buf
+ PAGE_SIZE
;
2757 buffer
->page_order
= ilog2(nr_pages
);
2758 buffer
->nr_pages
= 1;
2760 perf_buffer_init(buffer
, watermark
, flags
);
2773 static unsigned long perf_data_size(struct perf_buffer
*buffer
)
2775 return buffer
->nr_pages
<< (PAGE_SHIFT
+ page_order(buffer
));
2778 static int perf_mmap_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
2780 struct perf_event
*event
= vma
->vm_file
->private_data
;
2781 struct perf_buffer
*buffer
;
2782 int ret
= VM_FAULT_SIGBUS
;
2784 if (vmf
->flags
& FAULT_FLAG_MKWRITE
) {
2785 if (vmf
->pgoff
== 0)
2791 buffer
= rcu_dereference(event
->buffer
);
2795 if (vmf
->pgoff
&& (vmf
->flags
& FAULT_FLAG_WRITE
))
2798 vmf
->page
= perf_mmap_to_page(buffer
, vmf
->pgoff
);
2802 get_page(vmf
->page
);
2803 vmf
->page
->mapping
= vma
->vm_file
->f_mapping
;
2804 vmf
->page
->index
= vmf
->pgoff
;
2813 static void perf_buffer_free_rcu(struct rcu_head
*rcu_head
)
2815 struct perf_buffer
*buffer
;
2817 buffer
= container_of(rcu_head
, struct perf_buffer
, rcu_head
);
2818 perf_buffer_free(buffer
);
2821 static struct perf_buffer
*perf_buffer_get(struct perf_event
*event
)
2823 struct perf_buffer
*buffer
;
2826 buffer
= rcu_dereference(event
->buffer
);
2828 if (!atomic_inc_not_zero(&buffer
->refcount
))
2836 static void perf_buffer_put(struct perf_buffer
*buffer
)
2838 if (!atomic_dec_and_test(&buffer
->refcount
))
2841 call_rcu(&buffer
->rcu_head
, perf_buffer_free_rcu
);
2844 static void perf_mmap_open(struct vm_area_struct
*vma
)
2846 struct perf_event
*event
= vma
->vm_file
->private_data
;
2848 atomic_inc(&event
->mmap_count
);
2851 static void perf_mmap_close(struct vm_area_struct
*vma
)
2853 struct perf_event
*event
= vma
->vm_file
->private_data
;
2855 if (atomic_dec_and_mutex_lock(&event
->mmap_count
, &event
->mmap_mutex
)) {
2856 unsigned long size
= perf_data_size(event
->buffer
);
2857 struct user_struct
*user
= event
->mmap_user
;
2858 struct perf_buffer
*buffer
= event
->buffer
;
2860 atomic_long_sub((size
>> PAGE_SHIFT
) + 1, &user
->locked_vm
);
2861 vma
->vm_mm
->locked_vm
-= event
->mmap_locked
;
2862 rcu_assign_pointer(event
->buffer
, NULL
);
2863 mutex_unlock(&event
->mmap_mutex
);
2865 perf_buffer_put(buffer
);
2870 static const struct vm_operations_struct perf_mmap_vmops
= {
2871 .open
= perf_mmap_open
,
2872 .close
= perf_mmap_close
,
2873 .fault
= perf_mmap_fault
,
2874 .page_mkwrite
= perf_mmap_fault
,
2877 static int perf_mmap(struct file
*file
, struct vm_area_struct
*vma
)
2879 struct perf_event
*event
= file
->private_data
;
2880 unsigned long user_locked
, user_lock_limit
;
2881 struct user_struct
*user
= current_user();
2882 unsigned long locked
, lock_limit
;
2883 struct perf_buffer
*buffer
;
2884 unsigned long vma_size
;
2885 unsigned long nr_pages
;
2886 long user_extra
, extra
;
2887 int ret
= 0, flags
= 0;
2890 * Don't allow mmap() of inherited per-task counters. This would
2891 * create a performance issue due to all children writing to the
2894 if (event
->cpu
== -1 && event
->attr
.inherit
)
2897 if (!(vma
->vm_flags
& VM_SHARED
))
2900 vma_size
= vma
->vm_end
- vma
->vm_start
;
2901 nr_pages
= (vma_size
/ PAGE_SIZE
) - 1;
2904 * If we have buffer pages ensure they're a power-of-two number, so we
2905 * can do bitmasks instead of modulo.
2907 if (nr_pages
!= 0 && !is_power_of_2(nr_pages
))
2910 if (vma_size
!= PAGE_SIZE
* (1 + nr_pages
))
2913 if (vma
->vm_pgoff
!= 0)
2916 WARN_ON_ONCE(event
->ctx
->parent_ctx
);
2917 mutex_lock(&event
->mmap_mutex
);
2918 if (event
->buffer
) {
2919 if (event
->buffer
->nr_pages
== nr_pages
)
2920 atomic_inc(&event
->buffer
->refcount
);
2926 user_extra
= nr_pages
+ 1;
2927 user_lock_limit
= sysctl_perf_event_mlock
>> (PAGE_SHIFT
- 10);
2930 * Increase the limit linearly with more CPUs:
2932 user_lock_limit
*= num_online_cpus();
2934 user_locked
= atomic_long_read(&user
->locked_vm
) + user_extra
;
2937 if (user_locked
> user_lock_limit
)
2938 extra
= user_locked
- user_lock_limit
;
2940 lock_limit
= rlimit(RLIMIT_MEMLOCK
);
2941 lock_limit
>>= PAGE_SHIFT
;
2942 locked
= vma
->vm_mm
->locked_vm
+ extra
;
2944 if ((locked
> lock_limit
) && perf_paranoid_tracepoint_raw() &&
2945 !capable(CAP_IPC_LOCK
)) {
2950 WARN_ON(event
->buffer
);
2952 if (vma
->vm_flags
& VM_WRITE
)
2953 flags
|= PERF_BUFFER_WRITABLE
;
2955 buffer
= perf_buffer_alloc(nr_pages
, event
->attr
.wakeup_watermark
,
2961 rcu_assign_pointer(event
->buffer
, buffer
);
2963 atomic_long_add(user_extra
, &user
->locked_vm
);
2964 event
->mmap_locked
= extra
;
2965 event
->mmap_user
= get_current_user();
2966 vma
->vm_mm
->locked_vm
+= event
->mmap_locked
;
2970 atomic_inc(&event
->mmap_count
);
2971 mutex_unlock(&event
->mmap_mutex
);
2973 vma
->vm_flags
|= VM_RESERVED
;
2974 vma
->vm_ops
= &perf_mmap_vmops
;
2979 static int perf_fasync(int fd
, struct file
*filp
, int on
)
2981 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
2982 struct perf_event
*event
= filp
->private_data
;
2985 mutex_lock(&inode
->i_mutex
);
2986 retval
= fasync_helper(fd
, filp
, on
, &event
->fasync
);
2987 mutex_unlock(&inode
->i_mutex
);
2995 static const struct file_operations perf_fops
= {
2996 .llseek
= no_llseek
,
2997 .release
= perf_release
,
3000 .unlocked_ioctl
= perf_ioctl
,
3001 .compat_ioctl
= perf_ioctl
,
3003 .fasync
= perf_fasync
,
3009 * If there's data, ensure we set the poll() state and publish everything
3010 * to user-space before waking everybody up.
3013 void perf_event_wakeup(struct perf_event
*event
)
3015 wake_up_all(&event
->waitq
);
3017 if (event
->pending_kill
) {
3018 kill_fasync(&event
->fasync
, SIGIO
, event
->pending_kill
);
3019 event
->pending_kill
= 0;
3026 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
3028 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
3029 * single linked list and use cmpxchg() to add entries lockless.
3032 static void perf_pending_event(struct perf_pending_entry
*entry
)
3034 struct perf_event
*event
= container_of(entry
,
3035 struct perf_event
, pending
);
3037 if (event
->pending_disable
) {
3038 event
->pending_disable
= 0;
3039 __perf_event_disable(event
);
3042 if (event
->pending_wakeup
) {
3043 event
->pending_wakeup
= 0;
3044 perf_event_wakeup(event
);
3048 #define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
3050 static DEFINE_PER_CPU(struct perf_pending_entry
*, perf_pending_head
) = {
3054 static void perf_pending_queue(struct perf_pending_entry
*entry
,
3055 void (*func
)(struct perf_pending_entry
*))
3057 struct perf_pending_entry
**head
;
3059 if (cmpxchg(&entry
->next
, NULL
, PENDING_TAIL
) != NULL
)
3064 head
= &get_cpu_var(perf_pending_head
);
3067 entry
->next
= *head
;
3068 } while (cmpxchg(head
, entry
->next
, entry
) != entry
->next
);
3070 set_perf_event_pending();
3072 put_cpu_var(perf_pending_head
);
3075 static int __perf_pending_run(void)
3077 struct perf_pending_entry
*list
;
3080 list
= xchg(&__get_cpu_var(perf_pending_head
), PENDING_TAIL
);
3081 while (list
!= PENDING_TAIL
) {
3082 void (*func
)(struct perf_pending_entry
*);
3083 struct perf_pending_entry
*entry
= list
;
3090 * Ensure we observe the unqueue before we issue the wakeup,
3091 * so that we won't be waiting forever.
3092 * -- see perf_not_pending().
3103 static inline int perf_not_pending(struct perf_event
*event
)
3106 * If we flush on whatever cpu we run, there is a chance we don't
3110 __perf_pending_run();
3114 * Ensure we see the proper queue state before going to sleep
3115 * so that we do not miss the wakeup. -- see perf_pending_handle()
3118 return event
->pending
.next
== NULL
;
3121 static void perf_pending_sync(struct perf_event
*event
)
3123 wait_event(event
->waitq
, perf_not_pending(event
));
3126 void perf_event_do_pending(void)
3128 __perf_pending_run();
3132 * We assume there is only KVM supporting the callbacks.
3133 * Later on, we might change it to a list if there is
3134 * another virtualization implementation supporting the callbacks.
3136 struct perf_guest_info_callbacks
*perf_guest_cbs
;
3138 int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks
*cbs
)
3140 perf_guest_cbs
= cbs
;
3143 EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks
);
3145 int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks
*cbs
)
3147 perf_guest_cbs
= NULL
;
3150 EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks
);
3155 static bool perf_output_space(struct perf_buffer
*buffer
, unsigned long tail
,
3156 unsigned long offset
, unsigned long head
)
3160 if (!buffer
->writable
)
3163 mask
= perf_data_size(buffer
) - 1;
3165 offset
= (offset
- tail
) & mask
;
3166 head
= (head
- tail
) & mask
;
3168 if ((int)(head
- offset
) < 0)
3174 static void perf_output_wakeup(struct perf_output_handle
*handle
)
3176 atomic_set(&handle
->buffer
->poll
, POLL_IN
);
3179 handle
->event
->pending_wakeup
= 1;
3180 perf_pending_queue(&handle
->event
->pending
,
3181 perf_pending_event
);
3183 perf_event_wakeup(handle
->event
);
3187 * We need to ensure a later event_id doesn't publish a head when a former
3188 * event isn't done writing. However since we need to deal with NMIs we
3189 * cannot fully serialize things.
3191 * We only publish the head (and generate a wakeup) when the outer-most
3194 static void perf_output_get_handle(struct perf_output_handle
*handle
)
3196 struct perf_buffer
*buffer
= handle
->buffer
;
3199 local_inc(&buffer
->nest
);
3200 handle
->wakeup
= local_read(&buffer
->wakeup
);
3203 static void perf_output_put_handle(struct perf_output_handle
*handle
)
3205 struct perf_buffer
*buffer
= handle
->buffer
;
3209 head
= local_read(&buffer
->head
);
3212 * IRQ/NMI can happen here, which means we can miss a head update.
3215 if (!local_dec_and_test(&buffer
->nest
))
3219 * Publish the known good head. Rely on the full barrier implied
3220 * by atomic_dec_and_test() order the buffer->head read and this
3223 buffer
->user_page
->data_head
= head
;
3226 * Now check if we missed an update, rely on the (compiler)
3227 * barrier in atomic_dec_and_test() to re-read buffer->head.
3229 if (unlikely(head
!= local_read(&buffer
->head
))) {
3230 local_inc(&buffer
->nest
);
3234 if (handle
->wakeup
!= local_read(&buffer
->wakeup
))
3235 perf_output_wakeup(handle
);
3241 __always_inline
void perf_output_copy(struct perf_output_handle
*handle
,
3242 const void *buf
, unsigned int len
)
3245 unsigned long size
= min_t(unsigned long, handle
->size
, len
);
3247 memcpy(handle
->addr
, buf
, size
);
3250 handle
->addr
+= size
;
3252 handle
->size
-= size
;
3253 if (!handle
->size
) {
3254 struct perf_buffer
*buffer
= handle
->buffer
;
3257 handle
->page
&= buffer
->nr_pages
- 1;
3258 handle
->addr
= buffer
->data_pages
[handle
->page
];
3259 handle
->size
= PAGE_SIZE
<< page_order(buffer
);
3264 int perf_output_begin(struct perf_output_handle
*handle
,
3265 struct perf_event
*event
, unsigned int size
,
3266 int nmi
, int sample
)
3268 struct perf_buffer
*buffer
;
3269 unsigned long tail
, offset
, head
;
3272 struct perf_event_header header
;
3279 * For inherited events we send all the output towards the parent.
3282 event
= event
->parent
;
3284 buffer
= rcu_dereference(event
->buffer
);
3288 handle
->buffer
= buffer
;
3289 handle
->event
= event
;
3291 handle
->sample
= sample
;
3293 if (!buffer
->nr_pages
)
3296 have_lost
= local_read(&buffer
->lost
);
3298 size
+= sizeof(lost_event
);
3300 perf_output_get_handle(handle
);
3304 * Userspace could choose to issue a mb() before updating the
3305 * tail pointer. So that all reads will be completed before the
3308 tail
= ACCESS_ONCE(buffer
->user_page
->data_tail
);
3310 offset
= head
= local_read(&buffer
->head
);
3312 if (unlikely(!perf_output_space(buffer
, tail
, offset
, head
)))
3314 } while (local_cmpxchg(&buffer
->head
, offset
, head
) != offset
);
3316 if (head
- local_read(&buffer
->wakeup
) > buffer
->watermark
)
3317 local_add(buffer
->watermark
, &buffer
->wakeup
);
3319 handle
->page
= offset
>> (PAGE_SHIFT
+ page_order(buffer
));
3320 handle
->page
&= buffer
->nr_pages
- 1;
3321 handle
->size
= offset
& ((PAGE_SIZE
<< page_order(buffer
)) - 1);
3322 handle
->addr
= buffer
->data_pages
[handle
->page
];
3323 handle
->addr
+= handle
->size
;
3324 handle
->size
= (PAGE_SIZE
<< page_order(buffer
)) - handle
->size
;
3327 lost_event
.header
.type
= PERF_RECORD_LOST
;
3328 lost_event
.header
.misc
= 0;
3329 lost_event
.header
.size
= sizeof(lost_event
);
3330 lost_event
.id
= event
->id
;
3331 lost_event
.lost
= local_xchg(&buffer
->lost
, 0);
3333 perf_output_put(handle
, lost_event
);
3339 local_inc(&buffer
->lost
);
3340 perf_output_put_handle(handle
);
3347 void perf_output_end(struct perf_output_handle
*handle
)
3349 struct perf_event
*event
= handle
->event
;
3350 struct perf_buffer
*buffer
= handle
->buffer
;
3352 int wakeup_events
= event
->attr
.wakeup_events
;
3354 if (handle
->sample
&& wakeup_events
) {
3355 int events
= local_inc_return(&buffer
->events
);
3356 if (events
>= wakeup_events
) {
3357 local_sub(wakeup_events
, &buffer
->events
);
3358 local_inc(&buffer
->wakeup
);
3362 perf_output_put_handle(handle
);
3366 static u32
perf_event_pid(struct perf_event
*event
, struct task_struct
*p
)
3369 * only top level events have the pid namespace they were created in
3372 event
= event
->parent
;
3374 return task_tgid_nr_ns(p
, event
->ns
);
3377 static u32
perf_event_tid(struct perf_event
*event
, struct task_struct
*p
)
3380 * only top level events have the pid namespace they were created in
3383 event
= event
->parent
;
3385 return task_pid_nr_ns(p
, event
->ns
);
3388 static void perf_output_read_one(struct perf_output_handle
*handle
,
3389 struct perf_event
*event
)
3391 u64 read_format
= event
->attr
.read_format
;
3395 values
[n
++] = perf_event_count(event
);
3396 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
) {
3397 values
[n
++] = event
->total_time_enabled
+
3398 atomic64_read(&event
->child_total_time_enabled
);
3400 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
) {
3401 values
[n
++] = event
->total_time_running
+
3402 atomic64_read(&event
->child_total_time_running
);
3404 if (read_format
& PERF_FORMAT_ID
)
3405 values
[n
++] = primary_event_id(event
);
3407 perf_output_copy(handle
, values
, n
* sizeof(u64
));
3411 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
3413 static void perf_output_read_group(struct perf_output_handle
*handle
,
3414 struct perf_event
*event
)
3416 struct perf_event
*leader
= event
->group_leader
, *sub
;
3417 u64 read_format
= event
->attr
.read_format
;
3421 values
[n
++] = 1 + leader
->nr_siblings
;
3423 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
3424 values
[n
++] = leader
->total_time_enabled
;
3426 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
3427 values
[n
++] = leader
->total_time_running
;
3429 if (leader
!= event
)
3430 leader
->pmu
->read(leader
);
3432 values
[n
++] = perf_event_count(leader
);
3433 if (read_format
& PERF_FORMAT_ID
)
3434 values
[n
++] = primary_event_id(leader
);
3436 perf_output_copy(handle
, values
, n
* sizeof(u64
));
3438 list_for_each_entry(sub
, &leader
->sibling_list
, group_entry
) {
3442 sub
->pmu
->read(sub
);
3444 values
[n
++] = perf_event_count(sub
);
3445 if (read_format
& PERF_FORMAT_ID
)
3446 values
[n
++] = primary_event_id(sub
);
3448 perf_output_copy(handle
, values
, n
* sizeof(u64
));
3452 static void perf_output_read(struct perf_output_handle
*handle
,
3453 struct perf_event
*event
)
3455 if (event
->attr
.read_format
& PERF_FORMAT_GROUP
)
3456 perf_output_read_group(handle
, event
);
3458 perf_output_read_one(handle
, event
);
3461 void perf_output_sample(struct perf_output_handle
*handle
,
3462 struct perf_event_header
*header
,
3463 struct perf_sample_data
*data
,
3464 struct perf_event
*event
)
3466 u64 sample_type
= data
->type
;
3468 perf_output_put(handle
, *header
);
3470 if (sample_type
& PERF_SAMPLE_IP
)
3471 perf_output_put(handle
, data
->ip
);
3473 if (sample_type
& PERF_SAMPLE_TID
)
3474 perf_output_put(handle
, data
->tid_entry
);
3476 if (sample_type
& PERF_SAMPLE_TIME
)
3477 perf_output_put(handle
, data
->time
);
3479 if (sample_type
& PERF_SAMPLE_ADDR
)
3480 perf_output_put(handle
, data
->addr
);
3482 if (sample_type
& PERF_SAMPLE_ID
)
3483 perf_output_put(handle
, data
->id
);
3485 if (sample_type
& PERF_SAMPLE_STREAM_ID
)
3486 perf_output_put(handle
, data
->stream_id
);
3488 if (sample_type
& PERF_SAMPLE_CPU
)
3489 perf_output_put(handle
, data
->cpu_entry
);
3491 if (sample_type
& PERF_SAMPLE_PERIOD
)
3492 perf_output_put(handle
, data
->period
);
3494 if (sample_type
& PERF_SAMPLE_READ
)
3495 perf_output_read(handle
, event
);
3497 if (sample_type
& PERF_SAMPLE_CALLCHAIN
) {
3498 if (data
->callchain
) {
3501 if (data
->callchain
)
3502 size
+= data
->callchain
->nr
;
3504 size
*= sizeof(u64
);
3506 perf_output_copy(handle
, data
->callchain
, size
);
3509 perf_output_put(handle
, nr
);
3513 if (sample_type
& PERF_SAMPLE_RAW
) {
3515 perf_output_put(handle
, data
->raw
->size
);
3516 perf_output_copy(handle
, data
->raw
->data
,
3523 .size
= sizeof(u32
),
3526 perf_output_put(handle
, raw
);
3531 void perf_prepare_sample(struct perf_event_header
*header
,
3532 struct perf_sample_data
*data
,
3533 struct perf_event
*event
,
3534 struct pt_regs
*regs
)
3536 u64 sample_type
= event
->attr
.sample_type
;
3538 data
->type
= sample_type
;
3540 header
->type
= PERF_RECORD_SAMPLE
;
3541 header
->size
= sizeof(*header
);
3544 header
->misc
|= perf_misc_flags(regs
);
3546 if (sample_type
& PERF_SAMPLE_IP
) {
3547 data
->ip
= perf_instruction_pointer(regs
);
3549 header
->size
+= sizeof(data
->ip
);
3552 if (sample_type
& PERF_SAMPLE_TID
) {
3553 /* namespace issues */
3554 data
->tid_entry
.pid
= perf_event_pid(event
, current
);
3555 data
->tid_entry
.tid
= perf_event_tid(event
, current
);
3557 header
->size
+= sizeof(data
->tid_entry
);
3560 if (sample_type
& PERF_SAMPLE_TIME
) {
3561 data
->time
= perf_clock();
3563 header
->size
+= sizeof(data
->time
);
3566 if (sample_type
& PERF_SAMPLE_ADDR
)
3567 header
->size
+= sizeof(data
->addr
);
3569 if (sample_type
& PERF_SAMPLE_ID
) {
3570 data
->id
= primary_event_id(event
);
3572 header
->size
+= sizeof(data
->id
);
3575 if (sample_type
& PERF_SAMPLE_STREAM_ID
) {
3576 data
->stream_id
= event
->id
;
3578 header
->size
+= sizeof(data
->stream_id
);
3581 if (sample_type
& PERF_SAMPLE_CPU
) {
3582 data
->cpu_entry
.cpu
= raw_smp_processor_id();
3583 data
->cpu_entry
.reserved
= 0;
3585 header
->size
+= sizeof(data
->cpu_entry
);
3588 if (sample_type
& PERF_SAMPLE_PERIOD
)
3589 header
->size
+= sizeof(data
->period
);
3591 if (sample_type
& PERF_SAMPLE_READ
)
3592 header
->size
+= perf_event_read_size(event
);
3594 if (sample_type
& PERF_SAMPLE_CALLCHAIN
) {
3597 data
->callchain
= perf_callchain(regs
);
3599 if (data
->callchain
)
3600 size
+= data
->callchain
->nr
;
3602 header
->size
+= size
* sizeof(u64
);
3605 if (sample_type
& PERF_SAMPLE_RAW
) {
3606 int size
= sizeof(u32
);
3609 size
+= data
->raw
->size
;
3611 size
+= sizeof(u32
);
3613 WARN_ON_ONCE(size
& (sizeof(u64
)-1));
3614 header
->size
+= size
;
3618 static void perf_event_output(struct perf_event
*event
, int nmi
,
3619 struct perf_sample_data
*data
,
3620 struct pt_regs
*regs
)
3622 struct perf_output_handle handle
;
3623 struct perf_event_header header
;
3625 /* protect the callchain buffers */
3628 perf_prepare_sample(&header
, data
, event
, regs
);
3630 if (perf_output_begin(&handle
, event
, header
.size
, nmi
, 1))
3633 perf_output_sample(&handle
, &header
, data
, event
);
3635 perf_output_end(&handle
);
3645 struct perf_read_event
{
3646 struct perf_event_header header
;
3653 perf_event_read_event(struct perf_event
*event
,
3654 struct task_struct
*task
)
3656 struct perf_output_handle handle
;
3657 struct perf_read_event read_event
= {
3659 .type
= PERF_RECORD_READ
,
3661 .size
= sizeof(read_event
) + perf_event_read_size(event
),
3663 .pid
= perf_event_pid(event
, task
),
3664 .tid
= perf_event_tid(event
, task
),
3668 ret
= perf_output_begin(&handle
, event
, read_event
.header
.size
, 0, 0);
3672 perf_output_put(&handle
, read_event
);
3673 perf_output_read(&handle
, event
);
3675 perf_output_end(&handle
);
3679 * task tracking -- fork/exit
3681 * enabled by: attr.comm | attr.mmap | attr.mmap_data | attr.task
3684 struct perf_task_event
{
3685 struct task_struct
*task
;
3686 struct perf_event_context
*task_ctx
;
3689 struct perf_event_header header
;
3699 static void perf_event_task_output(struct perf_event
*event
,
3700 struct perf_task_event
*task_event
)
3702 struct perf_output_handle handle
;
3703 struct task_struct
*task
= task_event
->task
;
3706 size
= task_event
->event_id
.header
.size
;
3707 ret
= perf_output_begin(&handle
, event
, size
, 0, 0);
3712 task_event
->event_id
.pid
= perf_event_pid(event
, task
);
3713 task_event
->event_id
.ppid
= perf_event_pid(event
, current
);
3715 task_event
->event_id
.tid
= perf_event_tid(event
, task
);
3716 task_event
->event_id
.ptid
= perf_event_tid(event
, current
);
3718 perf_output_put(&handle
, task_event
->event_id
);
3720 perf_output_end(&handle
);
3723 static int perf_event_task_match(struct perf_event
*event
)
3725 if (event
->state
< PERF_EVENT_STATE_INACTIVE
)
3728 if (event
->cpu
!= -1 && event
->cpu
!= smp_processor_id())
3731 if (event
->attr
.comm
|| event
->attr
.mmap
||
3732 event
->attr
.mmap_data
|| event
->attr
.task
)
3738 static void perf_event_task_ctx(struct perf_event_context
*ctx
,
3739 struct perf_task_event
*task_event
)
3741 struct perf_event
*event
;
3743 list_for_each_entry_rcu(event
, &ctx
->event_list
, event_entry
) {
3744 if (perf_event_task_match(event
))
3745 perf_event_task_output(event
, task_event
);
3749 static void perf_event_task_event(struct perf_task_event
*task_event
)
3751 struct perf_cpu_context
*cpuctx
;
3752 struct perf_event_context
*ctx
= task_event
->task_ctx
;
3755 cpuctx
= &get_cpu_var(perf_cpu_context
);
3756 perf_event_task_ctx(&cpuctx
->ctx
, task_event
);
3758 ctx
= rcu_dereference(current
->perf_event_ctxp
);
3760 perf_event_task_ctx(ctx
, task_event
);
3761 put_cpu_var(perf_cpu_context
);
3765 static void perf_event_task(struct task_struct
*task
,
3766 struct perf_event_context
*task_ctx
,
3769 struct perf_task_event task_event
;
3771 if (!atomic_read(&nr_comm_events
) &&
3772 !atomic_read(&nr_mmap_events
) &&
3773 !atomic_read(&nr_task_events
))
3776 task_event
= (struct perf_task_event
){
3778 .task_ctx
= task_ctx
,
3781 .type
= new ? PERF_RECORD_FORK
: PERF_RECORD_EXIT
,
3783 .size
= sizeof(task_event
.event_id
),
3789 .time
= perf_clock(),
3793 perf_event_task_event(&task_event
);
3796 void perf_event_fork(struct task_struct
*task
)
3798 perf_event_task(task
, NULL
, 1);
3805 struct perf_comm_event
{
3806 struct task_struct
*task
;
3811 struct perf_event_header header
;
3818 static void perf_event_comm_output(struct perf_event
*event
,
3819 struct perf_comm_event
*comm_event
)
3821 struct perf_output_handle handle
;
3822 int size
= comm_event
->event_id
.header
.size
;
3823 int ret
= perf_output_begin(&handle
, event
, size
, 0, 0);
3828 comm_event
->event_id
.pid
= perf_event_pid(event
, comm_event
->task
);
3829 comm_event
->event_id
.tid
= perf_event_tid(event
, comm_event
->task
);
3831 perf_output_put(&handle
, comm_event
->event_id
);
3832 perf_output_copy(&handle
, comm_event
->comm
,
3833 comm_event
->comm_size
);
3834 perf_output_end(&handle
);
3837 static int perf_event_comm_match(struct perf_event
*event
)
3839 if (event
->state
< PERF_EVENT_STATE_INACTIVE
)
3842 if (event
->cpu
!= -1 && event
->cpu
!= smp_processor_id())
3845 if (event
->attr
.comm
)
3851 static void perf_event_comm_ctx(struct perf_event_context
*ctx
,
3852 struct perf_comm_event
*comm_event
)
3854 struct perf_event
*event
;
3856 list_for_each_entry_rcu(event
, &ctx
->event_list
, event_entry
) {
3857 if (perf_event_comm_match(event
))
3858 perf_event_comm_output(event
, comm_event
);
3862 static void perf_event_comm_event(struct perf_comm_event
*comm_event
)
3864 struct perf_cpu_context
*cpuctx
;
3865 struct perf_event_context
*ctx
;
3867 char comm
[TASK_COMM_LEN
];
3869 memset(comm
, 0, sizeof(comm
));
3870 strlcpy(comm
, comm_event
->task
->comm
, sizeof(comm
));
3871 size
= ALIGN(strlen(comm
)+1, sizeof(u64
));
3873 comm_event
->comm
= comm
;
3874 comm_event
->comm_size
= size
;
3876 comm_event
->event_id
.header
.size
= sizeof(comm_event
->event_id
) + size
;
3879 cpuctx
= &get_cpu_var(perf_cpu_context
);
3880 perf_event_comm_ctx(&cpuctx
->ctx
, comm_event
);
3881 ctx
= rcu_dereference(current
->perf_event_ctxp
);
3883 perf_event_comm_ctx(ctx
, comm_event
);
3884 put_cpu_var(perf_cpu_context
);
3888 void perf_event_comm(struct task_struct
*task
)
3890 struct perf_comm_event comm_event
;
3892 if (task
->perf_event_ctxp
)
3893 perf_event_enable_on_exec(task
);
3895 if (!atomic_read(&nr_comm_events
))
3898 comm_event
= (struct perf_comm_event
){
3904 .type
= PERF_RECORD_COMM
,
3913 perf_event_comm_event(&comm_event
);
3920 struct perf_mmap_event
{
3921 struct vm_area_struct
*vma
;
3923 const char *file_name
;
3927 struct perf_event_header header
;
3937 static void perf_event_mmap_output(struct perf_event
*event
,
3938 struct perf_mmap_event
*mmap_event
)
3940 struct perf_output_handle handle
;
3941 int size
= mmap_event
->event_id
.header
.size
;
3942 int ret
= perf_output_begin(&handle
, event
, size
, 0, 0);
3947 mmap_event
->event_id
.pid
= perf_event_pid(event
, current
);
3948 mmap_event
->event_id
.tid
= perf_event_tid(event
, current
);
3950 perf_output_put(&handle
, mmap_event
->event_id
);
3951 perf_output_copy(&handle
, mmap_event
->file_name
,
3952 mmap_event
->file_size
);
3953 perf_output_end(&handle
);
3956 static int perf_event_mmap_match(struct perf_event
*event
,
3957 struct perf_mmap_event
*mmap_event
,
3960 if (event
->state
< PERF_EVENT_STATE_INACTIVE
)
3963 if (event
->cpu
!= -1 && event
->cpu
!= smp_processor_id())
3966 if ((!executable
&& event
->attr
.mmap_data
) ||
3967 (executable
&& event
->attr
.mmap
))
3973 static void perf_event_mmap_ctx(struct perf_event_context
*ctx
,
3974 struct perf_mmap_event
*mmap_event
,
3977 struct perf_event
*event
;
3979 list_for_each_entry_rcu(event
, &ctx
->event_list
, event_entry
) {
3980 if (perf_event_mmap_match(event
, mmap_event
, executable
))
3981 perf_event_mmap_output(event
, mmap_event
);
3985 static void perf_event_mmap_event(struct perf_mmap_event
*mmap_event
)
3987 struct perf_cpu_context
*cpuctx
;
3988 struct perf_event_context
*ctx
;
3989 struct vm_area_struct
*vma
= mmap_event
->vma
;
3990 struct file
*file
= vma
->vm_file
;
3996 memset(tmp
, 0, sizeof(tmp
));
4000 * d_path works from the end of the buffer backwards, so we
4001 * need to add enough zero bytes after the string to handle
4002 * the 64bit alignment we do later.
4004 buf
= kzalloc(PATH_MAX
+ sizeof(u64
), GFP_KERNEL
);
4006 name
= strncpy(tmp
, "//enomem", sizeof(tmp
));
4009 name
= d_path(&file
->f_path
, buf
, PATH_MAX
);
4011 name
= strncpy(tmp
, "//toolong", sizeof(tmp
));
4015 if (arch_vma_name(mmap_event
->vma
)) {
4016 name
= strncpy(tmp
, arch_vma_name(mmap_event
->vma
),
4022 name
= strncpy(tmp
, "[vdso]", sizeof(tmp
));
4024 } else if (vma
->vm_start
<= vma
->vm_mm
->start_brk
&&
4025 vma
->vm_end
>= vma
->vm_mm
->brk
) {
4026 name
= strncpy(tmp
, "[heap]", sizeof(tmp
));
4028 } else if (vma
->vm_start
<= vma
->vm_mm
->start_stack
&&
4029 vma
->vm_end
>= vma
->vm_mm
->start_stack
) {
4030 name
= strncpy(tmp
, "[stack]", sizeof(tmp
));
4034 name
= strncpy(tmp
, "//anon", sizeof(tmp
));
4039 size
= ALIGN(strlen(name
)+1, sizeof(u64
));
4041 mmap_event
->file_name
= name
;
4042 mmap_event
->file_size
= size
;
4044 mmap_event
->event_id
.header
.size
= sizeof(mmap_event
->event_id
) + size
;
4047 cpuctx
= &get_cpu_var(perf_cpu_context
);
4048 perf_event_mmap_ctx(&cpuctx
->ctx
, mmap_event
, vma
->vm_flags
& VM_EXEC
);
4049 ctx
= rcu_dereference(current
->perf_event_ctxp
);
4051 perf_event_mmap_ctx(ctx
, mmap_event
, vma
->vm_flags
& VM_EXEC
);
4052 put_cpu_var(perf_cpu_context
);
4058 void perf_event_mmap(struct vm_area_struct
*vma
)
4060 struct perf_mmap_event mmap_event
;
4062 if (!atomic_read(&nr_mmap_events
))
4065 mmap_event
= (struct perf_mmap_event
){
4071 .type
= PERF_RECORD_MMAP
,
4072 .misc
= PERF_RECORD_MISC_USER
,
4077 .start
= vma
->vm_start
,
4078 .len
= vma
->vm_end
- vma
->vm_start
,
4079 .pgoff
= (u64
)vma
->vm_pgoff
<< PAGE_SHIFT
,
4083 perf_event_mmap_event(&mmap_event
);
4087 * IRQ throttle logging
4090 static void perf_log_throttle(struct perf_event
*event
, int enable
)
4092 struct perf_output_handle handle
;
4096 struct perf_event_header header
;
4100 } throttle_event
= {
4102 .type
= PERF_RECORD_THROTTLE
,
4104 .size
= sizeof(throttle_event
),
4106 .time
= perf_clock(),
4107 .id
= primary_event_id(event
),
4108 .stream_id
= event
->id
,
4112 throttle_event
.header
.type
= PERF_RECORD_UNTHROTTLE
;
4114 ret
= perf_output_begin(&handle
, event
, sizeof(throttle_event
), 1, 0);
4118 perf_output_put(&handle
, throttle_event
);
4119 perf_output_end(&handle
);
4123 * Generic event overflow handling, sampling.
4126 static int __perf_event_overflow(struct perf_event
*event
, int nmi
,
4127 int throttle
, struct perf_sample_data
*data
,
4128 struct pt_regs
*regs
)
4130 int events
= atomic_read(&event
->event_limit
);
4131 struct hw_perf_event
*hwc
= &event
->hw
;
4134 throttle
= (throttle
&& event
->pmu
->unthrottle
!= NULL
);
4139 if (hwc
->interrupts
!= MAX_INTERRUPTS
) {
4141 if (HZ
* hwc
->interrupts
>
4142 (u64
)sysctl_perf_event_sample_rate
) {
4143 hwc
->interrupts
= MAX_INTERRUPTS
;
4144 perf_log_throttle(event
, 0);
4149 * Keep re-disabling events even though on the previous
4150 * pass we disabled it - just in case we raced with a
4151 * sched-in and the event got enabled again:
4157 if (event
->attr
.freq
) {
4158 u64 now
= perf_clock();
4159 s64 delta
= now
- hwc
->freq_time_stamp
;
4161 hwc
->freq_time_stamp
= now
;
4163 if (delta
> 0 && delta
< 2*TICK_NSEC
)
4164 perf_adjust_period(event
, delta
, hwc
->last_period
);
4168 * XXX event_limit might not quite work as expected on inherited
4172 event
->pending_kill
= POLL_IN
;
4173 if (events
&& atomic_dec_and_test(&event
->event_limit
)) {
4175 event
->pending_kill
= POLL_HUP
;
4177 event
->pending_disable
= 1;
4178 perf_pending_queue(&event
->pending
,
4179 perf_pending_event
);
4181 perf_event_disable(event
);
4184 if (event
->overflow_handler
)
4185 event
->overflow_handler(event
, nmi
, data
, regs
);
4187 perf_event_output(event
, nmi
, data
, regs
);
4192 int perf_event_overflow(struct perf_event
*event
, int nmi
,
4193 struct perf_sample_data
*data
,
4194 struct pt_regs
*regs
)
4196 return __perf_event_overflow(event
, nmi
, 1, data
, regs
);
4200 * Generic software event infrastructure
4204 * We directly increment event->count and keep a second value in
4205 * event->hw.period_left to count intervals. This period event
4206 * is kept in the range [-sample_period, 0] so that we can use the
4210 static u64
perf_swevent_set_period(struct perf_event
*event
)
4212 struct hw_perf_event
*hwc
= &event
->hw
;
4213 u64 period
= hwc
->last_period
;
4217 hwc
->last_period
= hwc
->sample_period
;
4220 old
= val
= local64_read(&hwc
->period_left
);
4224 nr
= div64_u64(period
+ val
, period
);
4225 offset
= nr
* period
;
4227 if (local64_cmpxchg(&hwc
->period_left
, old
, val
) != old
)
4233 static void perf_swevent_overflow(struct perf_event
*event
, u64 overflow
,
4234 int nmi
, struct perf_sample_data
*data
,
4235 struct pt_regs
*regs
)
4237 struct hw_perf_event
*hwc
= &event
->hw
;
4240 data
->period
= event
->hw
.last_period
;
4242 overflow
= perf_swevent_set_period(event
);
4244 if (hwc
->interrupts
== MAX_INTERRUPTS
)
4247 for (; overflow
; overflow
--) {
4248 if (__perf_event_overflow(event
, nmi
, throttle
,
4251 * We inhibit the overflow from happening when
4252 * hwc->interrupts == MAX_INTERRUPTS.
4260 static void perf_swevent_add(struct perf_event
*event
, u64 nr
,
4261 int nmi
, struct perf_sample_data
*data
,
4262 struct pt_regs
*regs
)
4264 struct hw_perf_event
*hwc
= &event
->hw
;
4266 local64_add(nr
, &event
->count
);
4271 if (!hwc
->sample_period
)
4274 if (nr
== 1 && hwc
->sample_period
== 1 && !event
->attr
.freq
)
4275 return perf_swevent_overflow(event
, 1, nmi
, data
, regs
);
4277 if (local64_add_negative(nr
, &hwc
->period_left
))
4280 perf_swevent_overflow(event
, 0, nmi
, data
, regs
);
4283 static int perf_exclude_event(struct perf_event
*event
,
4284 struct pt_regs
*regs
)
4287 if (event
->attr
.exclude_user
&& user_mode(regs
))
4290 if (event
->attr
.exclude_kernel
&& !user_mode(regs
))
4297 static int perf_swevent_match(struct perf_event
*event
,
4298 enum perf_type_id type
,
4300 struct perf_sample_data
*data
,
4301 struct pt_regs
*regs
)
4303 if (event
->attr
.type
!= type
)
4306 if (event
->attr
.config
!= event_id
)
4309 if (perf_exclude_event(event
, regs
))
4315 static inline u64
swevent_hash(u64 type
, u32 event_id
)
4317 u64 val
= event_id
| (type
<< 32);
4319 return hash_64(val
, SWEVENT_HLIST_BITS
);
4322 static inline struct hlist_head
*
4323 __find_swevent_head(struct swevent_hlist
*hlist
, u64 type
, u32 event_id
)
4325 u64 hash
= swevent_hash(type
, event_id
);
4327 return &hlist
->heads
[hash
];
4330 /* For the read side: events when they trigger */
4331 static inline struct hlist_head
*
4332 find_swevent_head_rcu(struct perf_cpu_context
*ctx
, u64 type
, u32 event_id
)
4334 struct swevent_hlist
*hlist
;
4336 hlist
= rcu_dereference(ctx
->swevent_hlist
);
4340 return __find_swevent_head(hlist
, type
, event_id
);
4343 /* For the event head insertion and removal in the hlist */
4344 static inline struct hlist_head
*
4345 find_swevent_head(struct perf_cpu_context
*ctx
, struct perf_event
*event
)
4347 struct swevent_hlist
*hlist
;
4348 u32 event_id
= event
->attr
.config
;
4349 u64 type
= event
->attr
.type
;
4352 * Event scheduling is always serialized against hlist allocation
4353 * and release. Which makes the protected version suitable here.
4354 * The context lock guarantees that.
4356 hlist
= rcu_dereference_protected(ctx
->swevent_hlist
,
4357 lockdep_is_held(&event
->ctx
->lock
));
4361 return __find_swevent_head(hlist
, type
, event_id
);
4364 static void do_perf_sw_event(enum perf_type_id type
, u32 event_id
,
4366 struct perf_sample_data
*data
,
4367 struct pt_regs
*regs
)
4369 struct perf_cpu_context
*cpuctx
;
4370 struct perf_event
*event
;
4371 struct hlist_node
*node
;
4372 struct hlist_head
*head
;
4374 cpuctx
= &__get_cpu_var(perf_cpu_context
);
4378 head
= find_swevent_head_rcu(cpuctx
, type
, event_id
);
4383 hlist_for_each_entry_rcu(event
, node
, head
, hlist_entry
) {
4384 if (perf_swevent_match(event
, type
, event_id
, data
, regs
))
4385 perf_swevent_add(event
, nr
, nmi
, data
, regs
);
4391 int perf_swevent_get_recursion_context(void)
4393 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
4395 return get_recursion_context(cpuctx
->recursion
);
4397 EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context
);
4399 void inline perf_swevent_put_recursion_context(int rctx
)
4401 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
4403 put_recursion_context(cpuctx
->recursion
, rctx
);
4406 void __perf_sw_event(u32 event_id
, u64 nr
, int nmi
,
4407 struct pt_regs
*regs
, u64 addr
)
4409 struct perf_sample_data data
;
4412 preempt_disable_notrace();
4413 rctx
= perf_swevent_get_recursion_context();
4417 perf_sample_data_init(&data
, addr
);
4419 do_perf_sw_event(PERF_TYPE_SOFTWARE
, event_id
, nr
, nmi
, &data
, regs
);
4421 perf_swevent_put_recursion_context(rctx
);
4422 preempt_enable_notrace();
4425 static void perf_swevent_read(struct perf_event
*event
)
4429 static int perf_swevent_enable(struct perf_event
*event
)
4431 struct hw_perf_event
*hwc
= &event
->hw
;
4432 struct perf_cpu_context
*cpuctx
;
4433 struct hlist_head
*head
;
4435 cpuctx
= &__get_cpu_var(perf_cpu_context
);
4437 if (hwc
->sample_period
) {
4438 hwc
->last_period
= hwc
->sample_period
;
4439 perf_swevent_set_period(event
);
4442 head
= find_swevent_head(cpuctx
, event
);
4443 if (WARN_ON_ONCE(!head
))
4446 hlist_add_head_rcu(&event
->hlist_entry
, head
);
4451 static void perf_swevent_disable(struct perf_event
*event
)
4453 hlist_del_rcu(&event
->hlist_entry
);
4456 static void perf_swevent_void(struct perf_event
*event
)
4460 static int perf_swevent_int(struct perf_event
*event
)
4465 /* Deref the hlist from the update side */
4466 static inline struct swevent_hlist
*
4467 swevent_hlist_deref(struct perf_cpu_context
*cpuctx
)
4469 return rcu_dereference_protected(cpuctx
->swevent_hlist
,
4470 lockdep_is_held(&cpuctx
->hlist_mutex
));
4473 static void swevent_hlist_release_rcu(struct rcu_head
*rcu_head
)
4475 struct swevent_hlist
*hlist
;
4477 hlist
= container_of(rcu_head
, struct swevent_hlist
, rcu_head
);
4481 static void swevent_hlist_release(struct perf_cpu_context
*cpuctx
)
4483 struct swevent_hlist
*hlist
= swevent_hlist_deref(cpuctx
);
4488 rcu_assign_pointer(cpuctx
->swevent_hlist
, NULL
);
4489 call_rcu(&hlist
->rcu_head
, swevent_hlist_release_rcu
);
4492 static void swevent_hlist_put_cpu(struct perf_event
*event
, int cpu
)
4494 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
4496 mutex_lock(&cpuctx
->hlist_mutex
);
4498 if (!--cpuctx
->hlist_refcount
)
4499 swevent_hlist_release(cpuctx
);
4501 mutex_unlock(&cpuctx
->hlist_mutex
);
4504 static void swevent_hlist_put(struct perf_event
*event
)
4508 if (event
->cpu
!= -1) {
4509 swevent_hlist_put_cpu(event
, event
->cpu
);
4513 for_each_possible_cpu(cpu
)
4514 swevent_hlist_put_cpu(event
, cpu
);
4517 static int swevent_hlist_get_cpu(struct perf_event
*event
, int cpu
)
4519 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
4522 mutex_lock(&cpuctx
->hlist_mutex
);
4524 if (!swevent_hlist_deref(cpuctx
) && cpu_online(cpu
)) {
4525 struct swevent_hlist
*hlist
;
4527 hlist
= kzalloc(sizeof(*hlist
), GFP_KERNEL
);
4532 rcu_assign_pointer(cpuctx
->swevent_hlist
, hlist
);
4534 cpuctx
->hlist_refcount
++;
4536 mutex_unlock(&cpuctx
->hlist_mutex
);
4541 static int swevent_hlist_get(struct perf_event
*event
)
4544 int cpu
, failed_cpu
;
4546 if (event
->cpu
!= -1)
4547 return swevent_hlist_get_cpu(event
, event
->cpu
);
4550 for_each_possible_cpu(cpu
) {
4551 err
= swevent_hlist_get_cpu(event
, cpu
);
4561 for_each_possible_cpu(cpu
) {
4562 if (cpu
== failed_cpu
)
4564 swevent_hlist_put_cpu(event
, cpu
);
4571 atomic_t perf_swevent_enabled
[PERF_COUNT_SW_MAX
];
4573 static void sw_perf_event_destroy(struct perf_event
*event
)
4575 u64 event_id
= event
->attr
.config
;
4577 WARN_ON(event
->parent
);
4579 atomic_dec(&perf_swevent_enabled
[event_id
]);
4580 swevent_hlist_put(event
);
4583 static int perf_swevent_init(struct perf_event
*event
)
4585 int event_id
= event
->attr
.config
;
4587 if (event
->attr
.type
!= PERF_TYPE_SOFTWARE
)
4591 case PERF_COUNT_SW_CPU_CLOCK
:
4592 case PERF_COUNT_SW_TASK_CLOCK
:
4599 if (event_id
> PERF_COUNT_SW_MAX
)
4602 if (!event
->parent
) {
4605 err
= swevent_hlist_get(event
);
4609 atomic_inc(&perf_swevent_enabled
[event_id
]);
4610 event
->destroy
= sw_perf_event_destroy
;
4616 static struct pmu perf_swevent
= {
4617 .event_init
= perf_swevent_init
,
4618 .enable
= perf_swevent_enable
,
4619 .disable
= perf_swevent_disable
,
4620 .start
= perf_swevent_int
,
4621 .stop
= perf_swevent_void
,
4622 .read
= perf_swevent_read
,
4623 .unthrottle
= perf_swevent_void
, /* hwc->interrupts already reset */
4626 #ifdef CONFIG_EVENT_TRACING
4628 static int perf_tp_filter_match(struct perf_event
*event
,
4629 struct perf_sample_data
*data
)
4631 void *record
= data
->raw
->data
;
4633 if (likely(!event
->filter
) || filter_match_preds(event
->filter
, record
))
4638 static int perf_tp_event_match(struct perf_event
*event
,
4639 struct perf_sample_data
*data
,
4640 struct pt_regs
*regs
)
4643 * All tracepoints are from kernel-space.
4645 if (event
->attr
.exclude_kernel
)
4648 if (!perf_tp_filter_match(event
, data
))
4654 void perf_tp_event(u64 addr
, u64 count
, void *record
, int entry_size
,
4655 struct pt_regs
*regs
, struct hlist_head
*head
, int rctx
)
4657 struct perf_sample_data data
;
4658 struct perf_event
*event
;
4659 struct hlist_node
*node
;
4661 struct perf_raw_record raw
= {
4666 perf_sample_data_init(&data
, addr
);
4669 hlist_for_each_entry_rcu(event
, node
, head
, hlist_entry
) {
4670 if (perf_tp_event_match(event
, &data
, regs
))
4671 perf_swevent_add(event
, count
, 1, &data
, regs
);
4674 perf_swevent_put_recursion_context(rctx
);
4676 EXPORT_SYMBOL_GPL(perf_tp_event
);
4678 static void tp_perf_event_destroy(struct perf_event
*event
)
4680 perf_trace_destroy(event
);
4683 static int perf_tp_event_init(struct perf_event
*event
)
4687 if (event
->attr
.type
!= PERF_TYPE_TRACEPOINT
)
4691 * Raw tracepoint data is a severe data leak, only allow root to
4694 if ((event
->attr
.sample_type
& PERF_SAMPLE_RAW
) &&
4695 perf_paranoid_tracepoint_raw() &&
4696 !capable(CAP_SYS_ADMIN
))
4699 err
= perf_trace_init(event
);
4703 event
->destroy
= tp_perf_event_destroy
;
4708 static struct pmu perf_tracepoint
= {
4709 .event_init
= perf_tp_event_init
,
4710 .enable
= perf_trace_enable
,
4711 .disable
= perf_trace_disable
,
4712 .start
= perf_swevent_int
,
4713 .stop
= perf_swevent_void
,
4714 .read
= perf_swevent_read
,
4715 .unthrottle
= perf_swevent_void
,
4718 static inline void perf_tp_register(void)
4720 perf_pmu_register(&perf_tracepoint
);
4723 static int perf_event_set_filter(struct perf_event
*event
, void __user
*arg
)
4728 if (event
->attr
.type
!= PERF_TYPE_TRACEPOINT
)
4731 filter_str
= strndup_user(arg
, PAGE_SIZE
);
4732 if (IS_ERR(filter_str
))
4733 return PTR_ERR(filter_str
);
4735 ret
= ftrace_profile_set_filter(event
, event
->attr
.config
, filter_str
);
4741 static void perf_event_free_filter(struct perf_event
*event
)
4743 ftrace_profile_free_filter(event
);
4748 static inline void perf_tp_register(void)
4752 static int perf_event_set_filter(struct perf_event
*event
, void __user
*arg
)
4757 static void perf_event_free_filter(struct perf_event
*event
)
4761 #endif /* CONFIG_EVENT_TRACING */
4763 #ifdef CONFIG_HAVE_HW_BREAKPOINT
4764 void perf_bp_event(struct perf_event
*bp
, void *data
)
4766 struct perf_sample_data sample
;
4767 struct pt_regs
*regs
= data
;
4769 perf_sample_data_init(&sample
, bp
->attr
.bp_addr
);
4771 if (!perf_exclude_event(bp
, regs
))
4772 perf_swevent_add(bp
, 1, 1, &sample
, regs
);
4777 * hrtimer based swevent callback
4780 static enum hrtimer_restart
perf_swevent_hrtimer(struct hrtimer
*hrtimer
)
4782 enum hrtimer_restart ret
= HRTIMER_RESTART
;
4783 struct perf_sample_data data
;
4784 struct pt_regs
*regs
;
4785 struct perf_event
*event
;
4788 event
= container_of(hrtimer
, struct perf_event
, hw
.hrtimer
);
4789 event
->pmu
->read(event
);
4791 perf_sample_data_init(&data
, 0);
4792 data
.period
= event
->hw
.last_period
;
4793 regs
= get_irq_regs();
4795 if (regs
&& !perf_exclude_event(event
, regs
)) {
4796 if (!(event
->attr
.exclude_idle
&& current
->pid
== 0))
4797 if (perf_event_overflow(event
, 0, &data
, regs
))
4798 ret
= HRTIMER_NORESTART
;
4801 period
= max_t(u64
, 10000, event
->hw
.sample_period
);
4802 hrtimer_forward_now(hrtimer
, ns_to_ktime(period
));
4807 static void perf_swevent_start_hrtimer(struct perf_event
*event
)
4809 struct hw_perf_event
*hwc
= &event
->hw
;
4811 hrtimer_init(&hwc
->hrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
4812 hwc
->hrtimer
.function
= perf_swevent_hrtimer
;
4813 if (hwc
->sample_period
) {
4816 if (hwc
->remaining
) {
4817 if (hwc
->remaining
< 0)
4820 period
= hwc
->remaining
;
4823 period
= max_t(u64
, 10000, hwc
->sample_period
);
4825 __hrtimer_start_range_ns(&hwc
->hrtimer
,
4826 ns_to_ktime(period
), 0,
4827 HRTIMER_MODE_REL
, 0);
4831 static void perf_swevent_cancel_hrtimer(struct perf_event
*event
)
4833 struct hw_perf_event
*hwc
= &event
->hw
;
4835 if (hwc
->sample_period
) {
4836 ktime_t remaining
= hrtimer_get_remaining(&hwc
->hrtimer
);
4837 hwc
->remaining
= ktime_to_ns(remaining
);
4839 hrtimer_cancel(&hwc
->hrtimer
);
4844 * Software event: cpu wall time clock
4847 static void cpu_clock_event_update(struct perf_event
*event
)
4849 int cpu
= raw_smp_processor_id();
4853 now
= cpu_clock(cpu
);
4854 prev
= local64_xchg(&event
->hw
.prev_count
, now
);
4855 local64_add(now
- prev
, &event
->count
);
4858 static int cpu_clock_event_enable(struct perf_event
*event
)
4860 struct hw_perf_event
*hwc
= &event
->hw
;
4861 int cpu
= raw_smp_processor_id();
4863 local64_set(&hwc
->prev_count
, cpu_clock(cpu
));
4864 perf_swevent_start_hrtimer(event
);
4869 static void cpu_clock_event_disable(struct perf_event
*event
)
4871 perf_swevent_cancel_hrtimer(event
);
4872 cpu_clock_event_update(event
);
4875 static void cpu_clock_event_read(struct perf_event
*event
)
4877 cpu_clock_event_update(event
);
4880 static int cpu_clock_event_init(struct perf_event
*event
)
4882 if (event
->attr
.type
!= PERF_TYPE_SOFTWARE
)
4885 if (event
->attr
.config
!= PERF_COUNT_SW_CPU_CLOCK
)
4891 static struct pmu perf_cpu_clock
= {
4892 .event_init
= cpu_clock_event_init
,
4893 .enable
= cpu_clock_event_enable
,
4894 .disable
= cpu_clock_event_disable
,
4895 .read
= cpu_clock_event_read
,
4899 * Software event: task time clock
4902 static void task_clock_event_update(struct perf_event
*event
, u64 now
)
4907 prev
= local64_xchg(&event
->hw
.prev_count
, now
);
4909 local64_add(delta
, &event
->count
);
4912 static int task_clock_event_enable(struct perf_event
*event
)
4914 struct hw_perf_event
*hwc
= &event
->hw
;
4917 now
= event
->ctx
->time
;
4919 local64_set(&hwc
->prev_count
, now
);
4921 perf_swevent_start_hrtimer(event
);
4926 static void task_clock_event_disable(struct perf_event
*event
)
4928 perf_swevent_cancel_hrtimer(event
);
4929 task_clock_event_update(event
, event
->ctx
->time
);
4933 static void task_clock_event_read(struct perf_event
*event
)
4938 update_context_time(event
->ctx
);
4939 time
= event
->ctx
->time
;
4941 u64 now
= perf_clock();
4942 u64 delta
= now
- event
->ctx
->timestamp
;
4943 time
= event
->ctx
->time
+ delta
;
4946 task_clock_event_update(event
, time
);
4949 static int task_clock_event_init(struct perf_event
*event
)
4951 if (event
->attr
.type
!= PERF_TYPE_SOFTWARE
)
4954 if (event
->attr
.config
!= PERF_COUNT_SW_TASK_CLOCK
)
4960 static struct pmu perf_task_clock
= {
4961 .event_init
= task_clock_event_init
,
4962 .enable
= task_clock_event_enable
,
4963 .disable
= task_clock_event_disable
,
4964 .read
= task_clock_event_read
,
4967 static LIST_HEAD(pmus
);
4968 static DEFINE_MUTEX(pmus_lock
);
4969 static struct srcu_struct pmus_srcu
;
4971 int perf_pmu_register(struct pmu
*pmu
)
4973 mutex_lock(&pmus_lock
);
4974 list_add_rcu(&pmu
->entry
, &pmus
);
4975 mutex_unlock(&pmus_lock
);
4980 void perf_pmu_unregister(struct pmu
*pmu
)
4982 mutex_lock(&pmus_lock
);
4983 list_del_rcu(&pmu
->entry
);
4984 mutex_unlock(&pmus_lock
);
4986 synchronize_srcu(&pmus_srcu
);
4989 struct pmu
*perf_init_event(struct perf_event
*event
)
4991 struct pmu
*pmu
= NULL
;
4994 idx
= srcu_read_lock(&pmus_srcu
);
4995 list_for_each_entry_rcu(pmu
, &pmus
, entry
) {
4996 int ret
= pmu
->event_init(event
);
4999 if (ret
!= -ENOENT
) {
5004 srcu_read_unlock(&pmus_srcu
, idx
);
5010 * Allocate and initialize a event structure
5012 static struct perf_event
*
5013 perf_event_alloc(struct perf_event_attr
*attr
,
5015 struct perf_event_context
*ctx
,
5016 struct perf_event
*group_leader
,
5017 struct perf_event
*parent_event
,
5018 perf_overflow_handler_t overflow_handler
,
5022 struct perf_event
*event
;
5023 struct hw_perf_event
*hwc
;
5026 event
= kzalloc(sizeof(*event
), gfpflags
);
5028 return ERR_PTR(-ENOMEM
);
5031 * Single events are their own group leaders, with an
5032 * empty sibling list:
5035 group_leader
= event
;
5037 mutex_init(&event
->child_mutex
);
5038 INIT_LIST_HEAD(&event
->child_list
);
5040 INIT_LIST_HEAD(&event
->group_entry
);
5041 INIT_LIST_HEAD(&event
->event_entry
);
5042 INIT_LIST_HEAD(&event
->sibling_list
);
5043 init_waitqueue_head(&event
->waitq
);
5045 mutex_init(&event
->mmap_mutex
);
5048 event
->attr
= *attr
;
5049 event
->group_leader
= group_leader
;
5054 event
->parent
= parent_event
;
5056 event
->ns
= get_pid_ns(current
->nsproxy
->pid_ns
);
5057 event
->id
= atomic64_inc_return(&perf_event_id
);
5059 event
->state
= PERF_EVENT_STATE_INACTIVE
;
5061 if (!overflow_handler
&& parent_event
)
5062 overflow_handler
= parent_event
->overflow_handler
;
5064 event
->overflow_handler
= overflow_handler
;
5067 event
->state
= PERF_EVENT_STATE_OFF
;
5072 hwc
->sample_period
= attr
->sample_period
;
5073 if (attr
->freq
&& attr
->sample_freq
)
5074 hwc
->sample_period
= 1;
5075 hwc
->last_period
= hwc
->sample_period
;
5077 local64_set(&hwc
->period_left
, hwc
->sample_period
);
5080 * we currently do not support PERF_FORMAT_GROUP on inherited events
5082 if (attr
->inherit
&& (attr
->read_format
& PERF_FORMAT_GROUP
))
5085 pmu
= perf_init_event(event
);
5091 else if (IS_ERR(pmu
))
5096 put_pid_ns(event
->ns
);
5098 return ERR_PTR(err
);
5103 if (!event
->parent
) {
5104 atomic_inc(&nr_events
);
5105 if (event
->attr
.mmap
|| event
->attr
.mmap_data
)
5106 atomic_inc(&nr_mmap_events
);
5107 if (event
->attr
.comm
)
5108 atomic_inc(&nr_comm_events
);
5109 if (event
->attr
.task
)
5110 atomic_inc(&nr_task_events
);
5111 if (event
->attr
.sample_type
& PERF_SAMPLE_CALLCHAIN
) {
5112 err
= get_callchain_buffers();
5115 return ERR_PTR(err
);
5123 static int perf_copy_attr(struct perf_event_attr __user
*uattr
,
5124 struct perf_event_attr
*attr
)
5129 if (!access_ok(VERIFY_WRITE
, uattr
, PERF_ATTR_SIZE_VER0
))
5133 * zero the full structure, so that a short copy will be nice.
5135 memset(attr
, 0, sizeof(*attr
));
5137 ret
= get_user(size
, &uattr
->size
);
5141 if (size
> PAGE_SIZE
) /* silly large */
5144 if (!size
) /* abi compat */
5145 size
= PERF_ATTR_SIZE_VER0
;
5147 if (size
< PERF_ATTR_SIZE_VER0
)
5151 * If we're handed a bigger struct than we know of,
5152 * ensure all the unknown bits are 0 - i.e. new
5153 * user-space does not rely on any kernel feature
5154 * extensions we dont know about yet.
5156 if (size
> sizeof(*attr
)) {
5157 unsigned char __user
*addr
;
5158 unsigned char __user
*end
;
5161 addr
= (void __user
*)uattr
+ sizeof(*attr
);
5162 end
= (void __user
*)uattr
+ size
;
5164 for (; addr
< end
; addr
++) {
5165 ret
= get_user(val
, addr
);
5171 size
= sizeof(*attr
);
5174 ret
= copy_from_user(attr
, uattr
, size
);
5179 * If the type exists, the corresponding creation will verify
5182 if (attr
->type
>= PERF_TYPE_MAX
)
5185 if (attr
->__reserved_1
)
5188 if (attr
->sample_type
& ~(PERF_SAMPLE_MAX
-1))
5191 if (attr
->read_format
& ~(PERF_FORMAT_MAX
-1))
5198 put_user(sizeof(*attr
), &uattr
->size
);
5204 perf_event_set_output(struct perf_event
*event
, struct perf_event
*output_event
)
5206 struct perf_buffer
*buffer
= NULL
, *old_buffer
= NULL
;
5212 /* don't allow circular references */
5213 if (event
== output_event
)
5217 * Don't allow cross-cpu buffers
5219 if (output_event
->cpu
!= event
->cpu
)
5223 * If its not a per-cpu buffer, it must be the same task.
5225 if (output_event
->cpu
== -1 && output_event
->ctx
!= event
->ctx
)
5229 mutex_lock(&event
->mmap_mutex
);
5230 /* Can't redirect output if we've got an active mmap() */
5231 if (atomic_read(&event
->mmap_count
))
5235 /* get the buffer we want to redirect to */
5236 buffer
= perf_buffer_get(output_event
);
5241 old_buffer
= event
->buffer
;
5242 rcu_assign_pointer(event
->buffer
, buffer
);
5245 mutex_unlock(&event
->mmap_mutex
);
5248 perf_buffer_put(old_buffer
);
5254 * sys_perf_event_open - open a performance event, associate it to a task/cpu
5256 * @attr_uptr: event_id type attributes for monitoring/sampling
5259 * @group_fd: group leader event fd
5261 SYSCALL_DEFINE5(perf_event_open
,
5262 struct perf_event_attr __user
*, attr_uptr
,
5263 pid_t
, pid
, int, cpu
, int, group_fd
, unsigned long, flags
)
5265 struct perf_event
*event
, *group_leader
= NULL
, *output_event
= NULL
;
5266 struct perf_event_attr attr
;
5267 struct perf_event_context
*ctx
;
5268 struct file
*event_file
= NULL
;
5269 struct file
*group_file
= NULL
;
5271 int fput_needed
= 0;
5274 /* for future expandability... */
5275 if (flags
& ~(PERF_FLAG_FD_NO_GROUP
| PERF_FLAG_FD_OUTPUT
))
5278 err
= perf_copy_attr(attr_uptr
, &attr
);
5282 if (!attr
.exclude_kernel
) {
5283 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN
))
5288 if (attr
.sample_freq
> sysctl_perf_event_sample_rate
)
5292 event_fd
= get_unused_fd_flags(O_RDWR
);
5297 * Get the target context (task or percpu):
5299 ctx
= find_get_context(pid
, cpu
);
5305 if (group_fd
!= -1) {
5306 group_leader
= perf_fget_light(group_fd
, &fput_needed
);
5307 if (IS_ERR(group_leader
)) {
5308 err
= PTR_ERR(group_leader
);
5309 goto err_put_context
;
5311 group_file
= group_leader
->filp
;
5312 if (flags
& PERF_FLAG_FD_OUTPUT
)
5313 output_event
= group_leader
;
5314 if (flags
& PERF_FLAG_FD_NO_GROUP
)
5315 group_leader
= NULL
;
5319 * Look up the group leader (we will attach this event to it):
5325 * Do not allow a recursive hierarchy (this new sibling
5326 * becoming part of another group-sibling):
5328 if (group_leader
->group_leader
!= group_leader
)
5329 goto err_put_context
;
5331 * Do not allow to attach to a group in a different
5332 * task or CPU context:
5334 if (group_leader
->ctx
!= ctx
)
5335 goto err_put_context
;
5337 * Only a group leader can be exclusive or pinned
5339 if (attr
.exclusive
|| attr
.pinned
)
5340 goto err_put_context
;
5343 event
= perf_event_alloc(&attr
, cpu
, ctx
, group_leader
,
5344 NULL
, NULL
, GFP_KERNEL
);
5345 if (IS_ERR(event
)) {
5346 err
= PTR_ERR(event
);
5347 goto err_put_context
;
5351 err
= perf_event_set_output(event
, output_event
);
5353 goto err_free_put_context
;
5356 event_file
= anon_inode_getfile("[perf_event]", &perf_fops
, event
, O_RDWR
);
5357 if (IS_ERR(event_file
)) {
5358 err
= PTR_ERR(event_file
);
5359 goto err_free_put_context
;
5362 event
->filp
= event_file
;
5363 WARN_ON_ONCE(ctx
->parent_ctx
);
5364 mutex_lock(&ctx
->mutex
);
5365 perf_install_in_context(ctx
, event
, cpu
);
5367 mutex_unlock(&ctx
->mutex
);
5369 event
->owner
= current
;
5370 get_task_struct(current
);
5371 mutex_lock(¤t
->perf_event_mutex
);
5372 list_add_tail(&event
->owner_entry
, ¤t
->perf_event_list
);
5373 mutex_unlock(¤t
->perf_event_mutex
);
5376 * Drop the reference on the group_event after placing the
5377 * new event on the sibling_list. This ensures destruction
5378 * of the group leader will find the pointer to itself in
5379 * perf_group_detach().
5381 fput_light(group_file
, fput_needed
);
5382 fd_install(event_fd
, event_file
);
5385 err_free_put_context
:
5388 fput_light(group_file
, fput_needed
);
5391 put_unused_fd(event_fd
);
5396 * perf_event_create_kernel_counter
5398 * @attr: attributes of the counter to create
5399 * @cpu: cpu in which the counter is bound
5400 * @pid: task to profile
5403 perf_event_create_kernel_counter(struct perf_event_attr
*attr
, int cpu
,
5405 perf_overflow_handler_t overflow_handler
)
5407 struct perf_event
*event
;
5408 struct perf_event_context
*ctx
;
5412 * Get the target context (task or percpu):
5415 ctx
= find_get_context(pid
, cpu
);
5421 event
= perf_event_alloc(attr
, cpu
, ctx
, NULL
,
5422 NULL
, overflow_handler
, GFP_KERNEL
);
5423 if (IS_ERR(event
)) {
5424 err
= PTR_ERR(event
);
5425 goto err_put_context
;
5429 WARN_ON_ONCE(ctx
->parent_ctx
);
5430 mutex_lock(&ctx
->mutex
);
5431 perf_install_in_context(ctx
, event
, cpu
);
5433 mutex_unlock(&ctx
->mutex
);
5435 event
->owner
= current
;
5436 get_task_struct(current
);
5437 mutex_lock(¤t
->perf_event_mutex
);
5438 list_add_tail(&event
->owner_entry
, ¤t
->perf_event_list
);
5439 mutex_unlock(¤t
->perf_event_mutex
);
5446 return ERR_PTR(err
);
5448 EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter
);
5451 * inherit a event from parent task to child task:
5453 static struct perf_event
*
5454 inherit_event(struct perf_event
*parent_event
,
5455 struct task_struct
*parent
,
5456 struct perf_event_context
*parent_ctx
,
5457 struct task_struct
*child
,
5458 struct perf_event
*group_leader
,
5459 struct perf_event_context
*child_ctx
)
5461 struct perf_event
*child_event
;
5464 * Instead of creating recursive hierarchies of events,
5465 * we link inherited events back to the original parent,
5466 * which has a filp for sure, which we use as the reference
5469 if (parent_event
->parent
)
5470 parent_event
= parent_event
->parent
;
5472 child_event
= perf_event_alloc(&parent_event
->attr
,
5473 parent_event
->cpu
, child_ctx
,
5474 group_leader
, parent_event
,
5476 if (IS_ERR(child_event
))
5481 * Make the child state follow the state of the parent event,
5482 * not its attr.disabled bit. We hold the parent's mutex,
5483 * so we won't race with perf_event_{en, dis}able_family.
5485 if (parent_event
->state
>= PERF_EVENT_STATE_INACTIVE
)
5486 child_event
->state
= PERF_EVENT_STATE_INACTIVE
;
5488 child_event
->state
= PERF_EVENT_STATE_OFF
;
5490 if (parent_event
->attr
.freq
) {
5491 u64 sample_period
= parent_event
->hw
.sample_period
;
5492 struct hw_perf_event
*hwc
= &child_event
->hw
;
5494 hwc
->sample_period
= sample_period
;
5495 hwc
->last_period
= sample_period
;
5497 local64_set(&hwc
->period_left
, sample_period
);
5500 child_event
->overflow_handler
= parent_event
->overflow_handler
;
5503 * Link it up in the child's context:
5505 add_event_to_ctx(child_event
, child_ctx
);
5508 * Get a reference to the parent filp - we will fput it
5509 * when the child event exits. This is safe to do because
5510 * we are in the parent and we know that the filp still
5511 * exists and has a nonzero count:
5513 atomic_long_inc(&parent_event
->filp
->f_count
);
5516 * Link this into the parent event's child list
5518 WARN_ON_ONCE(parent_event
->ctx
->parent_ctx
);
5519 mutex_lock(&parent_event
->child_mutex
);
5520 list_add_tail(&child_event
->child_list
, &parent_event
->child_list
);
5521 mutex_unlock(&parent_event
->child_mutex
);
5526 static int inherit_group(struct perf_event
*parent_event
,
5527 struct task_struct
*parent
,
5528 struct perf_event_context
*parent_ctx
,
5529 struct task_struct
*child
,
5530 struct perf_event_context
*child_ctx
)
5532 struct perf_event
*leader
;
5533 struct perf_event
*sub
;
5534 struct perf_event
*child_ctr
;
5536 leader
= inherit_event(parent_event
, parent
, parent_ctx
,
5537 child
, NULL
, child_ctx
);
5539 return PTR_ERR(leader
);
5540 list_for_each_entry(sub
, &parent_event
->sibling_list
, group_entry
) {
5541 child_ctr
= inherit_event(sub
, parent
, parent_ctx
,
5542 child
, leader
, child_ctx
);
5543 if (IS_ERR(child_ctr
))
5544 return PTR_ERR(child_ctr
);
5549 static void sync_child_event(struct perf_event
*child_event
,
5550 struct task_struct
*child
)
5552 struct perf_event
*parent_event
= child_event
->parent
;
5555 if (child_event
->attr
.inherit_stat
)
5556 perf_event_read_event(child_event
, child
);
5558 child_val
= perf_event_count(child_event
);
5561 * Add back the child's count to the parent's count:
5563 atomic64_add(child_val
, &parent_event
->child_count
);
5564 atomic64_add(child_event
->total_time_enabled
,
5565 &parent_event
->child_total_time_enabled
);
5566 atomic64_add(child_event
->total_time_running
,
5567 &parent_event
->child_total_time_running
);
5570 * Remove this event from the parent's list
5572 WARN_ON_ONCE(parent_event
->ctx
->parent_ctx
);
5573 mutex_lock(&parent_event
->child_mutex
);
5574 list_del_init(&child_event
->child_list
);
5575 mutex_unlock(&parent_event
->child_mutex
);
5578 * Release the parent event, if this was the last
5581 fput(parent_event
->filp
);
5585 __perf_event_exit_task(struct perf_event
*child_event
,
5586 struct perf_event_context
*child_ctx
,
5587 struct task_struct
*child
)
5589 struct perf_event
*parent_event
;
5591 perf_event_remove_from_context(child_event
);
5593 parent_event
= child_event
->parent
;
5595 * It can happen that parent exits first, and has events
5596 * that are still around due to the child reference. These
5597 * events need to be zapped - but otherwise linger.
5600 sync_child_event(child_event
, child
);
5601 free_event(child_event
);
5606 * When a child task exits, feed back event values to parent events.
5608 void perf_event_exit_task(struct task_struct
*child
)
5610 struct perf_event
*child_event
, *tmp
;
5611 struct perf_event_context
*child_ctx
;
5612 unsigned long flags
;
5614 if (likely(!child
->perf_event_ctxp
)) {
5615 perf_event_task(child
, NULL
, 0);
5619 local_irq_save(flags
);
5621 * We can't reschedule here because interrupts are disabled,
5622 * and either child is current or it is a task that can't be
5623 * scheduled, so we are now safe from rescheduling changing
5626 child_ctx
= child
->perf_event_ctxp
;
5627 __perf_event_task_sched_out(child_ctx
);
5630 * Take the context lock here so that if find_get_context is
5631 * reading child->perf_event_ctxp, we wait until it has
5632 * incremented the context's refcount before we do put_ctx below.
5634 raw_spin_lock(&child_ctx
->lock
);
5635 child
->perf_event_ctxp
= NULL
;
5637 * If this context is a clone; unclone it so it can't get
5638 * swapped to another process while we're removing all
5639 * the events from it.
5641 unclone_ctx(child_ctx
);
5642 update_context_time(child_ctx
);
5643 raw_spin_unlock_irqrestore(&child_ctx
->lock
, flags
);
5646 * Report the task dead after unscheduling the events so that we
5647 * won't get any samples after PERF_RECORD_EXIT. We can however still
5648 * get a few PERF_RECORD_READ events.
5650 perf_event_task(child
, child_ctx
, 0);
5653 * We can recurse on the same lock type through:
5655 * __perf_event_exit_task()
5656 * sync_child_event()
5657 * fput(parent_event->filp)
5659 * mutex_lock(&ctx->mutex)
5661 * But since its the parent context it won't be the same instance.
5663 mutex_lock(&child_ctx
->mutex
);
5666 list_for_each_entry_safe(child_event
, tmp
, &child_ctx
->pinned_groups
,
5668 __perf_event_exit_task(child_event
, child_ctx
, child
);
5670 list_for_each_entry_safe(child_event
, tmp
, &child_ctx
->flexible_groups
,
5672 __perf_event_exit_task(child_event
, child_ctx
, child
);
5675 * If the last event was a group event, it will have appended all
5676 * its siblings to the list, but we obtained 'tmp' before that which
5677 * will still point to the list head terminating the iteration.
5679 if (!list_empty(&child_ctx
->pinned_groups
) ||
5680 !list_empty(&child_ctx
->flexible_groups
))
5683 mutex_unlock(&child_ctx
->mutex
);
5688 static void perf_free_event(struct perf_event
*event
,
5689 struct perf_event_context
*ctx
)
5691 struct perf_event
*parent
= event
->parent
;
5693 if (WARN_ON_ONCE(!parent
))
5696 mutex_lock(&parent
->child_mutex
);
5697 list_del_init(&event
->child_list
);
5698 mutex_unlock(&parent
->child_mutex
);
5702 perf_group_detach(event
);
5703 list_del_event(event
, ctx
);
5708 * free an unexposed, unused context as created by inheritance by
5709 * init_task below, used by fork() in case of fail.
5711 void perf_event_free_task(struct task_struct
*task
)
5713 struct perf_event_context
*ctx
= task
->perf_event_ctxp
;
5714 struct perf_event
*event
, *tmp
;
5719 mutex_lock(&ctx
->mutex
);
5721 list_for_each_entry_safe(event
, tmp
, &ctx
->pinned_groups
, group_entry
)
5722 perf_free_event(event
, ctx
);
5724 list_for_each_entry_safe(event
, tmp
, &ctx
->flexible_groups
,
5726 perf_free_event(event
, ctx
);
5728 if (!list_empty(&ctx
->pinned_groups
) ||
5729 !list_empty(&ctx
->flexible_groups
))
5732 mutex_unlock(&ctx
->mutex
);
5738 inherit_task_group(struct perf_event
*event
, struct task_struct
*parent
,
5739 struct perf_event_context
*parent_ctx
,
5740 struct task_struct
*child
,
5744 struct perf_event_context
*child_ctx
= child
->perf_event_ctxp
;
5746 if (!event
->attr
.inherit
) {
5753 * This is executed from the parent task context, so
5754 * inherit events that have been marked for cloning.
5755 * First allocate and initialize a context for the
5759 child_ctx
= kzalloc(sizeof(struct perf_event_context
),
5764 __perf_event_init_context(child_ctx
, child
);
5765 child
->perf_event_ctxp
= child_ctx
;
5766 get_task_struct(child
);
5769 ret
= inherit_group(event
, parent
, parent_ctx
,
5780 * Initialize the perf_event context in task_struct
5782 int perf_event_init_task(struct task_struct
*child
)
5784 struct perf_event_context
*child_ctx
, *parent_ctx
;
5785 struct perf_event_context
*cloned_ctx
;
5786 struct perf_event
*event
;
5787 struct task_struct
*parent
= current
;
5788 int inherited_all
= 1;
5791 child
->perf_event_ctxp
= NULL
;
5793 mutex_init(&child
->perf_event_mutex
);
5794 INIT_LIST_HEAD(&child
->perf_event_list
);
5796 if (likely(!parent
->perf_event_ctxp
))
5800 * If the parent's context is a clone, pin it so it won't get
5803 parent_ctx
= perf_pin_task_context(parent
);
5806 * No need to check if parent_ctx != NULL here; since we saw
5807 * it non-NULL earlier, the only reason for it to become NULL
5808 * is if we exit, and since we're currently in the middle of
5809 * a fork we can't be exiting at the same time.
5813 * Lock the parent list. No need to lock the child - not PID
5814 * hashed yet and not running, so nobody can access it.
5816 mutex_lock(&parent_ctx
->mutex
);
5819 * We dont have to disable NMIs - we are only looking at
5820 * the list, not manipulating it:
5822 list_for_each_entry(event
, &parent_ctx
->pinned_groups
, group_entry
) {
5823 ret
= inherit_task_group(event
, parent
, parent_ctx
, child
,
5829 list_for_each_entry(event
, &parent_ctx
->flexible_groups
, group_entry
) {
5830 ret
= inherit_task_group(event
, parent
, parent_ctx
, child
,
5836 child_ctx
= child
->perf_event_ctxp
;
5838 if (child_ctx
&& inherited_all
) {
5840 * Mark the child context as a clone of the parent
5841 * context, or of whatever the parent is a clone of.
5842 * Note that if the parent is a clone, it could get
5843 * uncloned at any point, but that doesn't matter
5844 * because the list of events and the generation
5845 * count can't have changed since we took the mutex.
5847 cloned_ctx
= rcu_dereference(parent_ctx
->parent_ctx
);
5849 child_ctx
->parent_ctx
= cloned_ctx
;
5850 child_ctx
->parent_gen
= parent_ctx
->parent_gen
;
5852 child_ctx
->parent_ctx
= parent_ctx
;
5853 child_ctx
->parent_gen
= parent_ctx
->generation
;
5855 get_ctx(child_ctx
->parent_ctx
);
5858 mutex_unlock(&parent_ctx
->mutex
);
5860 perf_unpin_context(parent_ctx
);
5865 static void __init
perf_event_init_all_cpus(void)
5868 struct perf_cpu_context
*cpuctx
;
5870 for_each_possible_cpu(cpu
) {
5871 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
5872 mutex_init(&cpuctx
->hlist_mutex
);
5873 __perf_event_init_context(&cpuctx
->ctx
, NULL
);
5877 static void __cpuinit
perf_event_init_cpu(int cpu
)
5879 struct perf_cpu_context
*cpuctx
;
5881 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
5883 spin_lock(&perf_resource_lock
);
5884 cpuctx
->max_pertask
= perf_max_events
- perf_reserved_percpu
;
5885 spin_unlock(&perf_resource_lock
);
5887 mutex_lock(&cpuctx
->hlist_mutex
);
5888 if (cpuctx
->hlist_refcount
> 0) {
5889 struct swevent_hlist
*hlist
;
5891 hlist
= kzalloc(sizeof(*hlist
), GFP_KERNEL
);
5892 WARN_ON_ONCE(!hlist
);
5893 rcu_assign_pointer(cpuctx
->swevent_hlist
, hlist
);
5895 mutex_unlock(&cpuctx
->hlist_mutex
);
5898 #ifdef CONFIG_HOTPLUG_CPU
5899 static void __perf_event_exit_cpu(void *info
)
5901 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
5902 struct perf_event_context
*ctx
= &cpuctx
->ctx
;
5903 struct perf_event
*event
, *tmp
;
5905 list_for_each_entry_safe(event
, tmp
, &ctx
->pinned_groups
, group_entry
)
5906 __perf_event_remove_from_context(event
);
5907 list_for_each_entry_safe(event
, tmp
, &ctx
->flexible_groups
, group_entry
)
5908 __perf_event_remove_from_context(event
);
5910 static void perf_event_exit_cpu(int cpu
)
5912 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
5913 struct perf_event_context
*ctx
= &cpuctx
->ctx
;
5915 mutex_lock(&cpuctx
->hlist_mutex
);
5916 swevent_hlist_release(cpuctx
);
5917 mutex_unlock(&cpuctx
->hlist_mutex
);
5919 mutex_lock(&ctx
->mutex
);
5920 smp_call_function_single(cpu
, __perf_event_exit_cpu
, NULL
, 1);
5921 mutex_unlock(&ctx
->mutex
);
5924 static inline void perf_event_exit_cpu(int cpu
) { }
5927 static int __cpuinit
5928 perf_cpu_notify(struct notifier_block
*self
, unsigned long action
, void *hcpu
)
5930 unsigned int cpu
= (long)hcpu
;
5932 switch (action
& ~CPU_TASKS_FROZEN
) {
5934 case CPU_UP_PREPARE
:
5935 case CPU_DOWN_FAILED
:
5936 perf_event_init_cpu(cpu
);
5939 case CPU_UP_CANCELED
:
5940 case CPU_DOWN_PREPARE
:
5941 perf_event_exit_cpu(cpu
);
5951 void __init
perf_event_init(void)
5953 perf_event_init_all_cpus();
5954 init_srcu_struct(&pmus_srcu
);
5955 perf_pmu_register(&perf_swevent
);
5956 perf_pmu_register(&perf_cpu_clock
);
5957 perf_pmu_register(&perf_task_clock
);
5959 perf_cpu_notifier(perf_cpu_notify
);
5962 static ssize_t
perf_show_reserve_percpu(struct sysdev_class
*class,
5963 struct sysdev_class_attribute
*attr
,
5966 return sprintf(buf
, "%d\n", perf_reserved_percpu
);
5970 perf_set_reserve_percpu(struct sysdev_class
*class,
5971 struct sysdev_class_attribute
*attr
,
5975 struct perf_cpu_context
*cpuctx
;
5979 err
= strict_strtoul(buf
, 10, &val
);
5982 if (val
> perf_max_events
)
5985 spin_lock(&perf_resource_lock
);
5986 perf_reserved_percpu
= val
;
5987 for_each_online_cpu(cpu
) {
5988 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
5989 raw_spin_lock_irq(&cpuctx
->ctx
.lock
);
5990 mpt
= min(perf_max_events
- cpuctx
->ctx
.nr_events
,
5991 perf_max_events
- perf_reserved_percpu
);
5992 cpuctx
->max_pertask
= mpt
;
5993 raw_spin_unlock_irq(&cpuctx
->ctx
.lock
);
5995 spin_unlock(&perf_resource_lock
);
6000 static ssize_t
perf_show_overcommit(struct sysdev_class
*class,
6001 struct sysdev_class_attribute
*attr
,
6004 return sprintf(buf
, "%d\n", perf_overcommit
);
6008 perf_set_overcommit(struct sysdev_class
*class,
6009 struct sysdev_class_attribute
*attr
,
6010 const char *buf
, size_t count
)
6015 err
= strict_strtoul(buf
, 10, &val
);
6021 spin_lock(&perf_resource_lock
);
6022 perf_overcommit
= val
;
6023 spin_unlock(&perf_resource_lock
);
6028 static SYSDEV_CLASS_ATTR(
6031 perf_show_reserve_percpu
,
6032 perf_set_reserve_percpu
6035 static SYSDEV_CLASS_ATTR(
6038 perf_show_overcommit
,
6042 static struct attribute
*perfclass_attrs
[] = {
6043 &attr_reserve_percpu
.attr
,
6044 &attr_overcommit
.attr
,
6048 static struct attribute_group perfclass_attr_group
= {
6049 .attrs
= perfclass_attrs
,
6050 .name
= "perf_events",
6053 static int __init
perf_event_sysfs_init(void)
6055 return sysfs_create_group(&cpu_sysdev_class
.kset
.kobj
,
6056 &perfclass_attr_group
);
6058 device_initcall(perf_event_sysfs_init
);