2 * Performance events core code:
4 * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
5 * Copyright (C) 2008-2009 Red Hat, Inc., Ingo Molnar
6 * Copyright (C) 2008-2009 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
7 * Copyright © 2009 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
9 * For licensing details see kernel-base/COPYING
14 #include <linux/cpu.h>
15 #include <linux/smp.h>
16 #include <linux/file.h>
17 #include <linux/poll.h>
18 #include <linux/slab.h>
19 #include <linux/hash.h>
20 #include <linux/sysfs.h>
21 #include <linux/dcache.h>
22 #include <linux/percpu.h>
23 #include <linux/ptrace.h>
24 #include <linux/vmstat.h>
25 #include <linux/vmalloc.h>
26 #include <linux/hardirq.h>
27 #include <linux/rculist.h>
28 #include <linux/uaccess.h>
29 #include <linux/syscalls.h>
30 #include <linux/anon_inodes.h>
31 #include <linux/kernel_stat.h>
32 #include <linux/perf_event.h>
33 #include <linux/ftrace_event.h>
35 #include <asm/irq_regs.h>
38 * Each CPU has a list of per CPU events:
40 static DEFINE_PER_CPU(struct perf_cpu_context, perf_cpu_context);
42 static atomic_t nr_events __read_mostly;
43 static atomic_t nr_mmap_events __read_mostly;
44 static atomic_t nr_comm_events __read_mostly;
45 static atomic_t nr_task_events __read_mostly;
48 * perf event paranoia level:
49 * -1 - not paranoid at all
50 * 0 - disallow raw tracepoint access for unpriv
51 * 1 - disallow cpu events for unpriv
52 * 2 - disallow kernel profiling for unpriv
54 int sysctl_perf_event_paranoid __read_mostly = 1;
56 int sysctl_perf_event_mlock __read_mostly = 512; /* 'free' kb per user */
59 * max perf event sample rate
61 int sysctl_perf_event_sample_rate __read_mostly = 100000;
63 static atomic64_t perf_event_id;
65 void __weak perf_event_print_debug(void) { }
67 void perf_pmu_disable(struct pmu *pmu)
69 int *count = this_cpu_ptr(pmu->pmu_disable_count);
71 pmu->pmu_disable(pmu);
74 void perf_pmu_enable(struct pmu *pmu)
76 int *count = this_cpu_ptr(pmu->pmu_disable_count);
81 static void perf_pmu_rotate_start(void)
83 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
85 if (hrtimer_active(&cpuctx->timer))
88 __hrtimer_start_range_ns(&cpuctx->timer,
89 ns_to_ktime(cpuctx->timer_interval), 0,
90 HRTIMER_MODE_REL_PINNED, 0);
93 static void perf_pmu_rotate_stop(void)
95 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
97 hrtimer_cancel(&cpuctx->timer);
100 static void get_ctx(struct perf_event_context *ctx)
102 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
105 static void free_ctx(struct rcu_head *head)
107 struct perf_event_context *ctx;
109 ctx = container_of(head, struct perf_event_context, rcu_head);
113 static void put_ctx(struct perf_event_context *ctx)
115 if (atomic_dec_and_test(&ctx->refcount)) {
117 put_ctx(ctx->parent_ctx);
119 put_task_struct(ctx->task);
120 call_rcu(&ctx->rcu_head, free_ctx);
124 static void unclone_ctx(struct perf_event_context *ctx)
126 if (ctx->parent_ctx) {
127 put_ctx(ctx->parent_ctx);
128 ctx->parent_ctx = NULL;
133 * If we inherit events we want to return the parent event id
136 static u64 primary_event_id(struct perf_event *event)
141 id = event->parent->id;
147 * Get the perf_event_context for a task and lock it.
148 * This has to cope with with the fact that until it is locked,
149 * the context could get moved to another task.
151 static struct perf_event_context *
152 perf_lock_task_context(struct task_struct *task, unsigned long *flags)
154 struct perf_event_context *ctx;
158 ctx = rcu_dereference(task->perf_event_ctxp);
161 * If this context is a clone of another, it might
162 * get swapped for another underneath us by
163 * perf_event_task_sched_out, though the
164 * rcu_read_lock() protects us from any context
165 * getting freed. Lock the context and check if it
166 * got swapped before we could get the lock, and retry
167 * if so. If we locked the right context, then it
168 * can't get swapped on us any more.
170 raw_spin_lock_irqsave(&ctx->lock, *flags);
171 if (ctx != rcu_dereference(task->perf_event_ctxp)) {
172 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
176 if (!atomic_inc_not_zero(&ctx->refcount)) {
177 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
186 * Get the context for a task and increment its pin_count so it
187 * can't get swapped to another task. This also increments its
188 * reference count so that the context can't get freed.
190 static struct perf_event_context *perf_pin_task_context(struct task_struct *task)
192 struct perf_event_context *ctx;
195 ctx = perf_lock_task_context(task, &flags);
198 raw_spin_unlock_irqrestore(&ctx->lock, flags);
203 static void perf_unpin_context(struct perf_event_context *ctx)
207 raw_spin_lock_irqsave(&ctx->lock, flags);
209 raw_spin_unlock_irqrestore(&ctx->lock, flags);
213 static inline u64 perf_clock(void)
215 return local_clock();
219 * Update the record of the current time in a context.
221 static void update_context_time(struct perf_event_context *ctx)
223 u64 now = perf_clock();
225 ctx->time += now - ctx->timestamp;
226 ctx->timestamp = now;
230 * Update the total_time_enabled and total_time_running fields for a event.
232 static void update_event_times(struct perf_event *event)
234 struct perf_event_context *ctx = event->ctx;
237 if (event->state < PERF_EVENT_STATE_INACTIVE ||
238 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
244 run_end = event->tstamp_stopped;
246 event->total_time_enabled = run_end - event->tstamp_enabled;
248 if (event->state == PERF_EVENT_STATE_INACTIVE)
249 run_end = event->tstamp_stopped;
253 event->total_time_running = run_end - event->tstamp_running;
257 * Update total_time_enabled and total_time_running for all events in a group.
259 static void update_group_times(struct perf_event *leader)
261 struct perf_event *event;
263 update_event_times(leader);
264 list_for_each_entry(event, &leader->sibling_list, group_entry)
265 update_event_times(event);
268 static struct list_head *
269 ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
271 if (event->attr.pinned)
272 return &ctx->pinned_groups;
274 return &ctx->flexible_groups;
278 * Add a event from the lists for its context.
279 * Must be called with ctx->mutex and ctx->lock held.
282 list_add_event(struct perf_event *event, struct perf_event_context *ctx)
284 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
285 event->attach_state |= PERF_ATTACH_CONTEXT;
288 * If we're a stand alone event or group leader, we go to the context
289 * list, group events are kept attached to the group so that
290 * perf_group_detach can, at all times, locate all siblings.
292 if (event->group_leader == event) {
293 struct list_head *list;
295 if (is_software_event(event))
296 event->group_flags |= PERF_GROUP_SOFTWARE;
298 list = ctx_group_list(event, ctx);
299 list_add_tail(&event->group_entry, list);
302 list_add_rcu(&event->event_entry, &ctx->event_list);
304 perf_pmu_rotate_start();
306 if (event->attr.inherit_stat)
310 static void perf_group_attach(struct perf_event *event)
312 struct perf_event *group_leader = event->group_leader;
314 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_GROUP);
315 event->attach_state |= PERF_ATTACH_GROUP;
317 if (group_leader == event)
320 if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
321 !is_software_event(event))
322 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
324 list_add_tail(&event->group_entry, &group_leader->sibling_list);
325 group_leader->nr_siblings++;
329 * Remove a event from the lists for its context.
330 * Must be called with ctx->mutex and ctx->lock held.
333 list_del_event(struct perf_event *event, struct perf_event_context *ctx)
336 * We can have double detach due to exit/hot-unplug + close.
338 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
341 event->attach_state &= ~PERF_ATTACH_CONTEXT;
344 if (event->attr.inherit_stat)
347 list_del_rcu(&event->event_entry);
349 if (event->group_leader == event)
350 list_del_init(&event->group_entry);
352 update_group_times(event);
355 * If event was in error state, then keep it
356 * that way, otherwise bogus counts will be
357 * returned on read(). The only way to get out
358 * of error state is by explicit re-enabling
361 if (event->state > PERF_EVENT_STATE_OFF)
362 event->state = PERF_EVENT_STATE_OFF;
365 static void perf_group_detach(struct perf_event *event)
367 struct perf_event *sibling, *tmp;
368 struct list_head *list = NULL;
371 * We can have double detach due to exit/hot-unplug + close.
373 if (!(event->attach_state & PERF_ATTACH_GROUP))
376 event->attach_state &= ~PERF_ATTACH_GROUP;
379 * If this is a sibling, remove it from its group.
381 if (event->group_leader != event) {
382 list_del_init(&event->group_entry);
383 event->group_leader->nr_siblings--;
387 if (!list_empty(&event->group_entry))
388 list = &event->group_entry;
391 * If this was a group event with sibling events then
392 * upgrade the siblings to singleton events by adding them
393 * to whatever list we are on.
395 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
397 list_move_tail(&sibling->group_entry, list);
398 sibling->group_leader = sibling;
400 /* Inherit group flags from the previous leader */
401 sibling->group_flags = event->group_flags;
406 event_filter_match(struct perf_event *event)
408 return event->cpu == -1 || event->cpu == smp_processor_id();
412 event_sched_out(struct perf_event *event,
413 struct perf_cpu_context *cpuctx,
414 struct perf_event_context *ctx)
418 * An event which could not be activated because of
419 * filter mismatch still needs to have its timings
420 * maintained, otherwise bogus information is return
421 * via read() for time_enabled, time_running:
423 if (event->state == PERF_EVENT_STATE_INACTIVE
424 && !event_filter_match(event)) {
425 delta = ctx->time - event->tstamp_stopped;
426 event->tstamp_running += delta;
427 event->tstamp_stopped = ctx->time;
430 if (event->state != PERF_EVENT_STATE_ACTIVE)
433 event->state = PERF_EVENT_STATE_INACTIVE;
434 if (event->pending_disable) {
435 event->pending_disable = 0;
436 event->state = PERF_EVENT_STATE_OFF;
438 event->tstamp_stopped = ctx->time;
439 event->pmu->del(event, 0);
442 if (!is_software_event(event))
443 cpuctx->active_oncpu--;
445 if (event->attr.exclusive || !cpuctx->active_oncpu)
446 cpuctx->exclusive = 0;
450 group_sched_out(struct perf_event *group_event,
451 struct perf_cpu_context *cpuctx,
452 struct perf_event_context *ctx)
454 struct perf_event *event;
455 int state = group_event->state;
457 event_sched_out(group_event, cpuctx, ctx);
460 * Schedule out siblings (if any):
462 list_for_each_entry(event, &group_event->sibling_list, group_entry)
463 event_sched_out(event, cpuctx, ctx);
465 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
466 cpuctx->exclusive = 0;
470 * Cross CPU call to remove a performance event
472 * We disable the event on the hardware level first. After that we
473 * remove it from the context list.
475 static void __perf_event_remove_from_context(void *info)
477 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
478 struct perf_event *event = info;
479 struct perf_event_context *ctx = event->ctx;
482 * If this is a task context, we need to check whether it is
483 * the current task context of this cpu. If not it has been
484 * scheduled out before the smp call arrived.
486 if (ctx->task && cpuctx->task_ctx != ctx)
489 raw_spin_lock(&ctx->lock);
491 event_sched_out(event, cpuctx, ctx);
493 list_del_event(event, ctx);
495 raw_spin_unlock(&ctx->lock);
500 * Remove the event from a task's (or a CPU's) list of events.
502 * Must be called with ctx->mutex held.
504 * CPU events are removed with a smp call. For task events we only
505 * call when the task is on a CPU.
507 * If event->ctx is a cloned context, callers must make sure that
508 * every task struct that event->ctx->task could possibly point to
509 * remains valid. This is OK when called from perf_release since
510 * that only calls us on the top-level context, which can't be a clone.
511 * When called from perf_event_exit_task, it's OK because the
512 * context has been detached from its task.
514 static void perf_event_remove_from_context(struct perf_event *event)
516 struct perf_event_context *ctx = event->ctx;
517 struct task_struct *task = ctx->task;
521 * Per cpu events are removed via an smp call and
522 * the removal is always successful.
524 smp_call_function_single(event->cpu,
525 __perf_event_remove_from_context,
531 task_oncpu_function_call(task, __perf_event_remove_from_context,
534 raw_spin_lock_irq(&ctx->lock);
536 * If the context is active we need to retry the smp call.
538 if (ctx->nr_active && !list_empty(&event->group_entry)) {
539 raw_spin_unlock_irq(&ctx->lock);
544 * The lock prevents that this context is scheduled in so we
545 * can remove the event safely, if the call above did not
548 if (!list_empty(&event->group_entry))
549 list_del_event(event, ctx);
550 raw_spin_unlock_irq(&ctx->lock);
554 * Cross CPU call to disable a performance event
556 static void __perf_event_disable(void *info)
558 struct perf_event *event = info;
559 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
560 struct perf_event_context *ctx = event->ctx;
563 * If this is a per-task event, need to check whether this
564 * event's task is the current task on this cpu.
566 if (ctx->task && cpuctx->task_ctx != ctx)
569 raw_spin_lock(&ctx->lock);
572 * If the event is on, turn it off.
573 * If it is in error state, leave it in error state.
575 if (event->state >= PERF_EVENT_STATE_INACTIVE) {
576 update_context_time(ctx);
577 update_group_times(event);
578 if (event == event->group_leader)
579 group_sched_out(event, cpuctx, ctx);
581 event_sched_out(event, cpuctx, ctx);
582 event->state = PERF_EVENT_STATE_OFF;
585 raw_spin_unlock(&ctx->lock);
591 * If event->ctx is a cloned context, callers must make sure that
592 * every task struct that event->ctx->task could possibly point to
593 * remains valid. This condition is satisifed when called through
594 * perf_event_for_each_child or perf_event_for_each because they
595 * hold the top-level event's child_mutex, so any descendant that
596 * goes to exit will block in sync_child_event.
597 * When called from perf_pending_event it's OK because event->ctx
598 * is the current context on this CPU and preemption is disabled,
599 * hence we can't get into perf_event_task_sched_out for this context.
601 void perf_event_disable(struct perf_event *event)
603 struct perf_event_context *ctx = event->ctx;
604 struct task_struct *task = ctx->task;
608 * Disable the event on the cpu that it's on
610 smp_call_function_single(event->cpu, __perf_event_disable,
616 task_oncpu_function_call(task, __perf_event_disable, event);
618 raw_spin_lock_irq(&ctx->lock);
620 * If the event is still active, we need to retry the cross-call.
622 if (event->state == PERF_EVENT_STATE_ACTIVE) {
623 raw_spin_unlock_irq(&ctx->lock);
628 * Since we have the lock this context can't be scheduled
629 * in, so we can change the state safely.
631 if (event->state == PERF_EVENT_STATE_INACTIVE) {
632 update_group_times(event);
633 event->state = PERF_EVENT_STATE_OFF;
636 raw_spin_unlock_irq(&ctx->lock);
640 event_sched_in(struct perf_event *event,
641 struct perf_cpu_context *cpuctx,
642 struct perf_event_context *ctx)
644 if (event->state <= PERF_EVENT_STATE_OFF)
647 event->state = PERF_EVENT_STATE_ACTIVE;
648 event->oncpu = smp_processor_id();
650 * The new state must be visible before we turn it on in the hardware:
654 if (event->pmu->add(event, PERF_EF_START)) {
655 event->state = PERF_EVENT_STATE_INACTIVE;
660 event->tstamp_running += ctx->time - event->tstamp_stopped;
662 if (!is_software_event(event))
663 cpuctx->active_oncpu++;
666 if (event->attr.exclusive)
667 cpuctx->exclusive = 1;
673 group_sched_in(struct perf_event *group_event,
674 struct perf_cpu_context *cpuctx,
675 struct perf_event_context *ctx)
677 struct perf_event *event, *partial_group = NULL;
678 struct pmu *pmu = group_event->pmu;
680 if (group_event->state == PERF_EVENT_STATE_OFF)
685 if (event_sched_in(group_event, cpuctx, ctx)) {
686 pmu->cancel_txn(pmu);
691 * Schedule in siblings as one group (if any):
693 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
694 if (event_sched_in(event, cpuctx, ctx)) {
695 partial_group = event;
700 if (!pmu->commit_txn(pmu))
705 * Groups can be scheduled in as one unit only, so undo any
706 * partial group before returning:
708 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
709 if (event == partial_group)
711 event_sched_out(event, cpuctx, ctx);
713 event_sched_out(group_event, cpuctx, ctx);
715 pmu->cancel_txn(pmu);
721 * Work out whether we can put this event group on the CPU now.
723 static int group_can_go_on(struct perf_event *event,
724 struct perf_cpu_context *cpuctx,
728 * Groups consisting entirely of software events can always go on.
730 if (event->group_flags & PERF_GROUP_SOFTWARE)
733 * If an exclusive group is already on, no other hardware
736 if (cpuctx->exclusive)
739 * If this group is exclusive and there are already
740 * events on the CPU, it can't go on.
742 if (event->attr.exclusive && cpuctx->active_oncpu)
745 * Otherwise, try to add it if all previous groups were able
751 static void add_event_to_ctx(struct perf_event *event,
752 struct perf_event_context *ctx)
754 list_add_event(event, ctx);
755 perf_group_attach(event);
756 event->tstamp_enabled = ctx->time;
757 event->tstamp_running = ctx->time;
758 event->tstamp_stopped = ctx->time;
762 * Cross CPU call to install and enable a performance event
764 * Must be called with ctx->mutex held
766 static void __perf_install_in_context(void *info)
768 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
769 struct perf_event *event = info;
770 struct perf_event_context *ctx = event->ctx;
771 struct perf_event *leader = event->group_leader;
775 * If this is a task context, we need to check whether it is
776 * the current task context of this cpu. If not it has been
777 * scheduled out before the smp call arrived.
778 * Or possibly this is the right context but it isn't
779 * on this cpu because it had no events.
781 if (ctx->task && cpuctx->task_ctx != ctx) {
782 if (cpuctx->task_ctx || ctx->task != current)
784 cpuctx->task_ctx = ctx;
787 raw_spin_lock(&ctx->lock);
789 update_context_time(ctx);
791 add_event_to_ctx(event, ctx);
793 if (event->cpu != -1 && event->cpu != smp_processor_id())
797 * Don't put the event on if it is disabled or if
798 * it is in a group and the group isn't on.
800 if (event->state != PERF_EVENT_STATE_INACTIVE ||
801 (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE))
805 * An exclusive event can't go on if there are already active
806 * hardware events, and no hardware event can go on if there
807 * is already an exclusive event on.
809 if (!group_can_go_on(event, cpuctx, 1))
812 err = event_sched_in(event, cpuctx, ctx);
816 * This event couldn't go on. If it is in a group
817 * then we have to pull the whole group off.
818 * If the event group is pinned then put it in error state.
821 group_sched_out(leader, cpuctx, ctx);
822 if (leader->attr.pinned) {
823 update_group_times(leader);
824 leader->state = PERF_EVENT_STATE_ERROR;
829 raw_spin_unlock(&ctx->lock);
833 * Attach a performance event to a context
835 * First we add the event to the list with the hardware enable bit
836 * in event->hw_config cleared.
838 * If the event is attached to a task which is on a CPU we use a smp
839 * call to enable it in the task context. The task might have been
840 * scheduled away, but we check this in the smp call again.
842 * Must be called with ctx->mutex held.
845 perf_install_in_context(struct perf_event_context *ctx,
846 struct perf_event *event,
849 struct task_struct *task = ctx->task;
855 * Per cpu events are installed via an smp call and
856 * the install is always successful.
858 smp_call_function_single(cpu, __perf_install_in_context,
864 task_oncpu_function_call(task, __perf_install_in_context,
867 raw_spin_lock_irq(&ctx->lock);
869 * we need to retry the smp call.
871 if (ctx->is_active && list_empty(&event->group_entry)) {
872 raw_spin_unlock_irq(&ctx->lock);
877 * The lock prevents that this context is scheduled in so we
878 * can add the event safely, if it the call above did not
881 if (list_empty(&event->group_entry))
882 add_event_to_ctx(event, ctx);
883 raw_spin_unlock_irq(&ctx->lock);
887 * Put a event into inactive state and update time fields.
888 * Enabling the leader of a group effectively enables all
889 * the group members that aren't explicitly disabled, so we
890 * have to update their ->tstamp_enabled also.
891 * Note: this works for group members as well as group leaders
892 * since the non-leader members' sibling_lists will be empty.
894 static void __perf_event_mark_enabled(struct perf_event *event,
895 struct perf_event_context *ctx)
897 struct perf_event *sub;
899 event->state = PERF_EVENT_STATE_INACTIVE;
900 event->tstamp_enabled = ctx->time - event->total_time_enabled;
901 list_for_each_entry(sub, &event->sibling_list, group_entry) {
902 if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
903 sub->tstamp_enabled =
904 ctx->time - sub->total_time_enabled;
910 * Cross CPU call to enable a performance event
912 static void __perf_event_enable(void *info)
914 struct perf_event *event = info;
915 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
916 struct perf_event_context *ctx = event->ctx;
917 struct perf_event *leader = event->group_leader;
921 * If this is a per-task event, need to check whether this
922 * event's task is the current task on this cpu.
924 if (ctx->task && cpuctx->task_ctx != ctx) {
925 if (cpuctx->task_ctx || ctx->task != current)
927 cpuctx->task_ctx = ctx;
930 raw_spin_lock(&ctx->lock);
932 update_context_time(ctx);
934 if (event->state >= PERF_EVENT_STATE_INACTIVE)
936 __perf_event_mark_enabled(event, ctx);
938 if (event->cpu != -1 && event->cpu != smp_processor_id())
942 * If the event is in a group and isn't the group leader,
943 * then don't put it on unless the group is on.
945 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
948 if (!group_can_go_on(event, cpuctx, 1)) {
952 err = group_sched_in(event, cpuctx, ctx);
954 err = event_sched_in(event, cpuctx, ctx);
959 * If this event can't go on and it's part of a
960 * group, then the whole group has to come off.
963 group_sched_out(leader, cpuctx, ctx);
964 if (leader->attr.pinned) {
965 update_group_times(leader);
966 leader->state = PERF_EVENT_STATE_ERROR;
971 raw_spin_unlock(&ctx->lock);
977 * If event->ctx is a cloned context, callers must make sure that
978 * every task struct that event->ctx->task could possibly point to
979 * remains valid. This condition is satisfied when called through
980 * perf_event_for_each_child or perf_event_for_each as described
981 * for perf_event_disable.
983 void perf_event_enable(struct perf_event *event)
985 struct perf_event_context *ctx = event->ctx;
986 struct task_struct *task = ctx->task;
990 * Enable the event on the cpu that it's on
992 smp_call_function_single(event->cpu, __perf_event_enable,
997 raw_spin_lock_irq(&ctx->lock);
998 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1002 * If the event is in error state, clear that first.
1003 * That way, if we see the event in error state below, we
1004 * know that it has gone back into error state, as distinct
1005 * from the task having been scheduled away before the
1006 * cross-call arrived.
1008 if (event->state == PERF_EVENT_STATE_ERROR)
1009 event->state = PERF_EVENT_STATE_OFF;
1012 raw_spin_unlock_irq(&ctx->lock);
1013 task_oncpu_function_call(task, __perf_event_enable, event);
1015 raw_spin_lock_irq(&ctx->lock);
1018 * If the context is active and the event is still off,
1019 * we need to retry the cross-call.
1021 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF)
1025 * Since we have the lock this context can't be scheduled
1026 * in, so we can change the state safely.
1028 if (event->state == PERF_EVENT_STATE_OFF)
1029 __perf_event_mark_enabled(event, ctx);
1032 raw_spin_unlock_irq(&ctx->lock);
1035 static int perf_event_refresh(struct perf_event *event, int refresh)
1038 * not supported on inherited events
1040 if (event->attr.inherit)
1043 atomic_add(refresh, &event->event_limit);
1044 perf_event_enable(event);
1050 EVENT_FLEXIBLE = 0x1,
1052 EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
1055 static void ctx_sched_out(struct perf_event_context *ctx,
1056 struct perf_cpu_context *cpuctx,
1057 enum event_type_t event_type)
1059 struct perf_event *event;
1061 raw_spin_lock(&ctx->lock);
1063 if (likely(!ctx->nr_events))
1065 update_context_time(ctx);
1067 if (!ctx->nr_active)
1070 if (event_type & EVENT_PINNED) {
1071 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
1072 group_sched_out(event, cpuctx, ctx);
1075 if (event_type & EVENT_FLEXIBLE) {
1076 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
1077 group_sched_out(event, cpuctx, ctx);
1080 raw_spin_unlock(&ctx->lock);
1084 * Test whether two contexts are equivalent, i.e. whether they
1085 * have both been cloned from the same version of the same context
1086 * and they both have the same number of enabled events.
1087 * If the number of enabled events is the same, then the set
1088 * of enabled events should be the same, because these are both
1089 * inherited contexts, therefore we can't access individual events
1090 * in them directly with an fd; we can only enable/disable all
1091 * events via prctl, or enable/disable all events in a family
1092 * via ioctl, which will have the same effect on both contexts.
1094 static int context_equiv(struct perf_event_context *ctx1,
1095 struct perf_event_context *ctx2)
1097 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
1098 && ctx1->parent_gen == ctx2->parent_gen
1099 && !ctx1->pin_count && !ctx2->pin_count;
1102 static void __perf_event_sync_stat(struct perf_event *event,
1103 struct perf_event *next_event)
1107 if (!event->attr.inherit_stat)
1111 * Update the event value, we cannot use perf_event_read()
1112 * because we're in the middle of a context switch and have IRQs
1113 * disabled, which upsets smp_call_function_single(), however
1114 * we know the event must be on the current CPU, therefore we
1115 * don't need to use it.
1117 switch (event->state) {
1118 case PERF_EVENT_STATE_ACTIVE:
1119 event->pmu->read(event);
1122 case PERF_EVENT_STATE_INACTIVE:
1123 update_event_times(event);
1131 * In order to keep per-task stats reliable we need to flip the event
1132 * values when we flip the contexts.
1134 value = local64_read(&next_event->count);
1135 value = local64_xchg(&event->count, value);
1136 local64_set(&next_event->count, value);
1138 swap(event->total_time_enabled, next_event->total_time_enabled);
1139 swap(event->total_time_running, next_event->total_time_running);
1142 * Since we swizzled the values, update the user visible data too.
1144 perf_event_update_userpage(event);
1145 perf_event_update_userpage(next_event);
1148 #define list_next_entry(pos, member) \
1149 list_entry(pos->member.next, typeof(*pos), member)
1151 static void perf_event_sync_stat(struct perf_event_context *ctx,
1152 struct perf_event_context *next_ctx)
1154 struct perf_event *event, *next_event;
1159 update_context_time(ctx);
1161 event = list_first_entry(&ctx->event_list,
1162 struct perf_event, event_entry);
1164 next_event = list_first_entry(&next_ctx->event_list,
1165 struct perf_event, event_entry);
1167 while (&event->event_entry != &ctx->event_list &&
1168 &next_event->event_entry != &next_ctx->event_list) {
1170 __perf_event_sync_stat(event, next_event);
1172 event = list_next_entry(event, event_entry);
1173 next_event = list_next_entry(next_event, event_entry);
1178 * Called from scheduler to remove the events of the current task,
1179 * with interrupts disabled.
1181 * We stop each event and update the event value in event->count.
1183 * This does not protect us against NMI, but disable()
1184 * sets the disabled bit in the control field of event _before_
1185 * accessing the event control register. If a NMI hits, then it will
1186 * not restart the event.
1188 void perf_event_task_sched_out(struct task_struct *task,
1189 struct task_struct *next)
1191 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1192 struct perf_event_context *ctx = task->perf_event_ctxp;
1193 struct perf_event_context *next_ctx;
1194 struct perf_event_context *parent;
1197 perf_sw_event(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 1, NULL, 0);
1199 if (likely(!ctx || !cpuctx->task_ctx))
1203 parent = rcu_dereference(ctx->parent_ctx);
1204 next_ctx = next->perf_event_ctxp;
1205 if (parent && next_ctx &&
1206 rcu_dereference(next_ctx->parent_ctx) == parent) {
1208 * Looks like the two contexts are clones, so we might be
1209 * able to optimize the context switch. We lock both
1210 * contexts and check that they are clones under the
1211 * lock (including re-checking that neither has been
1212 * uncloned in the meantime). It doesn't matter which
1213 * order we take the locks because no other cpu could
1214 * be trying to lock both of these tasks.
1216 raw_spin_lock(&ctx->lock);
1217 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
1218 if (context_equiv(ctx, next_ctx)) {
1220 * XXX do we need a memory barrier of sorts
1221 * wrt to rcu_dereference() of perf_event_ctxp
1223 task->perf_event_ctxp = next_ctx;
1224 next->perf_event_ctxp = ctx;
1226 next_ctx->task = task;
1229 perf_event_sync_stat(ctx, next_ctx);
1231 raw_spin_unlock(&next_ctx->lock);
1232 raw_spin_unlock(&ctx->lock);
1237 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
1238 cpuctx->task_ctx = NULL;
1242 static void task_ctx_sched_out(struct perf_event_context *ctx,
1243 enum event_type_t event_type)
1245 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1247 if (!cpuctx->task_ctx)
1250 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
1253 ctx_sched_out(ctx, cpuctx, event_type);
1254 cpuctx->task_ctx = NULL;
1258 * Called with IRQs disabled
1260 static void __perf_event_task_sched_out(struct perf_event_context *ctx)
1262 task_ctx_sched_out(ctx, EVENT_ALL);
1266 * Called with IRQs disabled
1268 static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
1269 enum event_type_t event_type)
1271 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
1275 ctx_pinned_sched_in(struct perf_event_context *ctx,
1276 struct perf_cpu_context *cpuctx)
1278 struct perf_event *event;
1280 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
1281 if (event->state <= PERF_EVENT_STATE_OFF)
1283 if (event->cpu != -1 && event->cpu != smp_processor_id())
1286 if (group_can_go_on(event, cpuctx, 1))
1287 group_sched_in(event, cpuctx, ctx);
1290 * If this pinned group hasn't been scheduled,
1291 * put it in error state.
1293 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1294 update_group_times(event);
1295 event->state = PERF_EVENT_STATE_ERROR;
1301 ctx_flexible_sched_in(struct perf_event_context *ctx,
1302 struct perf_cpu_context *cpuctx)
1304 struct perf_event *event;
1307 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
1308 /* Ignore events in OFF or ERROR state */
1309 if (event->state <= PERF_EVENT_STATE_OFF)
1312 * Listen to the 'cpu' scheduling filter constraint
1315 if (event->cpu != -1 && event->cpu != smp_processor_id())
1318 if (group_can_go_on(event, cpuctx, can_add_hw)) {
1319 if (group_sched_in(event, cpuctx, ctx))
1326 ctx_sched_in(struct perf_event_context *ctx,
1327 struct perf_cpu_context *cpuctx,
1328 enum event_type_t event_type)
1330 raw_spin_lock(&ctx->lock);
1332 if (likely(!ctx->nr_events))
1335 ctx->timestamp = perf_clock();
1338 * First go through the list and put on any pinned groups
1339 * in order to give them the best chance of going on.
1341 if (event_type & EVENT_PINNED)
1342 ctx_pinned_sched_in(ctx, cpuctx);
1344 /* Then walk through the lower prio flexible groups */
1345 if (event_type & EVENT_FLEXIBLE)
1346 ctx_flexible_sched_in(ctx, cpuctx);
1349 raw_spin_unlock(&ctx->lock);
1352 static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
1353 enum event_type_t event_type)
1355 struct perf_event_context *ctx = &cpuctx->ctx;
1357 ctx_sched_in(ctx, cpuctx, event_type);
1360 static void task_ctx_sched_in(struct task_struct *task,
1361 enum event_type_t event_type)
1363 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1364 struct perf_event_context *ctx = task->perf_event_ctxp;
1368 if (cpuctx->task_ctx == ctx)
1370 ctx_sched_in(ctx, cpuctx, event_type);
1371 cpuctx->task_ctx = ctx;
1374 * Called from scheduler to add the events of the current task
1375 * with interrupts disabled.
1377 * We restore the event value and then enable it.
1379 * This does not protect us against NMI, but enable()
1380 * sets the enabled bit in the control field of event _before_
1381 * accessing the event control register. If a NMI hits, then it will
1382 * keep the event running.
1384 void perf_event_task_sched_in(struct task_struct *task)
1386 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1387 struct perf_event_context *ctx = task->perf_event_ctxp;
1392 if (cpuctx->task_ctx == ctx)
1396 * We want to keep the following priority order:
1397 * cpu pinned (that don't need to move), task pinned,
1398 * cpu flexible, task flexible.
1400 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
1402 ctx_sched_in(ctx, cpuctx, EVENT_PINNED);
1403 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE);
1404 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE);
1406 cpuctx->task_ctx = ctx;
1409 * Since these rotations are per-cpu, we need to ensure the
1410 * cpu-context we got scheduled on is actually rotating.
1412 perf_pmu_rotate_start();
1415 #define MAX_INTERRUPTS (~0ULL)
1417 static void perf_log_throttle(struct perf_event *event, int enable);
1419 static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
1421 u64 frequency = event->attr.sample_freq;
1422 u64 sec = NSEC_PER_SEC;
1423 u64 divisor, dividend;
1425 int count_fls, nsec_fls, frequency_fls, sec_fls;
1427 count_fls = fls64(count);
1428 nsec_fls = fls64(nsec);
1429 frequency_fls = fls64(frequency);
1433 * We got @count in @nsec, with a target of sample_freq HZ
1434 * the target period becomes:
1437 * period = -------------------
1438 * @nsec * sample_freq
1443 * Reduce accuracy by one bit such that @a and @b converge
1444 * to a similar magnitude.
1446 #define REDUCE_FLS(a, b) \
1448 if (a##_fls > b##_fls) { \
1458 * Reduce accuracy until either term fits in a u64, then proceed with
1459 * the other, so that finally we can do a u64/u64 division.
1461 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
1462 REDUCE_FLS(nsec, frequency);
1463 REDUCE_FLS(sec, count);
1466 if (count_fls + sec_fls > 64) {
1467 divisor = nsec * frequency;
1469 while (count_fls + sec_fls > 64) {
1470 REDUCE_FLS(count, sec);
1474 dividend = count * sec;
1476 dividend = count * sec;
1478 while (nsec_fls + frequency_fls > 64) {
1479 REDUCE_FLS(nsec, frequency);
1483 divisor = nsec * frequency;
1489 return div64_u64(dividend, divisor);
1492 static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count)
1494 struct hw_perf_event *hwc = &event->hw;
1495 s64 period, sample_period;
1498 period = perf_calculate_period(event, nsec, count);
1500 delta = (s64)(period - hwc->sample_period);
1501 delta = (delta + 7) / 8; /* low pass filter */
1503 sample_period = hwc->sample_period + delta;
1508 hwc->sample_period = sample_period;
1510 if (local64_read(&hwc->period_left) > 8*sample_period) {
1511 event->pmu->stop(event, PERF_EF_UPDATE);
1512 local64_set(&hwc->period_left, 0);
1513 event->pmu->start(event, PERF_EF_RELOAD);
1517 static void perf_ctx_adjust_freq(struct perf_event_context *ctx, u64 period)
1519 struct perf_event *event;
1520 struct hw_perf_event *hwc;
1521 u64 interrupts, now;
1524 raw_spin_lock(&ctx->lock);
1525 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
1526 if (event->state != PERF_EVENT_STATE_ACTIVE)
1529 if (event->cpu != -1 && event->cpu != smp_processor_id())
1534 interrupts = hwc->interrupts;
1535 hwc->interrupts = 0;
1538 * unthrottle events on the tick
1540 if (interrupts == MAX_INTERRUPTS) {
1541 perf_log_throttle(event, 1);
1542 event->pmu->start(event, 0);
1545 if (!event->attr.freq || !event->attr.sample_freq)
1548 event->pmu->read(event);
1549 now = local64_read(&event->count);
1550 delta = now - hwc->freq_count_stamp;
1551 hwc->freq_count_stamp = now;
1554 perf_adjust_period(event, period, delta);
1556 raw_spin_unlock(&ctx->lock);
1560 * Round-robin a context's events:
1562 static void rotate_ctx(struct perf_event_context *ctx)
1564 raw_spin_lock(&ctx->lock);
1566 /* Rotate the first entry last of non-pinned groups */
1567 list_rotate_left(&ctx->flexible_groups);
1569 raw_spin_unlock(&ctx->lock);
1573 * Cannot race with ->pmu_rotate_start() because this is ran from hardirq
1574 * context, and ->pmu_rotate_start() is called with irqs disabled (both are
1575 * cpu affine, so there are no SMP races).
1577 static enum hrtimer_restart perf_event_context_tick(struct hrtimer *timer)
1579 enum hrtimer_restart restart = HRTIMER_NORESTART;
1580 struct perf_cpu_context *cpuctx;
1581 struct perf_event_context *ctx;
1584 cpuctx = container_of(timer, struct perf_cpu_context, timer);
1586 if (cpuctx->ctx.nr_events) {
1587 restart = HRTIMER_RESTART;
1588 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
1592 ctx = current->perf_event_ctxp;
1593 if (ctx && ctx->nr_events) {
1594 restart = HRTIMER_RESTART;
1595 if (ctx->nr_events != ctx->nr_active)
1599 perf_ctx_adjust_freq(&cpuctx->ctx, cpuctx->timer_interval);
1601 perf_ctx_adjust_freq(ctx, cpuctx->timer_interval);
1606 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
1608 task_ctx_sched_out(ctx, EVENT_FLEXIBLE);
1610 rotate_ctx(&cpuctx->ctx);
1614 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE);
1616 task_ctx_sched_in(current, EVENT_FLEXIBLE);
1619 hrtimer_forward_now(timer, ns_to_ktime(cpuctx->timer_interval));
1624 static int event_enable_on_exec(struct perf_event *event,
1625 struct perf_event_context *ctx)
1627 if (!event->attr.enable_on_exec)
1630 event->attr.enable_on_exec = 0;
1631 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1634 __perf_event_mark_enabled(event, ctx);
1640 * Enable all of a task's events that have been marked enable-on-exec.
1641 * This expects task == current.
1643 static void perf_event_enable_on_exec(struct task_struct *task)
1645 struct perf_event_context *ctx;
1646 struct perf_event *event;
1647 unsigned long flags;
1651 local_irq_save(flags);
1652 ctx = task->perf_event_ctxp;
1653 if (!ctx || !ctx->nr_events)
1656 __perf_event_task_sched_out(ctx);
1658 raw_spin_lock(&ctx->lock);
1660 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
1661 ret = event_enable_on_exec(event, ctx);
1666 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
1667 ret = event_enable_on_exec(event, ctx);
1673 * Unclone this context if we enabled any event.
1678 raw_spin_unlock(&ctx->lock);
1680 perf_event_task_sched_in(task);
1682 local_irq_restore(flags);
1686 * Cross CPU call to read the hardware event
1688 static void __perf_event_read(void *info)
1690 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1691 struct perf_event *event = info;
1692 struct perf_event_context *ctx = event->ctx;
1695 * If this is a task context, we need to check whether it is
1696 * the current task context of this cpu. If not it has been
1697 * scheduled out before the smp call arrived. In that case
1698 * event->count would have been updated to a recent sample
1699 * when the event was scheduled out.
1701 if (ctx->task && cpuctx->task_ctx != ctx)
1704 raw_spin_lock(&ctx->lock);
1705 update_context_time(ctx);
1706 update_event_times(event);
1707 raw_spin_unlock(&ctx->lock);
1709 event->pmu->read(event);
1712 static inline u64 perf_event_count(struct perf_event *event)
1714 return local64_read(&event->count) + atomic64_read(&event->child_count);
1717 static u64 perf_event_read(struct perf_event *event)
1720 * If event is enabled and currently active on a CPU, update the
1721 * value in the event structure:
1723 if (event->state == PERF_EVENT_STATE_ACTIVE) {
1724 smp_call_function_single(event->oncpu,
1725 __perf_event_read, event, 1);
1726 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
1727 struct perf_event_context *ctx = event->ctx;
1728 unsigned long flags;
1730 raw_spin_lock_irqsave(&ctx->lock, flags);
1731 update_context_time(ctx);
1732 update_event_times(event);
1733 raw_spin_unlock_irqrestore(&ctx->lock, flags);
1736 return perf_event_count(event);
1743 struct callchain_cpus_entries {
1744 struct rcu_head rcu_head;
1745 struct perf_callchain_entry *cpu_entries[0];
1748 static DEFINE_PER_CPU(int, callchain_recursion[PERF_NR_CONTEXTS]);
1749 static atomic_t nr_callchain_events;
1750 static DEFINE_MUTEX(callchain_mutex);
1751 struct callchain_cpus_entries *callchain_cpus_entries;
1754 __weak void perf_callchain_kernel(struct perf_callchain_entry *entry,
1755 struct pt_regs *regs)
1759 __weak void perf_callchain_user(struct perf_callchain_entry *entry,
1760 struct pt_regs *regs)
1764 static void release_callchain_buffers_rcu(struct rcu_head *head)
1766 struct callchain_cpus_entries *entries;
1769 entries = container_of(head, struct callchain_cpus_entries, rcu_head);
1771 for_each_possible_cpu(cpu)
1772 kfree(entries->cpu_entries[cpu]);
1777 static void release_callchain_buffers(void)
1779 struct callchain_cpus_entries *entries;
1781 entries = callchain_cpus_entries;
1782 rcu_assign_pointer(callchain_cpus_entries, NULL);
1783 call_rcu(&entries->rcu_head, release_callchain_buffers_rcu);
1786 static int alloc_callchain_buffers(void)
1790 struct callchain_cpus_entries *entries;
1793 * We can't use the percpu allocation API for data that can be
1794 * accessed from NMI. Use a temporary manual per cpu allocation
1795 * until that gets sorted out.
1797 size = sizeof(*entries) + sizeof(struct perf_callchain_entry *) *
1798 num_possible_cpus();
1800 entries = kzalloc(size, GFP_KERNEL);
1804 size = sizeof(struct perf_callchain_entry) * PERF_NR_CONTEXTS;
1806 for_each_possible_cpu(cpu) {
1807 entries->cpu_entries[cpu] = kmalloc_node(size, GFP_KERNEL,
1809 if (!entries->cpu_entries[cpu])
1813 rcu_assign_pointer(callchain_cpus_entries, entries);
1818 for_each_possible_cpu(cpu)
1819 kfree(entries->cpu_entries[cpu]);
1825 static int get_callchain_buffers(void)
1830 mutex_lock(&callchain_mutex);
1832 count = atomic_inc_return(&nr_callchain_events);
1833 if (WARN_ON_ONCE(count < 1)) {
1839 /* If the allocation failed, give up */
1840 if (!callchain_cpus_entries)
1845 err = alloc_callchain_buffers();
1847 release_callchain_buffers();
1849 mutex_unlock(&callchain_mutex);
1854 static void put_callchain_buffers(void)
1856 if (atomic_dec_and_mutex_lock(&nr_callchain_events, &callchain_mutex)) {
1857 release_callchain_buffers();
1858 mutex_unlock(&callchain_mutex);
1862 static int get_recursion_context(int *recursion)
1870 else if (in_softirq())
1875 if (recursion[rctx])
1884 static inline void put_recursion_context(int *recursion, int rctx)
1890 static struct perf_callchain_entry *get_callchain_entry(int *rctx)
1893 struct callchain_cpus_entries *entries;
1895 *rctx = get_recursion_context(__get_cpu_var(callchain_recursion));
1899 entries = rcu_dereference(callchain_cpus_entries);
1903 cpu = smp_processor_id();
1905 return &entries->cpu_entries[cpu][*rctx];
1909 put_callchain_entry(int rctx)
1911 put_recursion_context(__get_cpu_var(callchain_recursion), rctx);
1914 static struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
1917 struct perf_callchain_entry *entry;
1920 entry = get_callchain_entry(&rctx);
1929 if (!user_mode(regs)) {
1930 perf_callchain_store(entry, PERF_CONTEXT_KERNEL);
1931 perf_callchain_kernel(entry, regs);
1933 regs = task_pt_regs(current);
1939 perf_callchain_store(entry, PERF_CONTEXT_USER);
1940 perf_callchain_user(entry, regs);
1944 put_callchain_entry(rctx);
1950 * Initialize the perf_event context in a task_struct:
1953 __perf_event_init_context(struct perf_event_context *ctx,
1954 struct task_struct *task)
1956 raw_spin_lock_init(&ctx->lock);
1957 mutex_init(&ctx->mutex);
1958 INIT_LIST_HEAD(&ctx->pinned_groups);
1959 INIT_LIST_HEAD(&ctx->flexible_groups);
1960 INIT_LIST_HEAD(&ctx->event_list);
1961 atomic_set(&ctx->refcount, 1);
1965 static struct perf_event_context *find_get_context(pid_t pid, int cpu)
1967 struct perf_event_context *ctx;
1968 struct perf_cpu_context *cpuctx;
1969 struct task_struct *task;
1970 unsigned long flags;
1973 if (pid == -1 && cpu != -1) {
1974 /* Must be root to operate on a CPU event: */
1975 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
1976 return ERR_PTR(-EACCES);
1978 if (cpu < 0 || cpu >= nr_cpumask_bits)
1979 return ERR_PTR(-EINVAL);
1982 * We could be clever and allow to attach a event to an
1983 * offline CPU and activate it when the CPU comes up, but
1986 if (!cpu_online(cpu))
1987 return ERR_PTR(-ENODEV);
1989 cpuctx = &per_cpu(perf_cpu_context, cpu);
2000 task = find_task_by_vpid(pid);
2002 get_task_struct(task);
2006 return ERR_PTR(-ESRCH);
2009 * Can't attach events to a dying task.
2012 if (task->flags & PF_EXITING)
2015 /* Reuse ptrace permission checks for now. */
2017 if (!ptrace_may_access(task, PTRACE_MODE_READ))
2021 ctx = perf_lock_task_context(task, &flags);
2024 raw_spin_unlock_irqrestore(&ctx->lock, flags);
2028 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
2032 __perf_event_init_context(ctx, task);
2034 if (cmpxchg(&task->perf_event_ctxp, NULL, ctx)) {
2036 * We raced with some other task; use
2037 * the context they set.
2042 get_task_struct(task);
2045 put_task_struct(task);
2049 put_task_struct(task);
2050 return ERR_PTR(err);
2053 static void perf_event_free_filter(struct perf_event *event);
2055 static void free_event_rcu(struct rcu_head *head)
2057 struct perf_event *event;
2059 event = container_of(head, struct perf_event, rcu_head);
2061 put_pid_ns(event->ns);
2062 perf_event_free_filter(event);
2066 static void perf_pending_sync(struct perf_event *event);
2067 static void perf_buffer_put(struct perf_buffer *buffer);
2069 static void free_event(struct perf_event *event)
2071 perf_pending_sync(event);
2073 if (!event->parent) {
2074 atomic_dec(&nr_events);
2075 if (event->attr.mmap || event->attr.mmap_data)
2076 atomic_dec(&nr_mmap_events);
2077 if (event->attr.comm)
2078 atomic_dec(&nr_comm_events);
2079 if (event->attr.task)
2080 atomic_dec(&nr_task_events);
2081 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
2082 put_callchain_buffers();
2085 if (event->buffer) {
2086 perf_buffer_put(event->buffer);
2087 event->buffer = NULL;
2091 event->destroy(event);
2093 put_ctx(event->ctx);
2094 call_rcu(&event->rcu_head, free_event_rcu);
2097 int perf_event_release_kernel(struct perf_event *event)
2099 struct perf_event_context *ctx = event->ctx;
2102 * Remove from the PMU, can't get re-enabled since we got
2103 * here because the last ref went.
2105 perf_event_disable(event);
2107 WARN_ON_ONCE(ctx->parent_ctx);
2109 * There are two ways this annotation is useful:
2111 * 1) there is a lock recursion from perf_event_exit_task
2112 * see the comment there.
2114 * 2) there is a lock-inversion with mmap_sem through
2115 * perf_event_read_group(), which takes faults while
2116 * holding ctx->mutex, however this is called after
2117 * the last filedesc died, so there is no possibility
2118 * to trigger the AB-BA case.
2120 mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
2121 raw_spin_lock_irq(&ctx->lock);
2122 perf_group_detach(event);
2123 list_del_event(event, ctx);
2124 raw_spin_unlock_irq(&ctx->lock);
2125 mutex_unlock(&ctx->mutex);
2127 mutex_lock(&event->owner->perf_event_mutex);
2128 list_del_init(&event->owner_entry);
2129 mutex_unlock(&event->owner->perf_event_mutex);
2130 put_task_struct(event->owner);
2136 EXPORT_SYMBOL_GPL(perf_event_release_kernel);
2139 * Called when the last reference to the file is gone.
2141 static int perf_release(struct inode *inode, struct file *file)
2143 struct perf_event *event = file->private_data;
2145 file->private_data = NULL;
2147 return perf_event_release_kernel(event);
2150 static int perf_event_read_size(struct perf_event *event)
2152 int entry = sizeof(u64); /* value */
2156 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2157 size += sizeof(u64);
2159 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2160 size += sizeof(u64);
2162 if (event->attr.read_format & PERF_FORMAT_ID)
2163 entry += sizeof(u64);
2165 if (event->attr.read_format & PERF_FORMAT_GROUP) {
2166 nr += event->group_leader->nr_siblings;
2167 size += sizeof(u64);
2175 u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
2177 struct perf_event *child;
2183 mutex_lock(&event->child_mutex);
2184 total += perf_event_read(event);
2185 *enabled += event->total_time_enabled +
2186 atomic64_read(&event->child_total_time_enabled);
2187 *running += event->total_time_running +
2188 atomic64_read(&event->child_total_time_running);
2190 list_for_each_entry(child, &event->child_list, child_list) {
2191 total += perf_event_read(child);
2192 *enabled += child->total_time_enabled;
2193 *running += child->total_time_running;
2195 mutex_unlock(&event->child_mutex);
2199 EXPORT_SYMBOL_GPL(perf_event_read_value);
2201 static int perf_event_read_group(struct perf_event *event,
2202 u64 read_format, char __user *buf)
2204 struct perf_event *leader = event->group_leader, *sub;
2205 int n = 0, size = 0, ret = -EFAULT;
2206 struct perf_event_context *ctx = leader->ctx;
2208 u64 count, enabled, running;
2210 mutex_lock(&ctx->mutex);
2211 count = perf_event_read_value(leader, &enabled, &running);
2213 values[n++] = 1 + leader->nr_siblings;
2214 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2215 values[n++] = enabled;
2216 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2217 values[n++] = running;
2218 values[n++] = count;
2219 if (read_format & PERF_FORMAT_ID)
2220 values[n++] = primary_event_id(leader);
2222 size = n * sizeof(u64);
2224 if (copy_to_user(buf, values, size))
2229 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
2232 values[n++] = perf_event_read_value(sub, &enabled, &running);
2233 if (read_format & PERF_FORMAT_ID)
2234 values[n++] = primary_event_id(sub);
2236 size = n * sizeof(u64);
2238 if (copy_to_user(buf + ret, values, size)) {
2246 mutex_unlock(&ctx->mutex);
2251 static int perf_event_read_one(struct perf_event *event,
2252 u64 read_format, char __user *buf)
2254 u64 enabled, running;
2258 values[n++] = perf_event_read_value(event, &enabled, &running);
2259 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2260 values[n++] = enabled;
2261 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2262 values[n++] = running;
2263 if (read_format & PERF_FORMAT_ID)
2264 values[n++] = primary_event_id(event);
2266 if (copy_to_user(buf, values, n * sizeof(u64)))
2269 return n * sizeof(u64);
2273 * Read the performance event - simple non blocking version for now
2276 perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
2278 u64 read_format = event->attr.read_format;
2282 * Return end-of-file for a read on a event that is in
2283 * error state (i.e. because it was pinned but it couldn't be
2284 * scheduled on to the CPU at some point).
2286 if (event->state == PERF_EVENT_STATE_ERROR)
2289 if (count < perf_event_read_size(event))
2292 WARN_ON_ONCE(event->ctx->parent_ctx);
2293 if (read_format & PERF_FORMAT_GROUP)
2294 ret = perf_event_read_group(event, read_format, buf);
2296 ret = perf_event_read_one(event, read_format, buf);
2302 perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
2304 struct perf_event *event = file->private_data;
2306 return perf_read_hw(event, buf, count);
2309 static unsigned int perf_poll(struct file *file, poll_table *wait)
2311 struct perf_event *event = file->private_data;
2312 struct perf_buffer *buffer;
2313 unsigned int events = POLL_HUP;
2316 buffer = rcu_dereference(event->buffer);
2318 events = atomic_xchg(&buffer->poll, 0);
2321 poll_wait(file, &event->waitq, wait);
2326 static void perf_event_reset(struct perf_event *event)
2328 (void)perf_event_read(event);
2329 local64_set(&event->count, 0);
2330 perf_event_update_userpage(event);
2334 * Holding the top-level event's child_mutex means that any
2335 * descendant process that has inherited this event will block
2336 * in sync_child_event if it goes to exit, thus satisfying the
2337 * task existence requirements of perf_event_enable/disable.
2339 static void perf_event_for_each_child(struct perf_event *event,
2340 void (*func)(struct perf_event *))
2342 struct perf_event *child;
2344 WARN_ON_ONCE(event->ctx->parent_ctx);
2345 mutex_lock(&event->child_mutex);
2347 list_for_each_entry(child, &event->child_list, child_list)
2349 mutex_unlock(&event->child_mutex);
2352 static void perf_event_for_each(struct perf_event *event,
2353 void (*func)(struct perf_event *))
2355 struct perf_event_context *ctx = event->ctx;
2356 struct perf_event *sibling;
2358 WARN_ON_ONCE(ctx->parent_ctx);
2359 mutex_lock(&ctx->mutex);
2360 event = event->group_leader;
2362 perf_event_for_each_child(event, func);
2364 list_for_each_entry(sibling, &event->sibling_list, group_entry)
2365 perf_event_for_each_child(event, func);
2366 mutex_unlock(&ctx->mutex);
2369 static int perf_event_period(struct perf_event *event, u64 __user *arg)
2371 struct perf_event_context *ctx = event->ctx;
2376 if (!event->attr.sample_period)
2379 size = copy_from_user(&value, arg, sizeof(value));
2380 if (size != sizeof(value))
2386 raw_spin_lock_irq(&ctx->lock);
2387 if (event->attr.freq) {
2388 if (value > sysctl_perf_event_sample_rate) {
2393 event->attr.sample_freq = value;
2395 event->attr.sample_period = value;
2396 event->hw.sample_period = value;
2399 raw_spin_unlock_irq(&ctx->lock);
2404 static const struct file_operations perf_fops;
2406 static struct perf_event *perf_fget_light(int fd, int *fput_needed)
2410 file = fget_light(fd, fput_needed);
2412 return ERR_PTR(-EBADF);
2414 if (file->f_op != &perf_fops) {
2415 fput_light(file, *fput_needed);
2417 return ERR_PTR(-EBADF);
2420 return file->private_data;
2423 static int perf_event_set_output(struct perf_event *event,
2424 struct perf_event *output_event);
2425 static int perf_event_set_filter(struct perf_event *event, void __user *arg);
2427 static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2429 struct perf_event *event = file->private_data;
2430 void (*func)(struct perf_event *);
2434 case PERF_EVENT_IOC_ENABLE:
2435 func = perf_event_enable;
2437 case PERF_EVENT_IOC_DISABLE:
2438 func = perf_event_disable;
2440 case PERF_EVENT_IOC_RESET:
2441 func = perf_event_reset;
2444 case PERF_EVENT_IOC_REFRESH:
2445 return perf_event_refresh(event, arg);
2447 case PERF_EVENT_IOC_PERIOD:
2448 return perf_event_period(event, (u64 __user *)arg);
2450 case PERF_EVENT_IOC_SET_OUTPUT:
2452 struct perf_event *output_event = NULL;
2453 int fput_needed = 0;
2457 output_event = perf_fget_light(arg, &fput_needed);
2458 if (IS_ERR(output_event))
2459 return PTR_ERR(output_event);
2462 ret = perf_event_set_output(event, output_event);
2464 fput_light(output_event->filp, fput_needed);
2469 case PERF_EVENT_IOC_SET_FILTER:
2470 return perf_event_set_filter(event, (void __user *)arg);
2476 if (flags & PERF_IOC_FLAG_GROUP)
2477 perf_event_for_each(event, func);
2479 perf_event_for_each_child(event, func);
2484 int perf_event_task_enable(void)
2486 struct perf_event *event;
2488 mutex_lock(¤t->perf_event_mutex);
2489 list_for_each_entry(event, ¤t->perf_event_list, owner_entry)
2490 perf_event_for_each_child(event, perf_event_enable);
2491 mutex_unlock(¤t->perf_event_mutex);
2496 int perf_event_task_disable(void)
2498 struct perf_event *event;
2500 mutex_lock(¤t->perf_event_mutex);
2501 list_for_each_entry(event, ¤t->perf_event_list, owner_entry)
2502 perf_event_for_each_child(event, perf_event_disable);
2503 mutex_unlock(¤t->perf_event_mutex);
2508 #ifndef PERF_EVENT_INDEX_OFFSET
2509 # define PERF_EVENT_INDEX_OFFSET 0
2512 static int perf_event_index(struct perf_event *event)
2514 if (event->hw.state & PERF_HES_STOPPED)
2517 if (event->state != PERF_EVENT_STATE_ACTIVE)
2520 return event->hw.idx + 1 - PERF_EVENT_INDEX_OFFSET;
2524 * Callers need to ensure there can be no nesting of this function, otherwise
2525 * the seqlock logic goes bad. We can not serialize this because the arch
2526 * code calls this from NMI context.
2528 void perf_event_update_userpage(struct perf_event *event)
2530 struct perf_event_mmap_page *userpg;
2531 struct perf_buffer *buffer;
2534 buffer = rcu_dereference(event->buffer);
2538 userpg = buffer->user_page;
2541 * Disable preemption so as to not let the corresponding user-space
2542 * spin too long if we get preempted.
2547 userpg->index = perf_event_index(event);
2548 userpg->offset = perf_event_count(event);
2549 if (event->state == PERF_EVENT_STATE_ACTIVE)
2550 userpg->offset -= local64_read(&event->hw.prev_count);
2552 userpg->time_enabled = event->total_time_enabled +
2553 atomic64_read(&event->child_total_time_enabled);
2555 userpg->time_running = event->total_time_running +
2556 atomic64_read(&event->child_total_time_running);
2565 static unsigned long perf_data_size(struct perf_buffer *buffer);
2568 perf_buffer_init(struct perf_buffer *buffer, long watermark, int flags)
2570 long max_size = perf_data_size(buffer);
2573 buffer->watermark = min(max_size, watermark);
2575 if (!buffer->watermark)
2576 buffer->watermark = max_size / 2;
2578 if (flags & PERF_BUFFER_WRITABLE)
2579 buffer->writable = 1;
2581 atomic_set(&buffer->refcount, 1);
2584 #ifndef CONFIG_PERF_USE_VMALLOC
2587 * Back perf_mmap() with regular GFP_KERNEL-0 pages.
2590 static struct page *
2591 perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
2593 if (pgoff > buffer->nr_pages)
2597 return virt_to_page(buffer->user_page);
2599 return virt_to_page(buffer->data_pages[pgoff - 1]);
2602 static void *perf_mmap_alloc_page(int cpu)
2607 node = (cpu == -1) ? cpu : cpu_to_node(cpu);
2608 page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
2612 return page_address(page);
2615 static struct perf_buffer *
2616 perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
2618 struct perf_buffer *buffer;
2622 size = sizeof(struct perf_buffer);
2623 size += nr_pages * sizeof(void *);
2625 buffer = kzalloc(size, GFP_KERNEL);
2629 buffer->user_page = perf_mmap_alloc_page(cpu);
2630 if (!buffer->user_page)
2631 goto fail_user_page;
2633 for (i = 0; i < nr_pages; i++) {
2634 buffer->data_pages[i] = perf_mmap_alloc_page(cpu);
2635 if (!buffer->data_pages[i])
2636 goto fail_data_pages;
2639 buffer->nr_pages = nr_pages;
2641 perf_buffer_init(buffer, watermark, flags);
2646 for (i--; i >= 0; i--)
2647 free_page((unsigned long)buffer->data_pages[i]);
2649 free_page((unsigned long)buffer->user_page);
2658 static void perf_mmap_free_page(unsigned long addr)
2660 struct page *page = virt_to_page((void *)addr);
2662 page->mapping = NULL;
2666 static void perf_buffer_free(struct perf_buffer *buffer)
2670 perf_mmap_free_page((unsigned long)buffer->user_page);
2671 for (i = 0; i < buffer->nr_pages; i++)
2672 perf_mmap_free_page((unsigned long)buffer->data_pages[i]);
2676 static inline int page_order(struct perf_buffer *buffer)
2684 * Back perf_mmap() with vmalloc memory.
2686 * Required for architectures that have d-cache aliasing issues.
2689 static inline int page_order(struct perf_buffer *buffer)
2691 return buffer->page_order;
2694 static struct page *
2695 perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
2697 if (pgoff > (1UL << page_order(buffer)))
2700 return vmalloc_to_page((void *)buffer->user_page + pgoff * PAGE_SIZE);
2703 static void perf_mmap_unmark_page(void *addr)
2705 struct page *page = vmalloc_to_page(addr);
2707 page->mapping = NULL;
2710 static void perf_buffer_free_work(struct work_struct *work)
2712 struct perf_buffer *buffer;
2716 buffer = container_of(work, struct perf_buffer, work);
2717 nr = 1 << page_order(buffer);
2719 base = buffer->user_page;
2720 for (i = 0; i < nr + 1; i++)
2721 perf_mmap_unmark_page(base + (i * PAGE_SIZE));
2727 static void perf_buffer_free(struct perf_buffer *buffer)
2729 schedule_work(&buffer->work);
2732 static struct perf_buffer *
2733 perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
2735 struct perf_buffer *buffer;
2739 size = sizeof(struct perf_buffer);
2740 size += sizeof(void *);
2742 buffer = kzalloc(size, GFP_KERNEL);
2746 INIT_WORK(&buffer->work, perf_buffer_free_work);
2748 all_buf = vmalloc_user((nr_pages + 1) * PAGE_SIZE);
2752 buffer->user_page = all_buf;
2753 buffer->data_pages[0] = all_buf + PAGE_SIZE;
2754 buffer->page_order = ilog2(nr_pages);
2755 buffer->nr_pages = 1;
2757 perf_buffer_init(buffer, watermark, flags);
2770 static unsigned long perf_data_size(struct perf_buffer *buffer)
2772 return buffer->nr_pages << (PAGE_SHIFT + page_order(buffer));
2775 static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
2777 struct perf_event *event = vma->vm_file->private_data;
2778 struct perf_buffer *buffer;
2779 int ret = VM_FAULT_SIGBUS;
2781 if (vmf->flags & FAULT_FLAG_MKWRITE) {
2782 if (vmf->pgoff == 0)
2788 buffer = rcu_dereference(event->buffer);
2792 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
2795 vmf->page = perf_mmap_to_page(buffer, vmf->pgoff);
2799 get_page(vmf->page);
2800 vmf->page->mapping = vma->vm_file->f_mapping;
2801 vmf->page->index = vmf->pgoff;
2810 static void perf_buffer_free_rcu(struct rcu_head *rcu_head)
2812 struct perf_buffer *buffer;
2814 buffer = container_of(rcu_head, struct perf_buffer, rcu_head);
2815 perf_buffer_free(buffer);
2818 static struct perf_buffer *perf_buffer_get(struct perf_event *event)
2820 struct perf_buffer *buffer;
2823 buffer = rcu_dereference(event->buffer);
2825 if (!atomic_inc_not_zero(&buffer->refcount))
2833 static void perf_buffer_put(struct perf_buffer *buffer)
2835 if (!atomic_dec_and_test(&buffer->refcount))
2838 call_rcu(&buffer->rcu_head, perf_buffer_free_rcu);
2841 static void perf_mmap_open(struct vm_area_struct *vma)
2843 struct perf_event *event = vma->vm_file->private_data;
2845 atomic_inc(&event->mmap_count);
2848 static void perf_mmap_close(struct vm_area_struct *vma)
2850 struct perf_event *event = vma->vm_file->private_data;
2852 if (atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) {
2853 unsigned long size = perf_data_size(event->buffer);
2854 struct user_struct *user = event->mmap_user;
2855 struct perf_buffer *buffer = event->buffer;
2857 atomic_long_sub((size >> PAGE_SHIFT) + 1, &user->locked_vm);
2858 vma->vm_mm->locked_vm -= event->mmap_locked;
2859 rcu_assign_pointer(event->buffer, NULL);
2860 mutex_unlock(&event->mmap_mutex);
2862 perf_buffer_put(buffer);
2867 static const struct vm_operations_struct perf_mmap_vmops = {
2868 .open = perf_mmap_open,
2869 .close = perf_mmap_close,
2870 .fault = perf_mmap_fault,
2871 .page_mkwrite = perf_mmap_fault,
2874 static int perf_mmap(struct file *file, struct vm_area_struct *vma)
2876 struct perf_event *event = file->private_data;
2877 unsigned long user_locked, user_lock_limit;
2878 struct user_struct *user = current_user();
2879 unsigned long locked, lock_limit;
2880 struct perf_buffer *buffer;
2881 unsigned long vma_size;
2882 unsigned long nr_pages;
2883 long user_extra, extra;
2884 int ret = 0, flags = 0;
2887 * Don't allow mmap() of inherited per-task counters. This would
2888 * create a performance issue due to all children writing to the
2891 if (event->cpu == -1 && event->attr.inherit)
2894 if (!(vma->vm_flags & VM_SHARED))
2897 vma_size = vma->vm_end - vma->vm_start;
2898 nr_pages = (vma_size / PAGE_SIZE) - 1;
2901 * If we have buffer pages ensure they're a power-of-two number, so we
2902 * can do bitmasks instead of modulo.
2904 if (nr_pages != 0 && !is_power_of_2(nr_pages))
2907 if (vma_size != PAGE_SIZE * (1 + nr_pages))
2910 if (vma->vm_pgoff != 0)
2913 WARN_ON_ONCE(event->ctx->parent_ctx);
2914 mutex_lock(&event->mmap_mutex);
2915 if (event->buffer) {
2916 if (event->buffer->nr_pages == nr_pages)
2917 atomic_inc(&event->buffer->refcount);
2923 user_extra = nr_pages + 1;
2924 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
2927 * Increase the limit linearly with more CPUs:
2929 user_lock_limit *= num_online_cpus();
2931 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
2934 if (user_locked > user_lock_limit)
2935 extra = user_locked - user_lock_limit;
2937 lock_limit = rlimit(RLIMIT_MEMLOCK);
2938 lock_limit >>= PAGE_SHIFT;
2939 locked = vma->vm_mm->locked_vm + extra;
2941 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
2942 !capable(CAP_IPC_LOCK)) {
2947 WARN_ON(event->buffer);
2949 if (vma->vm_flags & VM_WRITE)
2950 flags |= PERF_BUFFER_WRITABLE;
2952 buffer = perf_buffer_alloc(nr_pages, event->attr.wakeup_watermark,
2958 rcu_assign_pointer(event->buffer, buffer);
2960 atomic_long_add(user_extra, &user->locked_vm);
2961 event->mmap_locked = extra;
2962 event->mmap_user = get_current_user();
2963 vma->vm_mm->locked_vm += event->mmap_locked;
2967 atomic_inc(&event->mmap_count);
2968 mutex_unlock(&event->mmap_mutex);
2970 vma->vm_flags |= VM_RESERVED;
2971 vma->vm_ops = &perf_mmap_vmops;
2976 static int perf_fasync(int fd, struct file *filp, int on)
2978 struct inode *inode = filp->f_path.dentry->d_inode;
2979 struct perf_event *event = filp->private_data;
2982 mutex_lock(&inode->i_mutex);
2983 retval = fasync_helper(fd, filp, on, &event->fasync);
2984 mutex_unlock(&inode->i_mutex);
2992 static const struct file_operations perf_fops = {
2993 .llseek = no_llseek,
2994 .release = perf_release,
2997 .unlocked_ioctl = perf_ioctl,
2998 .compat_ioctl = perf_ioctl,
3000 .fasync = perf_fasync,
3006 * If there's data, ensure we set the poll() state and publish everything
3007 * to user-space before waking everybody up.
3010 void perf_event_wakeup(struct perf_event *event)
3012 wake_up_all(&event->waitq);
3014 if (event->pending_kill) {
3015 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
3016 event->pending_kill = 0;
3023 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
3025 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
3026 * single linked list and use cmpxchg() to add entries lockless.
3029 static void perf_pending_event(struct perf_pending_entry *entry)
3031 struct perf_event *event = container_of(entry,
3032 struct perf_event, pending);
3034 if (event->pending_disable) {
3035 event->pending_disable = 0;
3036 __perf_event_disable(event);
3039 if (event->pending_wakeup) {
3040 event->pending_wakeup = 0;
3041 perf_event_wakeup(event);
3045 #define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
3047 static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
3051 static void perf_pending_queue(struct perf_pending_entry *entry,
3052 void (*func)(struct perf_pending_entry *))
3054 struct perf_pending_entry **head;
3056 if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
3061 head = &get_cpu_var(perf_pending_head);
3064 entry->next = *head;
3065 } while (cmpxchg(head, entry->next, entry) != entry->next);
3067 set_perf_event_pending();
3069 put_cpu_var(perf_pending_head);
3072 static int __perf_pending_run(void)
3074 struct perf_pending_entry *list;
3077 list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
3078 while (list != PENDING_TAIL) {
3079 void (*func)(struct perf_pending_entry *);
3080 struct perf_pending_entry *entry = list;
3087 * Ensure we observe the unqueue before we issue the wakeup,
3088 * so that we won't be waiting forever.
3089 * -- see perf_not_pending().
3100 static inline int perf_not_pending(struct perf_event *event)
3103 * If we flush on whatever cpu we run, there is a chance we don't
3107 __perf_pending_run();
3111 * Ensure we see the proper queue state before going to sleep
3112 * so that we do not miss the wakeup. -- see perf_pending_handle()
3115 return event->pending.next == NULL;
3118 static void perf_pending_sync(struct perf_event *event)
3120 wait_event(event->waitq, perf_not_pending(event));
3123 void perf_event_do_pending(void)
3125 __perf_pending_run();
3129 * We assume there is only KVM supporting the callbacks.
3130 * Later on, we might change it to a list if there is
3131 * another virtualization implementation supporting the callbacks.
3133 struct perf_guest_info_callbacks *perf_guest_cbs;
3135 int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3137 perf_guest_cbs = cbs;
3140 EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
3142 int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3144 perf_guest_cbs = NULL;
3147 EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
3152 static bool perf_output_space(struct perf_buffer *buffer, unsigned long tail,
3153 unsigned long offset, unsigned long head)
3157 if (!buffer->writable)
3160 mask = perf_data_size(buffer) - 1;
3162 offset = (offset - tail) & mask;
3163 head = (head - tail) & mask;
3165 if ((int)(head - offset) < 0)
3171 static void perf_output_wakeup(struct perf_output_handle *handle)
3173 atomic_set(&handle->buffer->poll, POLL_IN);
3176 handle->event->pending_wakeup = 1;
3177 perf_pending_queue(&handle->event->pending,
3178 perf_pending_event);
3180 perf_event_wakeup(handle->event);
3184 * We need to ensure a later event_id doesn't publish a head when a former
3185 * event isn't done writing. However since we need to deal with NMIs we
3186 * cannot fully serialize things.
3188 * We only publish the head (and generate a wakeup) when the outer-most
3191 static void perf_output_get_handle(struct perf_output_handle *handle)
3193 struct perf_buffer *buffer = handle->buffer;
3196 local_inc(&buffer->nest);
3197 handle->wakeup = local_read(&buffer->wakeup);
3200 static void perf_output_put_handle(struct perf_output_handle *handle)
3202 struct perf_buffer *buffer = handle->buffer;
3206 head = local_read(&buffer->head);
3209 * IRQ/NMI can happen here, which means we can miss a head update.
3212 if (!local_dec_and_test(&buffer->nest))
3216 * Publish the known good head. Rely on the full barrier implied
3217 * by atomic_dec_and_test() order the buffer->head read and this
3220 buffer->user_page->data_head = head;
3223 * Now check if we missed an update, rely on the (compiler)
3224 * barrier in atomic_dec_and_test() to re-read buffer->head.
3226 if (unlikely(head != local_read(&buffer->head))) {
3227 local_inc(&buffer->nest);
3231 if (handle->wakeup != local_read(&buffer->wakeup))
3232 perf_output_wakeup(handle);
3238 __always_inline void perf_output_copy(struct perf_output_handle *handle,
3239 const void *buf, unsigned int len)
3242 unsigned long size = min_t(unsigned long, handle->size, len);
3244 memcpy(handle->addr, buf, size);
3247 handle->addr += size;
3249 handle->size -= size;
3250 if (!handle->size) {
3251 struct perf_buffer *buffer = handle->buffer;
3254 handle->page &= buffer->nr_pages - 1;
3255 handle->addr = buffer->data_pages[handle->page];
3256 handle->size = PAGE_SIZE << page_order(buffer);
3261 int perf_output_begin(struct perf_output_handle *handle,
3262 struct perf_event *event, unsigned int size,
3263 int nmi, int sample)
3265 struct perf_buffer *buffer;
3266 unsigned long tail, offset, head;
3269 struct perf_event_header header;
3276 * For inherited events we send all the output towards the parent.
3279 event = event->parent;
3281 buffer = rcu_dereference(event->buffer);
3285 handle->buffer = buffer;
3286 handle->event = event;
3288 handle->sample = sample;
3290 if (!buffer->nr_pages)
3293 have_lost = local_read(&buffer->lost);
3295 size += sizeof(lost_event);
3297 perf_output_get_handle(handle);
3301 * Userspace could choose to issue a mb() before updating the
3302 * tail pointer. So that all reads will be completed before the
3305 tail = ACCESS_ONCE(buffer->user_page->data_tail);
3307 offset = head = local_read(&buffer->head);
3309 if (unlikely(!perf_output_space(buffer, tail, offset, head)))
3311 } while (local_cmpxchg(&buffer->head, offset, head) != offset);
3313 if (head - local_read(&buffer->wakeup) > buffer->watermark)
3314 local_add(buffer->watermark, &buffer->wakeup);
3316 handle->page = offset >> (PAGE_SHIFT + page_order(buffer));
3317 handle->page &= buffer->nr_pages - 1;
3318 handle->size = offset & ((PAGE_SIZE << page_order(buffer)) - 1);
3319 handle->addr = buffer->data_pages[handle->page];
3320 handle->addr += handle->size;
3321 handle->size = (PAGE_SIZE << page_order(buffer)) - handle->size;
3324 lost_event.header.type = PERF_RECORD_LOST;
3325 lost_event.header.misc = 0;
3326 lost_event.header.size = sizeof(lost_event);
3327 lost_event.id = event->id;
3328 lost_event.lost = local_xchg(&buffer->lost, 0);
3330 perf_output_put(handle, lost_event);
3336 local_inc(&buffer->lost);
3337 perf_output_put_handle(handle);
3344 void perf_output_end(struct perf_output_handle *handle)
3346 struct perf_event *event = handle->event;
3347 struct perf_buffer *buffer = handle->buffer;
3349 int wakeup_events = event->attr.wakeup_events;
3351 if (handle->sample && wakeup_events) {
3352 int events = local_inc_return(&buffer->events);
3353 if (events >= wakeup_events) {
3354 local_sub(wakeup_events, &buffer->events);
3355 local_inc(&buffer->wakeup);
3359 perf_output_put_handle(handle);
3363 static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
3366 * only top level events have the pid namespace they were created in
3369 event = event->parent;
3371 return task_tgid_nr_ns(p, event->ns);
3374 static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
3377 * only top level events have the pid namespace they were created in
3380 event = event->parent;
3382 return task_pid_nr_ns(p, event->ns);
3385 static void perf_output_read_one(struct perf_output_handle *handle,
3386 struct perf_event *event)
3388 u64 read_format = event->attr.read_format;
3392 values[n++] = perf_event_count(event);
3393 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
3394 values[n++] = event->total_time_enabled +
3395 atomic64_read(&event->child_total_time_enabled);
3397 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
3398 values[n++] = event->total_time_running +
3399 atomic64_read(&event->child_total_time_running);
3401 if (read_format & PERF_FORMAT_ID)
3402 values[n++] = primary_event_id(event);
3404 perf_output_copy(handle, values, n * sizeof(u64));
3408 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
3410 static void perf_output_read_group(struct perf_output_handle *handle,
3411 struct perf_event *event)
3413 struct perf_event *leader = event->group_leader, *sub;
3414 u64 read_format = event->attr.read_format;
3418 values[n++] = 1 + leader->nr_siblings;
3420 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3421 values[n++] = leader->total_time_enabled;
3423 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3424 values[n++] = leader->total_time_running;
3426 if (leader != event)
3427 leader->pmu->read(leader);
3429 values[n++] = perf_event_count(leader);
3430 if (read_format & PERF_FORMAT_ID)
3431 values[n++] = primary_event_id(leader);
3433 perf_output_copy(handle, values, n * sizeof(u64));
3435 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
3439 sub->pmu->read(sub);
3441 values[n++] = perf_event_count(sub);
3442 if (read_format & PERF_FORMAT_ID)
3443 values[n++] = primary_event_id(sub);
3445 perf_output_copy(handle, values, n * sizeof(u64));
3449 static void perf_output_read(struct perf_output_handle *handle,
3450 struct perf_event *event)
3452 if (event->attr.read_format & PERF_FORMAT_GROUP)
3453 perf_output_read_group(handle, event);
3455 perf_output_read_one(handle, event);
3458 void perf_output_sample(struct perf_output_handle *handle,
3459 struct perf_event_header *header,
3460 struct perf_sample_data *data,
3461 struct perf_event *event)
3463 u64 sample_type = data->type;
3465 perf_output_put(handle, *header);
3467 if (sample_type & PERF_SAMPLE_IP)
3468 perf_output_put(handle, data->ip);
3470 if (sample_type & PERF_SAMPLE_TID)
3471 perf_output_put(handle, data->tid_entry);
3473 if (sample_type & PERF_SAMPLE_TIME)
3474 perf_output_put(handle, data->time);
3476 if (sample_type & PERF_SAMPLE_ADDR)
3477 perf_output_put(handle, data->addr);
3479 if (sample_type & PERF_SAMPLE_ID)
3480 perf_output_put(handle, data->id);
3482 if (sample_type & PERF_SAMPLE_STREAM_ID)
3483 perf_output_put(handle, data->stream_id);
3485 if (sample_type & PERF_SAMPLE_CPU)
3486 perf_output_put(handle, data->cpu_entry);
3488 if (sample_type & PERF_SAMPLE_PERIOD)
3489 perf_output_put(handle, data->period);
3491 if (sample_type & PERF_SAMPLE_READ)
3492 perf_output_read(handle, event);
3494 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3495 if (data->callchain) {
3498 if (data->callchain)
3499 size += data->callchain->nr;
3501 size *= sizeof(u64);
3503 perf_output_copy(handle, data->callchain, size);
3506 perf_output_put(handle, nr);
3510 if (sample_type & PERF_SAMPLE_RAW) {
3512 perf_output_put(handle, data->raw->size);
3513 perf_output_copy(handle, data->raw->data,
3520 .size = sizeof(u32),
3523 perf_output_put(handle, raw);
3528 void perf_prepare_sample(struct perf_event_header *header,
3529 struct perf_sample_data *data,
3530 struct perf_event *event,
3531 struct pt_regs *regs)
3533 u64 sample_type = event->attr.sample_type;
3535 data->type = sample_type;
3537 header->type = PERF_RECORD_SAMPLE;
3538 header->size = sizeof(*header);
3541 header->misc |= perf_misc_flags(regs);
3543 if (sample_type & PERF_SAMPLE_IP) {
3544 data->ip = perf_instruction_pointer(regs);
3546 header->size += sizeof(data->ip);
3549 if (sample_type & PERF_SAMPLE_TID) {
3550 /* namespace issues */
3551 data->tid_entry.pid = perf_event_pid(event, current);
3552 data->tid_entry.tid = perf_event_tid(event, current);
3554 header->size += sizeof(data->tid_entry);
3557 if (sample_type & PERF_SAMPLE_TIME) {
3558 data->time = perf_clock();
3560 header->size += sizeof(data->time);
3563 if (sample_type & PERF_SAMPLE_ADDR)
3564 header->size += sizeof(data->addr);
3566 if (sample_type & PERF_SAMPLE_ID) {
3567 data->id = primary_event_id(event);
3569 header->size += sizeof(data->id);
3572 if (sample_type & PERF_SAMPLE_STREAM_ID) {
3573 data->stream_id = event->id;
3575 header->size += sizeof(data->stream_id);
3578 if (sample_type & PERF_SAMPLE_CPU) {
3579 data->cpu_entry.cpu = raw_smp_processor_id();
3580 data->cpu_entry.reserved = 0;
3582 header->size += sizeof(data->cpu_entry);
3585 if (sample_type & PERF_SAMPLE_PERIOD)
3586 header->size += sizeof(data->period);
3588 if (sample_type & PERF_SAMPLE_READ)
3589 header->size += perf_event_read_size(event);
3591 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3594 data->callchain = perf_callchain(regs);
3596 if (data->callchain)
3597 size += data->callchain->nr;
3599 header->size += size * sizeof(u64);
3602 if (sample_type & PERF_SAMPLE_RAW) {
3603 int size = sizeof(u32);
3606 size += data->raw->size;
3608 size += sizeof(u32);
3610 WARN_ON_ONCE(size & (sizeof(u64)-1));
3611 header->size += size;
3615 static void perf_event_output(struct perf_event *event, int nmi,
3616 struct perf_sample_data *data,
3617 struct pt_regs *regs)
3619 struct perf_output_handle handle;
3620 struct perf_event_header header;
3622 /* protect the callchain buffers */
3625 perf_prepare_sample(&header, data, event, regs);
3627 if (perf_output_begin(&handle, event, header.size, nmi, 1))
3630 perf_output_sample(&handle, &header, data, event);
3632 perf_output_end(&handle);
3642 struct perf_read_event {
3643 struct perf_event_header header;
3650 perf_event_read_event(struct perf_event *event,
3651 struct task_struct *task)
3653 struct perf_output_handle handle;
3654 struct perf_read_event read_event = {
3656 .type = PERF_RECORD_READ,
3658 .size = sizeof(read_event) + perf_event_read_size(event),
3660 .pid = perf_event_pid(event, task),
3661 .tid = perf_event_tid(event, task),
3665 ret = perf_output_begin(&handle, event, read_event.header.size, 0, 0);
3669 perf_output_put(&handle, read_event);
3670 perf_output_read(&handle, event);
3672 perf_output_end(&handle);
3676 * task tracking -- fork/exit
3678 * enabled by: attr.comm | attr.mmap | attr.mmap_data | attr.task
3681 struct perf_task_event {
3682 struct task_struct *task;
3683 struct perf_event_context *task_ctx;
3686 struct perf_event_header header;
3696 static void perf_event_task_output(struct perf_event *event,
3697 struct perf_task_event *task_event)
3699 struct perf_output_handle handle;
3700 struct task_struct *task = task_event->task;
3703 size = task_event->event_id.header.size;
3704 ret = perf_output_begin(&handle, event, size, 0, 0);
3709 task_event->event_id.pid = perf_event_pid(event, task);
3710 task_event->event_id.ppid = perf_event_pid(event, current);
3712 task_event->event_id.tid = perf_event_tid(event, task);
3713 task_event->event_id.ptid = perf_event_tid(event, current);
3715 perf_output_put(&handle, task_event->event_id);
3717 perf_output_end(&handle);
3720 static int perf_event_task_match(struct perf_event *event)
3722 if (event->state < PERF_EVENT_STATE_INACTIVE)
3725 if (event->cpu != -1 && event->cpu != smp_processor_id())
3728 if (event->attr.comm || event->attr.mmap ||
3729 event->attr.mmap_data || event->attr.task)
3735 static void perf_event_task_ctx(struct perf_event_context *ctx,
3736 struct perf_task_event *task_event)
3738 struct perf_event *event;
3740 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3741 if (perf_event_task_match(event))
3742 perf_event_task_output(event, task_event);
3746 static void perf_event_task_event(struct perf_task_event *task_event)
3748 struct perf_cpu_context *cpuctx;
3749 struct perf_event_context *ctx = task_event->task_ctx;
3752 cpuctx = &get_cpu_var(perf_cpu_context);
3753 perf_event_task_ctx(&cpuctx->ctx, task_event);
3755 ctx = rcu_dereference(current->perf_event_ctxp);
3757 perf_event_task_ctx(ctx, task_event);
3758 put_cpu_var(perf_cpu_context);
3762 static void perf_event_task(struct task_struct *task,
3763 struct perf_event_context *task_ctx,
3766 struct perf_task_event task_event;
3768 if (!atomic_read(&nr_comm_events) &&
3769 !atomic_read(&nr_mmap_events) &&
3770 !atomic_read(&nr_task_events))
3773 task_event = (struct perf_task_event){
3775 .task_ctx = task_ctx,
3778 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
3780 .size = sizeof(task_event.event_id),
3786 .time = perf_clock(),
3790 perf_event_task_event(&task_event);
3793 void perf_event_fork(struct task_struct *task)
3795 perf_event_task(task, NULL, 1);
3802 struct perf_comm_event {
3803 struct task_struct *task;
3808 struct perf_event_header header;
3815 static void perf_event_comm_output(struct perf_event *event,
3816 struct perf_comm_event *comm_event)
3818 struct perf_output_handle handle;
3819 int size = comm_event->event_id.header.size;
3820 int ret = perf_output_begin(&handle, event, size, 0, 0);
3825 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
3826 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
3828 perf_output_put(&handle, comm_event->event_id);
3829 perf_output_copy(&handle, comm_event->comm,
3830 comm_event->comm_size);
3831 perf_output_end(&handle);
3834 static int perf_event_comm_match(struct perf_event *event)
3836 if (event->state < PERF_EVENT_STATE_INACTIVE)
3839 if (event->cpu != -1 && event->cpu != smp_processor_id())
3842 if (event->attr.comm)
3848 static void perf_event_comm_ctx(struct perf_event_context *ctx,
3849 struct perf_comm_event *comm_event)
3851 struct perf_event *event;
3853 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3854 if (perf_event_comm_match(event))
3855 perf_event_comm_output(event, comm_event);
3859 static void perf_event_comm_event(struct perf_comm_event *comm_event)
3861 struct perf_cpu_context *cpuctx;
3862 struct perf_event_context *ctx;
3864 char comm[TASK_COMM_LEN];
3866 memset(comm, 0, sizeof(comm));
3867 strlcpy(comm, comm_event->task->comm, sizeof(comm));
3868 size = ALIGN(strlen(comm)+1, sizeof(u64));
3870 comm_event->comm = comm;
3871 comm_event->comm_size = size;
3873 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
3876 cpuctx = &get_cpu_var(perf_cpu_context);
3877 perf_event_comm_ctx(&cpuctx->ctx, comm_event);
3878 ctx = rcu_dereference(current->perf_event_ctxp);
3880 perf_event_comm_ctx(ctx, comm_event);
3881 put_cpu_var(perf_cpu_context);
3885 void perf_event_comm(struct task_struct *task)
3887 struct perf_comm_event comm_event;
3889 if (task->perf_event_ctxp)
3890 perf_event_enable_on_exec(task);
3892 if (!atomic_read(&nr_comm_events))
3895 comm_event = (struct perf_comm_event){
3901 .type = PERF_RECORD_COMM,
3910 perf_event_comm_event(&comm_event);
3917 struct perf_mmap_event {
3918 struct vm_area_struct *vma;
3920 const char *file_name;
3924 struct perf_event_header header;
3934 static void perf_event_mmap_output(struct perf_event *event,
3935 struct perf_mmap_event *mmap_event)
3937 struct perf_output_handle handle;
3938 int size = mmap_event->event_id.header.size;
3939 int ret = perf_output_begin(&handle, event, size, 0, 0);
3944 mmap_event->event_id.pid = perf_event_pid(event, current);
3945 mmap_event->event_id.tid = perf_event_tid(event, current);
3947 perf_output_put(&handle, mmap_event->event_id);
3948 perf_output_copy(&handle, mmap_event->file_name,
3949 mmap_event->file_size);
3950 perf_output_end(&handle);
3953 static int perf_event_mmap_match(struct perf_event *event,
3954 struct perf_mmap_event *mmap_event,
3957 if (event->state < PERF_EVENT_STATE_INACTIVE)
3960 if (event->cpu != -1 && event->cpu != smp_processor_id())
3963 if ((!executable && event->attr.mmap_data) ||
3964 (executable && event->attr.mmap))
3970 static void perf_event_mmap_ctx(struct perf_event_context *ctx,
3971 struct perf_mmap_event *mmap_event,
3974 struct perf_event *event;
3976 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3977 if (perf_event_mmap_match(event, mmap_event, executable))
3978 perf_event_mmap_output(event, mmap_event);
3982 static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
3984 struct perf_cpu_context *cpuctx;
3985 struct perf_event_context *ctx;
3986 struct vm_area_struct *vma = mmap_event->vma;
3987 struct file *file = vma->vm_file;
3993 memset(tmp, 0, sizeof(tmp));
3997 * d_path works from the end of the buffer backwards, so we
3998 * need to add enough zero bytes after the string to handle
3999 * the 64bit alignment we do later.
4001 buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
4003 name = strncpy(tmp, "//enomem", sizeof(tmp));
4006 name = d_path(&file->f_path, buf, PATH_MAX);
4008 name = strncpy(tmp, "//toolong", sizeof(tmp));
4012 if (arch_vma_name(mmap_event->vma)) {
4013 name = strncpy(tmp, arch_vma_name(mmap_event->vma),
4019 name = strncpy(tmp, "[vdso]", sizeof(tmp));
4021 } else if (vma->vm_start <= vma->vm_mm->start_brk &&
4022 vma->vm_end >= vma->vm_mm->brk) {
4023 name = strncpy(tmp, "[heap]", sizeof(tmp));
4025 } else if (vma->vm_start <= vma->vm_mm->start_stack &&
4026 vma->vm_end >= vma->vm_mm->start_stack) {
4027 name = strncpy(tmp, "[stack]", sizeof(tmp));
4031 name = strncpy(tmp, "//anon", sizeof(tmp));
4036 size = ALIGN(strlen(name)+1, sizeof(u64));
4038 mmap_event->file_name = name;
4039 mmap_event->file_size = size;
4041 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
4044 cpuctx = &get_cpu_var(perf_cpu_context);
4045 perf_event_mmap_ctx(&cpuctx->ctx, mmap_event, vma->vm_flags & VM_EXEC);
4046 ctx = rcu_dereference(current->perf_event_ctxp);
4048 perf_event_mmap_ctx(ctx, mmap_event, vma->vm_flags & VM_EXEC);
4049 put_cpu_var(perf_cpu_context);
4055 void perf_event_mmap(struct vm_area_struct *vma)
4057 struct perf_mmap_event mmap_event;
4059 if (!atomic_read(&nr_mmap_events))
4062 mmap_event = (struct perf_mmap_event){
4068 .type = PERF_RECORD_MMAP,
4069 .misc = PERF_RECORD_MISC_USER,
4074 .start = vma->vm_start,
4075 .len = vma->vm_end - vma->vm_start,
4076 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
4080 perf_event_mmap_event(&mmap_event);
4084 * IRQ throttle logging
4087 static void perf_log_throttle(struct perf_event *event, int enable)
4089 struct perf_output_handle handle;
4093 struct perf_event_header header;
4097 } throttle_event = {
4099 .type = PERF_RECORD_THROTTLE,
4101 .size = sizeof(throttle_event),
4103 .time = perf_clock(),
4104 .id = primary_event_id(event),
4105 .stream_id = event->id,
4109 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
4111 ret = perf_output_begin(&handle, event, sizeof(throttle_event), 1, 0);
4115 perf_output_put(&handle, throttle_event);
4116 perf_output_end(&handle);
4120 * Generic event overflow handling, sampling.
4123 static int __perf_event_overflow(struct perf_event *event, int nmi,
4124 int throttle, struct perf_sample_data *data,
4125 struct pt_regs *regs)
4127 int events = atomic_read(&event->event_limit);
4128 struct hw_perf_event *hwc = &event->hw;
4134 if (hwc->interrupts != MAX_INTERRUPTS) {
4136 if (HZ * hwc->interrupts >
4137 (u64)sysctl_perf_event_sample_rate) {
4138 hwc->interrupts = MAX_INTERRUPTS;
4139 perf_log_throttle(event, 0);
4144 * Keep re-disabling events even though on the previous
4145 * pass we disabled it - just in case we raced with a
4146 * sched-in and the event got enabled again:
4152 if (event->attr.freq) {
4153 u64 now = perf_clock();
4154 s64 delta = now - hwc->freq_time_stamp;
4156 hwc->freq_time_stamp = now;
4158 if (delta > 0 && delta < 2*TICK_NSEC)
4159 perf_adjust_period(event, delta, hwc->last_period);
4163 * XXX event_limit might not quite work as expected on inherited
4167 event->pending_kill = POLL_IN;
4168 if (events && atomic_dec_and_test(&event->event_limit)) {
4170 event->pending_kill = POLL_HUP;
4172 event->pending_disable = 1;
4173 perf_pending_queue(&event->pending,
4174 perf_pending_event);
4176 perf_event_disable(event);
4179 if (event->overflow_handler)
4180 event->overflow_handler(event, nmi, data, regs);
4182 perf_event_output(event, nmi, data, regs);
4187 int perf_event_overflow(struct perf_event *event, int nmi,
4188 struct perf_sample_data *data,
4189 struct pt_regs *regs)
4191 return __perf_event_overflow(event, nmi, 1, data, regs);
4195 * Generic software event infrastructure
4198 struct swevent_htable {
4199 struct swevent_hlist *swevent_hlist;
4200 struct mutex hlist_mutex;
4203 /* Recursion avoidance in each contexts */
4204 int recursion[PERF_NR_CONTEXTS];
4207 static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
4210 * We directly increment event->count and keep a second value in
4211 * event->hw.period_left to count intervals. This period event
4212 * is kept in the range [-sample_period, 0] so that we can use the
4216 static u64 perf_swevent_set_period(struct perf_event *event)
4218 struct hw_perf_event *hwc = &event->hw;
4219 u64 period = hwc->last_period;
4223 hwc->last_period = hwc->sample_period;
4226 old = val = local64_read(&hwc->period_left);
4230 nr = div64_u64(period + val, period);
4231 offset = nr * period;
4233 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
4239 static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
4240 int nmi, struct perf_sample_data *data,
4241 struct pt_regs *regs)
4243 struct hw_perf_event *hwc = &event->hw;
4246 data->period = event->hw.last_period;
4248 overflow = perf_swevent_set_period(event);
4250 if (hwc->interrupts == MAX_INTERRUPTS)
4253 for (; overflow; overflow--) {
4254 if (__perf_event_overflow(event, nmi, throttle,
4257 * We inhibit the overflow from happening when
4258 * hwc->interrupts == MAX_INTERRUPTS.
4266 static void perf_swevent_event(struct perf_event *event, u64 nr,
4267 int nmi, struct perf_sample_data *data,
4268 struct pt_regs *regs)
4270 struct hw_perf_event *hwc = &event->hw;
4272 local64_add(nr, &event->count);
4277 if (!hwc->sample_period)
4280 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
4281 return perf_swevent_overflow(event, 1, nmi, data, regs);
4283 if (local64_add_negative(nr, &hwc->period_left))
4286 perf_swevent_overflow(event, 0, nmi, data, regs);
4289 static int perf_exclude_event(struct perf_event *event,
4290 struct pt_regs *regs)
4292 if (event->hw.state & PERF_HES_STOPPED)
4296 if (event->attr.exclude_user && user_mode(regs))
4299 if (event->attr.exclude_kernel && !user_mode(regs))
4306 static int perf_swevent_match(struct perf_event *event,
4307 enum perf_type_id type,
4309 struct perf_sample_data *data,
4310 struct pt_regs *regs)
4312 if (event->attr.type != type)
4315 if (event->attr.config != event_id)
4318 if (perf_exclude_event(event, regs))
4324 static inline u64 swevent_hash(u64 type, u32 event_id)
4326 u64 val = event_id | (type << 32);
4328 return hash_64(val, SWEVENT_HLIST_BITS);
4331 static inline struct hlist_head *
4332 __find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
4334 u64 hash = swevent_hash(type, event_id);
4336 return &hlist->heads[hash];
4339 /* For the read side: events when they trigger */
4340 static inline struct hlist_head *
4341 find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
4343 struct swevent_hlist *hlist;
4345 hlist = rcu_dereference(swhash->swevent_hlist);
4349 return __find_swevent_head(hlist, type, event_id);
4352 /* For the event head insertion and removal in the hlist */
4353 static inline struct hlist_head *
4354 find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
4356 struct swevent_hlist *hlist;
4357 u32 event_id = event->attr.config;
4358 u64 type = event->attr.type;
4361 * Event scheduling is always serialized against hlist allocation
4362 * and release. Which makes the protected version suitable here.
4363 * The context lock guarantees that.
4365 hlist = rcu_dereference_protected(swhash->swevent_hlist,
4366 lockdep_is_held(&event->ctx->lock));
4370 return __find_swevent_head(hlist, type, event_id);
4373 static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
4375 struct perf_sample_data *data,
4376 struct pt_regs *regs)
4378 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
4379 struct perf_event *event;
4380 struct hlist_node *node;
4381 struct hlist_head *head;
4384 head = find_swevent_head_rcu(swhash, type, event_id);
4388 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
4389 if (perf_swevent_match(event, type, event_id, data, regs))
4390 perf_swevent_event(event, nr, nmi, data, regs);
4396 int perf_swevent_get_recursion_context(void)
4398 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
4400 return get_recursion_context(swhash->recursion);
4402 EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
4404 void inline perf_swevent_put_recursion_context(int rctx)
4406 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
4408 put_recursion_context(swhash->recursion, rctx);
4411 void __perf_sw_event(u32 event_id, u64 nr, int nmi,
4412 struct pt_regs *regs, u64 addr)
4414 struct perf_sample_data data;
4417 preempt_disable_notrace();
4418 rctx = perf_swevent_get_recursion_context();
4422 perf_sample_data_init(&data, addr);
4424 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, nmi, &data, regs);
4426 perf_swevent_put_recursion_context(rctx);
4427 preempt_enable_notrace();
4430 static void perf_swevent_read(struct perf_event *event)
4434 static int perf_swevent_add(struct perf_event *event, int flags)
4436 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
4437 struct hw_perf_event *hwc = &event->hw;
4438 struct hlist_head *head;
4440 if (hwc->sample_period) {
4441 hwc->last_period = hwc->sample_period;
4442 perf_swevent_set_period(event);
4445 hwc->state = !(flags & PERF_EF_START);
4447 head = find_swevent_head(swhash, event);
4448 if (WARN_ON_ONCE(!head))
4451 hlist_add_head_rcu(&event->hlist_entry, head);
4456 static void perf_swevent_del(struct perf_event *event, int flags)
4458 hlist_del_rcu(&event->hlist_entry);
4461 static void perf_swevent_start(struct perf_event *event, int flags)
4463 event->hw.state = 0;
4466 static void perf_swevent_stop(struct perf_event *event, int flags)
4468 event->hw.state = PERF_HES_STOPPED;
4471 /* Deref the hlist from the update side */
4472 static inline struct swevent_hlist *
4473 swevent_hlist_deref(struct swevent_htable *swhash)
4475 return rcu_dereference_protected(swhash->swevent_hlist,
4476 lockdep_is_held(&swhash->hlist_mutex));
4479 static void swevent_hlist_release_rcu(struct rcu_head *rcu_head)
4481 struct swevent_hlist *hlist;
4483 hlist = container_of(rcu_head, struct swevent_hlist, rcu_head);
4487 static void swevent_hlist_release(struct swevent_htable *swhash)
4489 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
4494 rcu_assign_pointer(swhash->swevent_hlist, NULL);
4495 call_rcu(&hlist->rcu_head, swevent_hlist_release_rcu);
4498 static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
4500 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
4502 mutex_lock(&swhash->hlist_mutex);
4504 if (!--swhash->hlist_refcount)
4505 swevent_hlist_release(swhash);
4507 mutex_unlock(&swhash->hlist_mutex);
4510 static void swevent_hlist_put(struct perf_event *event)
4514 if (event->cpu != -1) {
4515 swevent_hlist_put_cpu(event, event->cpu);
4519 for_each_possible_cpu(cpu)
4520 swevent_hlist_put_cpu(event, cpu);
4523 static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
4525 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
4528 mutex_lock(&swhash->hlist_mutex);
4530 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
4531 struct swevent_hlist *hlist;
4533 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
4538 rcu_assign_pointer(swhash->swevent_hlist, hlist);
4540 swhash->hlist_refcount++;
4542 mutex_unlock(&swhash->hlist_mutex);
4547 static int swevent_hlist_get(struct perf_event *event)
4550 int cpu, failed_cpu;
4552 if (event->cpu != -1)
4553 return swevent_hlist_get_cpu(event, event->cpu);
4556 for_each_possible_cpu(cpu) {
4557 err = swevent_hlist_get_cpu(event, cpu);
4567 for_each_possible_cpu(cpu) {
4568 if (cpu == failed_cpu)
4570 swevent_hlist_put_cpu(event, cpu);
4577 atomic_t perf_swevent_enabled[PERF_COUNT_SW_MAX];
4579 static void sw_perf_event_destroy(struct perf_event *event)
4581 u64 event_id = event->attr.config;
4583 WARN_ON(event->parent);
4585 atomic_dec(&perf_swevent_enabled[event_id]);
4586 swevent_hlist_put(event);
4589 static int perf_swevent_init(struct perf_event *event)
4591 int event_id = event->attr.config;
4593 if (event->attr.type != PERF_TYPE_SOFTWARE)
4597 case PERF_COUNT_SW_CPU_CLOCK:
4598 case PERF_COUNT_SW_TASK_CLOCK:
4605 if (event_id > PERF_COUNT_SW_MAX)
4608 if (!event->parent) {
4611 err = swevent_hlist_get(event);
4615 atomic_inc(&perf_swevent_enabled[event_id]);
4616 event->destroy = sw_perf_event_destroy;
4622 static struct pmu perf_swevent = {
4623 .event_init = perf_swevent_init,
4624 .add = perf_swevent_add,
4625 .del = perf_swevent_del,
4626 .start = perf_swevent_start,
4627 .stop = perf_swevent_stop,
4628 .read = perf_swevent_read,
4631 #ifdef CONFIG_EVENT_TRACING
4633 static int perf_tp_filter_match(struct perf_event *event,
4634 struct perf_sample_data *data)
4636 void *record = data->raw->data;
4638 if (likely(!event->filter) || filter_match_preds(event->filter, record))
4643 static int perf_tp_event_match(struct perf_event *event,
4644 struct perf_sample_data *data,
4645 struct pt_regs *regs)
4648 * All tracepoints are from kernel-space.
4650 if (event->attr.exclude_kernel)
4653 if (!perf_tp_filter_match(event, data))
4659 void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
4660 struct pt_regs *regs, struct hlist_head *head, int rctx)
4662 struct perf_sample_data data;
4663 struct perf_event *event;
4664 struct hlist_node *node;
4666 struct perf_raw_record raw = {
4671 perf_sample_data_init(&data, addr);
4674 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
4675 if (perf_tp_event_match(event, &data, regs))
4676 perf_swevent_event(event, count, 1, &data, regs);
4679 perf_swevent_put_recursion_context(rctx);
4681 EXPORT_SYMBOL_GPL(perf_tp_event);
4683 static void tp_perf_event_destroy(struct perf_event *event)
4685 perf_trace_destroy(event);
4688 static int perf_tp_event_init(struct perf_event *event)
4692 if (event->attr.type != PERF_TYPE_TRACEPOINT)
4696 * Raw tracepoint data is a severe data leak, only allow root to
4699 if ((event->attr.sample_type & PERF_SAMPLE_RAW) &&
4700 perf_paranoid_tracepoint_raw() &&
4701 !capable(CAP_SYS_ADMIN))
4704 err = perf_trace_init(event);
4708 event->destroy = tp_perf_event_destroy;
4713 static struct pmu perf_tracepoint = {
4714 .event_init = perf_tp_event_init,
4715 .add = perf_trace_add,
4716 .del = perf_trace_del,
4717 .start = perf_swevent_start,
4718 .stop = perf_swevent_stop,
4719 .read = perf_swevent_read,
4722 static inline void perf_tp_register(void)
4724 perf_pmu_register(&perf_tracepoint);
4727 static int perf_event_set_filter(struct perf_event *event, void __user *arg)
4732 if (event->attr.type != PERF_TYPE_TRACEPOINT)
4735 filter_str = strndup_user(arg, PAGE_SIZE);
4736 if (IS_ERR(filter_str))
4737 return PTR_ERR(filter_str);
4739 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
4745 static void perf_event_free_filter(struct perf_event *event)
4747 ftrace_profile_free_filter(event);
4752 static inline void perf_tp_register(void)
4756 static int perf_event_set_filter(struct perf_event *event, void __user *arg)
4761 static void perf_event_free_filter(struct perf_event *event)
4765 #endif /* CONFIG_EVENT_TRACING */
4767 #ifdef CONFIG_HAVE_HW_BREAKPOINT
4768 void perf_bp_event(struct perf_event *bp, void *data)
4770 struct perf_sample_data sample;
4771 struct pt_regs *regs = data;
4773 perf_sample_data_init(&sample, bp->attr.bp_addr);
4775 if (!bp->hw.state && !perf_exclude_event(bp, regs))
4776 perf_swevent_event(bp, 1, 1, &sample, regs);
4781 * hrtimer based swevent callback
4784 static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
4786 enum hrtimer_restart ret = HRTIMER_RESTART;
4787 struct perf_sample_data data;
4788 struct pt_regs *regs;
4789 struct perf_event *event;
4792 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
4793 event->pmu->read(event);
4795 perf_sample_data_init(&data, 0);
4796 data.period = event->hw.last_period;
4797 regs = get_irq_regs();
4799 if (regs && !perf_exclude_event(event, regs)) {
4800 if (!(event->attr.exclude_idle && current->pid == 0))
4801 if (perf_event_overflow(event, 0, &data, regs))
4802 ret = HRTIMER_NORESTART;
4805 period = max_t(u64, 10000, event->hw.sample_period);
4806 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
4811 static void perf_swevent_start_hrtimer(struct perf_event *event)
4813 struct hw_perf_event *hwc = &event->hw;
4815 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
4816 hwc->hrtimer.function = perf_swevent_hrtimer;
4817 if (hwc->sample_period) {
4818 s64 period = local64_read(&hwc->period_left);
4824 local64_set(&hwc->period_left, 0);
4826 period = max_t(u64, 10000, hwc->sample_period);
4828 __hrtimer_start_range_ns(&hwc->hrtimer,
4829 ns_to_ktime(period), 0,
4830 HRTIMER_MODE_REL_PINNED, 0);
4834 static void perf_swevent_cancel_hrtimer(struct perf_event *event)
4836 struct hw_perf_event *hwc = &event->hw;
4838 if (hwc->sample_period) {
4839 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
4840 local64_set(&hwc->period_left, ktime_to_ns(remaining));
4842 hrtimer_cancel(&hwc->hrtimer);
4847 * Software event: cpu wall time clock
4850 static void cpu_clock_event_update(struct perf_event *event)
4855 now = local_clock();
4856 prev = local64_xchg(&event->hw.prev_count, now);
4857 local64_add(now - prev, &event->count);
4860 static void cpu_clock_event_start(struct perf_event *event, int flags)
4862 local64_set(&event->hw.prev_count, local_clock());
4863 perf_swevent_start_hrtimer(event);
4866 static void cpu_clock_event_stop(struct perf_event *event, int flags)
4868 perf_swevent_cancel_hrtimer(event);
4869 cpu_clock_event_update(event);
4872 static int cpu_clock_event_add(struct perf_event *event, int flags)
4874 if (flags & PERF_EF_START)
4875 cpu_clock_event_start(event, flags);
4880 static void cpu_clock_event_del(struct perf_event *event, int flags)
4882 cpu_clock_event_stop(event, flags);
4885 static void cpu_clock_event_read(struct perf_event *event)
4887 cpu_clock_event_update(event);
4890 static int cpu_clock_event_init(struct perf_event *event)
4892 if (event->attr.type != PERF_TYPE_SOFTWARE)
4895 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
4901 static struct pmu perf_cpu_clock = {
4902 .event_init = cpu_clock_event_init,
4903 .add = cpu_clock_event_add,
4904 .del = cpu_clock_event_del,
4905 .start = cpu_clock_event_start,
4906 .stop = cpu_clock_event_stop,
4907 .read = cpu_clock_event_read,
4911 * Software event: task time clock
4914 static void task_clock_event_update(struct perf_event *event, u64 now)
4919 prev = local64_xchg(&event->hw.prev_count, now);
4921 local64_add(delta, &event->count);
4924 static void task_clock_event_start(struct perf_event *event, int flags)
4926 local64_set(&event->hw.prev_count, event->ctx->time);
4927 perf_swevent_start_hrtimer(event);
4930 static void task_clock_event_stop(struct perf_event *event, int flags)
4932 perf_swevent_cancel_hrtimer(event);
4933 task_clock_event_update(event, event->ctx->time);
4936 static int task_clock_event_add(struct perf_event *event, int flags)
4938 if (flags & PERF_EF_START)
4939 task_clock_event_start(event, flags);
4944 static void task_clock_event_del(struct perf_event *event, int flags)
4946 task_clock_event_stop(event, PERF_EF_UPDATE);
4949 static void task_clock_event_read(struct perf_event *event)
4954 update_context_time(event->ctx);
4955 time = event->ctx->time;
4957 u64 now = perf_clock();
4958 u64 delta = now - event->ctx->timestamp;
4959 time = event->ctx->time + delta;
4962 task_clock_event_update(event, time);
4965 static int task_clock_event_init(struct perf_event *event)
4967 if (event->attr.type != PERF_TYPE_SOFTWARE)
4970 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
4976 static struct pmu perf_task_clock = {
4977 .event_init = task_clock_event_init,
4978 .add = task_clock_event_add,
4979 .del = task_clock_event_del,
4980 .start = task_clock_event_start,
4981 .stop = task_clock_event_stop,
4982 .read = task_clock_event_read,
4985 static LIST_HEAD(pmus);
4986 static DEFINE_MUTEX(pmus_lock);
4987 static struct srcu_struct pmus_srcu;
4989 static void perf_pmu_nop_void(struct pmu *pmu)
4993 static int perf_pmu_nop_int(struct pmu *pmu)
4998 static void perf_pmu_start_txn(struct pmu *pmu)
5000 perf_pmu_disable(pmu);
5003 static int perf_pmu_commit_txn(struct pmu *pmu)
5005 perf_pmu_enable(pmu);
5009 static void perf_pmu_cancel_txn(struct pmu *pmu)
5011 perf_pmu_enable(pmu);
5014 int perf_pmu_register(struct pmu *pmu)
5018 mutex_lock(&pmus_lock);
5020 pmu->pmu_disable_count = alloc_percpu(int);
5021 if (!pmu->pmu_disable_count)
5024 if (!pmu->start_txn) {
5025 if (pmu->pmu_enable) {
5027 * If we have pmu_enable/pmu_disable calls, install
5028 * transaction stubs that use that to try and batch
5029 * hardware accesses.
5031 pmu->start_txn = perf_pmu_start_txn;
5032 pmu->commit_txn = perf_pmu_commit_txn;
5033 pmu->cancel_txn = perf_pmu_cancel_txn;
5035 pmu->start_txn = perf_pmu_nop_void;
5036 pmu->commit_txn = perf_pmu_nop_int;
5037 pmu->cancel_txn = perf_pmu_nop_void;
5041 if (!pmu->pmu_enable) {
5042 pmu->pmu_enable = perf_pmu_nop_void;
5043 pmu->pmu_disable = perf_pmu_nop_void;
5046 list_add_rcu(&pmu->entry, &pmus);
5049 mutex_unlock(&pmus_lock);
5054 void perf_pmu_unregister(struct pmu *pmu)
5056 mutex_lock(&pmus_lock);
5057 list_del_rcu(&pmu->entry);
5058 mutex_unlock(&pmus_lock);
5060 synchronize_srcu(&pmus_srcu);
5062 free_percpu(pmu->pmu_disable_count);
5065 struct pmu *perf_init_event(struct perf_event *event)
5067 struct pmu *pmu = NULL;
5070 idx = srcu_read_lock(&pmus_srcu);
5071 list_for_each_entry_rcu(pmu, &pmus, entry) {
5072 int ret = pmu->event_init(event);
5075 if (ret != -ENOENT) {
5080 srcu_read_unlock(&pmus_srcu, idx);
5086 * Allocate and initialize a event structure
5088 static struct perf_event *
5089 perf_event_alloc(struct perf_event_attr *attr, int cpu,
5090 struct perf_event *group_leader,
5091 struct perf_event *parent_event,
5092 perf_overflow_handler_t overflow_handler)
5095 struct perf_event *event;
5096 struct hw_perf_event *hwc;
5099 event = kzalloc(sizeof(*event), GFP_KERNEL);
5101 return ERR_PTR(-ENOMEM);
5104 * Single events are their own group leaders, with an
5105 * empty sibling list:
5108 group_leader = event;
5110 mutex_init(&event->child_mutex);
5111 INIT_LIST_HEAD(&event->child_list);
5113 INIT_LIST_HEAD(&event->group_entry);
5114 INIT_LIST_HEAD(&event->event_entry);
5115 INIT_LIST_HEAD(&event->sibling_list);
5116 init_waitqueue_head(&event->waitq);
5118 mutex_init(&event->mmap_mutex);
5121 event->attr = *attr;
5122 event->group_leader = group_leader;
5126 event->parent = parent_event;
5128 event->ns = get_pid_ns(current->nsproxy->pid_ns);
5129 event->id = atomic64_inc_return(&perf_event_id);
5131 event->state = PERF_EVENT_STATE_INACTIVE;
5133 if (!overflow_handler && parent_event)
5134 overflow_handler = parent_event->overflow_handler;
5136 event->overflow_handler = overflow_handler;
5139 event->state = PERF_EVENT_STATE_OFF;
5144 hwc->sample_period = attr->sample_period;
5145 if (attr->freq && attr->sample_freq)
5146 hwc->sample_period = 1;
5147 hwc->last_period = hwc->sample_period;
5149 local64_set(&hwc->period_left, hwc->sample_period);
5152 * we currently do not support PERF_FORMAT_GROUP on inherited events
5154 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
5157 pmu = perf_init_event(event);
5163 else if (IS_ERR(pmu))
5168 put_pid_ns(event->ns);
5170 return ERR_PTR(err);
5175 if (!event->parent) {
5176 atomic_inc(&nr_events);
5177 if (event->attr.mmap || event->attr.mmap_data)
5178 atomic_inc(&nr_mmap_events);
5179 if (event->attr.comm)
5180 atomic_inc(&nr_comm_events);
5181 if (event->attr.task)
5182 atomic_inc(&nr_task_events);
5183 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
5184 err = get_callchain_buffers();
5187 return ERR_PTR(err);
5195 static int perf_copy_attr(struct perf_event_attr __user *uattr,
5196 struct perf_event_attr *attr)
5201 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
5205 * zero the full structure, so that a short copy will be nice.
5207 memset(attr, 0, sizeof(*attr));
5209 ret = get_user(size, &uattr->size);
5213 if (size > PAGE_SIZE) /* silly large */
5216 if (!size) /* abi compat */
5217 size = PERF_ATTR_SIZE_VER0;
5219 if (size < PERF_ATTR_SIZE_VER0)
5223 * If we're handed a bigger struct than we know of,
5224 * ensure all the unknown bits are 0 - i.e. new
5225 * user-space does not rely on any kernel feature
5226 * extensions we dont know about yet.
5228 if (size > sizeof(*attr)) {
5229 unsigned char __user *addr;
5230 unsigned char __user *end;
5233 addr = (void __user *)uattr + sizeof(*attr);
5234 end = (void __user *)uattr + size;
5236 for (; addr < end; addr++) {
5237 ret = get_user(val, addr);
5243 size = sizeof(*attr);
5246 ret = copy_from_user(attr, uattr, size);
5251 * If the type exists, the corresponding creation will verify
5254 if (attr->type >= PERF_TYPE_MAX)
5257 if (attr->__reserved_1)
5260 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
5263 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
5270 put_user(sizeof(*attr), &uattr->size);
5276 perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
5278 struct perf_buffer *buffer = NULL, *old_buffer = NULL;
5284 /* don't allow circular references */
5285 if (event == output_event)
5289 * Don't allow cross-cpu buffers
5291 if (output_event->cpu != event->cpu)
5295 * If its not a per-cpu buffer, it must be the same task.
5297 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
5301 mutex_lock(&event->mmap_mutex);
5302 /* Can't redirect output if we've got an active mmap() */
5303 if (atomic_read(&event->mmap_count))
5307 /* get the buffer we want to redirect to */
5308 buffer = perf_buffer_get(output_event);
5313 old_buffer = event->buffer;
5314 rcu_assign_pointer(event->buffer, buffer);
5317 mutex_unlock(&event->mmap_mutex);
5320 perf_buffer_put(old_buffer);
5326 * sys_perf_event_open - open a performance event, associate it to a task/cpu
5328 * @attr_uptr: event_id type attributes for monitoring/sampling
5331 * @group_fd: group leader event fd
5333 SYSCALL_DEFINE5(perf_event_open,
5334 struct perf_event_attr __user *, attr_uptr,
5335 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
5337 struct perf_event *event, *group_leader = NULL, *output_event = NULL;
5338 struct perf_event_attr attr;
5339 struct perf_event_context *ctx;
5340 struct file *event_file = NULL;
5341 struct file *group_file = NULL;
5343 int fput_needed = 0;
5346 /* for future expandability... */
5347 if (flags & ~(PERF_FLAG_FD_NO_GROUP | PERF_FLAG_FD_OUTPUT))
5350 err = perf_copy_attr(attr_uptr, &attr);
5354 if (!attr.exclude_kernel) {
5355 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
5360 if (attr.sample_freq > sysctl_perf_event_sample_rate)
5364 event_fd = get_unused_fd_flags(O_RDWR);
5368 event = perf_event_alloc(&attr, cpu, group_leader, NULL, NULL);
5369 if (IS_ERR(event)) {
5370 err = PTR_ERR(event);
5375 * Get the target context (task or percpu):
5377 ctx = find_get_context(pid, cpu);
5383 if (group_fd != -1) {
5384 group_leader = perf_fget_light(group_fd, &fput_needed);
5385 if (IS_ERR(group_leader)) {
5386 err = PTR_ERR(group_leader);
5389 group_file = group_leader->filp;
5390 if (flags & PERF_FLAG_FD_OUTPUT)
5391 output_event = group_leader;
5392 if (flags & PERF_FLAG_FD_NO_GROUP)
5393 group_leader = NULL;
5397 * Look up the group leader (we will attach this event to it):
5403 * Do not allow a recursive hierarchy (this new sibling
5404 * becoming part of another group-sibling):
5406 if (group_leader->group_leader != group_leader)
5409 * Do not allow to attach to a group in a different
5410 * task or CPU context:
5412 if (group_leader->ctx != ctx)
5415 * Only a group leader can be exclusive or pinned
5417 if (attr.exclusive || attr.pinned)
5422 err = perf_event_set_output(event, output_event);
5427 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, O_RDWR);
5428 if (IS_ERR(event_file)) {
5429 err = PTR_ERR(event_file);
5433 event->filp = event_file;
5434 WARN_ON_ONCE(ctx->parent_ctx);
5435 mutex_lock(&ctx->mutex);
5436 perf_install_in_context(ctx, event, cpu);
5438 mutex_unlock(&ctx->mutex);
5440 event->owner = current;
5441 get_task_struct(current);
5442 mutex_lock(¤t->perf_event_mutex);
5443 list_add_tail(&event->owner_entry, ¤t->perf_event_list);
5444 mutex_unlock(¤t->perf_event_mutex);
5447 * Drop the reference on the group_event after placing the
5448 * new event on the sibling_list. This ensures destruction
5449 * of the group leader will find the pointer to itself in
5450 * perf_group_detach().
5452 fput_light(group_file, fput_needed);
5453 fd_install(event_fd, event_file);
5457 fput_light(group_file, fput_needed);
5462 put_unused_fd(event_fd);
5467 * perf_event_create_kernel_counter
5469 * @attr: attributes of the counter to create
5470 * @cpu: cpu in which the counter is bound
5471 * @pid: task to profile
5474 perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
5476 perf_overflow_handler_t overflow_handler)
5478 struct perf_event_context *ctx;
5479 struct perf_event *event;
5483 * Get the target context (task or percpu):
5486 event = perf_event_alloc(attr, cpu, NULL, NULL, overflow_handler);
5487 if (IS_ERR(event)) {
5488 err = PTR_ERR(event);
5492 ctx = find_get_context(pid, cpu);
5499 WARN_ON_ONCE(ctx->parent_ctx);
5500 mutex_lock(&ctx->mutex);
5501 perf_install_in_context(ctx, event, cpu);
5503 mutex_unlock(&ctx->mutex);
5505 event->owner = current;
5506 get_task_struct(current);
5507 mutex_lock(¤t->perf_event_mutex);
5508 list_add_tail(&event->owner_entry, ¤t->perf_event_list);
5509 mutex_unlock(¤t->perf_event_mutex);
5516 return ERR_PTR(err);
5518 EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
5521 * inherit a event from parent task to child task:
5523 static struct perf_event *
5524 inherit_event(struct perf_event *parent_event,
5525 struct task_struct *parent,
5526 struct perf_event_context *parent_ctx,
5527 struct task_struct *child,
5528 struct perf_event *group_leader,
5529 struct perf_event_context *child_ctx)
5531 struct perf_event *child_event;
5534 * Instead of creating recursive hierarchies of events,
5535 * we link inherited events back to the original parent,
5536 * which has a filp for sure, which we use as the reference
5539 if (parent_event->parent)
5540 parent_event = parent_event->parent;
5542 child_event = perf_event_alloc(&parent_event->attr,
5544 group_leader, parent_event,
5546 if (IS_ERR(child_event))
5551 * Make the child state follow the state of the parent event,
5552 * not its attr.disabled bit. We hold the parent's mutex,
5553 * so we won't race with perf_event_{en, dis}able_family.
5555 if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
5556 child_event->state = PERF_EVENT_STATE_INACTIVE;
5558 child_event->state = PERF_EVENT_STATE_OFF;
5560 if (parent_event->attr.freq) {
5561 u64 sample_period = parent_event->hw.sample_period;
5562 struct hw_perf_event *hwc = &child_event->hw;
5564 hwc->sample_period = sample_period;
5565 hwc->last_period = sample_period;
5567 local64_set(&hwc->period_left, sample_period);
5570 child_event->ctx = child_ctx;
5571 child_event->overflow_handler = parent_event->overflow_handler;
5574 * Link it up in the child's context:
5576 add_event_to_ctx(child_event, child_ctx);
5579 * Get a reference to the parent filp - we will fput it
5580 * when the child event exits. This is safe to do because
5581 * we are in the parent and we know that the filp still
5582 * exists and has a nonzero count:
5584 atomic_long_inc(&parent_event->filp->f_count);
5587 * Link this into the parent event's child list
5589 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
5590 mutex_lock(&parent_event->child_mutex);
5591 list_add_tail(&child_event->child_list, &parent_event->child_list);
5592 mutex_unlock(&parent_event->child_mutex);
5597 static int inherit_group(struct perf_event *parent_event,
5598 struct task_struct *parent,
5599 struct perf_event_context *parent_ctx,
5600 struct task_struct *child,
5601 struct perf_event_context *child_ctx)
5603 struct perf_event *leader;
5604 struct perf_event *sub;
5605 struct perf_event *child_ctr;
5607 leader = inherit_event(parent_event, parent, parent_ctx,
5608 child, NULL, child_ctx);
5610 return PTR_ERR(leader);
5611 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
5612 child_ctr = inherit_event(sub, parent, parent_ctx,
5613 child, leader, child_ctx);
5614 if (IS_ERR(child_ctr))
5615 return PTR_ERR(child_ctr);
5620 static void sync_child_event(struct perf_event *child_event,
5621 struct task_struct *child)
5623 struct perf_event *parent_event = child_event->parent;
5626 if (child_event->attr.inherit_stat)
5627 perf_event_read_event(child_event, child);
5629 child_val = perf_event_count(child_event);
5632 * Add back the child's count to the parent's count:
5634 atomic64_add(child_val, &parent_event->child_count);
5635 atomic64_add(child_event->total_time_enabled,
5636 &parent_event->child_total_time_enabled);
5637 atomic64_add(child_event->total_time_running,
5638 &parent_event->child_total_time_running);
5641 * Remove this event from the parent's list
5643 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
5644 mutex_lock(&parent_event->child_mutex);
5645 list_del_init(&child_event->child_list);
5646 mutex_unlock(&parent_event->child_mutex);
5649 * Release the parent event, if this was the last
5652 fput(parent_event->filp);
5656 __perf_event_exit_task(struct perf_event *child_event,
5657 struct perf_event_context *child_ctx,
5658 struct task_struct *child)
5660 struct perf_event *parent_event;
5662 perf_event_remove_from_context(child_event);
5664 parent_event = child_event->parent;
5666 * It can happen that parent exits first, and has events
5667 * that are still around due to the child reference. These
5668 * events need to be zapped - but otherwise linger.
5671 sync_child_event(child_event, child);
5672 free_event(child_event);
5677 * When a child task exits, feed back event values to parent events.
5679 void perf_event_exit_task(struct task_struct *child)
5681 struct perf_event *child_event, *tmp;
5682 struct perf_event_context *child_ctx;
5683 unsigned long flags;
5685 if (likely(!child->perf_event_ctxp)) {
5686 perf_event_task(child, NULL, 0);
5690 local_irq_save(flags);
5692 * We can't reschedule here because interrupts are disabled,
5693 * and either child is current or it is a task that can't be
5694 * scheduled, so we are now safe from rescheduling changing
5697 child_ctx = child->perf_event_ctxp;
5698 __perf_event_task_sched_out(child_ctx);
5701 * Take the context lock here so that if find_get_context is
5702 * reading child->perf_event_ctxp, we wait until it has
5703 * incremented the context's refcount before we do put_ctx below.
5705 raw_spin_lock(&child_ctx->lock);
5706 child->perf_event_ctxp = NULL;
5708 * If this context is a clone; unclone it so it can't get
5709 * swapped to another process while we're removing all
5710 * the events from it.
5712 unclone_ctx(child_ctx);
5713 update_context_time(child_ctx);
5714 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
5717 * Report the task dead after unscheduling the events so that we
5718 * won't get any samples after PERF_RECORD_EXIT. We can however still
5719 * get a few PERF_RECORD_READ events.
5721 perf_event_task(child, child_ctx, 0);
5724 * We can recurse on the same lock type through:
5726 * __perf_event_exit_task()
5727 * sync_child_event()
5728 * fput(parent_event->filp)
5730 * mutex_lock(&ctx->mutex)
5732 * But since its the parent context it won't be the same instance.
5734 mutex_lock(&child_ctx->mutex);
5737 list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
5739 __perf_event_exit_task(child_event, child_ctx, child);
5741 list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
5743 __perf_event_exit_task(child_event, child_ctx, child);
5746 * If the last event was a group event, it will have appended all
5747 * its siblings to the list, but we obtained 'tmp' before that which
5748 * will still point to the list head terminating the iteration.
5750 if (!list_empty(&child_ctx->pinned_groups) ||
5751 !list_empty(&child_ctx->flexible_groups))
5754 mutex_unlock(&child_ctx->mutex);
5759 static void perf_free_event(struct perf_event *event,
5760 struct perf_event_context *ctx)
5762 struct perf_event *parent = event->parent;
5764 if (WARN_ON_ONCE(!parent))
5767 mutex_lock(&parent->child_mutex);
5768 list_del_init(&event->child_list);
5769 mutex_unlock(&parent->child_mutex);
5773 perf_group_detach(event);
5774 list_del_event(event, ctx);
5779 * free an unexposed, unused context as created by inheritance by
5780 * init_task below, used by fork() in case of fail.
5782 void perf_event_free_task(struct task_struct *task)
5784 struct perf_event_context *ctx = task->perf_event_ctxp;
5785 struct perf_event *event, *tmp;
5790 mutex_lock(&ctx->mutex);
5792 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
5793 perf_free_event(event, ctx);
5795 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
5797 perf_free_event(event, ctx);
5799 if (!list_empty(&ctx->pinned_groups) ||
5800 !list_empty(&ctx->flexible_groups))
5803 mutex_unlock(&ctx->mutex);
5809 inherit_task_group(struct perf_event *event, struct task_struct *parent,
5810 struct perf_event_context *parent_ctx,
5811 struct task_struct *child,
5815 struct perf_event_context *child_ctx = child->perf_event_ctxp;
5817 if (!event->attr.inherit) {
5824 * This is executed from the parent task context, so
5825 * inherit events that have been marked for cloning.
5826 * First allocate and initialize a context for the
5830 child_ctx = kzalloc(sizeof(struct perf_event_context),
5835 __perf_event_init_context(child_ctx, child);
5836 child->perf_event_ctxp = child_ctx;
5837 get_task_struct(child);
5840 ret = inherit_group(event, parent, parent_ctx,
5851 * Initialize the perf_event context in task_struct
5853 int perf_event_init_task(struct task_struct *child)
5855 struct perf_event_context *child_ctx, *parent_ctx;
5856 struct perf_event_context *cloned_ctx;
5857 struct perf_event *event;
5858 struct task_struct *parent = current;
5859 int inherited_all = 1;
5862 child->perf_event_ctxp = NULL;
5864 mutex_init(&child->perf_event_mutex);
5865 INIT_LIST_HEAD(&child->perf_event_list);
5867 if (likely(!parent->perf_event_ctxp))
5871 * If the parent's context is a clone, pin it so it won't get
5874 parent_ctx = perf_pin_task_context(parent);
5877 * No need to check if parent_ctx != NULL here; since we saw
5878 * it non-NULL earlier, the only reason for it to become NULL
5879 * is if we exit, and since we're currently in the middle of
5880 * a fork we can't be exiting at the same time.
5884 * Lock the parent list. No need to lock the child - not PID
5885 * hashed yet and not running, so nobody can access it.
5887 mutex_lock(&parent_ctx->mutex);
5890 * We dont have to disable NMIs - we are only looking at
5891 * the list, not manipulating it:
5893 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
5894 ret = inherit_task_group(event, parent, parent_ctx, child,
5900 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
5901 ret = inherit_task_group(event, parent, parent_ctx, child,
5907 child_ctx = child->perf_event_ctxp;
5909 if (child_ctx && inherited_all) {
5911 * Mark the child context as a clone of the parent
5912 * context, or of whatever the parent is a clone of.
5913 * Note that if the parent is a clone, it could get
5914 * uncloned at any point, but that doesn't matter
5915 * because the list of events and the generation
5916 * count can't have changed since we took the mutex.
5918 cloned_ctx = rcu_dereference(parent_ctx->parent_ctx);
5920 child_ctx->parent_ctx = cloned_ctx;
5921 child_ctx->parent_gen = parent_ctx->parent_gen;
5923 child_ctx->parent_ctx = parent_ctx;
5924 child_ctx->parent_gen = parent_ctx->generation;
5926 get_ctx(child_ctx->parent_ctx);
5929 mutex_unlock(&parent_ctx->mutex);
5931 perf_unpin_context(parent_ctx);
5936 static void __init perf_event_init_all_cpus(void)
5938 struct perf_cpu_context *cpuctx;
5939 struct swevent_htable *swhash;
5942 for_each_possible_cpu(cpu) {
5943 swhash = &per_cpu(swevent_htable, cpu);
5944 mutex_init(&swhash->hlist_mutex);
5946 cpuctx = &per_cpu(perf_cpu_context, cpu);
5947 __perf_event_init_context(&cpuctx->ctx, NULL);
5948 cpuctx->timer_interval = TICK_NSEC;
5949 hrtimer_init(&cpuctx->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
5950 cpuctx->timer.function = perf_event_context_tick;
5954 static void __cpuinit perf_event_init_cpu(int cpu)
5956 struct perf_cpu_context *cpuctx;
5957 struct swevent_htable *swhash;
5959 cpuctx = &per_cpu(perf_cpu_context, cpu);
5961 swhash = &per_cpu(swevent_htable, cpu);
5963 mutex_lock(&swhash->hlist_mutex);
5964 if (swhash->hlist_refcount > 0) {
5965 struct swevent_hlist *hlist;
5967 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
5969 rcu_assign_pointer(swhash->swevent_hlist, hlist);
5971 mutex_unlock(&swhash->hlist_mutex);
5974 #ifdef CONFIG_HOTPLUG_CPU
5975 static void __perf_event_exit_cpu(void *info)
5977 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
5978 struct perf_event_context *ctx = &cpuctx->ctx;
5979 struct perf_event *event, *tmp;
5981 perf_pmu_rotate_stop();
5983 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
5984 __perf_event_remove_from_context(event);
5985 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, group_entry)
5986 __perf_event_remove_from_context(event);
5988 static void perf_event_exit_cpu(int cpu)
5990 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
5991 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
5992 struct perf_event_context *ctx = &cpuctx->ctx;
5994 mutex_lock(&swhash->hlist_mutex);
5995 swevent_hlist_release(swhash);
5996 mutex_unlock(&swhash->hlist_mutex);
5998 mutex_lock(&ctx->mutex);
5999 smp_call_function_single(cpu, __perf_event_exit_cpu, NULL, 1);
6000 mutex_unlock(&ctx->mutex);
6003 static inline void perf_event_exit_cpu(int cpu) { }
6006 static int __cpuinit
6007 perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
6009 unsigned int cpu = (long)hcpu;
6011 switch (action & ~CPU_TASKS_FROZEN) {
6013 case CPU_UP_PREPARE:
6014 case CPU_DOWN_FAILED:
6015 perf_event_init_cpu(cpu);
6018 case CPU_UP_CANCELED:
6019 case CPU_DOWN_PREPARE:
6020 perf_event_exit_cpu(cpu);
6030 void __init perf_event_init(void)
6032 perf_event_init_all_cpus();
6033 init_srcu_struct(&pmus_srcu);
6034 perf_pmu_register(&perf_swevent);
6035 perf_pmu_register(&perf_cpu_clock);
6036 perf_pmu_register(&perf_task_clock);
6038 perf_cpu_notifier(perf_cpu_notify);