2 * Performance events core code:
4 * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
5 * Copyright (C) 2008-2009 Red Hat, Inc., Ingo Molnar
6 * Copyright (C) 2008-2009 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
7 * Copyright © 2009 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
9 * For licensing details see kernel-base/COPYING
14 #include <linux/cpu.h>
15 #include <linux/smp.h>
16 #include <linux/file.h>
17 #include <linux/poll.h>
18 #include <linux/slab.h>
19 #include <linux/hash.h>
20 #include <linux/sysfs.h>
21 #include <linux/dcache.h>
22 #include <linux/percpu.h>
23 #include <linux/ptrace.h>
24 #include <linux/vmstat.h>
25 #include <linux/vmalloc.h>
26 #include <linux/hardirq.h>
27 #include <linux/rculist.h>
28 #include <linux/uaccess.h>
29 #include <linux/syscalls.h>
30 #include <linux/anon_inodes.h>
31 #include <linux/kernel_stat.h>
32 #include <linux/perf_event.h>
33 #include <linux/ftrace_event.h>
35 #include <asm/irq_regs.h>
37 static atomic_t nr_events __read_mostly;
38 static atomic_t nr_mmap_events __read_mostly;
39 static atomic_t nr_comm_events __read_mostly;
40 static atomic_t nr_task_events __read_mostly;
42 static LIST_HEAD(pmus);
43 static DEFINE_MUTEX(pmus_lock);
44 static struct srcu_struct pmus_srcu;
47 * perf event paranoia level:
48 * -1 - not paranoid at all
49 * 0 - disallow raw tracepoint access for unpriv
50 * 1 - disallow cpu events for unpriv
51 * 2 - disallow kernel profiling for unpriv
53 int sysctl_perf_event_paranoid __read_mostly = 1;
55 int sysctl_perf_event_mlock __read_mostly = 512; /* 'free' kb per user */
58 * max perf event sample rate
60 int sysctl_perf_event_sample_rate __read_mostly = 100000;
62 static atomic64_t perf_event_id;
64 void __weak perf_event_print_debug(void) { }
66 void perf_pmu_disable(struct pmu *pmu)
68 int *count = this_cpu_ptr(pmu->pmu_disable_count);
70 pmu->pmu_disable(pmu);
73 void perf_pmu_enable(struct pmu *pmu)
75 int *count = this_cpu_ptr(pmu->pmu_disable_count);
80 static DEFINE_PER_CPU(struct list_head, rotation_list);
83 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
84 * because they're strictly cpu affine and rotate_start is called with IRQs
85 * disabled, while rotate_context is called from IRQ context.
87 static void perf_pmu_rotate_start(struct pmu *pmu)
89 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
90 struct list_head *head = &__get_cpu_var(rotation_list);
92 WARN_ON(!irqs_disabled());
94 if (list_empty(&cpuctx->rotation_list))
95 list_add(&cpuctx->rotation_list, head);
98 static void get_ctx(struct perf_event_context *ctx)
100 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
103 static void free_ctx(struct rcu_head *head)
105 struct perf_event_context *ctx;
107 ctx = container_of(head, struct perf_event_context, rcu_head);
111 static void put_ctx(struct perf_event_context *ctx)
113 if (atomic_dec_and_test(&ctx->refcount)) {
115 put_ctx(ctx->parent_ctx);
117 put_task_struct(ctx->task);
118 call_rcu(&ctx->rcu_head, free_ctx);
122 static void unclone_ctx(struct perf_event_context *ctx)
124 if (ctx->parent_ctx) {
125 put_ctx(ctx->parent_ctx);
126 ctx->parent_ctx = NULL;
131 * If we inherit events we want to return the parent event id
134 static u64 primary_event_id(struct perf_event *event)
139 id = event->parent->id;
145 * Get the perf_event_context for a task and lock it.
146 * This has to cope with with the fact that until it is locked,
147 * the context could get moved to another task.
149 static struct perf_event_context *
150 perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
152 struct perf_event_context *ctx;
156 ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
159 * If this context is a clone of another, it might
160 * get swapped for another underneath us by
161 * perf_event_task_sched_out, though the
162 * rcu_read_lock() protects us from any context
163 * getting freed. Lock the context and check if it
164 * got swapped before we could get the lock, and retry
165 * if so. If we locked the right context, then it
166 * can't get swapped on us any more.
168 raw_spin_lock_irqsave(&ctx->lock, *flags);
169 if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
170 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
174 if (!atomic_inc_not_zero(&ctx->refcount)) {
175 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
184 * Get the context for a task and increment its pin_count so it
185 * can't get swapped to another task. This also increments its
186 * reference count so that the context can't get freed.
188 static struct perf_event_context *
189 perf_pin_task_context(struct task_struct *task, int ctxn)
191 struct perf_event_context *ctx;
194 ctx = perf_lock_task_context(task, ctxn, &flags);
197 raw_spin_unlock_irqrestore(&ctx->lock, flags);
202 static void perf_unpin_context(struct perf_event_context *ctx)
206 raw_spin_lock_irqsave(&ctx->lock, flags);
208 raw_spin_unlock_irqrestore(&ctx->lock, flags);
212 static inline u64 perf_clock(void)
214 return local_clock();
218 * Update the record of the current time in a context.
220 static void update_context_time(struct perf_event_context *ctx)
222 u64 now = perf_clock();
224 ctx->time += now - ctx->timestamp;
225 ctx->timestamp = now;
229 * Update the total_time_enabled and total_time_running fields for a event.
231 static void update_event_times(struct perf_event *event)
233 struct perf_event_context *ctx = event->ctx;
236 if (event->state < PERF_EVENT_STATE_INACTIVE ||
237 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
243 run_end = event->tstamp_stopped;
245 event->total_time_enabled = run_end - event->tstamp_enabled;
247 if (event->state == PERF_EVENT_STATE_INACTIVE)
248 run_end = event->tstamp_stopped;
252 event->total_time_running = run_end - event->tstamp_running;
256 * Update total_time_enabled and total_time_running for all events in a group.
258 static void update_group_times(struct perf_event *leader)
260 struct perf_event *event;
262 update_event_times(leader);
263 list_for_each_entry(event, &leader->sibling_list, group_entry)
264 update_event_times(event);
267 static struct list_head *
268 ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
270 if (event->attr.pinned)
271 return &ctx->pinned_groups;
273 return &ctx->flexible_groups;
277 * Add a event from the lists for its context.
278 * Must be called with ctx->mutex and ctx->lock held.
281 list_add_event(struct perf_event *event, struct perf_event_context *ctx)
283 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
284 event->attach_state |= PERF_ATTACH_CONTEXT;
287 * If we're a stand alone event or group leader, we go to the context
288 * list, group events are kept attached to the group so that
289 * perf_group_detach can, at all times, locate all siblings.
291 if (event->group_leader == event) {
292 struct list_head *list;
294 if (is_software_event(event))
295 event->group_flags |= PERF_GROUP_SOFTWARE;
297 list = ctx_group_list(event, ctx);
298 list_add_tail(&event->group_entry, list);
301 list_add_rcu(&event->event_entry, &ctx->event_list);
303 perf_pmu_rotate_start(ctx->pmu);
305 if (event->attr.inherit_stat)
309 static void perf_group_attach(struct perf_event *event)
311 struct perf_event *group_leader = event->group_leader;
313 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_GROUP);
314 event->attach_state |= PERF_ATTACH_GROUP;
316 if (group_leader == event)
319 if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
320 !is_software_event(event))
321 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
323 list_add_tail(&event->group_entry, &group_leader->sibling_list);
324 group_leader->nr_siblings++;
328 * Remove a event from the lists for its context.
329 * Must be called with ctx->mutex and ctx->lock held.
332 list_del_event(struct perf_event *event, struct perf_event_context *ctx)
335 * We can have double detach due to exit/hot-unplug + close.
337 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
340 event->attach_state &= ~PERF_ATTACH_CONTEXT;
343 if (event->attr.inherit_stat)
346 list_del_rcu(&event->event_entry);
348 if (event->group_leader == event)
349 list_del_init(&event->group_entry);
351 update_group_times(event);
354 * If event was in error state, then keep it
355 * that way, otherwise bogus counts will be
356 * returned on read(). The only way to get out
357 * of error state is by explicit re-enabling
360 if (event->state > PERF_EVENT_STATE_OFF)
361 event->state = PERF_EVENT_STATE_OFF;
364 static void perf_group_detach(struct perf_event *event)
366 struct perf_event *sibling, *tmp;
367 struct list_head *list = NULL;
370 * We can have double detach due to exit/hot-unplug + close.
372 if (!(event->attach_state & PERF_ATTACH_GROUP))
375 event->attach_state &= ~PERF_ATTACH_GROUP;
378 * If this is a sibling, remove it from its group.
380 if (event->group_leader != event) {
381 list_del_init(&event->group_entry);
382 event->group_leader->nr_siblings--;
386 if (!list_empty(&event->group_entry))
387 list = &event->group_entry;
390 * If this was a group event with sibling events then
391 * upgrade the siblings to singleton events by adding them
392 * to whatever list we are on.
394 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
396 list_move_tail(&sibling->group_entry, list);
397 sibling->group_leader = sibling;
399 /* Inherit group flags from the previous leader */
400 sibling->group_flags = event->group_flags;
405 event_filter_match(struct perf_event *event)
407 return event->cpu == -1 || event->cpu == smp_processor_id();
411 event_sched_out(struct perf_event *event,
412 struct perf_cpu_context *cpuctx,
413 struct perf_event_context *ctx)
417 * An event which could not be activated because of
418 * filter mismatch still needs to have its timings
419 * maintained, otherwise bogus information is return
420 * via read() for time_enabled, time_running:
422 if (event->state == PERF_EVENT_STATE_INACTIVE
423 && !event_filter_match(event)) {
424 delta = ctx->time - event->tstamp_stopped;
425 event->tstamp_running += delta;
426 event->tstamp_stopped = ctx->time;
429 if (event->state != PERF_EVENT_STATE_ACTIVE)
432 event->state = PERF_EVENT_STATE_INACTIVE;
433 if (event->pending_disable) {
434 event->pending_disable = 0;
435 event->state = PERF_EVENT_STATE_OFF;
437 event->tstamp_stopped = ctx->time;
438 event->pmu->del(event, 0);
441 if (!is_software_event(event))
442 cpuctx->active_oncpu--;
444 if (event->attr.exclusive || !cpuctx->active_oncpu)
445 cpuctx->exclusive = 0;
449 group_sched_out(struct perf_event *group_event,
450 struct perf_cpu_context *cpuctx,
451 struct perf_event_context *ctx)
453 struct perf_event *event;
454 int state = group_event->state;
456 event_sched_out(group_event, cpuctx, ctx);
459 * Schedule out siblings (if any):
461 list_for_each_entry(event, &group_event->sibling_list, group_entry)
462 event_sched_out(event, cpuctx, ctx);
464 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
465 cpuctx->exclusive = 0;
468 static inline struct perf_cpu_context *
469 __get_cpu_context(struct perf_event_context *ctx)
471 return this_cpu_ptr(ctx->pmu->pmu_cpu_context);
475 * Cross CPU call to remove a performance event
477 * We disable the event on the hardware level first. After that we
478 * remove it from the context list.
480 static void __perf_event_remove_from_context(void *info)
482 struct perf_event *event = info;
483 struct perf_event_context *ctx = event->ctx;
484 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
487 * If this is a task context, we need to check whether it is
488 * the current task context of this cpu. If not it has been
489 * scheduled out before the smp call arrived.
491 if (ctx->task && cpuctx->task_ctx != ctx)
494 raw_spin_lock(&ctx->lock);
496 event_sched_out(event, cpuctx, ctx);
498 list_del_event(event, ctx);
500 raw_spin_unlock(&ctx->lock);
505 * Remove the event from a task's (or a CPU's) list of events.
507 * Must be called with ctx->mutex held.
509 * CPU events are removed with a smp call. For task events we only
510 * call when the task is on a CPU.
512 * If event->ctx is a cloned context, callers must make sure that
513 * every task struct that event->ctx->task could possibly point to
514 * remains valid. This is OK when called from perf_release since
515 * that only calls us on the top-level context, which can't be a clone.
516 * When called from perf_event_exit_task, it's OK because the
517 * context has been detached from its task.
519 static void perf_event_remove_from_context(struct perf_event *event)
521 struct perf_event_context *ctx = event->ctx;
522 struct task_struct *task = ctx->task;
526 * Per cpu events are removed via an smp call and
527 * the removal is always successful.
529 smp_call_function_single(event->cpu,
530 __perf_event_remove_from_context,
536 task_oncpu_function_call(task, __perf_event_remove_from_context,
539 raw_spin_lock_irq(&ctx->lock);
541 * If the context is active we need to retry the smp call.
543 if (ctx->nr_active && !list_empty(&event->group_entry)) {
544 raw_spin_unlock_irq(&ctx->lock);
549 * The lock prevents that this context is scheduled in so we
550 * can remove the event safely, if the call above did not
553 if (!list_empty(&event->group_entry))
554 list_del_event(event, ctx);
555 raw_spin_unlock_irq(&ctx->lock);
559 * Cross CPU call to disable a performance event
561 static void __perf_event_disable(void *info)
563 struct perf_event *event = info;
564 struct perf_event_context *ctx = event->ctx;
565 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
568 * If this is a per-task event, need to check whether this
569 * event's task is the current task on this cpu.
571 if (ctx->task && cpuctx->task_ctx != ctx)
574 raw_spin_lock(&ctx->lock);
577 * If the event is on, turn it off.
578 * If it is in error state, leave it in error state.
580 if (event->state >= PERF_EVENT_STATE_INACTIVE) {
581 update_context_time(ctx);
582 update_group_times(event);
583 if (event == event->group_leader)
584 group_sched_out(event, cpuctx, ctx);
586 event_sched_out(event, cpuctx, ctx);
587 event->state = PERF_EVENT_STATE_OFF;
590 raw_spin_unlock(&ctx->lock);
596 * If event->ctx is a cloned context, callers must make sure that
597 * every task struct that event->ctx->task could possibly point to
598 * remains valid. This condition is satisifed when called through
599 * perf_event_for_each_child or perf_event_for_each because they
600 * hold the top-level event's child_mutex, so any descendant that
601 * goes to exit will block in sync_child_event.
602 * When called from perf_pending_event it's OK because event->ctx
603 * is the current context on this CPU and preemption is disabled,
604 * hence we can't get into perf_event_task_sched_out for this context.
606 void perf_event_disable(struct perf_event *event)
608 struct perf_event_context *ctx = event->ctx;
609 struct task_struct *task = ctx->task;
613 * Disable the event on the cpu that it's on
615 smp_call_function_single(event->cpu, __perf_event_disable,
621 task_oncpu_function_call(task, __perf_event_disable, event);
623 raw_spin_lock_irq(&ctx->lock);
625 * If the event is still active, we need to retry the cross-call.
627 if (event->state == PERF_EVENT_STATE_ACTIVE) {
628 raw_spin_unlock_irq(&ctx->lock);
633 * Since we have the lock this context can't be scheduled
634 * in, so we can change the state safely.
636 if (event->state == PERF_EVENT_STATE_INACTIVE) {
637 update_group_times(event);
638 event->state = PERF_EVENT_STATE_OFF;
641 raw_spin_unlock_irq(&ctx->lock);
645 event_sched_in(struct perf_event *event,
646 struct perf_cpu_context *cpuctx,
647 struct perf_event_context *ctx)
649 if (event->state <= PERF_EVENT_STATE_OFF)
652 event->state = PERF_EVENT_STATE_ACTIVE;
653 event->oncpu = smp_processor_id();
655 * The new state must be visible before we turn it on in the hardware:
659 if (event->pmu->add(event, PERF_EF_START)) {
660 event->state = PERF_EVENT_STATE_INACTIVE;
665 event->tstamp_running += ctx->time - event->tstamp_stopped;
667 if (!is_software_event(event))
668 cpuctx->active_oncpu++;
671 if (event->attr.exclusive)
672 cpuctx->exclusive = 1;
678 group_sched_in(struct perf_event *group_event,
679 struct perf_cpu_context *cpuctx,
680 struct perf_event_context *ctx)
682 struct perf_event *event, *partial_group = NULL;
683 struct pmu *pmu = group_event->pmu;
685 if (group_event->state == PERF_EVENT_STATE_OFF)
690 if (event_sched_in(group_event, cpuctx, ctx)) {
691 pmu->cancel_txn(pmu);
696 * Schedule in siblings as one group (if any):
698 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
699 if (event_sched_in(event, cpuctx, ctx)) {
700 partial_group = event;
705 if (!pmu->commit_txn(pmu))
710 * Groups can be scheduled in as one unit only, so undo any
711 * partial group before returning:
713 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
714 if (event == partial_group)
716 event_sched_out(event, cpuctx, ctx);
718 event_sched_out(group_event, cpuctx, ctx);
720 pmu->cancel_txn(pmu);
726 * Work out whether we can put this event group on the CPU now.
728 static int group_can_go_on(struct perf_event *event,
729 struct perf_cpu_context *cpuctx,
733 * Groups consisting entirely of software events can always go on.
735 if (event->group_flags & PERF_GROUP_SOFTWARE)
738 * If an exclusive group is already on, no other hardware
741 if (cpuctx->exclusive)
744 * If this group is exclusive and there are already
745 * events on the CPU, it can't go on.
747 if (event->attr.exclusive && cpuctx->active_oncpu)
750 * Otherwise, try to add it if all previous groups were able
756 static void add_event_to_ctx(struct perf_event *event,
757 struct perf_event_context *ctx)
759 list_add_event(event, ctx);
760 perf_group_attach(event);
761 event->tstamp_enabled = ctx->time;
762 event->tstamp_running = ctx->time;
763 event->tstamp_stopped = ctx->time;
767 * Cross CPU call to install and enable a performance event
769 * Must be called with ctx->mutex held
771 static void __perf_install_in_context(void *info)
773 struct perf_event *event = info;
774 struct perf_event_context *ctx = event->ctx;
775 struct perf_event *leader = event->group_leader;
776 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
780 * If this is a task context, we need to check whether it is
781 * the current task context of this cpu. If not it has been
782 * scheduled out before the smp call arrived.
783 * Or possibly this is the right context but it isn't
784 * on this cpu because it had no events.
786 if (ctx->task && cpuctx->task_ctx != ctx) {
787 if (cpuctx->task_ctx || ctx->task != current)
789 cpuctx->task_ctx = ctx;
792 raw_spin_lock(&ctx->lock);
794 update_context_time(ctx);
796 add_event_to_ctx(event, ctx);
798 if (event->cpu != -1 && event->cpu != smp_processor_id())
802 * Don't put the event on if it is disabled or if
803 * it is in a group and the group isn't on.
805 if (event->state != PERF_EVENT_STATE_INACTIVE ||
806 (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE))
810 * An exclusive event can't go on if there are already active
811 * hardware events, and no hardware event can go on if there
812 * is already an exclusive event on.
814 if (!group_can_go_on(event, cpuctx, 1))
817 err = event_sched_in(event, cpuctx, ctx);
821 * This event couldn't go on. If it is in a group
822 * then we have to pull the whole group off.
823 * If the event group is pinned then put it in error state.
826 group_sched_out(leader, cpuctx, ctx);
827 if (leader->attr.pinned) {
828 update_group_times(leader);
829 leader->state = PERF_EVENT_STATE_ERROR;
834 raw_spin_unlock(&ctx->lock);
838 * Attach a performance event to a context
840 * First we add the event to the list with the hardware enable bit
841 * in event->hw_config cleared.
843 * If the event is attached to a task which is on a CPU we use a smp
844 * call to enable it in the task context. The task might have been
845 * scheduled away, but we check this in the smp call again.
847 * Must be called with ctx->mutex held.
850 perf_install_in_context(struct perf_event_context *ctx,
851 struct perf_event *event,
854 struct task_struct *task = ctx->task;
860 * Per cpu events are installed via an smp call and
861 * the install is always successful.
863 smp_call_function_single(cpu, __perf_install_in_context,
869 task_oncpu_function_call(task, __perf_install_in_context,
872 raw_spin_lock_irq(&ctx->lock);
874 * we need to retry the smp call.
876 if (ctx->is_active && list_empty(&event->group_entry)) {
877 raw_spin_unlock_irq(&ctx->lock);
882 * The lock prevents that this context is scheduled in so we
883 * can add the event safely, if it the call above did not
886 if (list_empty(&event->group_entry))
887 add_event_to_ctx(event, ctx);
888 raw_spin_unlock_irq(&ctx->lock);
892 * Put a event into inactive state and update time fields.
893 * Enabling the leader of a group effectively enables all
894 * the group members that aren't explicitly disabled, so we
895 * have to update their ->tstamp_enabled also.
896 * Note: this works for group members as well as group leaders
897 * since the non-leader members' sibling_lists will be empty.
899 static void __perf_event_mark_enabled(struct perf_event *event,
900 struct perf_event_context *ctx)
902 struct perf_event *sub;
904 event->state = PERF_EVENT_STATE_INACTIVE;
905 event->tstamp_enabled = ctx->time - event->total_time_enabled;
906 list_for_each_entry(sub, &event->sibling_list, group_entry) {
907 if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
908 sub->tstamp_enabled =
909 ctx->time - sub->total_time_enabled;
915 * Cross CPU call to enable a performance event
917 static void __perf_event_enable(void *info)
919 struct perf_event *event = info;
920 struct perf_event_context *ctx = event->ctx;
921 struct perf_event *leader = event->group_leader;
922 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
926 * If this is a per-task event, need to check whether this
927 * event's task is the current task on this cpu.
929 if (ctx->task && cpuctx->task_ctx != ctx) {
930 if (cpuctx->task_ctx || ctx->task != current)
932 cpuctx->task_ctx = ctx;
935 raw_spin_lock(&ctx->lock);
937 update_context_time(ctx);
939 if (event->state >= PERF_EVENT_STATE_INACTIVE)
941 __perf_event_mark_enabled(event, ctx);
943 if (event->cpu != -1 && event->cpu != smp_processor_id())
947 * If the event is in a group and isn't the group leader,
948 * then don't put it on unless the group is on.
950 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
953 if (!group_can_go_on(event, cpuctx, 1)) {
957 err = group_sched_in(event, cpuctx, ctx);
959 err = event_sched_in(event, cpuctx, ctx);
964 * If this event can't go on and it's part of a
965 * group, then the whole group has to come off.
968 group_sched_out(leader, cpuctx, ctx);
969 if (leader->attr.pinned) {
970 update_group_times(leader);
971 leader->state = PERF_EVENT_STATE_ERROR;
976 raw_spin_unlock(&ctx->lock);
982 * If event->ctx is a cloned context, callers must make sure that
983 * every task struct that event->ctx->task could possibly point to
984 * remains valid. This condition is satisfied when called through
985 * perf_event_for_each_child or perf_event_for_each as described
986 * for perf_event_disable.
988 void perf_event_enable(struct perf_event *event)
990 struct perf_event_context *ctx = event->ctx;
991 struct task_struct *task = ctx->task;
995 * Enable the event on the cpu that it's on
997 smp_call_function_single(event->cpu, __perf_event_enable,
1002 raw_spin_lock_irq(&ctx->lock);
1003 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1007 * If the event is in error state, clear that first.
1008 * That way, if we see the event in error state below, we
1009 * know that it has gone back into error state, as distinct
1010 * from the task having been scheduled away before the
1011 * cross-call arrived.
1013 if (event->state == PERF_EVENT_STATE_ERROR)
1014 event->state = PERF_EVENT_STATE_OFF;
1017 raw_spin_unlock_irq(&ctx->lock);
1018 task_oncpu_function_call(task, __perf_event_enable, event);
1020 raw_spin_lock_irq(&ctx->lock);
1023 * If the context is active and the event is still off,
1024 * we need to retry the cross-call.
1026 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF)
1030 * Since we have the lock this context can't be scheduled
1031 * in, so we can change the state safely.
1033 if (event->state == PERF_EVENT_STATE_OFF)
1034 __perf_event_mark_enabled(event, ctx);
1037 raw_spin_unlock_irq(&ctx->lock);
1040 static int perf_event_refresh(struct perf_event *event, int refresh)
1043 * not supported on inherited events
1045 if (event->attr.inherit)
1048 atomic_add(refresh, &event->event_limit);
1049 perf_event_enable(event);
1055 EVENT_FLEXIBLE = 0x1,
1057 EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
1060 static void ctx_sched_out(struct perf_event_context *ctx,
1061 struct perf_cpu_context *cpuctx,
1062 enum event_type_t event_type)
1064 struct perf_event *event;
1066 raw_spin_lock(&ctx->lock);
1067 perf_pmu_disable(ctx->pmu);
1069 if (likely(!ctx->nr_events))
1071 update_context_time(ctx);
1073 if (!ctx->nr_active)
1076 if (event_type & EVENT_PINNED) {
1077 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
1078 group_sched_out(event, cpuctx, ctx);
1081 if (event_type & EVENT_FLEXIBLE) {
1082 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
1083 group_sched_out(event, cpuctx, ctx);
1086 perf_pmu_enable(ctx->pmu);
1087 raw_spin_unlock(&ctx->lock);
1091 * Test whether two contexts are equivalent, i.e. whether they
1092 * have both been cloned from the same version of the same context
1093 * and they both have the same number of enabled events.
1094 * If the number of enabled events is the same, then the set
1095 * of enabled events should be the same, because these are both
1096 * inherited contexts, therefore we can't access individual events
1097 * in them directly with an fd; we can only enable/disable all
1098 * events via prctl, or enable/disable all events in a family
1099 * via ioctl, which will have the same effect on both contexts.
1101 static int context_equiv(struct perf_event_context *ctx1,
1102 struct perf_event_context *ctx2)
1104 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
1105 && ctx1->parent_gen == ctx2->parent_gen
1106 && !ctx1->pin_count && !ctx2->pin_count;
1109 static void __perf_event_sync_stat(struct perf_event *event,
1110 struct perf_event *next_event)
1114 if (!event->attr.inherit_stat)
1118 * Update the event value, we cannot use perf_event_read()
1119 * because we're in the middle of a context switch and have IRQs
1120 * disabled, which upsets smp_call_function_single(), however
1121 * we know the event must be on the current CPU, therefore we
1122 * don't need to use it.
1124 switch (event->state) {
1125 case PERF_EVENT_STATE_ACTIVE:
1126 event->pmu->read(event);
1129 case PERF_EVENT_STATE_INACTIVE:
1130 update_event_times(event);
1138 * In order to keep per-task stats reliable we need to flip the event
1139 * values when we flip the contexts.
1141 value = local64_read(&next_event->count);
1142 value = local64_xchg(&event->count, value);
1143 local64_set(&next_event->count, value);
1145 swap(event->total_time_enabled, next_event->total_time_enabled);
1146 swap(event->total_time_running, next_event->total_time_running);
1149 * Since we swizzled the values, update the user visible data too.
1151 perf_event_update_userpage(event);
1152 perf_event_update_userpage(next_event);
1155 #define list_next_entry(pos, member) \
1156 list_entry(pos->member.next, typeof(*pos), member)
1158 static void perf_event_sync_stat(struct perf_event_context *ctx,
1159 struct perf_event_context *next_ctx)
1161 struct perf_event *event, *next_event;
1166 update_context_time(ctx);
1168 event = list_first_entry(&ctx->event_list,
1169 struct perf_event, event_entry);
1171 next_event = list_first_entry(&next_ctx->event_list,
1172 struct perf_event, event_entry);
1174 while (&event->event_entry != &ctx->event_list &&
1175 &next_event->event_entry != &next_ctx->event_list) {
1177 __perf_event_sync_stat(event, next_event);
1179 event = list_next_entry(event, event_entry);
1180 next_event = list_next_entry(next_event, event_entry);
1184 void perf_event_context_sched_out(struct task_struct *task, int ctxn,
1185 struct task_struct *next)
1187 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
1188 struct perf_event_context *next_ctx;
1189 struct perf_event_context *parent;
1190 struct perf_cpu_context *cpuctx;
1196 cpuctx = __get_cpu_context(ctx);
1197 if (!cpuctx->task_ctx)
1201 parent = rcu_dereference(ctx->parent_ctx);
1202 next_ctx = next->perf_event_ctxp[ctxn];
1203 if (parent && next_ctx &&
1204 rcu_dereference(next_ctx->parent_ctx) == parent) {
1206 * Looks like the two contexts are clones, so we might be
1207 * able to optimize the context switch. We lock both
1208 * contexts and check that they are clones under the
1209 * lock (including re-checking that neither has been
1210 * uncloned in the meantime). It doesn't matter which
1211 * order we take the locks because no other cpu could
1212 * be trying to lock both of these tasks.
1214 raw_spin_lock(&ctx->lock);
1215 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
1216 if (context_equiv(ctx, next_ctx)) {
1218 * XXX do we need a memory barrier of sorts
1219 * wrt to rcu_dereference() of perf_event_ctxp
1221 task->perf_event_ctxp[ctxn] = next_ctx;
1222 next->perf_event_ctxp[ctxn] = ctx;
1224 next_ctx->task = task;
1227 perf_event_sync_stat(ctx, next_ctx);
1229 raw_spin_unlock(&next_ctx->lock);
1230 raw_spin_unlock(&ctx->lock);
1235 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
1236 cpuctx->task_ctx = NULL;
1240 #define for_each_task_context_nr(ctxn) \
1241 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
1244 * Called from scheduler to remove the events of the current task,
1245 * with interrupts disabled.
1247 * We stop each event and update the event value in event->count.
1249 * This does not protect us against NMI, but disable()
1250 * sets the disabled bit in the control field of event _before_
1251 * accessing the event control register. If a NMI hits, then it will
1252 * not restart the event.
1254 void perf_event_task_sched_out(struct task_struct *task,
1255 struct task_struct *next)
1259 perf_sw_event(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 1, NULL, 0);
1261 for_each_task_context_nr(ctxn)
1262 perf_event_context_sched_out(task, ctxn, next);
1265 static void task_ctx_sched_out(struct perf_event_context *ctx,
1266 enum event_type_t event_type)
1268 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1270 if (!cpuctx->task_ctx)
1273 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
1276 ctx_sched_out(ctx, cpuctx, event_type);
1277 cpuctx->task_ctx = NULL;
1281 * Called with IRQs disabled
1283 static void __perf_event_task_sched_out(struct perf_event_context *ctx)
1285 task_ctx_sched_out(ctx, EVENT_ALL);
1289 * Called with IRQs disabled
1291 static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
1292 enum event_type_t event_type)
1294 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
1298 ctx_pinned_sched_in(struct perf_event_context *ctx,
1299 struct perf_cpu_context *cpuctx)
1301 struct perf_event *event;
1303 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
1304 if (event->state <= PERF_EVENT_STATE_OFF)
1306 if (event->cpu != -1 && event->cpu != smp_processor_id())
1309 if (group_can_go_on(event, cpuctx, 1))
1310 group_sched_in(event, cpuctx, ctx);
1313 * If this pinned group hasn't been scheduled,
1314 * put it in error state.
1316 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1317 update_group_times(event);
1318 event->state = PERF_EVENT_STATE_ERROR;
1324 ctx_flexible_sched_in(struct perf_event_context *ctx,
1325 struct perf_cpu_context *cpuctx)
1327 struct perf_event *event;
1330 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
1331 /* Ignore events in OFF or ERROR state */
1332 if (event->state <= PERF_EVENT_STATE_OFF)
1335 * Listen to the 'cpu' scheduling filter constraint
1338 if (event->cpu != -1 && event->cpu != smp_processor_id())
1341 if (group_can_go_on(event, cpuctx, can_add_hw)) {
1342 if (group_sched_in(event, cpuctx, ctx))
1349 ctx_sched_in(struct perf_event_context *ctx,
1350 struct perf_cpu_context *cpuctx,
1351 enum event_type_t event_type)
1353 raw_spin_lock(&ctx->lock);
1355 if (likely(!ctx->nr_events))
1358 ctx->timestamp = perf_clock();
1361 * First go through the list and put on any pinned groups
1362 * in order to give them the best chance of going on.
1364 if (event_type & EVENT_PINNED)
1365 ctx_pinned_sched_in(ctx, cpuctx);
1367 /* Then walk through the lower prio flexible groups */
1368 if (event_type & EVENT_FLEXIBLE)
1369 ctx_flexible_sched_in(ctx, cpuctx);
1372 raw_spin_unlock(&ctx->lock);
1375 static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
1376 enum event_type_t event_type)
1378 struct perf_event_context *ctx = &cpuctx->ctx;
1380 ctx_sched_in(ctx, cpuctx, event_type);
1383 static void task_ctx_sched_in(struct perf_event_context *ctx,
1384 enum event_type_t event_type)
1386 struct perf_cpu_context *cpuctx;
1388 cpuctx = __get_cpu_context(ctx);
1389 if (cpuctx->task_ctx == ctx)
1392 ctx_sched_in(ctx, cpuctx, event_type);
1393 cpuctx->task_ctx = ctx;
1396 void perf_event_context_sched_in(struct perf_event_context *ctx)
1398 struct perf_cpu_context *cpuctx;
1400 cpuctx = __get_cpu_context(ctx);
1401 if (cpuctx->task_ctx == ctx)
1404 perf_pmu_disable(ctx->pmu);
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 * Since these rotations are per-cpu, we need to ensure the
1420 * cpu-context we got scheduled on is actually rotating.
1422 perf_pmu_rotate_start(ctx->pmu);
1423 perf_pmu_enable(ctx->pmu);
1427 * Called from scheduler to add the events of the current task
1428 * with interrupts disabled.
1430 * We restore the event value and then enable it.
1432 * This does not protect us against NMI, but enable()
1433 * sets the enabled bit in the control field of event _before_
1434 * accessing the event control register. If a NMI hits, then it will
1435 * keep the event running.
1437 void perf_event_task_sched_in(struct task_struct *task)
1439 struct perf_event_context *ctx;
1442 for_each_task_context_nr(ctxn) {
1443 ctx = task->perf_event_ctxp[ctxn];
1447 perf_event_context_sched_in(ctx);
1451 #define MAX_INTERRUPTS (~0ULL)
1453 static void perf_log_throttle(struct perf_event *event, int enable);
1455 static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
1457 u64 frequency = event->attr.sample_freq;
1458 u64 sec = NSEC_PER_SEC;
1459 u64 divisor, dividend;
1461 int count_fls, nsec_fls, frequency_fls, sec_fls;
1463 count_fls = fls64(count);
1464 nsec_fls = fls64(nsec);
1465 frequency_fls = fls64(frequency);
1469 * We got @count in @nsec, with a target of sample_freq HZ
1470 * the target period becomes:
1473 * period = -------------------
1474 * @nsec * sample_freq
1479 * Reduce accuracy by one bit such that @a and @b converge
1480 * to a similar magnitude.
1482 #define REDUCE_FLS(a, b) \
1484 if (a##_fls > b##_fls) { \
1494 * Reduce accuracy until either term fits in a u64, then proceed with
1495 * the other, so that finally we can do a u64/u64 division.
1497 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
1498 REDUCE_FLS(nsec, frequency);
1499 REDUCE_FLS(sec, count);
1502 if (count_fls + sec_fls > 64) {
1503 divisor = nsec * frequency;
1505 while (count_fls + sec_fls > 64) {
1506 REDUCE_FLS(count, sec);
1510 dividend = count * sec;
1512 dividend = count * sec;
1514 while (nsec_fls + frequency_fls > 64) {
1515 REDUCE_FLS(nsec, frequency);
1519 divisor = nsec * frequency;
1525 return div64_u64(dividend, divisor);
1528 static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count)
1530 struct hw_perf_event *hwc = &event->hw;
1531 s64 period, sample_period;
1534 period = perf_calculate_period(event, nsec, count);
1536 delta = (s64)(period - hwc->sample_period);
1537 delta = (delta + 7) / 8; /* low pass filter */
1539 sample_period = hwc->sample_period + delta;
1544 hwc->sample_period = sample_period;
1546 if (local64_read(&hwc->period_left) > 8*sample_period) {
1547 event->pmu->stop(event, PERF_EF_UPDATE);
1548 local64_set(&hwc->period_left, 0);
1549 event->pmu->start(event, PERF_EF_RELOAD);
1553 static void perf_ctx_adjust_freq(struct perf_event_context *ctx, u64 period)
1555 struct perf_event *event;
1556 struct hw_perf_event *hwc;
1557 u64 interrupts, now;
1560 raw_spin_lock(&ctx->lock);
1561 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
1562 if (event->state != PERF_EVENT_STATE_ACTIVE)
1565 if (event->cpu != -1 && event->cpu != smp_processor_id())
1570 interrupts = hwc->interrupts;
1571 hwc->interrupts = 0;
1574 * unthrottle events on the tick
1576 if (interrupts == MAX_INTERRUPTS) {
1577 perf_log_throttle(event, 1);
1578 event->pmu->start(event, 0);
1581 if (!event->attr.freq || !event->attr.sample_freq)
1584 event->pmu->read(event);
1585 now = local64_read(&event->count);
1586 delta = now - hwc->freq_count_stamp;
1587 hwc->freq_count_stamp = now;
1590 perf_adjust_period(event, period, delta);
1592 raw_spin_unlock(&ctx->lock);
1596 * Round-robin a context's events:
1598 static void rotate_ctx(struct perf_event_context *ctx)
1600 raw_spin_lock(&ctx->lock);
1602 /* Rotate the first entry last of non-pinned groups */
1603 list_rotate_left(&ctx->flexible_groups);
1605 raw_spin_unlock(&ctx->lock);
1609 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
1610 * because they're strictly cpu affine and rotate_start is called with IRQs
1611 * disabled, while rotate_context is called from IRQ context.
1613 static void perf_rotate_context(struct perf_cpu_context *cpuctx)
1615 u64 interval = (u64)cpuctx->jiffies_interval * TICK_NSEC;
1616 struct perf_event_context *ctx = NULL;
1617 int rotate = 0, remove = 1;
1619 if (cpuctx->ctx.nr_events) {
1621 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
1625 ctx = cpuctx->task_ctx;
1626 if (ctx && ctx->nr_events) {
1628 if (ctx->nr_events != ctx->nr_active)
1632 perf_pmu_disable(cpuctx->ctx.pmu);
1633 perf_ctx_adjust_freq(&cpuctx->ctx, interval);
1635 perf_ctx_adjust_freq(ctx, interval);
1640 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
1642 task_ctx_sched_out(ctx, EVENT_FLEXIBLE);
1644 rotate_ctx(&cpuctx->ctx);
1648 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE);
1650 task_ctx_sched_in(ctx, EVENT_FLEXIBLE);
1654 list_del_init(&cpuctx->rotation_list);
1656 perf_pmu_enable(cpuctx->ctx.pmu);
1659 void perf_event_task_tick(void)
1661 struct list_head *head = &__get_cpu_var(rotation_list);
1662 struct perf_cpu_context *cpuctx, *tmp;
1664 WARN_ON(!irqs_disabled());
1666 list_for_each_entry_safe(cpuctx, tmp, head, rotation_list) {
1667 if (cpuctx->jiffies_interval == 1 ||
1668 !(jiffies % cpuctx->jiffies_interval))
1669 perf_rotate_context(cpuctx);
1673 static int event_enable_on_exec(struct perf_event *event,
1674 struct perf_event_context *ctx)
1676 if (!event->attr.enable_on_exec)
1679 event->attr.enable_on_exec = 0;
1680 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1683 __perf_event_mark_enabled(event, ctx);
1689 * Enable all of a task's events that have been marked enable-on-exec.
1690 * This expects task == current.
1692 static void perf_event_enable_on_exec(struct perf_event_context *ctx)
1694 struct perf_event *event;
1695 unsigned long flags;
1699 local_irq_save(flags);
1700 if (!ctx || !ctx->nr_events)
1703 task_ctx_sched_out(ctx, EVENT_ALL);
1705 raw_spin_lock(&ctx->lock);
1707 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
1708 ret = event_enable_on_exec(event, ctx);
1713 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
1714 ret = event_enable_on_exec(event, ctx);
1720 * Unclone this context if we enabled any event.
1725 raw_spin_unlock(&ctx->lock);
1727 perf_event_context_sched_in(ctx);
1729 local_irq_restore(flags);
1733 * Cross CPU call to read the hardware event
1735 static void __perf_event_read(void *info)
1737 struct perf_event *event = info;
1738 struct perf_event_context *ctx = event->ctx;
1739 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1742 * If this is a task context, we need to check whether it is
1743 * the current task context of this cpu. If not it has been
1744 * scheduled out before the smp call arrived. In that case
1745 * event->count would have been updated to a recent sample
1746 * when the event was scheduled out.
1748 if (ctx->task && cpuctx->task_ctx != ctx)
1751 raw_spin_lock(&ctx->lock);
1752 update_context_time(ctx);
1753 update_event_times(event);
1754 raw_spin_unlock(&ctx->lock);
1756 event->pmu->read(event);
1759 static inline u64 perf_event_count(struct perf_event *event)
1761 return local64_read(&event->count) + atomic64_read(&event->child_count);
1764 static u64 perf_event_read(struct perf_event *event)
1767 * If event is enabled and currently active on a CPU, update the
1768 * value in the event structure:
1770 if (event->state == PERF_EVENT_STATE_ACTIVE) {
1771 smp_call_function_single(event->oncpu,
1772 __perf_event_read, event, 1);
1773 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
1774 struct perf_event_context *ctx = event->ctx;
1775 unsigned long flags;
1777 raw_spin_lock_irqsave(&ctx->lock, flags);
1778 update_context_time(ctx);
1779 update_event_times(event);
1780 raw_spin_unlock_irqrestore(&ctx->lock, flags);
1783 return perf_event_count(event);
1790 struct callchain_cpus_entries {
1791 struct rcu_head rcu_head;
1792 struct perf_callchain_entry *cpu_entries[0];
1795 static DEFINE_PER_CPU(int, callchain_recursion[PERF_NR_CONTEXTS]);
1796 static atomic_t nr_callchain_events;
1797 static DEFINE_MUTEX(callchain_mutex);
1798 struct callchain_cpus_entries *callchain_cpus_entries;
1801 __weak void perf_callchain_kernel(struct perf_callchain_entry *entry,
1802 struct pt_regs *regs)
1806 __weak void perf_callchain_user(struct perf_callchain_entry *entry,
1807 struct pt_regs *regs)
1811 static void release_callchain_buffers_rcu(struct rcu_head *head)
1813 struct callchain_cpus_entries *entries;
1816 entries = container_of(head, struct callchain_cpus_entries, rcu_head);
1818 for_each_possible_cpu(cpu)
1819 kfree(entries->cpu_entries[cpu]);
1824 static void release_callchain_buffers(void)
1826 struct callchain_cpus_entries *entries;
1828 entries = callchain_cpus_entries;
1829 rcu_assign_pointer(callchain_cpus_entries, NULL);
1830 call_rcu(&entries->rcu_head, release_callchain_buffers_rcu);
1833 static int alloc_callchain_buffers(void)
1837 struct callchain_cpus_entries *entries;
1840 * We can't use the percpu allocation API for data that can be
1841 * accessed from NMI. Use a temporary manual per cpu allocation
1842 * until that gets sorted out.
1844 size = sizeof(*entries) + sizeof(struct perf_callchain_entry *) *
1845 num_possible_cpus();
1847 entries = kzalloc(size, GFP_KERNEL);
1851 size = sizeof(struct perf_callchain_entry) * PERF_NR_CONTEXTS;
1853 for_each_possible_cpu(cpu) {
1854 entries->cpu_entries[cpu] = kmalloc_node(size, GFP_KERNEL,
1856 if (!entries->cpu_entries[cpu])
1860 rcu_assign_pointer(callchain_cpus_entries, entries);
1865 for_each_possible_cpu(cpu)
1866 kfree(entries->cpu_entries[cpu]);
1872 static int get_callchain_buffers(void)
1877 mutex_lock(&callchain_mutex);
1879 count = atomic_inc_return(&nr_callchain_events);
1880 if (WARN_ON_ONCE(count < 1)) {
1886 /* If the allocation failed, give up */
1887 if (!callchain_cpus_entries)
1892 err = alloc_callchain_buffers();
1894 release_callchain_buffers();
1896 mutex_unlock(&callchain_mutex);
1901 static void put_callchain_buffers(void)
1903 if (atomic_dec_and_mutex_lock(&nr_callchain_events, &callchain_mutex)) {
1904 release_callchain_buffers();
1905 mutex_unlock(&callchain_mutex);
1909 static int get_recursion_context(int *recursion)
1917 else if (in_softirq())
1922 if (recursion[rctx])
1931 static inline void put_recursion_context(int *recursion, int rctx)
1937 static struct perf_callchain_entry *get_callchain_entry(int *rctx)
1940 struct callchain_cpus_entries *entries;
1942 *rctx = get_recursion_context(__get_cpu_var(callchain_recursion));
1946 entries = rcu_dereference(callchain_cpus_entries);
1950 cpu = smp_processor_id();
1952 return &entries->cpu_entries[cpu][*rctx];
1956 put_callchain_entry(int rctx)
1958 put_recursion_context(__get_cpu_var(callchain_recursion), rctx);
1961 static struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
1964 struct perf_callchain_entry *entry;
1967 entry = get_callchain_entry(&rctx);
1976 if (!user_mode(regs)) {
1977 perf_callchain_store(entry, PERF_CONTEXT_KERNEL);
1978 perf_callchain_kernel(entry, regs);
1980 regs = task_pt_regs(current);
1986 perf_callchain_store(entry, PERF_CONTEXT_USER);
1987 perf_callchain_user(entry, regs);
1991 put_callchain_entry(rctx);
1997 * Initialize the perf_event context in a task_struct:
1999 static void __perf_event_init_context(struct perf_event_context *ctx)
2001 raw_spin_lock_init(&ctx->lock);
2002 mutex_init(&ctx->mutex);
2003 INIT_LIST_HEAD(&ctx->pinned_groups);
2004 INIT_LIST_HEAD(&ctx->flexible_groups);
2005 INIT_LIST_HEAD(&ctx->event_list);
2006 atomic_set(&ctx->refcount, 1);
2009 static struct perf_event_context *
2010 alloc_perf_context(struct pmu *pmu, struct task_struct *task)
2012 struct perf_event_context *ctx;
2014 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
2018 __perf_event_init_context(ctx);
2021 get_task_struct(task);
2028 static struct task_struct *
2029 find_lively_task_by_vpid(pid_t vpid)
2031 struct task_struct *task;
2038 task = find_task_by_vpid(vpid);
2040 get_task_struct(task);
2044 return ERR_PTR(-ESRCH);
2047 * Can't attach events to a dying task.
2050 if (task->flags & PF_EXITING)
2053 /* Reuse ptrace permission checks for now. */
2055 if (!ptrace_may_access(task, PTRACE_MODE_READ))
2060 put_task_struct(task);
2061 return ERR_PTR(err);
2065 static struct perf_event_context *
2066 find_get_context(struct pmu *pmu, struct task_struct *task, int cpu)
2068 struct perf_event_context *ctx;
2069 struct perf_cpu_context *cpuctx;
2070 unsigned long flags;
2073 if (!task && cpu != -1) {
2074 /* Must be root to operate on a CPU event: */
2075 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
2076 return ERR_PTR(-EACCES);
2078 if (cpu < 0 || cpu >= nr_cpumask_bits)
2079 return ERR_PTR(-EINVAL);
2082 * We could be clever and allow to attach a event to an
2083 * offline CPU and activate it when the CPU comes up, but
2086 if (!cpu_online(cpu))
2087 return ERR_PTR(-ENODEV);
2089 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
2097 ctxn = pmu->task_ctx_nr;
2102 ctx = perf_lock_task_context(task, ctxn, &flags);
2105 raw_spin_unlock_irqrestore(&ctx->lock, flags);
2109 ctx = alloc_perf_context(pmu, task);
2116 if (cmpxchg(&task->perf_event_ctxp[ctxn], NULL, ctx)) {
2118 * We raced with some other task; use
2119 * the context they set.
2121 put_task_struct(task);
2127 put_task_struct(task);
2131 put_task_struct(task);
2132 return ERR_PTR(err);
2135 static void perf_event_free_filter(struct perf_event *event);
2137 static void free_event_rcu(struct rcu_head *head)
2139 struct perf_event *event;
2141 event = container_of(head, struct perf_event, rcu_head);
2143 put_pid_ns(event->ns);
2144 perf_event_free_filter(event);
2148 static void perf_pending_sync(struct perf_event *event);
2149 static void perf_buffer_put(struct perf_buffer *buffer);
2151 static void free_event(struct perf_event *event)
2153 perf_pending_sync(event);
2155 if (!event->parent) {
2156 atomic_dec(&nr_events);
2157 if (event->attr.mmap || event->attr.mmap_data)
2158 atomic_dec(&nr_mmap_events);
2159 if (event->attr.comm)
2160 atomic_dec(&nr_comm_events);
2161 if (event->attr.task)
2162 atomic_dec(&nr_task_events);
2163 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
2164 put_callchain_buffers();
2167 if (event->buffer) {
2168 perf_buffer_put(event->buffer);
2169 event->buffer = NULL;
2173 event->destroy(event);
2176 put_ctx(event->ctx);
2178 call_rcu(&event->rcu_head, free_event_rcu);
2181 int perf_event_release_kernel(struct perf_event *event)
2183 struct perf_event_context *ctx = event->ctx;
2186 * Remove from the PMU, can't get re-enabled since we got
2187 * here because the last ref went.
2189 perf_event_disable(event);
2191 WARN_ON_ONCE(ctx->parent_ctx);
2193 * There are two ways this annotation is useful:
2195 * 1) there is a lock recursion from perf_event_exit_task
2196 * see the comment there.
2198 * 2) there is a lock-inversion with mmap_sem through
2199 * perf_event_read_group(), which takes faults while
2200 * holding ctx->mutex, however this is called after
2201 * the last filedesc died, so there is no possibility
2202 * to trigger the AB-BA case.
2204 mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
2205 raw_spin_lock_irq(&ctx->lock);
2206 perf_group_detach(event);
2207 list_del_event(event, ctx);
2208 raw_spin_unlock_irq(&ctx->lock);
2209 mutex_unlock(&ctx->mutex);
2211 mutex_lock(&event->owner->perf_event_mutex);
2212 list_del_init(&event->owner_entry);
2213 mutex_unlock(&event->owner->perf_event_mutex);
2214 put_task_struct(event->owner);
2220 EXPORT_SYMBOL_GPL(perf_event_release_kernel);
2223 * Called when the last reference to the file is gone.
2225 static int perf_release(struct inode *inode, struct file *file)
2227 struct perf_event *event = file->private_data;
2229 file->private_data = NULL;
2231 return perf_event_release_kernel(event);
2234 static int perf_event_read_size(struct perf_event *event)
2236 int entry = sizeof(u64); /* value */
2240 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2241 size += sizeof(u64);
2243 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2244 size += sizeof(u64);
2246 if (event->attr.read_format & PERF_FORMAT_ID)
2247 entry += sizeof(u64);
2249 if (event->attr.read_format & PERF_FORMAT_GROUP) {
2250 nr += event->group_leader->nr_siblings;
2251 size += sizeof(u64);
2259 u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
2261 struct perf_event *child;
2267 mutex_lock(&event->child_mutex);
2268 total += perf_event_read(event);
2269 *enabled += event->total_time_enabled +
2270 atomic64_read(&event->child_total_time_enabled);
2271 *running += event->total_time_running +
2272 atomic64_read(&event->child_total_time_running);
2274 list_for_each_entry(child, &event->child_list, child_list) {
2275 total += perf_event_read(child);
2276 *enabled += child->total_time_enabled;
2277 *running += child->total_time_running;
2279 mutex_unlock(&event->child_mutex);
2283 EXPORT_SYMBOL_GPL(perf_event_read_value);
2285 static int perf_event_read_group(struct perf_event *event,
2286 u64 read_format, char __user *buf)
2288 struct perf_event *leader = event->group_leader, *sub;
2289 int n = 0, size = 0, ret = -EFAULT;
2290 struct perf_event_context *ctx = leader->ctx;
2292 u64 count, enabled, running;
2294 mutex_lock(&ctx->mutex);
2295 count = perf_event_read_value(leader, &enabled, &running);
2297 values[n++] = 1 + leader->nr_siblings;
2298 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2299 values[n++] = enabled;
2300 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2301 values[n++] = running;
2302 values[n++] = count;
2303 if (read_format & PERF_FORMAT_ID)
2304 values[n++] = primary_event_id(leader);
2306 size = n * sizeof(u64);
2308 if (copy_to_user(buf, values, size))
2313 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
2316 values[n++] = perf_event_read_value(sub, &enabled, &running);
2317 if (read_format & PERF_FORMAT_ID)
2318 values[n++] = primary_event_id(sub);
2320 size = n * sizeof(u64);
2322 if (copy_to_user(buf + ret, values, size)) {
2330 mutex_unlock(&ctx->mutex);
2335 static int perf_event_read_one(struct perf_event *event,
2336 u64 read_format, char __user *buf)
2338 u64 enabled, running;
2342 values[n++] = perf_event_read_value(event, &enabled, &running);
2343 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2344 values[n++] = enabled;
2345 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2346 values[n++] = running;
2347 if (read_format & PERF_FORMAT_ID)
2348 values[n++] = primary_event_id(event);
2350 if (copy_to_user(buf, values, n * sizeof(u64)))
2353 return n * sizeof(u64);
2357 * Read the performance event - simple non blocking version for now
2360 perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
2362 u64 read_format = event->attr.read_format;
2366 * Return end-of-file for a read on a event that is in
2367 * error state (i.e. because it was pinned but it couldn't be
2368 * scheduled on to the CPU at some point).
2370 if (event->state == PERF_EVENT_STATE_ERROR)
2373 if (count < perf_event_read_size(event))
2376 WARN_ON_ONCE(event->ctx->parent_ctx);
2377 if (read_format & PERF_FORMAT_GROUP)
2378 ret = perf_event_read_group(event, read_format, buf);
2380 ret = perf_event_read_one(event, read_format, buf);
2386 perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
2388 struct perf_event *event = file->private_data;
2390 return perf_read_hw(event, buf, count);
2393 static unsigned int perf_poll(struct file *file, poll_table *wait)
2395 struct perf_event *event = file->private_data;
2396 struct perf_buffer *buffer;
2397 unsigned int events = POLL_HUP;
2400 buffer = rcu_dereference(event->buffer);
2402 events = atomic_xchg(&buffer->poll, 0);
2405 poll_wait(file, &event->waitq, wait);
2410 static void perf_event_reset(struct perf_event *event)
2412 (void)perf_event_read(event);
2413 local64_set(&event->count, 0);
2414 perf_event_update_userpage(event);
2418 * Holding the top-level event's child_mutex means that any
2419 * descendant process that has inherited this event will block
2420 * in sync_child_event if it goes to exit, thus satisfying the
2421 * task existence requirements of perf_event_enable/disable.
2423 static void perf_event_for_each_child(struct perf_event *event,
2424 void (*func)(struct perf_event *))
2426 struct perf_event *child;
2428 WARN_ON_ONCE(event->ctx->parent_ctx);
2429 mutex_lock(&event->child_mutex);
2431 list_for_each_entry(child, &event->child_list, child_list)
2433 mutex_unlock(&event->child_mutex);
2436 static void perf_event_for_each(struct perf_event *event,
2437 void (*func)(struct perf_event *))
2439 struct perf_event_context *ctx = event->ctx;
2440 struct perf_event *sibling;
2442 WARN_ON_ONCE(ctx->parent_ctx);
2443 mutex_lock(&ctx->mutex);
2444 event = event->group_leader;
2446 perf_event_for_each_child(event, func);
2448 list_for_each_entry(sibling, &event->sibling_list, group_entry)
2449 perf_event_for_each_child(event, func);
2450 mutex_unlock(&ctx->mutex);
2453 static int perf_event_period(struct perf_event *event, u64 __user *arg)
2455 struct perf_event_context *ctx = event->ctx;
2460 if (!event->attr.sample_period)
2463 size = copy_from_user(&value, arg, sizeof(value));
2464 if (size != sizeof(value))
2470 raw_spin_lock_irq(&ctx->lock);
2471 if (event->attr.freq) {
2472 if (value > sysctl_perf_event_sample_rate) {
2477 event->attr.sample_freq = value;
2479 event->attr.sample_period = value;
2480 event->hw.sample_period = value;
2483 raw_spin_unlock_irq(&ctx->lock);
2488 static const struct file_operations perf_fops;
2490 static struct perf_event *perf_fget_light(int fd, int *fput_needed)
2494 file = fget_light(fd, fput_needed);
2496 return ERR_PTR(-EBADF);
2498 if (file->f_op != &perf_fops) {
2499 fput_light(file, *fput_needed);
2501 return ERR_PTR(-EBADF);
2504 return file->private_data;
2507 static int perf_event_set_output(struct perf_event *event,
2508 struct perf_event *output_event);
2509 static int perf_event_set_filter(struct perf_event *event, void __user *arg);
2511 static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2513 struct perf_event *event = file->private_data;
2514 void (*func)(struct perf_event *);
2518 case PERF_EVENT_IOC_ENABLE:
2519 func = perf_event_enable;
2521 case PERF_EVENT_IOC_DISABLE:
2522 func = perf_event_disable;
2524 case PERF_EVENT_IOC_RESET:
2525 func = perf_event_reset;
2528 case PERF_EVENT_IOC_REFRESH:
2529 return perf_event_refresh(event, arg);
2531 case PERF_EVENT_IOC_PERIOD:
2532 return perf_event_period(event, (u64 __user *)arg);
2534 case PERF_EVENT_IOC_SET_OUTPUT:
2536 struct perf_event *output_event = NULL;
2537 int fput_needed = 0;
2541 output_event = perf_fget_light(arg, &fput_needed);
2542 if (IS_ERR(output_event))
2543 return PTR_ERR(output_event);
2546 ret = perf_event_set_output(event, output_event);
2548 fput_light(output_event->filp, fput_needed);
2553 case PERF_EVENT_IOC_SET_FILTER:
2554 return perf_event_set_filter(event, (void __user *)arg);
2560 if (flags & PERF_IOC_FLAG_GROUP)
2561 perf_event_for_each(event, func);
2563 perf_event_for_each_child(event, func);
2568 int perf_event_task_enable(void)
2570 struct perf_event *event;
2572 mutex_lock(¤t->perf_event_mutex);
2573 list_for_each_entry(event, ¤t->perf_event_list, owner_entry)
2574 perf_event_for_each_child(event, perf_event_enable);
2575 mutex_unlock(¤t->perf_event_mutex);
2580 int perf_event_task_disable(void)
2582 struct perf_event *event;
2584 mutex_lock(¤t->perf_event_mutex);
2585 list_for_each_entry(event, ¤t->perf_event_list, owner_entry)
2586 perf_event_for_each_child(event, perf_event_disable);
2587 mutex_unlock(¤t->perf_event_mutex);
2592 #ifndef PERF_EVENT_INDEX_OFFSET
2593 # define PERF_EVENT_INDEX_OFFSET 0
2596 static int perf_event_index(struct perf_event *event)
2598 if (event->hw.state & PERF_HES_STOPPED)
2601 if (event->state != PERF_EVENT_STATE_ACTIVE)
2604 return event->hw.idx + 1 - PERF_EVENT_INDEX_OFFSET;
2608 * Callers need to ensure there can be no nesting of this function, otherwise
2609 * the seqlock logic goes bad. We can not serialize this because the arch
2610 * code calls this from NMI context.
2612 void perf_event_update_userpage(struct perf_event *event)
2614 struct perf_event_mmap_page *userpg;
2615 struct perf_buffer *buffer;
2618 buffer = rcu_dereference(event->buffer);
2622 userpg = buffer->user_page;
2625 * Disable preemption so as to not let the corresponding user-space
2626 * spin too long if we get preempted.
2631 userpg->index = perf_event_index(event);
2632 userpg->offset = perf_event_count(event);
2633 if (event->state == PERF_EVENT_STATE_ACTIVE)
2634 userpg->offset -= local64_read(&event->hw.prev_count);
2636 userpg->time_enabled = event->total_time_enabled +
2637 atomic64_read(&event->child_total_time_enabled);
2639 userpg->time_running = event->total_time_running +
2640 atomic64_read(&event->child_total_time_running);
2649 static unsigned long perf_data_size(struct perf_buffer *buffer);
2652 perf_buffer_init(struct perf_buffer *buffer, long watermark, int flags)
2654 long max_size = perf_data_size(buffer);
2657 buffer->watermark = min(max_size, watermark);
2659 if (!buffer->watermark)
2660 buffer->watermark = max_size / 2;
2662 if (flags & PERF_BUFFER_WRITABLE)
2663 buffer->writable = 1;
2665 atomic_set(&buffer->refcount, 1);
2668 #ifndef CONFIG_PERF_USE_VMALLOC
2671 * Back perf_mmap() with regular GFP_KERNEL-0 pages.
2674 static struct page *
2675 perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
2677 if (pgoff > buffer->nr_pages)
2681 return virt_to_page(buffer->user_page);
2683 return virt_to_page(buffer->data_pages[pgoff - 1]);
2686 static void *perf_mmap_alloc_page(int cpu)
2691 node = (cpu == -1) ? cpu : cpu_to_node(cpu);
2692 page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
2696 return page_address(page);
2699 static struct perf_buffer *
2700 perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
2702 struct perf_buffer *buffer;
2706 size = sizeof(struct perf_buffer);
2707 size += nr_pages * sizeof(void *);
2709 buffer = kzalloc(size, GFP_KERNEL);
2713 buffer->user_page = perf_mmap_alloc_page(cpu);
2714 if (!buffer->user_page)
2715 goto fail_user_page;
2717 for (i = 0; i < nr_pages; i++) {
2718 buffer->data_pages[i] = perf_mmap_alloc_page(cpu);
2719 if (!buffer->data_pages[i])
2720 goto fail_data_pages;
2723 buffer->nr_pages = nr_pages;
2725 perf_buffer_init(buffer, watermark, flags);
2730 for (i--; i >= 0; i--)
2731 free_page((unsigned long)buffer->data_pages[i]);
2733 free_page((unsigned long)buffer->user_page);
2742 static void perf_mmap_free_page(unsigned long addr)
2744 struct page *page = virt_to_page((void *)addr);
2746 page->mapping = NULL;
2750 static void perf_buffer_free(struct perf_buffer *buffer)
2754 perf_mmap_free_page((unsigned long)buffer->user_page);
2755 for (i = 0; i < buffer->nr_pages; i++)
2756 perf_mmap_free_page((unsigned long)buffer->data_pages[i]);
2760 static inline int page_order(struct perf_buffer *buffer)
2768 * Back perf_mmap() with vmalloc memory.
2770 * Required for architectures that have d-cache aliasing issues.
2773 static inline int page_order(struct perf_buffer *buffer)
2775 return buffer->page_order;
2778 static struct page *
2779 perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
2781 if (pgoff > (1UL << page_order(buffer)))
2784 return vmalloc_to_page((void *)buffer->user_page + pgoff * PAGE_SIZE);
2787 static void perf_mmap_unmark_page(void *addr)
2789 struct page *page = vmalloc_to_page(addr);
2791 page->mapping = NULL;
2794 static void perf_buffer_free_work(struct work_struct *work)
2796 struct perf_buffer *buffer;
2800 buffer = container_of(work, struct perf_buffer, work);
2801 nr = 1 << page_order(buffer);
2803 base = buffer->user_page;
2804 for (i = 0; i < nr + 1; i++)
2805 perf_mmap_unmark_page(base + (i * PAGE_SIZE));
2811 static void perf_buffer_free(struct perf_buffer *buffer)
2813 schedule_work(&buffer->work);
2816 static struct perf_buffer *
2817 perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
2819 struct perf_buffer *buffer;
2823 size = sizeof(struct perf_buffer);
2824 size += sizeof(void *);
2826 buffer = kzalloc(size, GFP_KERNEL);
2830 INIT_WORK(&buffer->work, perf_buffer_free_work);
2832 all_buf = vmalloc_user((nr_pages + 1) * PAGE_SIZE);
2836 buffer->user_page = all_buf;
2837 buffer->data_pages[0] = all_buf + PAGE_SIZE;
2838 buffer->page_order = ilog2(nr_pages);
2839 buffer->nr_pages = 1;
2841 perf_buffer_init(buffer, watermark, flags);
2854 static unsigned long perf_data_size(struct perf_buffer *buffer)
2856 return buffer->nr_pages << (PAGE_SHIFT + page_order(buffer));
2859 static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
2861 struct perf_event *event = vma->vm_file->private_data;
2862 struct perf_buffer *buffer;
2863 int ret = VM_FAULT_SIGBUS;
2865 if (vmf->flags & FAULT_FLAG_MKWRITE) {
2866 if (vmf->pgoff == 0)
2872 buffer = rcu_dereference(event->buffer);
2876 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
2879 vmf->page = perf_mmap_to_page(buffer, vmf->pgoff);
2883 get_page(vmf->page);
2884 vmf->page->mapping = vma->vm_file->f_mapping;
2885 vmf->page->index = vmf->pgoff;
2894 static void perf_buffer_free_rcu(struct rcu_head *rcu_head)
2896 struct perf_buffer *buffer;
2898 buffer = container_of(rcu_head, struct perf_buffer, rcu_head);
2899 perf_buffer_free(buffer);
2902 static struct perf_buffer *perf_buffer_get(struct perf_event *event)
2904 struct perf_buffer *buffer;
2907 buffer = rcu_dereference(event->buffer);
2909 if (!atomic_inc_not_zero(&buffer->refcount))
2917 static void perf_buffer_put(struct perf_buffer *buffer)
2919 if (!atomic_dec_and_test(&buffer->refcount))
2922 call_rcu(&buffer->rcu_head, perf_buffer_free_rcu);
2925 static void perf_mmap_open(struct vm_area_struct *vma)
2927 struct perf_event *event = vma->vm_file->private_data;
2929 atomic_inc(&event->mmap_count);
2932 static void perf_mmap_close(struct vm_area_struct *vma)
2934 struct perf_event *event = vma->vm_file->private_data;
2936 if (atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) {
2937 unsigned long size = perf_data_size(event->buffer);
2938 struct user_struct *user = event->mmap_user;
2939 struct perf_buffer *buffer = event->buffer;
2941 atomic_long_sub((size >> PAGE_SHIFT) + 1, &user->locked_vm);
2942 vma->vm_mm->locked_vm -= event->mmap_locked;
2943 rcu_assign_pointer(event->buffer, NULL);
2944 mutex_unlock(&event->mmap_mutex);
2946 perf_buffer_put(buffer);
2951 static const struct vm_operations_struct perf_mmap_vmops = {
2952 .open = perf_mmap_open,
2953 .close = perf_mmap_close,
2954 .fault = perf_mmap_fault,
2955 .page_mkwrite = perf_mmap_fault,
2958 static int perf_mmap(struct file *file, struct vm_area_struct *vma)
2960 struct perf_event *event = file->private_data;
2961 unsigned long user_locked, user_lock_limit;
2962 struct user_struct *user = current_user();
2963 unsigned long locked, lock_limit;
2964 struct perf_buffer *buffer;
2965 unsigned long vma_size;
2966 unsigned long nr_pages;
2967 long user_extra, extra;
2968 int ret = 0, flags = 0;
2971 * Don't allow mmap() of inherited per-task counters. This would
2972 * create a performance issue due to all children writing to the
2975 if (event->cpu == -1 && event->attr.inherit)
2978 if (!(vma->vm_flags & VM_SHARED))
2981 vma_size = vma->vm_end - vma->vm_start;
2982 nr_pages = (vma_size / PAGE_SIZE) - 1;
2985 * If we have buffer pages ensure they're a power-of-two number, so we
2986 * can do bitmasks instead of modulo.
2988 if (nr_pages != 0 && !is_power_of_2(nr_pages))
2991 if (vma_size != PAGE_SIZE * (1 + nr_pages))
2994 if (vma->vm_pgoff != 0)
2997 WARN_ON_ONCE(event->ctx->parent_ctx);
2998 mutex_lock(&event->mmap_mutex);
2999 if (event->buffer) {
3000 if (event->buffer->nr_pages == nr_pages)
3001 atomic_inc(&event->buffer->refcount);
3007 user_extra = nr_pages + 1;
3008 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
3011 * Increase the limit linearly with more CPUs:
3013 user_lock_limit *= num_online_cpus();
3015 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
3018 if (user_locked > user_lock_limit)
3019 extra = user_locked - user_lock_limit;
3021 lock_limit = rlimit(RLIMIT_MEMLOCK);
3022 lock_limit >>= PAGE_SHIFT;
3023 locked = vma->vm_mm->locked_vm + extra;
3025 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
3026 !capable(CAP_IPC_LOCK)) {
3031 WARN_ON(event->buffer);
3033 if (vma->vm_flags & VM_WRITE)
3034 flags |= PERF_BUFFER_WRITABLE;
3036 buffer = perf_buffer_alloc(nr_pages, event->attr.wakeup_watermark,
3042 rcu_assign_pointer(event->buffer, buffer);
3044 atomic_long_add(user_extra, &user->locked_vm);
3045 event->mmap_locked = extra;
3046 event->mmap_user = get_current_user();
3047 vma->vm_mm->locked_vm += event->mmap_locked;
3051 atomic_inc(&event->mmap_count);
3052 mutex_unlock(&event->mmap_mutex);
3054 vma->vm_flags |= VM_RESERVED;
3055 vma->vm_ops = &perf_mmap_vmops;
3060 static int perf_fasync(int fd, struct file *filp, int on)
3062 struct inode *inode = filp->f_path.dentry->d_inode;
3063 struct perf_event *event = filp->private_data;
3066 mutex_lock(&inode->i_mutex);
3067 retval = fasync_helper(fd, filp, on, &event->fasync);
3068 mutex_unlock(&inode->i_mutex);
3076 static const struct file_operations perf_fops = {
3077 .llseek = no_llseek,
3078 .release = perf_release,
3081 .unlocked_ioctl = perf_ioctl,
3082 .compat_ioctl = perf_ioctl,
3084 .fasync = perf_fasync,
3090 * If there's data, ensure we set the poll() state and publish everything
3091 * to user-space before waking everybody up.
3094 void perf_event_wakeup(struct perf_event *event)
3096 wake_up_all(&event->waitq);
3098 if (event->pending_kill) {
3099 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
3100 event->pending_kill = 0;
3107 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
3109 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
3110 * single linked list and use cmpxchg() to add entries lockless.
3113 static void perf_pending_event(struct perf_pending_entry *entry)
3115 struct perf_event *event = container_of(entry,
3116 struct perf_event, pending);
3118 if (event->pending_disable) {
3119 event->pending_disable = 0;
3120 __perf_event_disable(event);
3123 if (event->pending_wakeup) {
3124 event->pending_wakeup = 0;
3125 perf_event_wakeup(event);
3129 #define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
3131 static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
3135 static void perf_pending_queue(struct perf_pending_entry *entry,
3136 void (*func)(struct perf_pending_entry *))
3138 struct perf_pending_entry **head;
3140 if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
3145 head = &get_cpu_var(perf_pending_head);
3148 entry->next = *head;
3149 } while (cmpxchg(head, entry->next, entry) != entry->next);
3151 set_perf_event_pending();
3153 put_cpu_var(perf_pending_head);
3156 static int __perf_pending_run(void)
3158 struct perf_pending_entry *list;
3161 list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
3162 while (list != PENDING_TAIL) {
3163 void (*func)(struct perf_pending_entry *);
3164 struct perf_pending_entry *entry = list;
3171 * Ensure we observe the unqueue before we issue the wakeup,
3172 * so that we won't be waiting forever.
3173 * -- see perf_not_pending().
3184 static inline int perf_not_pending(struct perf_event *event)
3187 * If we flush on whatever cpu we run, there is a chance we don't
3191 __perf_pending_run();
3195 * Ensure we see the proper queue state before going to sleep
3196 * so that we do not miss the wakeup. -- see perf_pending_handle()
3199 return event->pending.next == NULL;
3202 static void perf_pending_sync(struct perf_event *event)
3204 wait_event(event->waitq, perf_not_pending(event));
3207 void perf_event_do_pending(void)
3209 __perf_pending_run();
3213 * We assume there is only KVM supporting the callbacks.
3214 * Later on, we might change it to a list if there is
3215 * another virtualization implementation supporting the callbacks.
3217 struct perf_guest_info_callbacks *perf_guest_cbs;
3219 int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3221 perf_guest_cbs = cbs;
3224 EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
3226 int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3228 perf_guest_cbs = NULL;
3231 EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
3236 static bool perf_output_space(struct perf_buffer *buffer, unsigned long tail,
3237 unsigned long offset, unsigned long head)
3241 if (!buffer->writable)
3244 mask = perf_data_size(buffer) - 1;
3246 offset = (offset - tail) & mask;
3247 head = (head - tail) & mask;
3249 if ((int)(head - offset) < 0)
3255 static void perf_output_wakeup(struct perf_output_handle *handle)
3257 atomic_set(&handle->buffer->poll, POLL_IN);
3260 handle->event->pending_wakeup = 1;
3261 perf_pending_queue(&handle->event->pending,
3262 perf_pending_event);
3264 perf_event_wakeup(handle->event);
3268 * We need to ensure a later event_id doesn't publish a head when a former
3269 * event isn't done writing. However since we need to deal with NMIs we
3270 * cannot fully serialize things.
3272 * We only publish the head (and generate a wakeup) when the outer-most
3275 static void perf_output_get_handle(struct perf_output_handle *handle)
3277 struct perf_buffer *buffer = handle->buffer;
3280 local_inc(&buffer->nest);
3281 handle->wakeup = local_read(&buffer->wakeup);
3284 static void perf_output_put_handle(struct perf_output_handle *handle)
3286 struct perf_buffer *buffer = handle->buffer;
3290 head = local_read(&buffer->head);
3293 * IRQ/NMI can happen here, which means we can miss a head update.
3296 if (!local_dec_and_test(&buffer->nest))
3300 * Publish the known good head. Rely on the full barrier implied
3301 * by atomic_dec_and_test() order the buffer->head read and this
3304 buffer->user_page->data_head = head;
3307 * Now check if we missed an update, rely on the (compiler)
3308 * barrier in atomic_dec_and_test() to re-read buffer->head.
3310 if (unlikely(head != local_read(&buffer->head))) {
3311 local_inc(&buffer->nest);
3315 if (handle->wakeup != local_read(&buffer->wakeup))
3316 perf_output_wakeup(handle);
3322 __always_inline void perf_output_copy(struct perf_output_handle *handle,
3323 const void *buf, unsigned int len)
3326 unsigned long size = min_t(unsigned long, handle->size, len);
3328 memcpy(handle->addr, buf, size);
3331 handle->addr += size;
3333 handle->size -= size;
3334 if (!handle->size) {
3335 struct perf_buffer *buffer = handle->buffer;
3338 handle->page &= buffer->nr_pages - 1;
3339 handle->addr = buffer->data_pages[handle->page];
3340 handle->size = PAGE_SIZE << page_order(buffer);
3345 int perf_output_begin(struct perf_output_handle *handle,
3346 struct perf_event *event, unsigned int size,
3347 int nmi, int sample)
3349 struct perf_buffer *buffer;
3350 unsigned long tail, offset, head;
3353 struct perf_event_header header;
3360 * For inherited events we send all the output towards the parent.
3363 event = event->parent;
3365 buffer = rcu_dereference(event->buffer);
3369 handle->buffer = buffer;
3370 handle->event = event;
3372 handle->sample = sample;
3374 if (!buffer->nr_pages)
3377 have_lost = local_read(&buffer->lost);
3379 size += sizeof(lost_event);
3381 perf_output_get_handle(handle);
3385 * Userspace could choose to issue a mb() before updating the
3386 * tail pointer. So that all reads will be completed before the
3389 tail = ACCESS_ONCE(buffer->user_page->data_tail);
3391 offset = head = local_read(&buffer->head);
3393 if (unlikely(!perf_output_space(buffer, tail, offset, head)))
3395 } while (local_cmpxchg(&buffer->head, offset, head) != offset);
3397 if (head - local_read(&buffer->wakeup) > buffer->watermark)
3398 local_add(buffer->watermark, &buffer->wakeup);
3400 handle->page = offset >> (PAGE_SHIFT + page_order(buffer));
3401 handle->page &= buffer->nr_pages - 1;
3402 handle->size = offset & ((PAGE_SIZE << page_order(buffer)) - 1);
3403 handle->addr = buffer->data_pages[handle->page];
3404 handle->addr += handle->size;
3405 handle->size = (PAGE_SIZE << page_order(buffer)) - handle->size;
3408 lost_event.header.type = PERF_RECORD_LOST;
3409 lost_event.header.misc = 0;
3410 lost_event.header.size = sizeof(lost_event);
3411 lost_event.id = event->id;
3412 lost_event.lost = local_xchg(&buffer->lost, 0);
3414 perf_output_put(handle, lost_event);
3420 local_inc(&buffer->lost);
3421 perf_output_put_handle(handle);
3428 void perf_output_end(struct perf_output_handle *handle)
3430 struct perf_event *event = handle->event;
3431 struct perf_buffer *buffer = handle->buffer;
3433 int wakeup_events = event->attr.wakeup_events;
3435 if (handle->sample && wakeup_events) {
3436 int events = local_inc_return(&buffer->events);
3437 if (events >= wakeup_events) {
3438 local_sub(wakeup_events, &buffer->events);
3439 local_inc(&buffer->wakeup);
3443 perf_output_put_handle(handle);
3447 static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
3450 * only top level events have the pid namespace they were created in
3453 event = event->parent;
3455 return task_tgid_nr_ns(p, event->ns);
3458 static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
3461 * only top level events have the pid namespace they were created in
3464 event = event->parent;
3466 return task_pid_nr_ns(p, event->ns);
3469 static void perf_output_read_one(struct perf_output_handle *handle,
3470 struct perf_event *event)
3472 u64 read_format = event->attr.read_format;
3476 values[n++] = perf_event_count(event);
3477 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
3478 values[n++] = event->total_time_enabled +
3479 atomic64_read(&event->child_total_time_enabled);
3481 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
3482 values[n++] = event->total_time_running +
3483 atomic64_read(&event->child_total_time_running);
3485 if (read_format & PERF_FORMAT_ID)
3486 values[n++] = primary_event_id(event);
3488 perf_output_copy(handle, values, n * sizeof(u64));
3492 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
3494 static void perf_output_read_group(struct perf_output_handle *handle,
3495 struct perf_event *event)
3497 struct perf_event *leader = event->group_leader, *sub;
3498 u64 read_format = event->attr.read_format;
3502 values[n++] = 1 + leader->nr_siblings;
3504 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3505 values[n++] = leader->total_time_enabled;
3507 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3508 values[n++] = leader->total_time_running;
3510 if (leader != event)
3511 leader->pmu->read(leader);
3513 values[n++] = perf_event_count(leader);
3514 if (read_format & PERF_FORMAT_ID)
3515 values[n++] = primary_event_id(leader);
3517 perf_output_copy(handle, values, n * sizeof(u64));
3519 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
3523 sub->pmu->read(sub);
3525 values[n++] = perf_event_count(sub);
3526 if (read_format & PERF_FORMAT_ID)
3527 values[n++] = primary_event_id(sub);
3529 perf_output_copy(handle, values, n * sizeof(u64));
3533 static void perf_output_read(struct perf_output_handle *handle,
3534 struct perf_event *event)
3536 if (event->attr.read_format & PERF_FORMAT_GROUP)
3537 perf_output_read_group(handle, event);
3539 perf_output_read_one(handle, event);
3542 void perf_output_sample(struct perf_output_handle *handle,
3543 struct perf_event_header *header,
3544 struct perf_sample_data *data,
3545 struct perf_event *event)
3547 u64 sample_type = data->type;
3549 perf_output_put(handle, *header);
3551 if (sample_type & PERF_SAMPLE_IP)
3552 perf_output_put(handle, data->ip);
3554 if (sample_type & PERF_SAMPLE_TID)
3555 perf_output_put(handle, data->tid_entry);
3557 if (sample_type & PERF_SAMPLE_TIME)
3558 perf_output_put(handle, data->time);
3560 if (sample_type & PERF_SAMPLE_ADDR)
3561 perf_output_put(handle, data->addr);
3563 if (sample_type & PERF_SAMPLE_ID)
3564 perf_output_put(handle, data->id);
3566 if (sample_type & PERF_SAMPLE_STREAM_ID)
3567 perf_output_put(handle, data->stream_id);
3569 if (sample_type & PERF_SAMPLE_CPU)
3570 perf_output_put(handle, data->cpu_entry);
3572 if (sample_type & PERF_SAMPLE_PERIOD)
3573 perf_output_put(handle, data->period);
3575 if (sample_type & PERF_SAMPLE_READ)
3576 perf_output_read(handle, event);
3578 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3579 if (data->callchain) {
3582 if (data->callchain)
3583 size += data->callchain->nr;
3585 size *= sizeof(u64);
3587 perf_output_copy(handle, data->callchain, size);
3590 perf_output_put(handle, nr);
3594 if (sample_type & PERF_SAMPLE_RAW) {
3596 perf_output_put(handle, data->raw->size);
3597 perf_output_copy(handle, data->raw->data,
3604 .size = sizeof(u32),
3607 perf_output_put(handle, raw);
3612 void perf_prepare_sample(struct perf_event_header *header,
3613 struct perf_sample_data *data,
3614 struct perf_event *event,
3615 struct pt_regs *regs)
3617 u64 sample_type = event->attr.sample_type;
3619 data->type = sample_type;
3621 header->type = PERF_RECORD_SAMPLE;
3622 header->size = sizeof(*header);
3625 header->misc |= perf_misc_flags(regs);
3627 if (sample_type & PERF_SAMPLE_IP) {
3628 data->ip = perf_instruction_pointer(regs);
3630 header->size += sizeof(data->ip);
3633 if (sample_type & PERF_SAMPLE_TID) {
3634 /* namespace issues */
3635 data->tid_entry.pid = perf_event_pid(event, current);
3636 data->tid_entry.tid = perf_event_tid(event, current);
3638 header->size += sizeof(data->tid_entry);
3641 if (sample_type & PERF_SAMPLE_TIME) {
3642 data->time = perf_clock();
3644 header->size += sizeof(data->time);
3647 if (sample_type & PERF_SAMPLE_ADDR)
3648 header->size += sizeof(data->addr);
3650 if (sample_type & PERF_SAMPLE_ID) {
3651 data->id = primary_event_id(event);
3653 header->size += sizeof(data->id);
3656 if (sample_type & PERF_SAMPLE_STREAM_ID) {
3657 data->stream_id = event->id;
3659 header->size += sizeof(data->stream_id);
3662 if (sample_type & PERF_SAMPLE_CPU) {
3663 data->cpu_entry.cpu = raw_smp_processor_id();
3664 data->cpu_entry.reserved = 0;
3666 header->size += sizeof(data->cpu_entry);
3669 if (sample_type & PERF_SAMPLE_PERIOD)
3670 header->size += sizeof(data->period);
3672 if (sample_type & PERF_SAMPLE_READ)
3673 header->size += perf_event_read_size(event);
3675 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3678 data->callchain = perf_callchain(regs);
3680 if (data->callchain)
3681 size += data->callchain->nr;
3683 header->size += size * sizeof(u64);
3686 if (sample_type & PERF_SAMPLE_RAW) {
3687 int size = sizeof(u32);
3690 size += data->raw->size;
3692 size += sizeof(u32);
3694 WARN_ON_ONCE(size & (sizeof(u64)-1));
3695 header->size += size;
3699 static void perf_event_output(struct perf_event *event, int nmi,
3700 struct perf_sample_data *data,
3701 struct pt_regs *regs)
3703 struct perf_output_handle handle;
3704 struct perf_event_header header;
3706 /* protect the callchain buffers */
3709 perf_prepare_sample(&header, data, event, regs);
3711 if (perf_output_begin(&handle, event, header.size, nmi, 1))
3714 perf_output_sample(&handle, &header, data, event);
3716 perf_output_end(&handle);
3726 struct perf_read_event {
3727 struct perf_event_header header;
3734 perf_event_read_event(struct perf_event *event,
3735 struct task_struct *task)
3737 struct perf_output_handle handle;
3738 struct perf_read_event read_event = {
3740 .type = PERF_RECORD_READ,
3742 .size = sizeof(read_event) + perf_event_read_size(event),
3744 .pid = perf_event_pid(event, task),
3745 .tid = perf_event_tid(event, task),
3749 ret = perf_output_begin(&handle, event, read_event.header.size, 0, 0);
3753 perf_output_put(&handle, read_event);
3754 perf_output_read(&handle, event);
3756 perf_output_end(&handle);
3760 * task tracking -- fork/exit
3762 * enabled by: attr.comm | attr.mmap | attr.mmap_data | attr.task
3765 struct perf_task_event {
3766 struct task_struct *task;
3767 struct perf_event_context *task_ctx;
3770 struct perf_event_header header;
3780 static void perf_event_task_output(struct perf_event *event,
3781 struct perf_task_event *task_event)
3783 struct perf_output_handle handle;
3784 struct task_struct *task = task_event->task;
3787 size = task_event->event_id.header.size;
3788 ret = perf_output_begin(&handle, event, size, 0, 0);
3793 task_event->event_id.pid = perf_event_pid(event, task);
3794 task_event->event_id.ppid = perf_event_pid(event, current);
3796 task_event->event_id.tid = perf_event_tid(event, task);
3797 task_event->event_id.ptid = perf_event_tid(event, current);
3799 perf_output_put(&handle, task_event->event_id);
3801 perf_output_end(&handle);
3804 static int perf_event_task_match(struct perf_event *event)
3806 if (event->state < PERF_EVENT_STATE_INACTIVE)
3809 if (event->cpu != -1 && event->cpu != smp_processor_id())
3812 if (event->attr.comm || event->attr.mmap ||
3813 event->attr.mmap_data || event->attr.task)
3819 static void perf_event_task_ctx(struct perf_event_context *ctx,
3820 struct perf_task_event *task_event)
3822 struct perf_event *event;
3824 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3825 if (perf_event_task_match(event))
3826 perf_event_task_output(event, task_event);
3830 static void perf_event_task_event(struct perf_task_event *task_event)
3832 struct perf_cpu_context *cpuctx;
3833 struct perf_event_context *ctx;
3838 list_for_each_entry_rcu(pmu, &pmus, entry) {
3839 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
3840 perf_event_task_ctx(&cpuctx->ctx, task_event);
3842 ctx = task_event->task_ctx;
3844 ctxn = pmu->task_ctx_nr;
3847 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
3850 perf_event_task_ctx(ctx, task_event);
3852 put_cpu_ptr(pmu->pmu_cpu_context);
3857 static void perf_event_task(struct task_struct *task,
3858 struct perf_event_context *task_ctx,
3861 struct perf_task_event task_event;
3863 if (!atomic_read(&nr_comm_events) &&
3864 !atomic_read(&nr_mmap_events) &&
3865 !atomic_read(&nr_task_events))
3868 task_event = (struct perf_task_event){
3870 .task_ctx = task_ctx,
3873 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
3875 .size = sizeof(task_event.event_id),
3881 .time = perf_clock(),
3885 perf_event_task_event(&task_event);
3888 void perf_event_fork(struct task_struct *task)
3890 perf_event_task(task, NULL, 1);
3897 struct perf_comm_event {
3898 struct task_struct *task;
3903 struct perf_event_header header;
3910 static void perf_event_comm_output(struct perf_event *event,
3911 struct perf_comm_event *comm_event)
3913 struct perf_output_handle handle;
3914 int size = comm_event->event_id.header.size;
3915 int ret = perf_output_begin(&handle, event, size, 0, 0);
3920 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
3921 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
3923 perf_output_put(&handle, comm_event->event_id);
3924 perf_output_copy(&handle, comm_event->comm,
3925 comm_event->comm_size);
3926 perf_output_end(&handle);
3929 static int perf_event_comm_match(struct perf_event *event)
3931 if (event->state < PERF_EVENT_STATE_INACTIVE)
3934 if (event->cpu != -1 && event->cpu != smp_processor_id())
3937 if (event->attr.comm)
3943 static void perf_event_comm_ctx(struct perf_event_context *ctx,
3944 struct perf_comm_event *comm_event)
3946 struct perf_event *event;
3948 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3949 if (perf_event_comm_match(event))
3950 perf_event_comm_output(event, comm_event);
3954 static void perf_event_comm_event(struct perf_comm_event *comm_event)
3956 struct perf_cpu_context *cpuctx;
3957 struct perf_event_context *ctx;
3958 char comm[TASK_COMM_LEN];
3963 memset(comm, 0, sizeof(comm));
3964 strlcpy(comm, comm_event->task->comm, sizeof(comm));
3965 size = ALIGN(strlen(comm)+1, sizeof(u64));
3967 comm_event->comm = comm;
3968 comm_event->comm_size = size;
3970 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
3973 list_for_each_entry_rcu(pmu, &pmus, entry) {
3974 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
3975 perf_event_comm_ctx(&cpuctx->ctx, comm_event);
3977 ctxn = pmu->task_ctx_nr;
3981 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
3983 perf_event_comm_ctx(ctx, comm_event);
3985 put_cpu_ptr(pmu->pmu_cpu_context);
3990 void perf_event_comm(struct task_struct *task)
3992 struct perf_comm_event comm_event;
3993 struct perf_event_context *ctx;
3996 for_each_task_context_nr(ctxn) {
3997 ctx = task->perf_event_ctxp[ctxn];
4001 perf_event_enable_on_exec(ctx);
4004 if (!atomic_read(&nr_comm_events))
4007 comm_event = (struct perf_comm_event){
4013 .type = PERF_RECORD_COMM,
4022 perf_event_comm_event(&comm_event);
4029 struct perf_mmap_event {
4030 struct vm_area_struct *vma;
4032 const char *file_name;
4036 struct perf_event_header header;
4046 static void perf_event_mmap_output(struct perf_event *event,
4047 struct perf_mmap_event *mmap_event)
4049 struct perf_output_handle handle;
4050 int size = mmap_event->event_id.header.size;
4051 int ret = perf_output_begin(&handle, event, size, 0, 0);
4056 mmap_event->event_id.pid = perf_event_pid(event, current);
4057 mmap_event->event_id.tid = perf_event_tid(event, current);
4059 perf_output_put(&handle, mmap_event->event_id);
4060 perf_output_copy(&handle, mmap_event->file_name,
4061 mmap_event->file_size);
4062 perf_output_end(&handle);
4065 static int perf_event_mmap_match(struct perf_event *event,
4066 struct perf_mmap_event *mmap_event,
4069 if (event->state < PERF_EVENT_STATE_INACTIVE)
4072 if (event->cpu != -1 && event->cpu != smp_processor_id())
4075 if ((!executable && event->attr.mmap_data) ||
4076 (executable && event->attr.mmap))
4082 static void perf_event_mmap_ctx(struct perf_event_context *ctx,
4083 struct perf_mmap_event *mmap_event,
4086 struct perf_event *event;
4088 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4089 if (perf_event_mmap_match(event, mmap_event, executable))
4090 perf_event_mmap_output(event, mmap_event);
4094 static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
4096 struct perf_cpu_context *cpuctx;
4097 struct perf_event_context *ctx;
4098 struct vm_area_struct *vma = mmap_event->vma;
4099 struct file *file = vma->vm_file;
4107 memset(tmp, 0, sizeof(tmp));
4111 * d_path works from the end of the buffer backwards, so we
4112 * need to add enough zero bytes after the string to handle
4113 * the 64bit alignment we do later.
4115 buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
4117 name = strncpy(tmp, "//enomem", sizeof(tmp));
4120 name = d_path(&file->f_path, buf, PATH_MAX);
4122 name = strncpy(tmp, "//toolong", sizeof(tmp));
4126 if (arch_vma_name(mmap_event->vma)) {
4127 name = strncpy(tmp, arch_vma_name(mmap_event->vma),
4133 name = strncpy(tmp, "[vdso]", sizeof(tmp));
4135 } else if (vma->vm_start <= vma->vm_mm->start_brk &&
4136 vma->vm_end >= vma->vm_mm->brk) {
4137 name = strncpy(tmp, "[heap]", sizeof(tmp));
4139 } else if (vma->vm_start <= vma->vm_mm->start_stack &&
4140 vma->vm_end >= vma->vm_mm->start_stack) {
4141 name = strncpy(tmp, "[stack]", sizeof(tmp));
4145 name = strncpy(tmp, "//anon", sizeof(tmp));
4150 size = ALIGN(strlen(name)+1, sizeof(u64));
4152 mmap_event->file_name = name;
4153 mmap_event->file_size = size;
4155 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
4158 list_for_each_entry_rcu(pmu, &pmus, entry) {
4159 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
4160 perf_event_mmap_ctx(&cpuctx->ctx, mmap_event,
4161 vma->vm_flags & VM_EXEC);
4163 ctxn = pmu->task_ctx_nr;
4167 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4169 perf_event_mmap_ctx(ctx, mmap_event,
4170 vma->vm_flags & VM_EXEC);
4173 put_cpu_ptr(pmu->pmu_cpu_context);
4180 void perf_event_mmap(struct vm_area_struct *vma)
4182 struct perf_mmap_event mmap_event;
4184 if (!atomic_read(&nr_mmap_events))
4187 mmap_event = (struct perf_mmap_event){
4193 .type = PERF_RECORD_MMAP,
4194 .misc = PERF_RECORD_MISC_USER,
4199 .start = vma->vm_start,
4200 .len = vma->vm_end - vma->vm_start,
4201 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
4205 perf_event_mmap_event(&mmap_event);
4209 * IRQ throttle logging
4212 static void perf_log_throttle(struct perf_event *event, int enable)
4214 struct perf_output_handle handle;
4218 struct perf_event_header header;
4222 } throttle_event = {
4224 .type = PERF_RECORD_THROTTLE,
4226 .size = sizeof(throttle_event),
4228 .time = perf_clock(),
4229 .id = primary_event_id(event),
4230 .stream_id = event->id,
4234 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
4236 ret = perf_output_begin(&handle, event, sizeof(throttle_event), 1, 0);
4240 perf_output_put(&handle, throttle_event);
4241 perf_output_end(&handle);
4245 * Generic event overflow handling, sampling.
4248 static int __perf_event_overflow(struct perf_event *event, int nmi,
4249 int throttle, struct perf_sample_data *data,
4250 struct pt_regs *regs)
4252 int events = atomic_read(&event->event_limit);
4253 struct hw_perf_event *hwc = &event->hw;
4259 if (hwc->interrupts != MAX_INTERRUPTS) {
4261 if (HZ * hwc->interrupts >
4262 (u64)sysctl_perf_event_sample_rate) {
4263 hwc->interrupts = MAX_INTERRUPTS;
4264 perf_log_throttle(event, 0);
4269 * Keep re-disabling events even though on the previous
4270 * pass we disabled it - just in case we raced with a
4271 * sched-in and the event got enabled again:
4277 if (event->attr.freq) {
4278 u64 now = perf_clock();
4279 s64 delta = now - hwc->freq_time_stamp;
4281 hwc->freq_time_stamp = now;
4283 if (delta > 0 && delta < 2*TICK_NSEC)
4284 perf_adjust_period(event, delta, hwc->last_period);
4288 * XXX event_limit might not quite work as expected on inherited
4292 event->pending_kill = POLL_IN;
4293 if (events && atomic_dec_and_test(&event->event_limit)) {
4295 event->pending_kill = POLL_HUP;
4297 event->pending_disable = 1;
4298 perf_pending_queue(&event->pending,
4299 perf_pending_event);
4301 perf_event_disable(event);
4304 if (event->overflow_handler)
4305 event->overflow_handler(event, nmi, data, regs);
4307 perf_event_output(event, nmi, data, regs);
4312 int perf_event_overflow(struct perf_event *event, int nmi,
4313 struct perf_sample_data *data,
4314 struct pt_regs *regs)
4316 return __perf_event_overflow(event, nmi, 1, data, regs);
4320 * Generic software event infrastructure
4323 struct swevent_htable {
4324 struct swevent_hlist *swevent_hlist;
4325 struct mutex hlist_mutex;
4328 /* Recursion avoidance in each contexts */
4329 int recursion[PERF_NR_CONTEXTS];
4332 static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
4335 * We directly increment event->count and keep a second value in
4336 * event->hw.period_left to count intervals. This period event
4337 * is kept in the range [-sample_period, 0] so that we can use the
4341 static u64 perf_swevent_set_period(struct perf_event *event)
4343 struct hw_perf_event *hwc = &event->hw;
4344 u64 period = hwc->last_period;
4348 hwc->last_period = hwc->sample_period;
4351 old = val = local64_read(&hwc->period_left);
4355 nr = div64_u64(period + val, period);
4356 offset = nr * period;
4358 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
4364 static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
4365 int nmi, struct perf_sample_data *data,
4366 struct pt_regs *regs)
4368 struct hw_perf_event *hwc = &event->hw;
4371 data->period = event->hw.last_period;
4373 overflow = perf_swevent_set_period(event);
4375 if (hwc->interrupts == MAX_INTERRUPTS)
4378 for (; overflow; overflow--) {
4379 if (__perf_event_overflow(event, nmi, throttle,
4382 * We inhibit the overflow from happening when
4383 * hwc->interrupts == MAX_INTERRUPTS.
4391 static void perf_swevent_event(struct perf_event *event, u64 nr,
4392 int nmi, struct perf_sample_data *data,
4393 struct pt_regs *regs)
4395 struct hw_perf_event *hwc = &event->hw;
4397 local64_add(nr, &event->count);
4402 if (!hwc->sample_period)
4405 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
4406 return perf_swevent_overflow(event, 1, nmi, data, regs);
4408 if (local64_add_negative(nr, &hwc->period_left))
4411 perf_swevent_overflow(event, 0, nmi, data, regs);
4414 static int perf_exclude_event(struct perf_event *event,
4415 struct pt_regs *regs)
4417 if (event->hw.state & PERF_HES_STOPPED)
4421 if (event->attr.exclude_user && user_mode(regs))
4424 if (event->attr.exclude_kernel && !user_mode(regs))
4431 static int perf_swevent_match(struct perf_event *event,
4432 enum perf_type_id type,
4434 struct perf_sample_data *data,
4435 struct pt_regs *regs)
4437 if (event->attr.type != type)
4440 if (event->attr.config != event_id)
4443 if (perf_exclude_event(event, regs))
4449 static inline u64 swevent_hash(u64 type, u32 event_id)
4451 u64 val = event_id | (type << 32);
4453 return hash_64(val, SWEVENT_HLIST_BITS);
4456 static inline struct hlist_head *
4457 __find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
4459 u64 hash = swevent_hash(type, event_id);
4461 return &hlist->heads[hash];
4464 /* For the read side: events when they trigger */
4465 static inline struct hlist_head *
4466 find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
4468 struct swevent_hlist *hlist;
4470 hlist = rcu_dereference(swhash->swevent_hlist);
4474 return __find_swevent_head(hlist, type, event_id);
4477 /* For the event head insertion and removal in the hlist */
4478 static inline struct hlist_head *
4479 find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
4481 struct swevent_hlist *hlist;
4482 u32 event_id = event->attr.config;
4483 u64 type = event->attr.type;
4486 * Event scheduling is always serialized against hlist allocation
4487 * and release. Which makes the protected version suitable here.
4488 * The context lock guarantees that.
4490 hlist = rcu_dereference_protected(swhash->swevent_hlist,
4491 lockdep_is_held(&event->ctx->lock));
4495 return __find_swevent_head(hlist, type, event_id);
4498 static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
4500 struct perf_sample_data *data,
4501 struct pt_regs *regs)
4503 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
4504 struct perf_event *event;
4505 struct hlist_node *node;
4506 struct hlist_head *head;
4509 head = find_swevent_head_rcu(swhash, type, event_id);
4513 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
4514 if (perf_swevent_match(event, type, event_id, data, regs))
4515 perf_swevent_event(event, nr, nmi, data, regs);
4521 int perf_swevent_get_recursion_context(void)
4523 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
4525 return get_recursion_context(swhash->recursion);
4527 EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
4529 void inline perf_swevent_put_recursion_context(int rctx)
4531 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
4533 put_recursion_context(swhash->recursion, rctx);
4536 void __perf_sw_event(u32 event_id, u64 nr, int nmi,
4537 struct pt_regs *regs, u64 addr)
4539 struct perf_sample_data data;
4542 preempt_disable_notrace();
4543 rctx = perf_swevent_get_recursion_context();
4547 perf_sample_data_init(&data, addr);
4549 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, nmi, &data, regs);
4551 perf_swevent_put_recursion_context(rctx);
4552 preempt_enable_notrace();
4555 static void perf_swevent_read(struct perf_event *event)
4559 static int perf_swevent_add(struct perf_event *event, int flags)
4561 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
4562 struct hw_perf_event *hwc = &event->hw;
4563 struct hlist_head *head;
4565 if (hwc->sample_period) {
4566 hwc->last_period = hwc->sample_period;
4567 perf_swevent_set_period(event);
4570 hwc->state = !(flags & PERF_EF_START);
4572 head = find_swevent_head(swhash, event);
4573 if (WARN_ON_ONCE(!head))
4576 hlist_add_head_rcu(&event->hlist_entry, head);
4581 static void perf_swevent_del(struct perf_event *event, int flags)
4583 hlist_del_rcu(&event->hlist_entry);
4586 static void perf_swevent_start(struct perf_event *event, int flags)
4588 event->hw.state = 0;
4591 static void perf_swevent_stop(struct perf_event *event, int flags)
4593 event->hw.state = PERF_HES_STOPPED;
4596 /* Deref the hlist from the update side */
4597 static inline struct swevent_hlist *
4598 swevent_hlist_deref(struct swevent_htable *swhash)
4600 return rcu_dereference_protected(swhash->swevent_hlist,
4601 lockdep_is_held(&swhash->hlist_mutex));
4604 static void swevent_hlist_release_rcu(struct rcu_head *rcu_head)
4606 struct swevent_hlist *hlist;
4608 hlist = container_of(rcu_head, struct swevent_hlist, rcu_head);
4612 static void swevent_hlist_release(struct swevent_htable *swhash)
4614 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
4619 rcu_assign_pointer(swhash->swevent_hlist, NULL);
4620 call_rcu(&hlist->rcu_head, swevent_hlist_release_rcu);
4623 static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
4625 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
4627 mutex_lock(&swhash->hlist_mutex);
4629 if (!--swhash->hlist_refcount)
4630 swevent_hlist_release(swhash);
4632 mutex_unlock(&swhash->hlist_mutex);
4635 static void swevent_hlist_put(struct perf_event *event)
4639 if (event->cpu != -1) {
4640 swevent_hlist_put_cpu(event, event->cpu);
4644 for_each_possible_cpu(cpu)
4645 swevent_hlist_put_cpu(event, cpu);
4648 static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
4650 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
4653 mutex_lock(&swhash->hlist_mutex);
4655 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
4656 struct swevent_hlist *hlist;
4658 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
4663 rcu_assign_pointer(swhash->swevent_hlist, hlist);
4665 swhash->hlist_refcount++;
4667 mutex_unlock(&swhash->hlist_mutex);
4672 static int swevent_hlist_get(struct perf_event *event)
4675 int cpu, failed_cpu;
4677 if (event->cpu != -1)
4678 return swevent_hlist_get_cpu(event, event->cpu);
4681 for_each_possible_cpu(cpu) {
4682 err = swevent_hlist_get_cpu(event, cpu);
4692 for_each_possible_cpu(cpu) {
4693 if (cpu == failed_cpu)
4695 swevent_hlist_put_cpu(event, cpu);
4702 atomic_t perf_swevent_enabled[PERF_COUNT_SW_MAX];
4704 static void sw_perf_event_destroy(struct perf_event *event)
4706 u64 event_id = event->attr.config;
4708 WARN_ON(event->parent);
4710 atomic_dec(&perf_swevent_enabled[event_id]);
4711 swevent_hlist_put(event);
4714 static int perf_swevent_init(struct perf_event *event)
4716 int event_id = event->attr.config;
4718 if (event->attr.type != PERF_TYPE_SOFTWARE)
4722 case PERF_COUNT_SW_CPU_CLOCK:
4723 case PERF_COUNT_SW_TASK_CLOCK:
4730 if (event_id > PERF_COUNT_SW_MAX)
4733 if (!event->parent) {
4736 err = swevent_hlist_get(event);
4740 atomic_inc(&perf_swevent_enabled[event_id]);
4741 event->destroy = sw_perf_event_destroy;
4747 static struct pmu perf_swevent = {
4748 .task_ctx_nr = perf_sw_context,
4750 .event_init = perf_swevent_init,
4751 .add = perf_swevent_add,
4752 .del = perf_swevent_del,
4753 .start = perf_swevent_start,
4754 .stop = perf_swevent_stop,
4755 .read = perf_swevent_read,
4758 #ifdef CONFIG_EVENT_TRACING
4760 static int perf_tp_filter_match(struct perf_event *event,
4761 struct perf_sample_data *data)
4763 void *record = data->raw->data;
4765 if (likely(!event->filter) || filter_match_preds(event->filter, record))
4770 static int perf_tp_event_match(struct perf_event *event,
4771 struct perf_sample_data *data,
4772 struct pt_regs *regs)
4775 * All tracepoints are from kernel-space.
4777 if (event->attr.exclude_kernel)
4780 if (!perf_tp_filter_match(event, data))
4786 void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
4787 struct pt_regs *regs, struct hlist_head *head, int rctx)
4789 struct perf_sample_data data;
4790 struct perf_event *event;
4791 struct hlist_node *node;
4793 struct perf_raw_record raw = {
4798 perf_sample_data_init(&data, addr);
4801 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
4802 if (perf_tp_event_match(event, &data, regs))
4803 perf_swevent_event(event, count, 1, &data, regs);
4806 perf_swevent_put_recursion_context(rctx);
4808 EXPORT_SYMBOL_GPL(perf_tp_event);
4810 static void tp_perf_event_destroy(struct perf_event *event)
4812 perf_trace_destroy(event);
4815 static int perf_tp_event_init(struct perf_event *event)
4819 if (event->attr.type != PERF_TYPE_TRACEPOINT)
4823 * Raw tracepoint data is a severe data leak, only allow root to
4826 if ((event->attr.sample_type & PERF_SAMPLE_RAW) &&
4827 perf_paranoid_tracepoint_raw() &&
4828 !capable(CAP_SYS_ADMIN))
4831 err = perf_trace_init(event);
4835 event->destroy = tp_perf_event_destroy;
4840 static struct pmu perf_tracepoint = {
4841 .task_ctx_nr = perf_sw_context,
4843 .event_init = perf_tp_event_init,
4844 .add = perf_trace_add,
4845 .del = perf_trace_del,
4846 .start = perf_swevent_start,
4847 .stop = perf_swevent_stop,
4848 .read = perf_swevent_read,
4851 static inline void perf_tp_register(void)
4853 perf_pmu_register(&perf_tracepoint);
4856 static int perf_event_set_filter(struct perf_event *event, void __user *arg)
4861 if (event->attr.type != PERF_TYPE_TRACEPOINT)
4864 filter_str = strndup_user(arg, PAGE_SIZE);
4865 if (IS_ERR(filter_str))
4866 return PTR_ERR(filter_str);
4868 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
4874 static void perf_event_free_filter(struct perf_event *event)
4876 ftrace_profile_free_filter(event);
4881 static inline void perf_tp_register(void)
4885 static int perf_event_set_filter(struct perf_event *event, void __user *arg)
4890 static void perf_event_free_filter(struct perf_event *event)
4894 #endif /* CONFIG_EVENT_TRACING */
4896 #ifdef CONFIG_HAVE_HW_BREAKPOINT
4897 void perf_bp_event(struct perf_event *bp, void *data)
4899 struct perf_sample_data sample;
4900 struct pt_regs *regs = data;
4902 perf_sample_data_init(&sample, bp->attr.bp_addr);
4904 if (!bp->hw.state && !perf_exclude_event(bp, regs))
4905 perf_swevent_event(bp, 1, 1, &sample, regs);
4910 * hrtimer based swevent callback
4913 static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
4915 enum hrtimer_restart ret = HRTIMER_RESTART;
4916 struct perf_sample_data data;
4917 struct pt_regs *regs;
4918 struct perf_event *event;
4921 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
4922 event->pmu->read(event);
4924 perf_sample_data_init(&data, 0);
4925 data.period = event->hw.last_period;
4926 regs = get_irq_regs();
4928 if (regs && !perf_exclude_event(event, regs)) {
4929 if (!(event->attr.exclude_idle && current->pid == 0))
4930 if (perf_event_overflow(event, 0, &data, regs))
4931 ret = HRTIMER_NORESTART;
4934 period = max_t(u64, 10000, event->hw.sample_period);
4935 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
4940 static void perf_swevent_start_hrtimer(struct perf_event *event)
4942 struct hw_perf_event *hwc = &event->hw;
4944 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
4945 hwc->hrtimer.function = perf_swevent_hrtimer;
4946 if (hwc->sample_period) {
4947 s64 period = local64_read(&hwc->period_left);
4953 local64_set(&hwc->period_left, 0);
4955 period = max_t(u64, 10000, hwc->sample_period);
4957 __hrtimer_start_range_ns(&hwc->hrtimer,
4958 ns_to_ktime(period), 0,
4959 HRTIMER_MODE_REL_PINNED, 0);
4963 static void perf_swevent_cancel_hrtimer(struct perf_event *event)
4965 struct hw_perf_event *hwc = &event->hw;
4967 if (hwc->sample_period) {
4968 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
4969 local64_set(&hwc->period_left, ktime_to_ns(remaining));
4971 hrtimer_cancel(&hwc->hrtimer);
4976 * Software event: cpu wall time clock
4979 static void cpu_clock_event_update(struct perf_event *event)
4984 now = local_clock();
4985 prev = local64_xchg(&event->hw.prev_count, now);
4986 local64_add(now - prev, &event->count);
4989 static void cpu_clock_event_start(struct perf_event *event, int flags)
4991 local64_set(&event->hw.prev_count, local_clock());
4992 perf_swevent_start_hrtimer(event);
4995 static void cpu_clock_event_stop(struct perf_event *event, int flags)
4997 perf_swevent_cancel_hrtimer(event);
4998 cpu_clock_event_update(event);
5001 static int cpu_clock_event_add(struct perf_event *event, int flags)
5003 if (flags & PERF_EF_START)
5004 cpu_clock_event_start(event, flags);
5009 static void cpu_clock_event_del(struct perf_event *event, int flags)
5011 cpu_clock_event_stop(event, flags);
5014 static void cpu_clock_event_read(struct perf_event *event)
5016 cpu_clock_event_update(event);
5019 static int cpu_clock_event_init(struct perf_event *event)
5021 if (event->attr.type != PERF_TYPE_SOFTWARE)
5024 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
5030 static struct pmu perf_cpu_clock = {
5031 .task_ctx_nr = perf_sw_context,
5033 .event_init = cpu_clock_event_init,
5034 .add = cpu_clock_event_add,
5035 .del = cpu_clock_event_del,
5036 .start = cpu_clock_event_start,
5037 .stop = cpu_clock_event_stop,
5038 .read = cpu_clock_event_read,
5042 * Software event: task time clock
5045 static void task_clock_event_update(struct perf_event *event, u64 now)
5050 prev = local64_xchg(&event->hw.prev_count, now);
5052 local64_add(delta, &event->count);
5055 static void task_clock_event_start(struct perf_event *event, int flags)
5057 local64_set(&event->hw.prev_count, event->ctx->time);
5058 perf_swevent_start_hrtimer(event);
5061 static void task_clock_event_stop(struct perf_event *event, int flags)
5063 perf_swevent_cancel_hrtimer(event);
5064 task_clock_event_update(event, event->ctx->time);
5067 static int task_clock_event_add(struct perf_event *event, int flags)
5069 if (flags & PERF_EF_START)
5070 task_clock_event_start(event, flags);
5075 static void task_clock_event_del(struct perf_event *event, int flags)
5077 task_clock_event_stop(event, PERF_EF_UPDATE);
5080 static void task_clock_event_read(struct perf_event *event)
5085 update_context_time(event->ctx);
5086 time = event->ctx->time;
5088 u64 now = perf_clock();
5089 u64 delta = now - event->ctx->timestamp;
5090 time = event->ctx->time + delta;
5093 task_clock_event_update(event, time);
5096 static int task_clock_event_init(struct perf_event *event)
5098 if (event->attr.type != PERF_TYPE_SOFTWARE)
5101 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
5107 static struct pmu perf_task_clock = {
5108 .task_ctx_nr = perf_sw_context,
5110 .event_init = task_clock_event_init,
5111 .add = task_clock_event_add,
5112 .del = task_clock_event_del,
5113 .start = task_clock_event_start,
5114 .stop = task_clock_event_stop,
5115 .read = task_clock_event_read,
5118 static void perf_pmu_nop_void(struct pmu *pmu)
5122 static int perf_pmu_nop_int(struct pmu *pmu)
5127 static void perf_pmu_start_txn(struct pmu *pmu)
5129 perf_pmu_disable(pmu);
5132 static int perf_pmu_commit_txn(struct pmu *pmu)
5134 perf_pmu_enable(pmu);
5138 static void perf_pmu_cancel_txn(struct pmu *pmu)
5140 perf_pmu_enable(pmu);
5144 * Ensures all contexts with the same task_ctx_nr have the same
5145 * pmu_cpu_context too.
5147 static void *find_pmu_context(int ctxn)
5154 list_for_each_entry(pmu, &pmus, entry) {
5155 if (pmu->task_ctx_nr == ctxn)
5156 return pmu->pmu_cpu_context;
5162 static void free_pmu_context(void * __percpu cpu_context)
5166 mutex_lock(&pmus_lock);
5168 * Like a real lame refcount.
5170 list_for_each_entry(pmu, &pmus, entry) {
5171 if (pmu->pmu_cpu_context == cpu_context)
5175 free_percpu(cpu_context);
5177 mutex_unlock(&pmus_lock);
5180 int perf_pmu_register(struct pmu *pmu)
5184 mutex_lock(&pmus_lock);
5186 pmu->pmu_disable_count = alloc_percpu(int);
5187 if (!pmu->pmu_disable_count)
5190 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
5191 if (pmu->pmu_cpu_context)
5192 goto got_cpu_context;
5194 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
5195 if (!pmu->pmu_cpu_context)
5198 for_each_possible_cpu(cpu) {
5199 struct perf_cpu_context *cpuctx;
5201 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
5202 __perf_event_init_context(&cpuctx->ctx);
5203 cpuctx->ctx.type = cpu_context;
5204 cpuctx->ctx.pmu = pmu;
5205 cpuctx->jiffies_interval = 1;
5206 INIT_LIST_HEAD(&cpuctx->rotation_list);
5210 if (!pmu->start_txn) {
5211 if (pmu->pmu_enable) {
5213 * If we have pmu_enable/pmu_disable calls, install
5214 * transaction stubs that use that to try and batch
5215 * hardware accesses.
5217 pmu->start_txn = perf_pmu_start_txn;
5218 pmu->commit_txn = perf_pmu_commit_txn;
5219 pmu->cancel_txn = perf_pmu_cancel_txn;
5221 pmu->start_txn = perf_pmu_nop_void;
5222 pmu->commit_txn = perf_pmu_nop_int;
5223 pmu->cancel_txn = perf_pmu_nop_void;
5227 if (!pmu->pmu_enable) {
5228 pmu->pmu_enable = perf_pmu_nop_void;
5229 pmu->pmu_disable = perf_pmu_nop_void;
5232 list_add_rcu(&pmu->entry, &pmus);
5235 mutex_unlock(&pmus_lock);
5240 free_percpu(pmu->pmu_disable_count);
5244 void perf_pmu_unregister(struct pmu *pmu)
5246 mutex_lock(&pmus_lock);
5247 list_del_rcu(&pmu->entry);
5248 mutex_unlock(&pmus_lock);
5251 * We dereference the pmu list under both SRCU and regular RCU, so
5252 * synchronize against both of those.
5254 synchronize_srcu(&pmus_srcu);
5257 free_percpu(pmu->pmu_disable_count);
5258 free_pmu_context(pmu->pmu_cpu_context);
5261 struct pmu *perf_init_event(struct perf_event *event)
5263 struct pmu *pmu = NULL;
5266 idx = srcu_read_lock(&pmus_srcu);
5267 list_for_each_entry_rcu(pmu, &pmus, entry) {
5268 int ret = pmu->event_init(event);
5272 if (ret != -ENOENT) {
5277 pmu = ERR_PTR(-ENOENT);
5279 srcu_read_unlock(&pmus_srcu, idx);
5285 * Allocate and initialize a event structure
5287 static struct perf_event *
5288 perf_event_alloc(struct perf_event_attr *attr, int cpu,
5289 struct perf_event *group_leader,
5290 struct perf_event *parent_event,
5291 perf_overflow_handler_t overflow_handler)
5294 struct perf_event *event;
5295 struct hw_perf_event *hwc;
5298 event = kzalloc(sizeof(*event), GFP_KERNEL);
5300 return ERR_PTR(-ENOMEM);
5303 * Single events are their own group leaders, with an
5304 * empty sibling list:
5307 group_leader = event;
5309 mutex_init(&event->child_mutex);
5310 INIT_LIST_HEAD(&event->child_list);
5312 INIT_LIST_HEAD(&event->group_entry);
5313 INIT_LIST_HEAD(&event->event_entry);
5314 INIT_LIST_HEAD(&event->sibling_list);
5315 init_waitqueue_head(&event->waitq);
5317 mutex_init(&event->mmap_mutex);
5320 event->attr = *attr;
5321 event->group_leader = group_leader;
5325 event->parent = parent_event;
5327 event->ns = get_pid_ns(current->nsproxy->pid_ns);
5328 event->id = atomic64_inc_return(&perf_event_id);
5330 event->state = PERF_EVENT_STATE_INACTIVE;
5332 if (!overflow_handler && parent_event)
5333 overflow_handler = parent_event->overflow_handler;
5335 event->overflow_handler = overflow_handler;
5338 event->state = PERF_EVENT_STATE_OFF;
5343 hwc->sample_period = attr->sample_period;
5344 if (attr->freq && attr->sample_freq)
5345 hwc->sample_period = 1;
5346 hwc->last_period = hwc->sample_period;
5348 local64_set(&hwc->period_left, hwc->sample_period);
5351 * we currently do not support PERF_FORMAT_GROUP on inherited events
5353 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
5356 pmu = perf_init_event(event);
5362 else if (IS_ERR(pmu))
5367 put_pid_ns(event->ns);
5369 return ERR_PTR(err);
5374 if (!event->parent) {
5375 atomic_inc(&nr_events);
5376 if (event->attr.mmap || event->attr.mmap_data)
5377 atomic_inc(&nr_mmap_events);
5378 if (event->attr.comm)
5379 atomic_inc(&nr_comm_events);
5380 if (event->attr.task)
5381 atomic_inc(&nr_task_events);
5382 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
5383 err = get_callchain_buffers();
5386 return ERR_PTR(err);
5394 static int perf_copy_attr(struct perf_event_attr __user *uattr,
5395 struct perf_event_attr *attr)
5400 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
5404 * zero the full structure, so that a short copy will be nice.
5406 memset(attr, 0, sizeof(*attr));
5408 ret = get_user(size, &uattr->size);
5412 if (size > PAGE_SIZE) /* silly large */
5415 if (!size) /* abi compat */
5416 size = PERF_ATTR_SIZE_VER0;
5418 if (size < PERF_ATTR_SIZE_VER0)
5422 * If we're handed a bigger struct than we know of,
5423 * ensure all the unknown bits are 0 - i.e. new
5424 * user-space does not rely on any kernel feature
5425 * extensions we dont know about yet.
5427 if (size > sizeof(*attr)) {
5428 unsigned char __user *addr;
5429 unsigned char __user *end;
5432 addr = (void __user *)uattr + sizeof(*attr);
5433 end = (void __user *)uattr + size;
5435 for (; addr < end; addr++) {
5436 ret = get_user(val, addr);
5442 size = sizeof(*attr);
5445 ret = copy_from_user(attr, uattr, size);
5450 * If the type exists, the corresponding creation will verify
5453 if (attr->type >= PERF_TYPE_MAX)
5456 if (attr->__reserved_1)
5459 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
5462 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
5469 put_user(sizeof(*attr), &uattr->size);
5475 perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
5477 struct perf_buffer *buffer = NULL, *old_buffer = NULL;
5483 /* don't allow circular references */
5484 if (event == output_event)
5488 * Don't allow cross-cpu buffers
5490 if (output_event->cpu != event->cpu)
5494 * If its not a per-cpu buffer, it must be the same task.
5496 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
5500 mutex_lock(&event->mmap_mutex);
5501 /* Can't redirect output if we've got an active mmap() */
5502 if (atomic_read(&event->mmap_count))
5506 /* get the buffer we want to redirect to */
5507 buffer = perf_buffer_get(output_event);
5512 old_buffer = event->buffer;
5513 rcu_assign_pointer(event->buffer, buffer);
5516 mutex_unlock(&event->mmap_mutex);
5519 perf_buffer_put(old_buffer);
5525 * sys_perf_event_open - open a performance event, associate it to a task/cpu
5527 * @attr_uptr: event_id type attributes for monitoring/sampling
5530 * @group_fd: group leader event fd
5532 SYSCALL_DEFINE5(perf_event_open,
5533 struct perf_event_attr __user *, attr_uptr,
5534 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
5536 struct perf_event *group_leader = NULL, *output_event = NULL;
5537 struct perf_event *event, *sibling;
5538 struct perf_event_attr attr;
5539 struct perf_event_context *ctx;
5540 struct file *event_file = NULL;
5541 struct file *group_file = NULL;
5542 struct task_struct *task = NULL;
5546 int fput_needed = 0;
5549 /* for future expandability... */
5550 if (flags & ~(PERF_FLAG_FD_NO_GROUP | PERF_FLAG_FD_OUTPUT))
5553 err = perf_copy_attr(attr_uptr, &attr);
5557 if (!attr.exclude_kernel) {
5558 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
5563 if (attr.sample_freq > sysctl_perf_event_sample_rate)
5567 event_fd = get_unused_fd_flags(O_RDWR);
5571 if (group_fd != -1) {
5572 group_leader = perf_fget_light(group_fd, &fput_needed);
5573 if (IS_ERR(group_leader)) {
5574 err = PTR_ERR(group_leader);
5577 group_file = group_leader->filp;
5578 if (flags & PERF_FLAG_FD_OUTPUT)
5579 output_event = group_leader;
5580 if (flags & PERF_FLAG_FD_NO_GROUP)
5581 group_leader = NULL;
5584 event = perf_event_alloc(&attr, cpu, group_leader, NULL, NULL);
5585 if (IS_ERR(event)) {
5586 err = PTR_ERR(event);
5591 * Special case software events and allow them to be part of
5592 * any hardware group.
5597 (is_software_event(event) != is_software_event(group_leader))) {
5598 if (is_software_event(event)) {
5600 * If event and group_leader are not both a software
5601 * event, and event is, then group leader is not.
5603 * Allow the addition of software events to !software
5604 * groups, this is safe because software events never
5607 pmu = group_leader->pmu;
5608 } else if (is_software_event(group_leader) &&
5609 (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
5611 * In case the group is a pure software group, and we
5612 * try to add a hardware event, move the whole group to
5613 * the hardware context.
5620 task = find_lively_task_by_vpid(pid);
5623 * Get the target context (task or percpu):
5625 ctx = find_get_context(pmu, task, cpu);
5632 * Look up the group leader (we will attach this event to it):
5638 * Do not allow a recursive hierarchy (this new sibling
5639 * becoming part of another group-sibling):
5641 if (group_leader->group_leader != group_leader)
5644 * Do not allow to attach to a group in a different
5645 * task or CPU context:
5648 if (group_leader->ctx->type != ctx->type)
5651 if (group_leader->ctx != ctx)
5656 * Only a group leader can be exclusive or pinned
5658 if (attr.exclusive || attr.pinned)
5663 err = perf_event_set_output(event, output_event);
5668 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, O_RDWR);
5669 if (IS_ERR(event_file)) {
5670 err = PTR_ERR(event_file);
5675 struct perf_event_context *gctx = group_leader->ctx;
5677 mutex_lock(&gctx->mutex);
5678 perf_event_remove_from_context(group_leader);
5679 list_for_each_entry(sibling, &group_leader->sibling_list,
5681 perf_event_remove_from_context(sibling);
5684 mutex_unlock(&gctx->mutex);
5688 event->filp = event_file;
5689 WARN_ON_ONCE(ctx->parent_ctx);
5690 mutex_lock(&ctx->mutex);
5693 perf_install_in_context(ctx, group_leader, cpu);
5695 list_for_each_entry(sibling, &group_leader->sibling_list,
5697 perf_install_in_context(ctx, sibling, cpu);
5702 perf_install_in_context(ctx, event, cpu);
5704 mutex_unlock(&ctx->mutex);
5706 event->owner = current;
5707 get_task_struct(current);
5708 mutex_lock(¤t->perf_event_mutex);
5709 list_add_tail(&event->owner_entry, ¤t->perf_event_list);
5710 mutex_unlock(¤t->perf_event_mutex);
5713 * Drop the reference on the group_event after placing the
5714 * new event on the sibling_list. This ensures destruction
5715 * of the group leader will find the pointer to itself in
5716 * perf_group_detach().
5718 fput_light(group_file, fput_needed);
5719 fd_install(event_fd, event_file);
5725 fput_light(group_file, fput_needed);
5728 put_unused_fd(event_fd);
5733 * perf_event_create_kernel_counter
5735 * @attr: attributes of the counter to create
5736 * @cpu: cpu in which the counter is bound
5737 * @task: task to profile (NULL for percpu)
5740 perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
5741 struct task_struct *task,
5742 perf_overflow_handler_t overflow_handler)
5744 struct perf_event_context *ctx;
5745 struct perf_event *event;
5749 * Get the target context (task or percpu):
5752 event = perf_event_alloc(attr, cpu, NULL, NULL, overflow_handler);
5753 if (IS_ERR(event)) {
5754 err = PTR_ERR(event);
5758 ctx = find_get_context(event->pmu, task, cpu);
5765 WARN_ON_ONCE(ctx->parent_ctx);
5766 mutex_lock(&ctx->mutex);
5767 perf_install_in_context(ctx, event, cpu);
5769 mutex_unlock(&ctx->mutex);
5771 event->owner = current;
5772 get_task_struct(current);
5773 mutex_lock(¤t->perf_event_mutex);
5774 list_add_tail(&event->owner_entry, ¤t->perf_event_list);
5775 mutex_unlock(¤t->perf_event_mutex);
5782 return ERR_PTR(err);
5784 EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
5786 static void sync_child_event(struct perf_event *child_event,
5787 struct task_struct *child)
5789 struct perf_event *parent_event = child_event->parent;
5792 if (child_event->attr.inherit_stat)
5793 perf_event_read_event(child_event, child);
5795 child_val = perf_event_count(child_event);
5798 * Add back the child's count to the parent's count:
5800 atomic64_add(child_val, &parent_event->child_count);
5801 atomic64_add(child_event->total_time_enabled,
5802 &parent_event->child_total_time_enabled);
5803 atomic64_add(child_event->total_time_running,
5804 &parent_event->child_total_time_running);
5807 * Remove this event from the parent's list
5809 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
5810 mutex_lock(&parent_event->child_mutex);
5811 list_del_init(&child_event->child_list);
5812 mutex_unlock(&parent_event->child_mutex);
5815 * Release the parent event, if this was the last
5818 fput(parent_event->filp);
5822 __perf_event_exit_task(struct perf_event *child_event,
5823 struct perf_event_context *child_ctx,
5824 struct task_struct *child)
5826 struct perf_event *parent_event;
5828 perf_event_remove_from_context(child_event);
5830 parent_event = child_event->parent;
5832 * It can happen that parent exits first, and has events
5833 * that are still around due to the child reference. These
5834 * events need to be zapped - but otherwise linger.
5837 sync_child_event(child_event, child);
5838 free_event(child_event);
5842 static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
5844 struct perf_event *child_event, *tmp;
5845 struct perf_event_context *child_ctx;
5846 unsigned long flags;
5848 if (likely(!child->perf_event_ctxp[ctxn])) {
5849 perf_event_task(child, NULL, 0);
5853 local_irq_save(flags);
5855 * We can't reschedule here because interrupts are disabled,
5856 * and either child is current or it is a task that can't be
5857 * scheduled, so we are now safe from rescheduling changing
5860 child_ctx = child->perf_event_ctxp[ctxn];
5861 __perf_event_task_sched_out(child_ctx);
5864 * Take the context lock here so that if find_get_context is
5865 * reading child->perf_event_ctxp, we wait until it has
5866 * incremented the context's refcount before we do put_ctx below.
5868 raw_spin_lock(&child_ctx->lock);
5869 child->perf_event_ctxp[ctxn] = NULL;
5871 * If this context is a clone; unclone it so it can't get
5872 * swapped to another process while we're removing all
5873 * the events from it.
5875 unclone_ctx(child_ctx);
5876 update_context_time(child_ctx);
5877 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
5880 * Report the task dead after unscheduling the events so that we
5881 * won't get any samples after PERF_RECORD_EXIT. We can however still
5882 * get a few PERF_RECORD_READ events.
5884 perf_event_task(child, child_ctx, 0);
5887 * We can recurse on the same lock type through:
5889 * __perf_event_exit_task()
5890 * sync_child_event()
5891 * fput(parent_event->filp)
5893 * mutex_lock(&ctx->mutex)
5895 * But since its the parent context it won't be the same instance.
5897 mutex_lock(&child_ctx->mutex);
5900 list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
5902 __perf_event_exit_task(child_event, child_ctx, child);
5904 list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
5906 __perf_event_exit_task(child_event, child_ctx, child);
5909 * If the last event was a group event, it will have appended all
5910 * its siblings to the list, but we obtained 'tmp' before that which
5911 * will still point to the list head terminating the iteration.
5913 if (!list_empty(&child_ctx->pinned_groups) ||
5914 !list_empty(&child_ctx->flexible_groups))
5917 mutex_unlock(&child_ctx->mutex);
5923 * When a child task exits, feed back event values to parent events.
5925 void perf_event_exit_task(struct task_struct *child)
5929 for_each_task_context_nr(ctxn)
5930 perf_event_exit_task_context(child, ctxn);
5933 static void perf_free_event(struct perf_event *event,
5934 struct perf_event_context *ctx)
5936 struct perf_event *parent = event->parent;
5938 if (WARN_ON_ONCE(!parent))
5941 mutex_lock(&parent->child_mutex);
5942 list_del_init(&event->child_list);
5943 mutex_unlock(&parent->child_mutex);
5947 perf_group_detach(event);
5948 list_del_event(event, ctx);
5953 * free an unexposed, unused context as created by inheritance by
5954 * perf_event_init_task below, used by fork() in case of fail.
5956 void perf_event_free_task(struct task_struct *task)
5958 struct perf_event_context *ctx;
5959 struct perf_event *event, *tmp;
5962 for_each_task_context_nr(ctxn) {
5963 ctx = task->perf_event_ctxp[ctxn];
5967 mutex_lock(&ctx->mutex);
5969 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
5971 perf_free_event(event, ctx);
5973 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
5975 perf_free_event(event, ctx);
5977 if (!list_empty(&ctx->pinned_groups) ||
5978 !list_empty(&ctx->flexible_groups))
5981 mutex_unlock(&ctx->mutex);
5987 void perf_event_delayed_put(struct task_struct *task)
5991 for_each_task_context_nr(ctxn)
5992 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
5996 * inherit a event from parent task to child task:
5998 static struct perf_event *
5999 inherit_event(struct perf_event *parent_event,
6000 struct task_struct *parent,
6001 struct perf_event_context *parent_ctx,
6002 struct task_struct *child,
6003 struct perf_event *group_leader,
6004 struct perf_event_context *child_ctx)
6006 struct perf_event *child_event;
6007 unsigned long flags;
6010 * Instead of creating recursive hierarchies of events,
6011 * we link inherited events back to the original parent,
6012 * which has a filp for sure, which we use as the reference
6015 if (parent_event->parent)
6016 parent_event = parent_event->parent;
6018 child_event = perf_event_alloc(&parent_event->attr,
6020 group_leader, parent_event,
6022 if (IS_ERR(child_event))
6027 * Make the child state follow the state of the parent event,
6028 * not its attr.disabled bit. We hold the parent's mutex,
6029 * so we won't race with perf_event_{en, dis}able_family.
6031 if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
6032 child_event->state = PERF_EVENT_STATE_INACTIVE;
6034 child_event->state = PERF_EVENT_STATE_OFF;
6036 if (parent_event->attr.freq) {
6037 u64 sample_period = parent_event->hw.sample_period;
6038 struct hw_perf_event *hwc = &child_event->hw;
6040 hwc->sample_period = sample_period;
6041 hwc->last_period = sample_period;
6043 local64_set(&hwc->period_left, sample_period);
6046 child_event->ctx = child_ctx;
6047 child_event->overflow_handler = parent_event->overflow_handler;
6050 * Link it up in the child's context:
6052 raw_spin_lock_irqsave(&child_ctx->lock, flags);
6053 add_event_to_ctx(child_event, child_ctx);
6054 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
6057 * Get a reference to the parent filp - we will fput it
6058 * when the child event exits. This is safe to do because
6059 * we are in the parent and we know that the filp still
6060 * exists and has a nonzero count:
6062 atomic_long_inc(&parent_event->filp->f_count);
6065 * Link this into the parent event's child list
6067 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
6068 mutex_lock(&parent_event->child_mutex);
6069 list_add_tail(&child_event->child_list, &parent_event->child_list);
6070 mutex_unlock(&parent_event->child_mutex);
6075 static int inherit_group(struct perf_event *parent_event,
6076 struct task_struct *parent,
6077 struct perf_event_context *parent_ctx,
6078 struct task_struct *child,
6079 struct perf_event_context *child_ctx)
6081 struct perf_event *leader;
6082 struct perf_event *sub;
6083 struct perf_event *child_ctr;
6085 leader = inherit_event(parent_event, parent, parent_ctx,
6086 child, NULL, child_ctx);
6088 return PTR_ERR(leader);
6089 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
6090 child_ctr = inherit_event(sub, parent, parent_ctx,
6091 child, leader, child_ctx);
6092 if (IS_ERR(child_ctr))
6093 return PTR_ERR(child_ctr);
6099 inherit_task_group(struct perf_event *event, struct task_struct *parent,
6100 struct perf_event_context *parent_ctx,
6101 struct task_struct *child, int ctxn,
6105 struct perf_event_context *child_ctx;
6107 if (!event->attr.inherit) {
6112 child_ctx = child->perf_event_ctxp[ctxn];
6115 * This is executed from the parent task context, so
6116 * inherit events that have been marked for cloning.
6117 * First allocate and initialize a context for the
6121 child_ctx = alloc_perf_context(event->pmu, child);
6125 child->perf_event_ctxp[ctxn] = child_ctx;
6128 ret = inherit_group(event, parent, parent_ctx,
6138 * Initialize the perf_event context in task_struct
6140 int perf_event_init_context(struct task_struct *child, int ctxn)
6142 struct perf_event_context *child_ctx, *parent_ctx;
6143 struct perf_event_context *cloned_ctx;
6144 struct perf_event *event;
6145 struct task_struct *parent = current;
6146 int inherited_all = 1;
6149 child->perf_event_ctxp[ctxn] = NULL;
6151 mutex_init(&child->perf_event_mutex);
6152 INIT_LIST_HEAD(&child->perf_event_list);
6154 if (likely(!parent->perf_event_ctxp[ctxn]))
6158 * If the parent's context is a clone, pin it so it won't get
6161 parent_ctx = perf_pin_task_context(parent, ctxn);
6164 * No need to check if parent_ctx != NULL here; since we saw
6165 * it non-NULL earlier, the only reason for it to become NULL
6166 * is if we exit, and since we're currently in the middle of
6167 * a fork we can't be exiting at the same time.
6171 * Lock the parent list. No need to lock the child - not PID
6172 * hashed yet and not running, so nobody can access it.
6174 mutex_lock(&parent_ctx->mutex);
6177 * We dont have to disable NMIs - we are only looking at
6178 * the list, not manipulating it:
6180 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
6181 ret = inherit_task_group(event, parent, parent_ctx,
6182 child, ctxn, &inherited_all);
6187 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
6188 ret = inherit_task_group(event, parent, parent_ctx,
6189 child, ctxn, &inherited_all);
6194 child_ctx = child->perf_event_ctxp[ctxn];
6196 if (child_ctx && inherited_all) {
6198 * Mark the child context as a clone of the parent
6199 * context, or of whatever the parent is a clone of.
6200 * Note that if the parent is a clone, it could get
6201 * uncloned at any point, but that doesn't matter
6202 * because the list of events and the generation
6203 * count can't have changed since we took the mutex.
6205 cloned_ctx = rcu_dereference(parent_ctx->parent_ctx);
6207 child_ctx->parent_ctx = cloned_ctx;
6208 child_ctx->parent_gen = parent_ctx->parent_gen;
6210 child_ctx->parent_ctx = parent_ctx;
6211 child_ctx->parent_gen = parent_ctx->generation;
6213 get_ctx(child_ctx->parent_ctx);
6216 mutex_unlock(&parent_ctx->mutex);
6218 perf_unpin_context(parent_ctx);
6224 * Initialize the perf_event context in task_struct
6226 int perf_event_init_task(struct task_struct *child)
6230 for_each_task_context_nr(ctxn) {
6231 ret = perf_event_init_context(child, ctxn);
6239 static void __init perf_event_init_all_cpus(void)
6241 struct swevent_htable *swhash;
6244 for_each_possible_cpu(cpu) {
6245 swhash = &per_cpu(swevent_htable, cpu);
6246 mutex_init(&swhash->hlist_mutex);
6247 INIT_LIST_HEAD(&per_cpu(rotation_list, cpu));
6251 static void __cpuinit perf_event_init_cpu(int cpu)
6253 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
6255 mutex_lock(&swhash->hlist_mutex);
6256 if (swhash->hlist_refcount > 0) {
6257 struct swevent_hlist *hlist;
6259 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
6261 rcu_assign_pointer(swhash->swevent_hlist, hlist);
6263 mutex_unlock(&swhash->hlist_mutex);
6266 #ifdef CONFIG_HOTPLUG_CPU
6267 static void perf_pmu_rotate_stop(struct pmu *pmu)
6269 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
6271 WARN_ON(!irqs_disabled());
6273 list_del_init(&cpuctx->rotation_list);
6276 static void __perf_event_exit_context(void *__info)
6278 struct perf_event_context *ctx = __info;
6279 struct perf_event *event, *tmp;
6281 perf_pmu_rotate_stop(ctx->pmu);
6283 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
6284 __perf_event_remove_from_context(event);
6285 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, group_entry)
6286 __perf_event_remove_from_context(event);
6289 static void perf_event_exit_cpu_context(int cpu)
6291 struct perf_event_context *ctx;
6295 idx = srcu_read_lock(&pmus_srcu);
6296 list_for_each_entry_rcu(pmu, &pmus, entry) {
6297 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
6299 mutex_lock(&ctx->mutex);
6300 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
6301 mutex_unlock(&ctx->mutex);
6303 srcu_read_unlock(&pmus_srcu, idx);
6306 static void perf_event_exit_cpu(int cpu)
6308 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
6310 mutex_lock(&swhash->hlist_mutex);
6311 swevent_hlist_release(swhash);
6312 mutex_unlock(&swhash->hlist_mutex);
6314 perf_event_exit_cpu_context(cpu);
6317 static inline void perf_event_exit_cpu(int cpu) { }
6320 static int __cpuinit
6321 perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
6323 unsigned int cpu = (long)hcpu;
6325 switch (action & ~CPU_TASKS_FROZEN) {
6327 case CPU_UP_PREPARE:
6328 case CPU_DOWN_FAILED:
6329 perf_event_init_cpu(cpu);
6332 case CPU_UP_CANCELED:
6333 case CPU_DOWN_PREPARE:
6334 perf_event_exit_cpu(cpu);
6344 void __init perf_event_init(void)
6346 perf_event_init_all_cpus();
6347 init_srcu_struct(&pmus_srcu);
6348 perf_pmu_register(&perf_swevent);
6349 perf_pmu_register(&perf_cpu_clock);
6350 perf_pmu_register(&perf_task_clock);
6352 perf_cpu_notifier(perf_cpu_notify);