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>
34 #include <linux/hw_breakpoint.h>
36 #include <asm/irq_regs.h>
38 atomic_t perf_task_events __read_mostly;
39 static atomic_t nr_mmap_events __read_mostly;
40 static atomic_t nr_comm_events __read_mostly;
41 static atomic_t nr_task_events __read_mostly;
43 static LIST_HEAD(pmus);
44 static DEFINE_MUTEX(pmus_lock);
45 static struct srcu_struct pmus_srcu;
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 extern __weak const char *perf_pmu_name(void)
72 void perf_pmu_disable(struct pmu *pmu)
74 int *count = this_cpu_ptr(pmu->pmu_disable_count);
76 pmu->pmu_disable(pmu);
79 void perf_pmu_enable(struct pmu *pmu)
81 int *count = this_cpu_ptr(pmu->pmu_disable_count);
86 static DEFINE_PER_CPU(struct list_head, rotation_list);
89 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
90 * because they're strictly cpu affine and rotate_start is called with IRQs
91 * disabled, while rotate_context is called from IRQ context.
93 static void perf_pmu_rotate_start(struct pmu *pmu)
95 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
96 struct list_head *head = &__get_cpu_var(rotation_list);
98 WARN_ON(!irqs_disabled());
100 if (list_empty(&cpuctx->rotation_list))
101 list_add(&cpuctx->rotation_list, head);
104 static void get_ctx(struct perf_event_context *ctx)
106 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
109 static void free_ctx(struct rcu_head *head)
111 struct perf_event_context *ctx;
113 ctx = container_of(head, struct perf_event_context, rcu_head);
117 static void put_ctx(struct perf_event_context *ctx)
119 if (atomic_dec_and_test(&ctx->refcount)) {
121 put_ctx(ctx->parent_ctx);
123 put_task_struct(ctx->task);
124 call_rcu(&ctx->rcu_head, free_ctx);
128 static void unclone_ctx(struct perf_event_context *ctx)
130 if (ctx->parent_ctx) {
131 put_ctx(ctx->parent_ctx);
132 ctx->parent_ctx = NULL;
137 * If we inherit events we want to return the parent event id
140 static u64 primary_event_id(struct perf_event *event)
145 id = event->parent->id;
151 * Get the perf_event_context for a task and lock it.
152 * This has to cope with with the fact that until it is locked,
153 * the context could get moved to another task.
155 static struct perf_event_context *
156 perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
158 struct perf_event_context *ctx;
162 ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
165 * If this context is a clone of another, it might
166 * get swapped for another underneath us by
167 * perf_event_task_sched_out, though the
168 * rcu_read_lock() protects us from any context
169 * getting freed. Lock the context and check if it
170 * got swapped before we could get the lock, and retry
171 * if so. If we locked the right context, then it
172 * can't get swapped on us any more.
174 raw_spin_lock_irqsave(&ctx->lock, *flags);
175 if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
176 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
180 if (!atomic_inc_not_zero(&ctx->refcount)) {
181 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
190 * Get the context for a task and increment its pin_count so it
191 * can't get swapped to another task. This also increments its
192 * reference count so that the context can't get freed.
194 static struct perf_event_context *
195 perf_pin_task_context(struct task_struct *task, int ctxn)
197 struct perf_event_context *ctx;
200 ctx = perf_lock_task_context(task, ctxn, &flags);
203 raw_spin_unlock_irqrestore(&ctx->lock, flags);
208 static void perf_unpin_context(struct perf_event_context *ctx)
212 raw_spin_lock_irqsave(&ctx->lock, flags);
214 raw_spin_unlock_irqrestore(&ctx->lock, flags);
218 static inline u64 perf_clock(void)
220 return local_clock();
224 * Update the record of the current time in a context.
226 static void update_context_time(struct perf_event_context *ctx)
228 u64 now = perf_clock();
230 ctx->time += now - ctx->timestamp;
231 ctx->timestamp = now;
235 * Update the total_time_enabled and total_time_running fields for a event.
237 static void update_event_times(struct perf_event *event)
239 struct perf_event_context *ctx = event->ctx;
242 if (event->state < PERF_EVENT_STATE_INACTIVE ||
243 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
249 run_end = event->tstamp_stopped;
251 event->total_time_enabled = run_end - event->tstamp_enabled;
253 if (event->state == PERF_EVENT_STATE_INACTIVE)
254 run_end = event->tstamp_stopped;
258 event->total_time_running = run_end - event->tstamp_running;
262 * Update total_time_enabled and total_time_running for all events in a group.
264 static void update_group_times(struct perf_event *leader)
266 struct perf_event *event;
268 update_event_times(leader);
269 list_for_each_entry(event, &leader->sibling_list, group_entry)
270 update_event_times(event);
273 static struct list_head *
274 ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
276 if (event->attr.pinned)
277 return &ctx->pinned_groups;
279 return &ctx->flexible_groups;
283 * Add a event from the lists for its context.
284 * Must be called with ctx->mutex and ctx->lock held.
287 list_add_event(struct perf_event *event, struct perf_event_context *ctx)
289 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
290 event->attach_state |= PERF_ATTACH_CONTEXT;
293 * If we're a stand alone event or group leader, we go to the context
294 * list, group events are kept attached to the group so that
295 * perf_group_detach can, at all times, locate all siblings.
297 if (event->group_leader == event) {
298 struct list_head *list;
300 if (is_software_event(event))
301 event->group_flags |= PERF_GROUP_SOFTWARE;
303 list = ctx_group_list(event, ctx);
304 list_add_tail(&event->group_entry, list);
307 list_add_rcu(&event->event_entry, &ctx->event_list);
309 perf_pmu_rotate_start(ctx->pmu);
311 if (event->attr.inherit_stat)
315 static void perf_group_attach(struct perf_event *event)
317 struct perf_event *group_leader = event->group_leader;
320 * We can have double attach due to group movement in perf_event_open.
322 if (event->attach_state & PERF_ATTACH_GROUP)
325 event->attach_state |= PERF_ATTACH_GROUP;
327 if (group_leader == event)
330 if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
331 !is_software_event(event))
332 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
334 list_add_tail(&event->group_entry, &group_leader->sibling_list);
335 group_leader->nr_siblings++;
339 * Remove a event from the lists for its context.
340 * Must be called with ctx->mutex and ctx->lock held.
343 list_del_event(struct perf_event *event, struct perf_event_context *ctx)
346 * We can have double detach due to exit/hot-unplug + close.
348 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
351 event->attach_state &= ~PERF_ATTACH_CONTEXT;
354 if (event->attr.inherit_stat)
357 list_del_rcu(&event->event_entry);
359 if (event->group_leader == event)
360 list_del_init(&event->group_entry);
362 update_group_times(event);
365 * If event was in error state, then keep it
366 * that way, otherwise bogus counts will be
367 * returned on read(). The only way to get out
368 * of error state is by explicit re-enabling
371 if (event->state > PERF_EVENT_STATE_OFF)
372 event->state = PERF_EVENT_STATE_OFF;
375 static void perf_group_detach(struct perf_event *event)
377 struct perf_event *sibling, *tmp;
378 struct list_head *list = NULL;
381 * We can have double detach due to exit/hot-unplug + close.
383 if (!(event->attach_state & PERF_ATTACH_GROUP))
386 event->attach_state &= ~PERF_ATTACH_GROUP;
389 * If this is a sibling, remove it from its group.
391 if (event->group_leader != event) {
392 list_del_init(&event->group_entry);
393 event->group_leader->nr_siblings--;
397 if (!list_empty(&event->group_entry))
398 list = &event->group_entry;
401 * If this was a group event with sibling events then
402 * upgrade the siblings to singleton events by adding them
403 * to whatever list we are on.
405 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
407 list_move_tail(&sibling->group_entry, list);
408 sibling->group_leader = sibling;
410 /* Inherit group flags from the previous leader */
411 sibling->group_flags = event->group_flags;
416 event_filter_match(struct perf_event *event)
418 return event->cpu == -1 || event->cpu == smp_processor_id();
422 event_sched_out(struct perf_event *event,
423 struct perf_cpu_context *cpuctx,
424 struct perf_event_context *ctx)
428 * An event which could not be activated because of
429 * filter mismatch still needs to have its timings
430 * maintained, otherwise bogus information is return
431 * via read() for time_enabled, time_running:
433 if (event->state == PERF_EVENT_STATE_INACTIVE
434 && !event_filter_match(event)) {
435 delta = ctx->time - event->tstamp_stopped;
436 event->tstamp_running += delta;
437 event->tstamp_stopped = ctx->time;
440 if (event->state != PERF_EVENT_STATE_ACTIVE)
443 event->state = PERF_EVENT_STATE_INACTIVE;
444 if (event->pending_disable) {
445 event->pending_disable = 0;
446 event->state = PERF_EVENT_STATE_OFF;
448 event->tstamp_stopped = ctx->time;
449 event->pmu->del(event, 0);
452 if (!is_software_event(event))
453 cpuctx->active_oncpu--;
455 if (event->attr.exclusive || !cpuctx->active_oncpu)
456 cpuctx->exclusive = 0;
460 group_sched_out(struct perf_event *group_event,
461 struct perf_cpu_context *cpuctx,
462 struct perf_event_context *ctx)
464 struct perf_event *event;
465 int state = group_event->state;
467 event_sched_out(group_event, cpuctx, ctx);
470 * Schedule out siblings (if any):
472 list_for_each_entry(event, &group_event->sibling_list, group_entry)
473 event_sched_out(event, cpuctx, ctx);
475 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
476 cpuctx->exclusive = 0;
479 static inline struct perf_cpu_context *
480 __get_cpu_context(struct perf_event_context *ctx)
482 return this_cpu_ptr(ctx->pmu->pmu_cpu_context);
486 * Cross CPU call to remove a performance event
488 * We disable the event on the hardware level first. After that we
489 * remove it from the context list.
491 static void __perf_event_remove_from_context(void *info)
493 struct perf_event *event = info;
494 struct perf_event_context *ctx = event->ctx;
495 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
498 * If this is a task context, we need to check whether it is
499 * the current task context of this cpu. If not it has been
500 * scheduled out before the smp call arrived.
502 if (ctx->task && cpuctx->task_ctx != ctx)
505 raw_spin_lock(&ctx->lock);
507 event_sched_out(event, cpuctx, ctx);
509 list_del_event(event, ctx);
511 raw_spin_unlock(&ctx->lock);
516 * Remove the event from a task's (or a CPU's) list of events.
518 * Must be called with ctx->mutex held.
520 * CPU events are removed with a smp call. For task events we only
521 * call when the task is on a CPU.
523 * If event->ctx is a cloned context, callers must make sure that
524 * every task struct that event->ctx->task could possibly point to
525 * remains valid. This is OK when called from perf_release since
526 * that only calls us on the top-level context, which can't be a clone.
527 * When called from perf_event_exit_task, it's OK because the
528 * context has been detached from its task.
530 static void perf_event_remove_from_context(struct perf_event *event)
532 struct perf_event_context *ctx = event->ctx;
533 struct task_struct *task = ctx->task;
537 * Per cpu events are removed via an smp call and
538 * the removal is always successful.
540 smp_call_function_single(event->cpu,
541 __perf_event_remove_from_context,
547 task_oncpu_function_call(task, __perf_event_remove_from_context,
550 raw_spin_lock_irq(&ctx->lock);
552 * If the context is active we need to retry the smp call.
554 if (ctx->nr_active && !list_empty(&event->group_entry)) {
555 raw_spin_unlock_irq(&ctx->lock);
560 * The lock prevents that this context is scheduled in so we
561 * can remove the event safely, if the call above did not
564 if (!list_empty(&event->group_entry))
565 list_del_event(event, ctx);
566 raw_spin_unlock_irq(&ctx->lock);
570 * Cross CPU call to disable a performance event
572 static void __perf_event_disable(void *info)
574 struct perf_event *event = info;
575 struct perf_event_context *ctx = event->ctx;
576 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
579 * If this is a per-task event, need to check whether this
580 * event's task is the current task on this cpu.
582 if (ctx->task && cpuctx->task_ctx != ctx)
585 raw_spin_lock(&ctx->lock);
588 * If the event is on, turn it off.
589 * If it is in error state, leave it in error state.
591 if (event->state >= PERF_EVENT_STATE_INACTIVE) {
592 update_context_time(ctx);
593 update_group_times(event);
594 if (event == event->group_leader)
595 group_sched_out(event, cpuctx, ctx);
597 event_sched_out(event, cpuctx, ctx);
598 event->state = PERF_EVENT_STATE_OFF;
601 raw_spin_unlock(&ctx->lock);
607 * If event->ctx is a cloned context, callers must make sure that
608 * every task struct that event->ctx->task could possibly point to
609 * remains valid. This condition is satisifed when called through
610 * perf_event_for_each_child or perf_event_for_each because they
611 * hold the top-level event's child_mutex, so any descendant that
612 * goes to exit will block in sync_child_event.
613 * When called from perf_pending_event it's OK because event->ctx
614 * is the current context on this CPU and preemption is disabled,
615 * hence we can't get into perf_event_task_sched_out for this context.
617 void perf_event_disable(struct perf_event *event)
619 struct perf_event_context *ctx = event->ctx;
620 struct task_struct *task = ctx->task;
624 * Disable the event on the cpu that it's on
626 smp_call_function_single(event->cpu, __perf_event_disable,
632 task_oncpu_function_call(task, __perf_event_disable, event);
634 raw_spin_lock_irq(&ctx->lock);
636 * If the event is still active, we need to retry the cross-call.
638 if (event->state == PERF_EVENT_STATE_ACTIVE) {
639 raw_spin_unlock_irq(&ctx->lock);
644 * Since we have the lock this context can't be scheduled
645 * in, so we can change the state safely.
647 if (event->state == PERF_EVENT_STATE_INACTIVE) {
648 update_group_times(event);
649 event->state = PERF_EVENT_STATE_OFF;
652 raw_spin_unlock_irq(&ctx->lock);
656 event_sched_in(struct perf_event *event,
657 struct perf_cpu_context *cpuctx,
658 struct perf_event_context *ctx)
660 if (event->state <= PERF_EVENT_STATE_OFF)
663 event->state = PERF_EVENT_STATE_ACTIVE;
664 event->oncpu = smp_processor_id();
666 * The new state must be visible before we turn it on in the hardware:
670 if (event->pmu->add(event, PERF_EF_START)) {
671 event->state = PERF_EVENT_STATE_INACTIVE;
676 event->tstamp_running += ctx->time - event->tstamp_stopped;
678 if (!is_software_event(event))
679 cpuctx->active_oncpu++;
682 if (event->attr.exclusive)
683 cpuctx->exclusive = 1;
689 group_sched_in(struct perf_event *group_event,
690 struct perf_cpu_context *cpuctx,
691 struct perf_event_context *ctx)
693 struct perf_event *event, *partial_group = NULL;
694 struct pmu *pmu = group_event->pmu;
696 bool simulate = false;
698 if (group_event->state == PERF_EVENT_STATE_OFF)
703 if (event_sched_in(group_event, cpuctx, ctx)) {
704 pmu->cancel_txn(pmu);
709 * Schedule in siblings as one group (if any):
711 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
712 if (event_sched_in(event, cpuctx, ctx)) {
713 partial_group = event;
718 if (!pmu->commit_txn(pmu))
723 * Groups can be scheduled in as one unit only, so undo any
724 * partial group before returning:
725 * The events up to the failed event are scheduled out normally,
726 * tstamp_stopped will be updated.
728 * The failed events and the remaining siblings need to have
729 * their timings updated as if they had gone thru event_sched_in()
730 * and event_sched_out(). This is required to get consistent timings
731 * across the group. This also takes care of the case where the group
732 * could never be scheduled by ensuring tstamp_stopped is set to mark
733 * the time the event was actually stopped, such that time delta
734 * calculation in update_event_times() is correct.
736 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
737 if (event == partial_group)
741 event->tstamp_running += now - event->tstamp_stopped;
742 event->tstamp_stopped = now;
744 event_sched_out(event, cpuctx, ctx);
747 event_sched_out(group_event, cpuctx, ctx);
749 pmu->cancel_txn(pmu);
755 * Work out whether we can put this event group on the CPU now.
757 static int group_can_go_on(struct perf_event *event,
758 struct perf_cpu_context *cpuctx,
762 * Groups consisting entirely of software events can always go on.
764 if (event->group_flags & PERF_GROUP_SOFTWARE)
767 * If an exclusive group is already on, no other hardware
770 if (cpuctx->exclusive)
773 * If this group is exclusive and there are already
774 * events on the CPU, it can't go on.
776 if (event->attr.exclusive && cpuctx->active_oncpu)
779 * Otherwise, try to add it if all previous groups were able
785 static void add_event_to_ctx(struct perf_event *event,
786 struct perf_event_context *ctx)
788 list_add_event(event, ctx);
789 perf_group_attach(event);
790 event->tstamp_enabled = ctx->time;
791 event->tstamp_running = ctx->time;
792 event->tstamp_stopped = ctx->time;
796 * Cross CPU call to install and enable a performance event
798 * Must be called with ctx->mutex held
800 static void __perf_install_in_context(void *info)
802 struct perf_event *event = info;
803 struct perf_event_context *ctx = event->ctx;
804 struct perf_event *leader = event->group_leader;
805 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
809 * If this is a task context, we need to check whether it is
810 * the current task context of this cpu. If not it has been
811 * scheduled out before the smp call arrived.
812 * Or possibly this is the right context but it isn't
813 * on this cpu because it had no events.
815 if (ctx->task && cpuctx->task_ctx != ctx) {
816 if (cpuctx->task_ctx || ctx->task != current)
818 cpuctx->task_ctx = ctx;
821 raw_spin_lock(&ctx->lock);
823 update_context_time(ctx);
825 add_event_to_ctx(event, ctx);
827 if (event->cpu != -1 && event->cpu != smp_processor_id())
831 * Don't put the event on if it is disabled or if
832 * it is in a group and the group isn't on.
834 if (event->state != PERF_EVENT_STATE_INACTIVE ||
835 (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE))
839 * An exclusive event can't go on if there are already active
840 * hardware events, and no hardware event can go on if there
841 * is already an exclusive event on.
843 if (!group_can_go_on(event, cpuctx, 1))
846 err = event_sched_in(event, cpuctx, ctx);
850 * This event couldn't go on. If it is in a group
851 * then we have to pull the whole group off.
852 * If the event group is pinned then put it in error state.
855 group_sched_out(leader, cpuctx, ctx);
856 if (leader->attr.pinned) {
857 update_group_times(leader);
858 leader->state = PERF_EVENT_STATE_ERROR;
863 raw_spin_unlock(&ctx->lock);
867 * Attach a performance event to a context
869 * First we add the event to the list with the hardware enable bit
870 * in event->hw_config cleared.
872 * If the event is attached to a task which is on a CPU we use a smp
873 * call to enable it in the task context. The task might have been
874 * scheduled away, but we check this in the smp call again.
876 * Must be called with ctx->mutex held.
879 perf_install_in_context(struct perf_event_context *ctx,
880 struct perf_event *event,
883 struct task_struct *task = ctx->task;
889 * Per cpu events are installed via an smp call and
890 * the install is always successful.
892 smp_call_function_single(cpu, __perf_install_in_context,
898 task_oncpu_function_call(task, __perf_install_in_context,
901 raw_spin_lock_irq(&ctx->lock);
903 * we need to retry the smp call.
905 if (ctx->is_active && list_empty(&event->group_entry)) {
906 raw_spin_unlock_irq(&ctx->lock);
911 * The lock prevents that this context is scheduled in so we
912 * can add the event safely, if it the call above did not
915 if (list_empty(&event->group_entry))
916 add_event_to_ctx(event, ctx);
917 raw_spin_unlock_irq(&ctx->lock);
921 * Put a event into inactive state and update time fields.
922 * Enabling the leader of a group effectively enables all
923 * the group members that aren't explicitly disabled, so we
924 * have to update their ->tstamp_enabled also.
925 * Note: this works for group members as well as group leaders
926 * since the non-leader members' sibling_lists will be empty.
928 static void __perf_event_mark_enabled(struct perf_event *event,
929 struct perf_event_context *ctx)
931 struct perf_event *sub;
933 event->state = PERF_EVENT_STATE_INACTIVE;
934 event->tstamp_enabled = ctx->time - event->total_time_enabled;
935 list_for_each_entry(sub, &event->sibling_list, group_entry) {
936 if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
937 sub->tstamp_enabled =
938 ctx->time - sub->total_time_enabled;
944 * Cross CPU call to enable a performance event
946 static void __perf_event_enable(void *info)
948 struct perf_event *event = info;
949 struct perf_event_context *ctx = event->ctx;
950 struct perf_event *leader = event->group_leader;
951 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
955 * If this is a per-task event, need to check whether this
956 * event's task is the current task on this cpu.
958 if (ctx->task && cpuctx->task_ctx != ctx) {
959 if (cpuctx->task_ctx || ctx->task != current)
961 cpuctx->task_ctx = ctx;
964 raw_spin_lock(&ctx->lock);
966 update_context_time(ctx);
968 if (event->state >= PERF_EVENT_STATE_INACTIVE)
970 __perf_event_mark_enabled(event, ctx);
972 if (event->cpu != -1 && event->cpu != smp_processor_id())
976 * If the event is in a group and isn't the group leader,
977 * then don't put it on unless the group is on.
979 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
982 if (!group_can_go_on(event, cpuctx, 1)) {
986 err = group_sched_in(event, cpuctx, ctx);
988 err = event_sched_in(event, cpuctx, ctx);
993 * If this event can't go on and it's part of a
994 * group, then the whole group has to come off.
997 group_sched_out(leader, cpuctx, ctx);
998 if (leader->attr.pinned) {
999 update_group_times(leader);
1000 leader->state = PERF_EVENT_STATE_ERROR;
1005 raw_spin_unlock(&ctx->lock);
1011 * If event->ctx is a cloned context, callers must make sure that
1012 * every task struct that event->ctx->task could possibly point to
1013 * remains valid. This condition is satisfied when called through
1014 * perf_event_for_each_child or perf_event_for_each as described
1015 * for perf_event_disable.
1017 void perf_event_enable(struct perf_event *event)
1019 struct perf_event_context *ctx = event->ctx;
1020 struct task_struct *task = ctx->task;
1024 * Enable the event on the cpu that it's on
1026 smp_call_function_single(event->cpu, __perf_event_enable,
1031 raw_spin_lock_irq(&ctx->lock);
1032 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1036 * If the event is in error state, clear that first.
1037 * That way, if we see the event in error state below, we
1038 * know that it has gone back into error state, as distinct
1039 * from the task having been scheduled away before the
1040 * cross-call arrived.
1042 if (event->state == PERF_EVENT_STATE_ERROR)
1043 event->state = PERF_EVENT_STATE_OFF;
1046 raw_spin_unlock_irq(&ctx->lock);
1047 task_oncpu_function_call(task, __perf_event_enable, event);
1049 raw_spin_lock_irq(&ctx->lock);
1052 * If the context is active and the event is still off,
1053 * we need to retry the cross-call.
1055 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF)
1059 * Since we have the lock this context can't be scheduled
1060 * in, so we can change the state safely.
1062 if (event->state == PERF_EVENT_STATE_OFF)
1063 __perf_event_mark_enabled(event, ctx);
1066 raw_spin_unlock_irq(&ctx->lock);
1069 static int perf_event_refresh(struct perf_event *event, int refresh)
1072 * not supported on inherited events
1074 if (event->attr.inherit)
1077 atomic_add(refresh, &event->event_limit);
1078 perf_event_enable(event);
1084 EVENT_FLEXIBLE = 0x1,
1086 EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
1089 static void ctx_sched_out(struct perf_event_context *ctx,
1090 struct perf_cpu_context *cpuctx,
1091 enum event_type_t event_type)
1093 struct perf_event *event;
1095 raw_spin_lock(&ctx->lock);
1096 perf_pmu_disable(ctx->pmu);
1098 if (likely(!ctx->nr_events))
1100 update_context_time(ctx);
1102 if (!ctx->nr_active)
1105 if (event_type & EVENT_PINNED) {
1106 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
1107 group_sched_out(event, cpuctx, ctx);
1110 if (event_type & EVENT_FLEXIBLE) {
1111 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
1112 group_sched_out(event, cpuctx, ctx);
1115 perf_pmu_enable(ctx->pmu);
1116 raw_spin_unlock(&ctx->lock);
1120 * Test whether two contexts are equivalent, i.e. whether they
1121 * have both been cloned from the same version of the same context
1122 * and they both have the same number of enabled events.
1123 * If the number of enabled events is the same, then the set
1124 * of enabled events should be the same, because these are both
1125 * inherited contexts, therefore we can't access individual events
1126 * in them directly with an fd; we can only enable/disable all
1127 * events via prctl, or enable/disable all events in a family
1128 * via ioctl, which will have the same effect on both contexts.
1130 static int context_equiv(struct perf_event_context *ctx1,
1131 struct perf_event_context *ctx2)
1133 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
1134 && ctx1->parent_gen == ctx2->parent_gen
1135 && !ctx1->pin_count && !ctx2->pin_count;
1138 static void __perf_event_sync_stat(struct perf_event *event,
1139 struct perf_event *next_event)
1143 if (!event->attr.inherit_stat)
1147 * Update the event value, we cannot use perf_event_read()
1148 * because we're in the middle of a context switch and have IRQs
1149 * disabled, which upsets smp_call_function_single(), however
1150 * we know the event must be on the current CPU, therefore we
1151 * don't need to use it.
1153 switch (event->state) {
1154 case PERF_EVENT_STATE_ACTIVE:
1155 event->pmu->read(event);
1158 case PERF_EVENT_STATE_INACTIVE:
1159 update_event_times(event);
1167 * In order to keep per-task stats reliable we need to flip the event
1168 * values when we flip the contexts.
1170 value = local64_read(&next_event->count);
1171 value = local64_xchg(&event->count, value);
1172 local64_set(&next_event->count, value);
1174 swap(event->total_time_enabled, next_event->total_time_enabled);
1175 swap(event->total_time_running, next_event->total_time_running);
1178 * Since we swizzled the values, update the user visible data too.
1180 perf_event_update_userpage(event);
1181 perf_event_update_userpage(next_event);
1184 #define list_next_entry(pos, member) \
1185 list_entry(pos->member.next, typeof(*pos), member)
1187 static void perf_event_sync_stat(struct perf_event_context *ctx,
1188 struct perf_event_context *next_ctx)
1190 struct perf_event *event, *next_event;
1195 update_context_time(ctx);
1197 event = list_first_entry(&ctx->event_list,
1198 struct perf_event, event_entry);
1200 next_event = list_first_entry(&next_ctx->event_list,
1201 struct perf_event, event_entry);
1203 while (&event->event_entry != &ctx->event_list &&
1204 &next_event->event_entry != &next_ctx->event_list) {
1206 __perf_event_sync_stat(event, next_event);
1208 event = list_next_entry(event, event_entry);
1209 next_event = list_next_entry(next_event, event_entry);
1213 void perf_event_context_sched_out(struct task_struct *task, int ctxn,
1214 struct task_struct *next)
1216 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
1217 struct perf_event_context *next_ctx;
1218 struct perf_event_context *parent;
1219 struct perf_cpu_context *cpuctx;
1225 cpuctx = __get_cpu_context(ctx);
1226 if (!cpuctx->task_ctx)
1230 parent = rcu_dereference(ctx->parent_ctx);
1231 next_ctx = next->perf_event_ctxp[ctxn];
1232 if (parent && next_ctx &&
1233 rcu_dereference(next_ctx->parent_ctx) == parent) {
1235 * Looks like the two contexts are clones, so we might be
1236 * able to optimize the context switch. We lock both
1237 * contexts and check that they are clones under the
1238 * lock (including re-checking that neither has been
1239 * uncloned in the meantime). It doesn't matter which
1240 * order we take the locks because no other cpu could
1241 * be trying to lock both of these tasks.
1243 raw_spin_lock(&ctx->lock);
1244 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
1245 if (context_equiv(ctx, next_ctx)) {
1247 * XXX do we need a memory barrier of sorts
1248 * wrt to rcu_dereference() of perf_event_ctxp
1250 task->perf_event_ctxp[ctxn] = next_ctx;
1251 next->perf_event_ctxp[ctxn] = ctx;
1253 next_ctx->task = task;
1256 perf_event_sync_stat(ctx, next_ctx);
1258 raw_spin_unlock(&next_ctx->lock);
1259 raw_spin_unlock(&ctx->lock);
1264 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
1265 cpuctx->task_ctx = NULL;
1269 #define for_each_task_context_nr(ctxn) \
1270 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
1273 * Called from scheduler to remove the events of the current task,
1274 * with interrupts disabled.
1276 * We stop each event and update the event value in event->count.
1278 * This does not protect us against NMI, but disable()
1279 * sets the disabled bit in the control field of event _before_
1280 * accessing the event control register. If a NMI hits, then it will
1281 * not restart the event.
1283 void __perf_event_task_sched_out(struct task_struct *task,
1284 struct task_struct *next)
1288 perf_sw_event(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 1, NULL, 0);
1290 for_each_task_context_nr(ctxn)
1291 perf_event_context_sched_out(task, ctxn, next);
1294 static void task_ctx_sched_out(struct perf_event_context *ctx,
1295 enum event_type_t event_type)
1297 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1299 if (!cpuctx->task_ctx)
1302 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
1305 ctx_sched_out(ctx, cpuctx, event_type);
1306 cpuctx->task_ctx = NULL;
1310 * Called with IRQs disabled
1312 static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
1313 enum event_type_t event_type)
1315 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
1319 ctx_pinned_sched_in(struct perf_event_context *ctx,
1320 struct perf_cpu_context *cpuctx)
1322 struct perf_event *event;
1324 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
1325 if (event->state <= PERF_EVENT_STATE_OFF)
1327 if (event->cpu != -1 && event->cpu != smp_processor_id())
1330 if (group_can_go_on(event, cpuctx, 1))
1331 group_sched_in(event, cpuctx, ctx);
1334 * If this pinned group hasn't been scheduled,
1335 * put it in error state.
1337 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1338 update_group_times(event);
1339 event->state = PERF_EVENT_STATE_ERROR;
1345 ctx_flexible_sched_in(struct perf_event_context *ctx,
1346 struct perf_cpu_context *cpuctx)
1348 struct perf_event *event;
1351 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
1352 /* Ignore events in OFF or ERROR state */
1353 if (event->state <= PERF_EVENT_STATE_OFF)
1356 * Listen to the 'cpu' scheduling filter constraint
1359 if (event->cpu != -1 && event->cpu != smp_processor_id())
1362 if (group_can_go_on(event, cpuctx, can_add_hw)) {
1363 if (group_sched_in(event, cpuctx, ctx))
1370 ctx_sched_in(struct perf_event_context *ctx,
1371 struct perf_cpu_context *cpuctx,
1372 enum event_type_t event_type)
1374 raw_spin_lock(&ctx->lock);
1376 if (likely(!ctx->nr_events))
1379 ctx->timestamp = perf_clock();
1382 * First go through the list and put on any pinned groups
1383 * in order to give them the best chance of going on.
1385 if (event_type & EVENT_PINNED)
1386 ctx_pinned_sched_in(ctx, cpuctx);
1388 /* Then walk through the lower prio flexible groups */
1389 if (event_type & EVENT_FLEXIBLE)
1390 ctx_flexible_sched_in(ctx, cpuctx);
1393 raw_spin_unlock(&ctx->lock);
1396 static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
1397 enum event_type_t event_type)
1399 struct perf_event_context *ctx = &cpuctx->ctx;
1401 ctx_sched_in(ctx, cpuctx, event_type);
1404 static void task_ctx_sched_in(struct perf_event_context *ctx,
1405 enum event_type_t event_type)
1407 struct perf_cpu_context *cpuctx;
1409 cpuctx = __get_cpu_context(ctx);
1410 if (cpuctx->task_ctx == ctx)
1413 ctx_sched_in(ctx, cpuctx, event_type);
1414 cpuctx->task_ctx = ctx;
1417 void perf_event_context_sched_in(struct perf_event_context *ctx)
1419 struct perf_cpu_context *cpuctx;
1421 cpuctx = __get_cpu_context(ctx);
1422 if (cpuctx->task_ctx == ctx)
1425 perf_pmu_disable(ctx->pmu);
1427 * We want to keep the following priority order:
1428 * cpu pinned (that don't need to move), task pinned,
1429 * cpu flexible, task flexible.
1431 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
1433 ctx_sched_in(ctx, cpuctx, EVENT_PINNED);
1434 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE);
1435 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE);
1437 cpuctx->task_ctx = ctx;
1440 * Since these rotations are per-cpu, we need to ensure the
1441 * cpu-context we got scheduled on is actually rotating.
1443 perf_pmu_rotate_start(ctx->pmu);
1444 perf_pmu_enable(ctx->pmu);
1448 * Called from scheduler to add the events of the current task
1449 * with interrupts disabled.
1451 * We restore the event value and then enable it.
1453 * This does not protect us against NMI, but enable()
1454 * sets the enabled bit in the control field of event _before_
1455 * accessing the event control register. If a NMI hits, then it will
1456 * keep the event running.
1458 void __perf_event_task_sched_in(struct task_struct *task)
1460 struct perf_event_context *ctx;
1463 for_each_task_context_nr(ctxn) {
1464 ctx = task->perf_event_ctxp[ctxn];
1468 perf_event_context_sched_in(ctx);
1472 #define MAX_INTERRUPTS (~0ULL)
1474 static void perf_log_throttle(struct perf_event *event, int enable);
1476 static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
1478 u64 frequency = event->attr.sample_freq;
1479 u64 sec = NSEC_PER_SEC;
1480 u64 divisor, dividend;
1482 int count_fls, nsec_fls, frequency_fls, sec_fls;
1484 count_fls = fls64(count);
1485 nsec_fls = fls64(nsec);
1486 frequency_fls = fls64(frequency);
1490 * We got @count in @nsec, with a target of sample_freq HZ
1491 * the target period becomes:
1494 * period = -------------------
1495 * @nsec * sample_freq
1500 * Reduce accuracy by one bit such that @a and @b converge
1501 * to a similar magnitude.
1503 #define REDUCE_FLS(a, b) \
1505 if (a##_fls > b##_fls) { \
1515 * Reduce accuracy until either term fits in a u64, then proceed with
1516 * the other, so that finally we can do a u64/u64 division.
1518 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
1519 REDUCE_FLS(nsec, frequency);
1520 REDUCE_FLS(sec, count);
1523 if (count_fls + sec_fls > 64) {
1524 divisor = nsec * frequency;
1526 while (count_fls + sec_fls > 64) {
1527 REDUCE_FLS(count, sec);
1531 dividend = count * sec;
1533 dividend = count * sec;
1535 while (nsec_fls + frequency_fls > 64) {
1536 REDUCE_FLS(nsec, frequency);
1540 divisor = nsec * frequency;
1546 return div64_u64(dividend, divisor);
1549 static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count)
1551 struct hw_perf_event *hwc = &event->hw;
1552 s64 period, sample_period;
1555 period = perf_calculate_period(event, nsec, count);
1557 delta = (s64)(period - hwc->sample_period);
1558 delta = (delta + 7) / 8; /* low pass filter */
1560 sample_period = hwc->sample_period + delta;
1565 hwc->sample_period = sample_period;
1567 if (local64_read(&hwc->period_left) > 8*sample_period) {
1568 event->pmu->stop(event, PERF_EF_UPDATE);
1569 local64_set(&hwc->period_left, 0);
1570 event->pmu->start(event, PERF_EF_RELOAD);
1574 static void perf_ctx_adjust_freq(struct perf_event_context *ctx, u64 period)
1576 struct perf_event *event;
1577 struct hw_perf_event *hwc;
1578 u64 interrupts, now;
1581 raw_spin_lock(&ctx->lock);
1582 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
1583 if (event->state != PERF_EVENT_STATE_ACTIVE)
1586 if (event->cpu != -1 && event->cpu != smp_processor_id())
1591 interrupts = hwc->interrupts;
1592 hwc->interrupts = 0;
1595 * unthrottle events on the tick
1597 if (interrupts == MAX_INTERRUPTS) {
1598 perf_log_throttle(event, 1);
1599 event->pmu->start(event, 0);
1602 if (!event->attr.freq || !event->attr.sample_freq)
1605 event->pmu->read(event);
1606 now = local64_read(&event->count);
1607 delta = now - hwc->freq_count_stamp;
1608 hwc->freq_count_stamp = now;
1611 perf_adjust_period(event, period, delta);
1613 raw_spin_unlock(&ctx->lock);
1617 * Round-robin a context's events:
1619 static void rotate_ctx(struct perf_event_context *ctx)
1621 raw_spin_lock(&ctx->lock);
1623 /* Rotate the first entry last of non-pinned groups */
1624 list_rotate_left(&ctx->flexible_groups);
1626 raw_spin_unlock(&ctx->lock);
1630 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
1631 * because they're strictly cpu affine and rotate_start is called with IRQs
1632 * disabled, while rotate_context is called from IRQ context.
1634 static void perf_rotate_context(struct perf_cpu_context *cpuctx)
1636 u64 interval = (u64)cpuctx->jiffies_interval * TICK_NSEC;
1637 struct perf_event_context *ctx = NULL;
1638 int rotate = 0, remove = 1;
1640 if (cpuctx->ctx.nr_events) {
1642 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
1646 ctx = cpuctx->task_ctx;
1647 if (ctx && ctx->nr_events) {
1649 if (ctx->nr_events != ctx->nr_active)
1653 perf_pmu_disable(cpuctx->ctx.pmu);
1654 perf_ctx_adjust_freq(&cpuctx->ctx, interval);
1656 perf_ctx_adjust_freq(ctx, interval);
1661 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
1663 task_ctx_sched_out(ctx, EVENT_FLEXIBLE);
1665 rotate_ctx(&cpuctx->ctx);
1669 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE);
1671 task_ctx_sched_in(ctx, EVENT_FLEXIBLE);
1675 list_del_init(&cpuctx->rotation_list);
1677 perf_pmu_enable(cpuctx->ctx.pmu);
1680 void perf_event_task_tick(void)
1682 struct list_head *head = &__get_cpu_var(rotation_list);
1683 struct perf_cpu_context *cpuctx, *tmp;
1685 WARN_ON(!irqs_disabled());
1687 list_for_each_entry_safe(cpuctx, tmp, head, rotation_list) {
1688 if (cpuctx->jiffies_interval == 1 ||
1689 !(jiffies % cpuctx->jiffies_interval))
1690 perf_rotate_context(cpuctx);
1694 static int event_enable_on_exec(struct perf_event *event,
1695 struct perf_event_context *ctx)
1697 if (!event->attr.enable_on_exec)
1700 event->attr.enable_on_exec = 0;
1701 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1704 __perf_event_mark_enabled(event, ctx);
1710 * Enable all of a task's events that have been marked enable-on-exec.
1711 * This expects task == current.
1713 static void perf_event_enable_on_exec(struct perf_event_context *ctx)
1715 struct perf_event *event;
1716 unsigned long flags;
1720 local_irq_save(flags);
1721 if (!ctx || !ctx->nr_events)
1724 task_ctx_sched_out(ctx, EVENT_ALL);
1726 raw_spin_lock(&ctx->lock);
1728 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
1729 ret = event_enable_on_exec(event, ctx);
1734 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
1735 ret = event_enable_on_exec(event, ctx);
1741 * Unclone this context if we enabled any event.
1746 raw_spin_unlock(&ctx->lock);
1748 perf_event_context_sched_in(ctx);
1750 local_irq_restore(flags);
1754 * Cross CPU call to read the hardware event
1756 static void __perf_event_read(void *info)
1758 struct perf_event *event = info;
1759 struct perf_event_context *ctx = event->ctx;
1760 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1763 * If this is a task context, we need to check whether it is
1764 * the current task context of this cpu. If not it has been
1765 * scheduled out before the smp call arrived. In that case
1766 * event->count would have been updated to a recent sample
1767 * when the event was scheduled out.
1769 if (ctx->task && cpuctx->task_ctx != ctx)
1772 raw_spin_lock(&ctx->lock);
1773 update_context_time(ctx);
1774 update_event_times(event);
1775 raw_spin_unlock(&ctx->lock);
1777 event->pmu->read(event);
1780 static inline u64 perf_event_count(struct perf_event *event)
1782 return local64_read(&event->count) + atomic64_read(&event->child_count);
1785 static u64 perf_event_read(struct perf_event *event)
1788 * If event is enabled and currently active on a CPU, update the
1789 * value in the event structure:
1791 if (event->state == PERF_EVENT_STATE_ACTIVE) {
1792 smp_call_function_single(event->oncpu,
1793 __perf_event_read, event, 1);
1794 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
1795 struct perf_event_context *ctx = event->ctx;
1796 unsigned long flags;
1798 raw_spin_lock_irqsave(&ctx->lock, flags);
1800 * may read while context is not active
1801 * (e.g., thread is blocked), in that case
1802 * we cannot update context time
1805 update_context_time(ctx);
1806 update_event_times(event);
1807 raw_spin_unlock_irqrestore(&ctx->lock, flags);
1810 return perf_event_count(event);
1817 struct callchain_cpus_entries {
1818 struct rcu_head rcu_head;
1819 struct perf_callchain_entry *cpu_entries[0];
1822 static DEFINE_PER_CPU(int, callchain_recursion[PERF_NR_CONTEXTS]);
1823 static atomic_t nr_callchain_events;
1824 static DEFINE_MUTEX(callchain_mutex);
1825 struct callchain_cpus_entries *callchain_cpus_entries;
1828 __weak void perf_callchain_kernel(struct perf_callchain_entry *entry,
1829 struct pt_regs *regs)
1833 __weak void perf_callchain_user(struct perf_callchain_entry *entry,
1834 struct pt_regs *regs)
1838 static void release_callchain_buffers_rcu(struct rcu_head *head)
1840 struct callchain_cpus_entries *entries;
1843 entries = container_of(head, struct callchain_cpus_entries, rcu_head);
1845 for_each_possible_cpu(cpu)
1846 kfree(entries->cpu_entries[cpu]);
1851 static void release_callchain_buffers(void)
1853 struct callchain_cpus_entries *entries;
1855 entries = callchain_cpus_entries;
1856 rcu_assign_pointer(callchain_cpus_entries, NULL);
1857 call_rcu(&entries->rcu_head, release_callchain_buffers_rcu);
1860 static int alloc_callchain_buffers(void)
1864 struct callchain_cpus_entries *entries;
1867 * We can't use the percpu allocation API for data that can be
1868 * accessed from NMI. Use a temporary manual per cpu allocation
1869 * until that gets sorted out.
1871 size = sizeof(*entries) + sizeof(struct perf_callchain_entry *) *
1872 num_possible_cpus();
1874 entries = kzalloc(size, GFP_KERNEL);
1878 size = sizeof(struct perf_callchain_entry) * PERF_NR_CONTEXTS;
1880 for_each_possible_cpu(cpu) {
1881 entries->cpu_entries[cpu] = kmalloc_node(size, GFP_KERNEL,
1883 if (!entries->cpu_entries[cpu])
1887 rcu_assign_pointer(callchain_cpus_entries, entries);
1892 for_each_possible_cpu(cpu)
1893 kfree(entries->cpu_entries[cpu]);
1899 static int get_callchain_buffers(void)
1904 mutex_lock(&callchain_mutex);
1906 count = atomic_inc_return(&nr_callchain_events);
1907 if (WARN_ON_ONCE(count < 1)) {
1913 /* If the allocation failed, give up */
1914 if (!callchain_cpus_entries)
1919 err = alloc_callchain_buffers();
1921 release_callchain_buffers();
1923 mutex_unlock(&callchain_mutex);
1928 static void put_callchain_buffers(void)
1930 if (atomic_dec_and_mutex_lock(&nr_callchain_events, &callchain_mutex)) {
1931 release_callchain_buffers();
1932 mutex_unlock(&callchain_mutex);
1936 static int get_recursion_context(int *recursion)
1944 else if (in_softirq())
1949 if (recursion[rctx])
1958 static inline void put_recursion_context(int *recursion, int rctx)
1964 static struct perf_callchain_entry *get_callchain_entry(int *rctx)
1967 struct callchain_cpus_entries *entries;
1969 *rctx = get_recursion_context(__get_cpu_var(callchain_recursion));
1973 entries = rcu_dereference(callchain_cpus_entries);
1977 cpu = smp_processor_id();
1979 return &entries->cpu_entries[cpu][*rctx];
1983 put_callchain_entry(int rctx)
1985 put_recursion_context(__get_cpu_var(callchain_recursion), rctx);
1988 static struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
1991 struct perf_callchain_entry *entry;
1994 entry = get_callchain_entry(&rctx);
2003 if (!user_mode(regs)) {
2004 perf_callchain_store(entry, PERF_CONTEXT_KERNEL);
2005 perf_callchain_kernel(entry, regs);
2007 regs = task_pt_regs(current);
2013 perf_callchain_store(entry, PERF_CONTEXT_USER);
2014 perf_callchain_user(entry, regs);
2018 put_callchain_entry(rctx);
2024 * Initialize the perf_event context in a task_struct:
2026 static void __perf_event_init_context(struct perf_event_context *ctx)
2028 raw_spin_lock_init(&ctx->lock);
2029 mutex_init(&ctx->mutex);
2030 INIT_LIST_HEAD(&ctx->pinned_groups);
2031 INIT_LIST_HEAD(&ctx->flexible_groups);
2032 INIT_LIST_HEAD(&ctx->event_list);
2033 atomic_set(&ctx->refcount, 1);
2036 static struct perf_event_context *
2037 alloc_perf_context(struct pmu *pmu, struct task_struct *task)
2039 struct perf_event_context *ctx;
2041 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
2045 __perf_event_init_context(ctx);
2048 get_task_struct(task);
2055 static struct task_struct *
2056 find_lively_task_by_vpid(pid_t vpid)
2058 struct task_struct *task;
2065 task = find_task_by_vpid(vpid);
2067 get_task_struct(task);
2071 return ERR_PTR(-ESRCH);
2074 * Can't attach events to a dying task.
2077 if (task->flags & PF_EXITING)
2080 /* Reuse ptrace permission checks for now. */
2082 if (!ptrace_may_access(task, PTRACE_MODE_READ))
2087 put_task_struct(task);
2088 return ERR_PTR(err);
2092 static struct perf_event_context *
2093 find_get_context(struct pmu *pmu, struct task_struct *task, int cpu)
2095 struct perf_event_context *ctx;
2096 struct perf_cpu_context *cpuctx;
2097 unsigned long flags;
2100 if (!task && cpu != -1) {
2101 /* Must be root to operate on a CPU event: */
2102 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
2103 return ERR_PTR(-EACCES);
2105 if (cpu < 0 || cpu >= nr_cpumask_bits)
2106 return ERR_PTR(-EINVAL);
2109 * We could be clever and allow to attach a event to an
2110 * offline CPU and activate it when the CPU comes up, but
2113 if (!cpu_online(cpu))
2114 return ERR_PTR(-ENODEV);
2116 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
2124 ctxn = pmu->task_ctx_nr;
2129 ctx = perf_lock_task_context(task, ctxn, &flags);
2132 raw_spin_unlock_irqrestore(&ctx->lock, flags);
2136 ctx = alloc_perf_context(pmu, task);
2143 if (cmpxchg(&task->perf_event_ctxp[ctxn], NULL, ctx)) {
2145 * We raced with some other task; use
2146 * the context they set.
2148 put_task_struct(task);
2157 return ERR_PTR(err);
2160 static void perf_event_free_filter(struct perf_event *event);
2162 static void free_event_rcu(struct rcu_head *head)
2164 struct perf_event *event;
2166 event = container_of(head, struct perf_event, rcu_head);
2168 put_pid_ns(event->ns);
2169 perf_event_free_filter(event);
2173 static void perf_buffer_put(struct perf_buffer *buffer);
2175 static void free_event(struct perf_event *event)
2177 irq_work_sync(&event->pending);
2179 if (!event->parent) {
2180 if (event->attach_state & PERF_ATTACH_TASK)
2181 jump_label_dec(&perf_task_events);
2182 if (event->attr.mmap || event->attr.mmap_data)
2183 atomic_dec(&nr_mmap_events);
2184 if (event->attr.comm)
2185 atomic_dec(&nr_comm_events);
2186 if (event->attr.task)
2187 atomic_dec(&nr_task_events);
2188 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
2189 put_callchain_buffers();
2192 if (event->buffer) {
2193 perf_buffer_put(event->buffer);
2194 event->buffer = NULL;
2198 event->destroy(event);
2201 put_ctx(event->ctx);
2203 call_rcu(&event->rcu_head, free_event_rcu);
2206 int perf_event_release_kernel(struct perf_event *event)
2208 struct perf_event_context *ctx = event->ctx;
2211 * Remove from the PMU, can't get re-enabled since we got
2212 * here because the last ref went.
2214 perf_event_disable(event);
2216 WARN_ON_ONCE(ctx->parent_ctx);
2218 * There are two ways this annotation is useful:
2220 * 1) there is a lock recursion from perf_event_exit_task
2221 * see the comment there.
2223 * 2) there is a lock-inversion with mmap_sem through
2224 * perf_event_read_group(), which takes faults while
2225 * holding ctx->mutex, however this is called after
2226 * the last filedesc died, so there is no possibility
2227 * to trigger the AB-BA case.
2229 mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
2230 raw_spin_lock_irq(&ctx->lock);
2231 perf_group_detach(event);
2232 list_del_event(event, ctx);
2233 raw_spin_unlock_irq(&ctx->lock);
2234 mutex_unlock(&ctx->mutex);
2236 mutex_lock(&event->owner->perf_event_mutex);
2237 list_del_init(&event->owner_entry);
2238 mutex_unlock(&event->owner->perf_event_mutex);
2239 put_task_struct(event->owner);
2245 EXPORT_SYMBOL_GPL(perf_event_release_kernel);
2248 * Called when the last reference to the file is gone.
2250 static int perf_release(struct inode *inode, struct file *file)
2252 struct perf_event *event = file->private_data;
2254 file->private_data = NULL;
2256 return perf_event_release_kernel(event);
2259 static int perf_event_read_size(struct perf_event *event)
2261 int entry = sizeof(u64); /* value */
2265 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2266 size += sizeof(u64);
2268 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2269 size += sizeof(u64);
2271 if (event->attr.read_format & PERF_FORMAT_ID)
2272 entry += sizeof(u64);
2274 if (event->attr.read_format & PERF_FORMAT_GROUP) {
2275 nr += event->group_leader->nr_siblings;
2276 size += sizeof(u64);
2284 u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
2286 struct perf_event *child;
2292 mutex_lock(&event->child_mutex);
2293 total += perf_event_read(event);
2294 *enabled += event->total_time_enabled +
2295 atomic64_read(&event->child_total_time_enabled);
2296 *running += event->total_time_running +
2297 atomic64_read(&event->child_total_time_running);
2299 list_for_each_entry(child, &event->child_list, child_list) {
2300 total += perf_event_read(child);
2301 *enabled += child->total_time_enabled;
2302 *running += child->total_time_running;
2304 mutex_unlock(&event->child_mutex);
2308 EXPORT_SYMBOL_GPL(perf_event_read_value);
2310 static int perf_event_read_group(struct perf_event *event,
2311 u64 read_format, char __user *buf)
2313 struct perf_event *leader = event->group_leader, *sub;
2314 int n = 0, size = 0, ret = -EFAULT;
2315 struct perf_event_context *ctx = leader->ctx;
2317 u64 count, enabled, running;
2319 mutex_lock(&ctx->mutex);
2320 count = perf_event_read_value(leader, &enabled, &running);
2322 values[n++] = 1 + leader->nr_siblings;
2323 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2324 values[n++] = enabled;
2325 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2326 values[n++] = running;
2327 values[n++] = count;
2328 if (read_format & PERF_FORMAT_ID)
2329 values[n++] = primary_event_id(leader);
2331 size = n * sizeof(u64);
2333 if (copy_to_user(buf, values, size))
2338 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
2341 values[n++] = perf_event_read_value(sub, &enabled, &running);
2342 if (read_format & PERF_FORMAT_ID)
2343 values[n++] = primary_event_id(sub);
2345 size = n * sizeof(u64);
2347 if (copy_to_user(buf + ret, values, size)) {
2355 mutex_unlock(&ctx->mutex);
2360 static int perf_event_read_one(struct perf_event *event,
2361 u64 read_format, char __user *buf)
2363 u64 enabled, running;
2367 values[n++] = perf_event_read_value(event, &enabled, &running);
2368 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2369 values[n++] = enabled;
2370 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2371 values[n++] = running;
2372 if (read_format & PERF_FORMAT_ID)
2373 values[n++] = primary_event_id(event);
2375 if (copy_to_user(buf, values, n * sizeof(u64)))
2378 return n * sizeof(u64);
2382 * Read the performance event - simple non blocking version for now
2385 perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
2387 u64 read_format = event->attr.read_format;
2391 * Return end-of-file for a read on a event that is in
2392 * error state (i.e. because it was pinned but it couldn't be
2393 * scheduled on to the CPU at some point).
2395 if (event->state == PERF_EVENT_STATE_ERROR)
2398 if (count < perf_event_read_size(event))
2401 WARN_ON_ONCE(event->ctx->parent_ctx);
2402 if (read_format & PERF_FORMAT_GROUP)
2403 ret = perf_event_read_group(event, read_format, buf);
2405 ret = perf_event_read_one(event, read_format, buf);
2411 perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
2413 struct perf_event *event = file->private_data;
2415 return perf_read_hw(event, buf, count);
2418 static unsigned int perf_poll(struct file *file, poll_table *wait)
2420 struct perf_event *event = file->private_data;
2421 struct perf_buffer *buffer;
2422 unsigned int events = POLL_HUP;
2425 buffer = rcu_dereference(event->buffer);
2427 events = atomic_xchg(&buffer->poll, 0);
2430 poll_wait(file, &event->waitq, wait);
2435 static void perf_event_reset(struct perf_event *event)
2437 (void)perf_event_read(event);
2438 local64_set(&event->count, 0);
2439 perf_event_update_userpage(event);
2443 * Holding the top-level event's child_mutex means that any
2444 * descendant process that has inherited this event will block
2445 * in sync_child_event if it goes to exit, thus satisfying the
2446 * task existence requirements of perf_event_enable/disable.
2448 static void perf_event_for_each_child(struct perf_event *event,
2449 void (*func)(struct perf_event *))
2451 struct perf_event *child;
2453 WARN_ON_ONCE(event->ctx->parent_ctx);
2454 mutex_lock(&event->child_mutex);
2456 list_for_each_entry(child, &event->child_list, child_list)
2458 mutex_unlock(&event->child_mutex);
2461 static void perf_event_for_each(struct perf_event *event,
2462 void (*func)(struct perf_event *))
2464 struct perf_event_context *ctx = event->ctx;
2465 struct perf_event *sibling;
2467 WARN_ON_ONCE(ctx->parent_ctx);
2468 mutex_lock(&ctx->mutex);
2469 event = event->group_leader;
2471 perf_event_for_each_child(event, func);
2473 list_for_each_entry(sibling, &event->sibling_list, group_entry)
2474 perf_event_for_each_child(event, func);
2475 mutex_unlock(&ctx->mutex);
2478 static int perf_event_period(struct perf_event *event, u64 __user *arg)
2480 struct perf_event_context *ctx = event->ctx;
2484 if (!event->attr.sample_period)
2487 if (copy_from_user(&value, arg, sizeof(value)))
2493 raw_spin_lock_irq(&ctx->lock);
2494 if (event->attr.freq) {
2495 if (value > sysctl_perf_event_sample_rate) {
2500 event->attr.sample_freq = value;
2502 event->attr.sample_period = value;
2503 event->hw.sample_period = value;
2506 raw_spin_unlock_irq(&ctx->lock);
2511 static const struct file_operations perf_fops;
2513 static struct perf_event *perf_fget_light(int fd, int *fput_needed)
2517 file = fget_light(fd, fput_needed);
2519 return ERR_PTR(-EBADF);
2521 if (file->f_op != &perf_fops) {
2522 fput_light(file, *fput_needed);
2524 return ERR_PTR(-EBADF);
2527 return file->private_data;
2530 static int perf_event_set_output(struct perf_event *event,
2531 struct perf_event *output_event);
2532 static int perf_event_set_filter(struct perf_event *event, void __user *arg);
2534 static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2536 struct perf_event *event = file->private_data;
2537 void (*func)(struct perf_event *);
2541 case PERF_EVENT_IOC_ENABLE:
2542 func = perf_event_enable;
2544 case PERF_EVENT_IOC_DISABLE:
2545 func = perf_event_disable;
2547 case PERF_EVENT_IOC_RESET:
2548 func = perf_event_reset;
2551 case PERF_EVENT_IOC_REFRESH:
2552 return perf_event_refresh(event, arg);
2554 case PERF_EVENT_IOC_PERIOD:
2555 return perf_event_period(event, (u64 __user *)arg);
2557 case PERF_EVENT_IOC_SET_OUTPUT:
2559 struct perf_event *output_event = NULL;
2560 int fput_needed = 0;
2564 output_event = perf_fget_light(arg, &fput_needed);
2565 if (IS_ERR(output_event))
2566 return PTR_ERR(output_event);
2569 ret = perf_event_set_output(event, output_event);
2571 fput_light(output_event->filp, fput_needed);
2576 case PERF_EVENT_IOC_SET_FILTER:
2577 return perf_event_set_filter(event, (void __user *)arg);
2583 if (flags & PERF_IOC_FLAG_GROUP)
2584 perf_event_for_each(event, func);
2586 perf_event_for_each_child(event, func);
2591 int perf_event_task_enable(void)
2593 struct perf_event *event;
2595 mutex_lock(¤t->perf_event_mutex);
2596 list_for_each_entry(event, ¤t->perf_event_list, owner_entry)
2597 perf_event_for_each_child(event, perf_event_enable);
2598 mutex_unlock(¤t->perf_event_mutex);
2603 int perf_event_task_disable(void)
2605 struct perf_event *event;
2607 mutex_lock(¤t->perf_event_mutex);
2608 list_for_each_entry(event, ¤t->perf_event_list, owner_entry)
2609 perf_event_for_each_child(event, perf_event_disable);
2610 mutex_unlock(¤t->perf_event_mutex);
2615 #ifndef PERF_EVENT_INDEX_OFFSET
2616 # define PERF_EVENT_INDEX_OFFSET 0
2619 static int perf_event_index(struct perf_event *event)
2621 if (event->hw.state & PERF_HES_STOPPED)
2624 if (event->state != PERF_EVENT_STATE_ACTIVE)
2627 return event->hw.idx + 1 - PERF_EVENT_INDEX_OFFSET;
2631 * Callers need to ensure there can be no nesting of this function, otherwise
2632 * the seqlock logic goes bad. We can not serialize this because the arch
2633 * code calls this from NMI context.
2635 void perf_event_update_userpage(struct perf_event *event)
2637 struct perf_event_mmap_page *userpg;
2638 struct perf_buffer *buffer;
2641 buffer = rcu_dereference(event->buffer);
2645 userpg = buffer->user_page;
2648 * Disable preemption so as to not let the corresponding user-space
2649 * spin too long if we get preempted.
2654 userpg->index = perf_event_index(event);
2655 userpg->offset = perf_event_count(event);
2656 if (event->state == PERF_EVENT_STATE_ACTIVE)
2657 userpg->offset -= local64_read(&event->hw.prev_count);
2659 userpg->time_enabled = event->total_time_enabled +
2660 atomic64_read(&event->child_total_time_enabled);
2662 userpg->time_running = event->total_time_running +
2663 atomic64_read(&event->child_total_time_running);
2672 static unsigned long perf_data_size(struct perf_buffer *buffer);
2675 perf_buffer_init(struct perf_buffer *buffer, long watermark, int flags)
2677 long max_size = perf_data_size(buffer);
2680 buffer->watermark = min(max_size, watermark);
2682 if (!buffer->watermark)
2683 buffer->watermark = max_size / 2;
2685 if (flags & PERF_BUFFER_WRITABLE)
2686 buffer->writable = 1;
2688 atomic_set(&buffer->refcount, 1);
2691 #ifndef CONFIG_PERF_USE_VMALLOC
2694 * Back perf_mmap() with regular GFP_KERNEL-0 pages.
2697 static struct page *
2698 perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
2700 if (pgoff > buffer->nr_pages)
2704 return virt_to_page(buffer->user_page);
2706 return virt_to_page(buffer->data_pages[pgoff - 1]);
2709 static void *perf_mmap_alloc_page(int cpu)
2714 node = (cpu == -1) ? cpu : cpu_to_node(cpu);
2715 page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
2719 return page_address(page);
2722 static struct perf_buffer *
2723 perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
2725 struct perf_buffer *buffer;
2729 size = sizeof(struct perf_buffer);
2730 size += nr_pages * sizeof(void *);
2732 buffer = kzalloc(size, GFP_KERNEL);
2736 buffer->user_page = perf_mmap_alloc_page(cpu);
2737 if (!buffer->user_page)
2738 goto fail_user_page;
2740 for (i = 0; i < nr_pages; i++) {
2741 buffer->data_pages[i] = perf_mmap_alloc_page(cpu);
2742 if (!buffer->data_pages[i])
2743 goto fail_data_pages;
2746 buffer->nr_pages = nr_pages;
2748 perf_buffer_init(buffer, watermark, flags);
2753 for (i--; i >= 0; i--)
2754 free_page((unsigned long)buffer->data_pages[i]);
2756 free_page((unsigned long)buffer->user_page);
2765 static void perf_mmap_free_page(unsigned long addr)
2767 struct page *page = virt_to_page((void *)addr);
2769 page->mapping = NULL;
2773 static void perf_buffer_free(struct perf_buffer *buffer)
2777 perf_mmap_free_page((unsigned long)buffer->user_page);
2778 for (i = 0; i < buffer->nr_pages; i++)
2779 perf_mmap_free_page((unsigned long)buffer->data_pages[i]);
2783 static inline int page_order(struct perf_buffer *buffer)
2791 * Back perf_mmap() with vmalloc memory.
2793 * Required for architectures that have d-cache aliasing issues.
2796 static inline int page_order(struct perf_buffer *buffer)
2798 return buffer->page_order;
2801 static struct page *
2802 perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
2804 if (pgoff > (1UL << page_order(buffer)))
2807 return vmalloc_to_page((void *)buffer->user_page + pgoff * PAGE_SIZE);
2810 static void perf_mmap_unmark_page(void *addr)
2812 struct page *page = vmalloc_to_page(addr);
2814 page->mapping = NULL;
2817 static void perf_buffer_free_work(struct work_struct *work)
2819 struct perf_buffer *buffer;
2823 buffer = container_of(work, struct perf_buffer, work);
2824 nr = 1 << page_order(buffer);
2826 base = buffer->user_page;
2827 for (i = 0; i < nr + 1; i++)
2828 perf_mmap_unmark_page(base + (i * PAGE_SIZE));
2834 static void perf_buffer_free(struct perf_buffer *buffer)
2836 schedule_work(&buffer->work);
2839 static struct perf_buffer *
2840 perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
2842 struct perf_buffer *buffer;
2846 size = sizeof(struct perf_buffer);
2847 size += sizeof(void *);
2849 buffer = kzalloc(size, GFP_KERNEL);
2853 INIT_WORK(&buffer->work, perf_buffer_free_work);
2855 all_buf = vmalloc_user((nr_pages + 1) * PAGE_SIZE);
2859 buffer->user_page = all_buf;
2860 buffer->data_pages[0] = all_buf + PAGE_SIZE;
2861 buffer->page_order = ilog2(nr_pages);
2862 buffer->nr_pages = 1;
2864 perf_buffer_init(buffer, watermark, flags);
2877 static unsigned long perf_data_size(struct perf_buffer *buffer)
2879 return buffer->nr_pages << (PAGE_SHIFT + page_order(buffer));
2882 static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
2884 struct perf_event *event = vma->vm_file->private_data;
2885 struct perf_buffer *buffer;
2886 int ret = VM_FAULT_SIGBUS;
2888 if (vmf->flags & FAULT_FLAG_MKWRITE) {
2889 if (vmf->pgoff == 0)
2895 buffer = rcu_dereference(event->buffer);
2899 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
2902 vmf->page = perf_mmap_to_page(buffer, vmf->pgoff);
2906 get_page(vmf->page);
2907 vmf->page->mapping = vma->vm_file->f_mapping;
2908 vmf->page->index = vmf->pgoff;
2917 static void perf_buffer_free_rcu(struct rcu_head *rcu_head)
2919 struct perf_buffer *buffer;
2921 buffer = container_of(rcu_head, struct perf_buffer, rcu_head);
2922 perf_buffer_free(buffer);
2925 static struct perf_buffer *perf_buffer_get(struct perf_event *event)
2927 struct perf_buffer *buffer;
2930 buffer = rcu_dereference(event->buffer);
2932 if (!atomic_inc_not_zero(&buffer->refcount))
2940 static void perf_buffer_put(struct perf_buffer *buffer)
2942 if (!atomic_dec_and_test(&buffer->refcount))
2945 call_rcu(&buffer->rcu_head, perf_buffer_free_rcu);
2948 static void perf_mmap_open(struct vm_area_struct *vma)
2950 struct perf_event *event = vma->vm_file->private_data;
2952 atomic_inc(&event->mmap_count);
2955 static void perf_mmap_close(struct vm_area_struct *vma)
2957 struct perf_event *event = vma->vm_file->private_data;
2959 if (atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) {
2960 unsigned long size = perf_data_size(event->buffer);
2961 struct user_struct *user = event->mmap_user;
2962 struct perf_buffer *buffer = event->buffer;
2964 atomic_long_sub((size >> PAGE_SHIFT) + 1, &user->locked_vm);
2965 vma->vm_mm->locked_vm -= event->mmap_locked;
2966 rcu_assign_pointer(event->buffer, NULL);
2967 mutex_unlock(&event->mmap_mutex);
2969 perf_buffer_put(buffer);
2974 static const struct vm_operations_struct perf_mmap_vmops = {
2975 .open = perf_mmap_open,
2976 .close = perf_mmap_close,
2977 .fault = perf_mmap_fault,
2978 .page_mkwrite = perf_mmap_fault,
2981 static int perf_mmap(struct file *file, struct vm_area_struct *vma)
2983 struct perf_event *event = file->private_data;
2984 unsigned long user_locked, user_lock_limit;
2985 struct user_struct *user = current_user();
2986 unsigned long locked, lock_limit;
2987 struct perf_buffer *buffer;
2988 unsigned long vma_size;
2989 unsigned long nr_pages;
2990 long user_extra, extra;
2991 int ret = 0, flags = 0;
2994 * Don't allow mmap() of inherited per-task counters. This would
2995 * create a performance issue due to all children writing to the
2998 if (event->cpu == -1 && event->attr.inherit)
3001 if (!(vma->vm_flags & VM_SHARED))
3004 vma_size = vma->vm_end - vma->vm_start;
3005 nr_pages = (vma_size / PAGE_SIZE) - 1;
3008 * If we have buffer pages ensure they're a power-of-two number, so we
3009 * can do bitmasks instead of modulo.
3011 if (nr_pages != 0 && !is_power_of_2(nr_pages))
3014 if (vma_size != PAGE_SIZE * (1 + nr_pages))
3017 if (vma->vm_pgoff != 0)
3020 WARN_ON_ONCE(event->ctx->parent_ctx);
3021 mutex_lock(&event->mmap_mutex);
3022 if (event->buffer) {
3023 if (event->buffer->nr_pages == nr_pages)
3024 atomic_inc(&event->buffer->refcount);
3030 user_extra = nr_pages + 1;
3031 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
3034 * Increase the limit linearly with more CPUs:
3036 user_lock_limit *= num_online_cpus();
3038 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
3041 if (user_locked > user_lock_limit)
3042 extra = user_locked - user_lock_limit;
3044 lock_limit = rlimit(RLIMIT_MEMLOCK);
3045 lock_limit >>= PAGE_SHIFT;
3046 locked = vma->vm_mm->locked_vm + extra;
3048 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
3049 !capable(CAP_IPC_LOCK)) {
3054 WARN_ON(event->buffer);
3056 if (vma->vm_flags & VM_WRITE)
3057 flags |= PERF_BUFFER_WRITABLE;
3059 buffer = perf_buffer_alloc(nr_pages, event->attr.wakeup_watermark,
3065 rcu_assign_pointer(event->buffer, buffer);
3067 atomic_long_add(user_extra, &user->locked_vm);
3068 event->mmap_locked = extra;
3069 event->mmap_user = get_current_user();
3070 vma->vm_mm->locked_vm += event->mmap_locked;
3074 atomic_inc(&event->mmap_count);
3075 mutex_unlock(&event->mmap_mutex);
3077 vma->vm_flags |= VM_RESERVED;
3078 vma->vm_ops = &perf_mmap_vmops;
3083 static int perf_fasync(int fd, struct file *filp, int on)
3085 struct inode *inode = filp->f_path.dentry->d_inode;
3086 struct perf_event *event = filp->private_data;
3089 mutex_lock(&inode->i_mutex);
3090 retval = fasync_helper(fd, filp, on, &event->fasync);
3091 mutex_unlock(&inode->i_mutex);
3099 static const struct file_operations perf_fops = {
3100 .llseek = no_llseek,
3101 .release = perf_release,
3104 .unlocked_ioctl = perf_ioctl,
3105 .compat_ioctl = perf_ioctl,
3107 .fasync = perf_fasync,
3113 * If there's data, ensure we set the poll() state and publish everything
3114 * to user-space before waking everybody up.
3117 void perf_event_wakeup(struct perf_event *event)
3119 wake_up_all(&event->waitq);
3121 if (event->pending_kill) {
3122 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
3123 event->pending_kill = 0;
3127 static void perf_pending_event(struct irq_work *entry)
3129 struct perf_event *event = container_of(entry,
3130 struct perf_event, pending);
3132 if (event->pending_disable) {
3133 event->pending_disable = 0;
3134 __perf_event_disable(event);
3137 if (event->pending_wakeup) {
3138 event->pending_wakeup = 0;
3139 perf_event_wakeup(event);
3144 * We assume there is only KVM supporting the callbacks.
3145 * Later on, we might change it to a list if there is
3146 * another virtualization implementation supporting the callbacks.
3148 struct perf_guest_info_callbacks *perf_guest_cbs;
3150 int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3152 perf_guest_cbs = cbs;
3155 EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
3157 int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3159 perf_guest_cbs = NULL;
3162 EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
3167 static bool perf_output_space(struct perf_buffer *buffer, unsigned long tail,
3168 unsigned long offset, unsigned long head)
3172 if (!buffer->writable)
3175 mask = perf_data_size(buffer) - 1;
3177 offset = (offset - tail) & mask;
3178 head = (head - tail) & mask;
3180 if ((int)(head - offset) < 0)
3186 static void perf_output_wakeup(struct perf_output_handle *handle)
3188 atomic_set(&handle->buffer->poll, POLL_IN);
3191 handle->event->pending_wakeup = 1;
3192 irq_work_queue(&handle->event->pending);
3194 perf_event_wakeup(handle->event);
3198 * We need to ensure a later event_id doesn't publish a head when a former
3199 * event isn't done writing. However since we need to deal with NMIs we
3200 * cannot fully serialize things.
3202 * We only publish the head (and generate a wakeup) when the outer-most
3205 static void perf_output_get_handle(struct perf_output_handle *handle)
3207 struct perf_buffer *buffer = handle->buffer;
3210 local_inc(&buffer->nest);
3211 handle->wakeup = local_read(&buffer->wakeup);
3214 static void perf_output_put_handle(struct perf_output_handle *handle)
3216 struct perf_buffer *buffer = handle->buffer;
3220 head = local_read(&buffer->head);
3223 * IRQ/NMI can happen here, which means we can miss a head update.
3226 if (!local_dec_and_test(&buffer->nest))
3230 * Publish the known good head. Rely on the full barrier implied
3231 * by atomic_dec_and_test() order the buffer->head read and this
3234 buffer->user_page->data_head = head;
3237 * Now check if we missed an update, rely on the (compiler)
3238 * barrier in atomic_dec_and_test() to re-read buffer->head.
3240 if (unlikely(head != local_read(&buffer->head))) {
3241 local_inc(&buffer->nest);
3245 if (handle->wakeup != local_read(&buffer->wakeup))
3246 perf_output_wakeup(handle);
3252 __always_inline void perf_output_copy(struct perf_output_handle *handle,
3253 const void *buf, unsigned int len)
3256 unsigned long size = min_t(unsigned long, handle->size, len);
3258 memcpy(handle->addr, buf, size);
3261 handle->addr += size;
3263 handle->size -= size;
3264 if (!handle->size) {
3265 struct perf_buffer *buffer = handle->buffer;
3268 handle->page &= buffer->nr_pages - 1;
3269 handle->addr = buffer->data_pages[handle->page];
3270 handle->size = PAGE_SIZE << page_order(buffer);
3275 int perf_output_begin(struct perf_output_handle *handle,
3276 struct perf_event *event, unsigned int size,
3277 int nmi, int sample)
3279 struct perf_buffer *buffer;
3280 unsigned long tail, offset, head;
3283 struct perf_event_header header;
3290 * For inherited events we send all the output towards the parent.
3293 event = event->parent;
3295 buffer = rcu_dereference(event->buffer);
3299 handle->buffer = buffer;
3300 handle->event = event;
3302 handle->sample = sample;
3304 if (!buffer->nr_pages)
3307 have_lost = local_read(&buffer->lost);
3309 size += sizeof(lost_event);
3311 perf_output_get_handle(handle);
3315 * Userspace could choose to issue a mb() before updating the
3316 * tail pointer. So that all reads will be completed before the
3319 tail = ACCESS_ONCE(buffer->user_page->data_tail);
3321 offset = head = local_read(&buffer->head);
3323 if (unlikely(!perf_output_space(buffer, tail, offset, head)))
3325 } while (local_cmpxchg(&buffer->head, offset, head) != offset);
3327 if (head - local_read(&buffer->wakeup) > buffer->watermark)
3328 local_add(buffer->watermark, &buffer->wakeup);
3330 handle->page = offset >> (PAGE_SHIFT + page_order(buffer));
3331 handle->page &= buffer->nr_pages - 1;
3332 handle->size = offset & ((PAGE_SIZE << page_order(buffer)) - 1);
3333 handle->addr = buffer->data_pages[handle->page];
3334 handle->addr += handle->size;
3335 handle->size = (PAGE_SIZE << page_order(buffer)) - handle->size;
3338 lost_event.header.type = PERF_RECORD_LOST;
3339 lost_event.header.misc = 0;
3340 lost_event.header.size = sizeof(lost_event);
3341 lost_event.id = event->id;
3342 lost_event.lost = local_xchg(&buffer->lost, 0);
3344 perf_output_put(handle, lost_event);
3350 local_inc(&buffer->lost);
3351 perf_output_put_handle(handle);
3358 void perf_output_end(struct perf_output_handle *handle)
3360 struct perf_event *event = handle->event;
3361 struct perf_buffer *buffer = handle->buffer;
3363 int wakeup_events = event->attr.wakeup_events;
3365 if (handle->sample && wakeup_events) {
3366 int events = local_inc_return(&buffer->events);
3367 if (events >= wakeup_events) {
3368 local_sub(wakeup_events, &buffer->events);
3369 local_inc(&buffer->wakeup);
3373 perf_output_put_handle(handle);
3377 static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
3380 * only top level events have the pid namespace they were created in
3383 event = event->parent;
3385 return task_tgid_nr_ns(p, event->ns);
3388 static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
3391 * only top level events have the pid namespace they were created in
3394 event = event->parent;
3396 return task_pid_nr_ns(p, event->ns);
3399 static void perf_output_read_one(struct perf_output_handle *handle,
3400 struct perf_event *event)
3402 u64 read_format = event->attr.read_format;
3406 values[n++] = perf_event_count(event);
3407 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
3408 values[n++] = event->total_time_enabled +
3409 atomic64_read(&event->child_total_time_enabled);
3411 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
3412 values[n++] = event->total_time_running +
3413 atomic64_read(&event->child_total_time_running);
3415 if (read_format & PERF_FORMAT_ID)
3416 values[n++] = primary_event_id(event);
3418 perf_output_copy(handle, values, n * sizeof(u64));
3422 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
3424 static void perf_output_read_group(struct perf_output_handle *handle,
3425 struct perf_event *event)
3427 struct perf_event *leader = event->group_leader, *sub;
3428 u64 read_format = event->attr.read_format;
3432 values[n++] = 1 + leader->nr_siblings;
3434 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3435 values[n++] = leader->total_time_enabled;
3437 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3438 values[n++] = leader->total_time_running;
3440 if (leader != event)
3441 leader->pmu->read(leader);
3443 values[n++] = perf_event_count(leader);
3444 if (read_format & PERF_FORMAT_ID)
3445 values[n++] = primary_event_id(leader);
3447 perf_output_copy(handle, values, n * sizeof(u64));
3449 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
3453 sub->pmu->read(sub);
3455 values[n++] = perf_event_count(sub);
3456 if (read_format & PERF_FORMAT_ID)
3457 values[n++] = primary_event_id(sub);
3459 perf_output_copy(handle, values, n * sizeof(u64));
3463 static void perf_output_read(struct perf_output_handle *handle,
3464 struct perf_event *event)
3466 if (event->attr.read_format & PERF_FORMAT_GROUP)
3467 perf_output_read_group(handle, event);
3469 perf_output_read_one(handle, event);
3472 void perf_output_sample(struct perf_output_handle *handle,
3473 struct perf_event_header *header,
3474 struct perf_sample_data *data,
3475 struct perf_event *event)
3477 u64 sample_type = data->type;
3479 perf_output_put(handle, *header);
3481 if (sample_type & PERF_SAMPLE_IP)
3482 perf_output_put(handle, data->ip);
3484 if (sample_type & PERF_SAMPLE_TID)
3485 perf_output_put(handle, data->tid_entry);
3487 if (sample_type & PERF_SAMPLE_TIME)
3488 perf_output_put(handle, data->time);
3490 if (sample_type & PERF_SAMPLE_ADDR)
3491 perf_output_put(handle, data->addr);
3493 if (sample_type & PERF_SAMPLE_ID)
3494 perf_output_put(handle, data->id);
3496 if (sample_type & PERF_SAMPLE_STREAM_ID)
3497 perf_output_put(handle, data->stream_id);
3499 if (sample_type & PERF_SAMPLE_CPU)
3500 perf_output_put(handle, data->cpu_entry);
3502 if (sample_type & PERF_SAMPLE_PERIOD)
3503 perf_output_put(handle, data->period);
3505 if (sample_type & PERF_SAMPLE_READ)
3506 perf_output_read(handle, event);
3508 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3509 if (data->callchain) {
3512 if (data->callchain)
3513 size += data->callchain->nr;
3515 size *= sizeof(u64);
3517 perf_output_copy(handle, data->callchain, size);
3520 perf_output_put(handle, nr);
3524 if (sample_type & PERF_SAMPLE_RAW) {
3526 perf_output_put(handle, data->raw->size);
3527 perf_output_copy(handle, data->raw->data,
3534 .size = sizeof(u32),
3537 perf_output_put(handle, raw);
3542 void perf_prepare_sample(struct perf_event_header *header,
3543 struct perf_sample_data *data,
3544 struct perf_event *event,
3545 struct pt_regs *regs)
3547 u64 sample_type = event->attr.sample_type;
3549 data->type = sample_type;
3551 header->type = PERF_RECORD_SAMPLE;
3552 header->size = sizeof(*header);
3555 header->misc |= perf_misc_flags(regs);
3557 if (sample_type & PERF_SAMPLE_IP) {
3558 data->ip = perf_instruction_pointer(regs);
3560 header->size += sizeof(data->ip);
3563 if (sample_type & PERF_SAMPLE_TID) {
3564 /* namespace issues */
3565 data->tid_entry.pid = perf_event_pid(event, current);
3566 data->tid_entry.tid = perf_event_tid(event, current);
3568 header->size += sizeof(data->tid_entry);
3571 if (sample_type & PERF_SAMPLE_TIME) {
3572 data->time = perf_clock();
3574 header->size += sizeof(data->time);
3577 if (sample_type & PERF_SAMPLE_ADDR)
3578 header->size += sizeof(data->addr);
3580 if (sample_type & PERF_SAMPLE_ID) {
3581 data->id = primary_event_id(event);
3583 header->size += sizeof(data->id);
3586 if (sample_type & PERF_SAMPLE_STREAM_ID) {
3587 data->stream_id = event->id;
3589 header->size += sizeof(data->stream_id);
3592 if (sample_type & PERF_SAMPLE_CPU) {
3593 data->cpu_entry.cpu = raw_smp_processor_id();
3594 data->cpu_entry.reserved = 0;
3596 header->size += sizeof(data->cpu_entry);
3599 if (sample_type & PERF_SAMPLE_PERIOD)
3600 header->size += sizeof(data->period);
3602 if (sample_type & PERF_SAMPLE_READ)
3603 header->size += perf_event_read_size(event);
3605 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3608 data->callchain = perf_callchain(regs);
3610 if (data->callchain)
3611 size += data->callchain->nr;
3613 header->size += size * sizeof(u64);
3616 if (sample_type & PERF_SAMPLE_RAW) {
3617 int size = sizeof(u32);
3620 size += data->raw->size;
3622 size += sizeof(u32);
3624 WARN_ON_ONCE(size & (sizeof(u64)-1));
3625 header->size += size;
3629 static void perf_event_output(struct perf_event *event, int nmi,
3630 struct perf_sample_data *data,
3631 struct pt_regs *regs)
3633 struct perf_output_handle handle;
3634 struct perf_event_header header;
3636 /* protect the callchain buffers */
3639 perf_prepare_sample(&header, data, event, regs);
3641 if (perf_output_begin(&handle, event, header.size, nmi, 1))
3644 perf_output_sample(&handle, &header, data, event);
3646 perf_output_end(&handle);
3656 struct perf_read_event {
3657 struct perf_event_header header;
3664 perf_event_read_event(struct perf_event *event,
3665 struct task_struct *task)
3667 struct perf_output_handle handle;
3668 struct perf_read_event read_event = {
3670 .type = PERF_RECORD_READ,
3672 .size = sizeof(read_event) + perf_event_read_size(event),
3674 .pid = perf_event_pid(event, task),
3675 .tid = perf_event_tid(event, task),
3679 ret = perf_output_begin(&handle, event, read_event.header.size, 0, 0);
3683 perf_output_put(&handle, read_event);
3684 perf_output_read(&handle, event);
3686 perf_output_end(&handle);
3690 * task tracking -- fork/exit
3692 * enabled by: attr.comm | attr.mmap | attr.mmap_data | attr.task
3695 struct perf_task_event {
3696 struct task_struct *task;
3697 struct perf_event_context *task_ctx;
3700 struct perf_event_header header;
3710 static void perf_event_task_output(struct perf_event *event,
3711 struct perf_task_event *task_event)
3713 struct perf_output_handle handle;
3714 struct task_struct *task = task_event->task;
3717 size = task_event->event_id.header.size;
3718 ret = perf_output_begin(&handle, event, size, 0, 0);
3723 task_event->event_id.pid = perf_event_pid(event, task);
3724 task_event->event_id.ppid = perf_event_pid(event, current);
3726 task_event->event_id.tid = perf_event_tid(event, task);
3727 task_event->event_id.ptid = perf_event_tid(event, current);
3729 perf_output_put(&handle, task_event->event_id);
3731 perf_output_end(&handle);
3734 static int perf_event_task_match(struct perf_event *event)
3736 if (event->state < PERF_EVENT_STATE_INACTIVE)
3739 if (event->cpu != -1 && event->cpu != smp_processor_id())
3742 if (event->attr.comm || event->attr.mmap ||
3743 event->attr.mmap_data || event->attr.task)
3749 static void perf_event_task_ctx(struct perf_event_context *ctx,
3750 struct perf_task_event *task_event)
3752 struct perf_event *event;
3754 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3755 if (perf_event_task_match(event))
3756 perf_event_task_output(event, task_event);
3760 static void perf_event_task_event(struct perf_task_event *task_event)
3762 struct perf_cpu_context *cpuctx;
3763 struct perf_event_context *ctx;
3768 list_for_each_entry_rcu(pmu, &pmus, entry) {
3769 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
3770 perf_event_task_ctx(&cpuctx->ctx, task_event);
3772 ctx = task_event->task_ctx;
3774 ctxn = pmu->task_ctx_nr;
3777 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
3780 perf_event_task_ctx(ctx, task_event);
3782 put_cpu_ptr(pmu->pmu_cpu_context);
3787 static void perf_event_task(struct task_struct *task,
3788 struct perf_event_context *task_ctx,
3791 struct perf_task_event task_event;
3793 if (!atomic_read(&nr_comm_events) &&
3794 !atomic_read(&nr_mmap_events) &&
3795 !atomic_read(&nr_task_events))
3798 task_event = (struct perf_task_event){
3800 .task_ctx = task_ctx,
3803 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
3805 .size = sizeof(task_event.event_id),
3811 .time = perf_clock(),
3815 perf_event_task_event(&task_event);
3818 void perf_event_fork(struct task_struct *task)
3820 perf_event_task(task, NULL, 1);
3827 struct perf_comm_event {
3828 struct task_struct *task;
3833 struct perf_event_header header;
3840 static void perf_event_comm_output(struct perf_event *event,
3841 struct perf_comm_event *comm_event)
3843 struct perf_output_handle handle;
3844 int size = comm_event->event_id.header.size;
3845 int ret = perf_output_begin(&handle, event, size, 0, 0);
3850 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
3851 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
3853 perf_output_put(&handle, comm_event->event_id);
3854 perf_output_copy(&handle, comm_event->comm,
3855 comm_event->comm_size);
3856 perf_output_end(&handle);
3859 static int perf_event_comm_match(struct perf_event *event)
3861 if (event->state < PERF_EVENT_STATE_INACTIVE)
3864 if (event->cpu != -1 && event->cpu != smp_processor_id())
3867 if (event->attr.comm)
3873 static void perf_event_comm_ctx(struct perf_event_context *ctx,
3874 struct perf_comm_event *comm_event)
3876 struct perf_event *event;
3878 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3879 if (perf_event_comm_match(event))
3880 perf_event_comm_output(event, comm_event);
3884 static void perf_event_comm_event(struct perf_comm_event *comm_event)
3886 struct perf_cpu_context *cpuctx;
3887 struct perf_event_context *ctx;
3888 char comm[TASK_COMM_LEN];
3893 memset(comm, 0, sizeof(comm));
3894 strlcpy(comm, comm_event->task->comm, sizeof(comm));
3895 size = ALIGN(strlen(comm)+1, sizeof(u64));
3897 comm_event->comm = comm;
3898 comm_event->comm_size = size;
3900 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
3903 list_for_each_entry_rcu(pmu, &pmus, entry) {
3904 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
3905 perf_event_comm_ctx(&cpuctx->ctx, comm_event);
3907 ctxn = pmu->task_ctx_nr;
3911 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
3913 perf_event_comm_ctx(ctx, comm_event);
3915 put_cpu_ptr(pmu->pmu_cpu_context);
3920 void perf_event_comm(struct task_struct *task)
3922 struct perf_comm_event comm_event;
3923 struct perf_event_context *ctx;
3926 for_each_task_context_nr(ctxn) {
3927 ctx = task->perf_event_ctxp[ctxn];
3931 perf_event_enable_on_exec(ctx);
3934 if (!atomic_read(&nr_comm_events))
3937 comm_event = (struct perf_comm_event){
3943 .type = PERF_RECORD_COMM,
3952 perf_event_comm_event(&comm_event);
3959 struct perf_mmap_event {
3960 struct vm_area_struct *vma;
3962 const char *file_name;
3966 struct perf_event_header header;
3976 static void perf_event_mmap_output(struct perf_event *event,
3977 struct perf_mmap_event *mmap_event)
3979 struct perf_output_handle handle;
3980 int size = mmap_event->event_id.header.size;
3981 int ret = perf_output_begin(&handle, event, size, 0, 0);
3986 mmap_event->event_id.pid = perf_event_pid(event, current);
3987 mmap_event->event_id.tid = perf_event_tid(event, current);
3989 perf_output_put(&handle, mmap_event->event_id);
3990 perf_output_copy(&handle, mmap_event->file_name,
3991 mmap_event->file_size);
3992 perf_output_end(&handle);
3995 static int perf_event_mmap_match(struct perf_event *event,
3996 struct perf_mmap_event *mmap_event,
3999 if (event->state < PERF_EVENT_STATE_INACTIVE)
4002 if (event->cpu != -1 && event->cpu != smp_processor_id())
4005 if ((!executable && event->attr.mmap_data) ||
4006 (executable && event->attr.mmap))
4012 static void perf_event_mmap_ctx(struct perf_event_context *ctx,
4013 struct perf_mmap_event *mmap_event,
4016 struct perf_event *event;
4018 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4019 if (perf_event_mmap_match(event, mmap_event, executable))
4020 perf_event_mmap_output(event, mmap_event);
4024 static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
4026 struct perf_cpu_context *cpuctx;
4027 struct perf_event_context *ctx;
4028 struct vm_area_struct *vma = mmap_event->vma;
4029 struct file *file = vma->vm_file;
4037 memset(tmp, 0, sizeof(tmp));
4041 * d_path works from the end of the buffer backwards, so we
4042 * need to add enough zero bytes after the string to handle
4043 * the 64bit alignment we do later.
4045 buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
4047 name = strncpy(tmp, "//enomem", sizeof(tmp));
4050 name = d_path(&file->f_path, buf, PATH_MAX);
4052 name = strncpy(tmp, "//toolong", sizeof(tmp));
4056 if (arch_vma_name(mmap_event->vma)) {
4057 name = strncpy(tmp, arch_vma_name(mmap_event->vma),
4063 name = strncpy(tmp, "[vdso]", sizeof(tmp));
4065 } else if (vma->vm_start <= vma->vm_mm->start_brk &&
4066 vma->vm_end >= vma->vm_mm->brk) {
4067 name = strncpy(tmp, "[heap]", sizeof(tmp));
4069 } else if (vma->vm_start <= vma->vm_mm->start_stack &&
4070 vma->vm_end >= vma->vm_mm->start_stack) {
4071 name = strncpy(tmp, "[stack]", sizeof(tmp));
4075 name = strncpy(tmp, "//anon", sizeof(tmp));
4080 size = ALIGN(strlen(name)+1, sizeof(u64));
4082 mmap_event->file_name = name;
4083 mmap_event->file_size = size;
4085 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
4088 list_for_each_entry_rcu(pmu, &pmus, entry) {
4089 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
4090 perf_event_mmap_ctx(&cpuctx->ctx, mmap_event,
4091 vma->vm_flags & VM_EXEC);
4093 ctxn = pmu->task_ctx_nr;
4097 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4099 perf_event_mmap_ctx(ctx, mmap_event,
4100 vma->vm_flags & VM_EXEC);
4103 put_cpu_ptr(pmu->pmu_cpu_context);
4110 void perf_event_mmap(struct vm_area_struct *vma)
4112 struct perf_mmap_event mmap_event;
4114 if (!atomic_read(&nr_mmap_events))
4117 mmap_event = (struct perf_mmap_event){
4123 .type = PERF_RECORD_MMAP,
4124 .misc = PERF_RECORD_MISC_USER,
4129 .start = vma->vm_start,
4130 .len = vma->vm_end - vma->vm_start,
4131 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
4135 perf_event_mmap_event(&mmap_event);
4139 * IRQ throttle logging
4142 static void perf_log_throttle(struct perf_event *event, int enable)
4144 struct perf_output_handle handle;
4148 struct perf_event_header header;
4152 } throttle_event = {
4154 .type = PERF_RECORD_THROTTLE,
4156 .size = sizeof(throttle_event),
4158 .time = perf_clock(),
4159 .id = primary_event_id(event),
4160 .stream_id = event->id,
4164 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
4166 ret = perf_output_begin(&handle, event, sizeof(throttle_event), 1, 0);
4170 perf_output_put(&handle, throttle_event);
4171 perf_output_end(&handle);
4175 * Generic event overflow handling, sampling.
4178 static int __perf_event_overflow(struct perf_event *event, int nmi,
4179 int throttle, struct perf_sample_data *data,
4180 struct pt_regs *regs)
4182 int events = atomic_read(&event->event_limit);
4183 struct hw_perf_event *hwc = &event->hw;
4189 if (hwc->interrupts != MAX_INTERRUPTS) {
4191 if (HZ * hwc->interrupts >
4192 (u64)sysctl_perf_event_sample_rate) {
4193 hwc->interrupts = MAX_INTERRUPTS;
4194 perf_log_throttle(event, 0);
4199 * Keep re-disabling events even though on the previous
4200 * pass we disabled it - just in case we raced with a
4201 * sched-in and the event got enabled again:
4207 if (event->attr.freq) {
4208 u64 now = perf_clock();
4209 s64 delta = now - hwc->freq_time_stamp;
4211 hwc->freq_time_stamp = now;
4213 if (delta > 0 && delta < 2*TICK_NSEC)
4214 perf_adjust_period(event, delta, hwc->last_period);
4218 * XXX event_limit might not quite work as expected on inherited
4222 event->pending_kill = POLL_IN;
4223 if (events && atomic_dec_and_test(&event->event_limit)) {
4225 event->pending_kill = POLL_HUP;
4227 event->pending_disable = 1;
4228 irq_work_queue(&event->pending);
4230 perf_event_disable(event);
4233 if (event->overflow_handler)
4234 event->overflow_handler(event, nmi, data, regs);
4236 perf_event_output(event, nmi, data, regs);
4241 int perf_event_overflow(struct perf_event *event, int nmi,
4242 struct perf_sample_data *data,
4243 struct pt_regs *regs)
4245 return __perf_event_overflow(event, nmi, 1, data, regs);
4249 * Generic software event infrastructure
4252 struct swevent_htable {
4253 struct swevent_hlist *swevent_hlist;
4254 struct mutex hlist_mutex;
4257 /* Recursion avoidance in each contexts */
4258 int recursion[PERF_NR_CONTEXTS];
4261 static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
4264 * We directly increment event->count and keep a second value in
4265 * event->hw.period_left to count intervals. This period event
4266 * is kept in the range [-sample_period, 0] so that we can use the
4270 static u64 perf_swevent_set_period(struct perf_event *event)
4272 struct hw_perf_event *hwc = &event->hw;
4273 u64 period = hwc->last_period;
4277 hwc->last_period = hwc->sample_period;
4280 old = val = local64_read(&hwc->period_left);
4284 nr = div64_u64(period + val, period);
4285 offset = nr * period;
4287 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
4293 static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
4294 int nmi, struct perf_sample_data *data,
4295 struct pt_regs *regs)
4297 struct hw_perf_event *hwc = &event->hw;
4300 data->period = event->hw.last_period;
4302 overflow = perf_swevent_set_period(event);
4304 if (hwc->interrupts == MAX_INTERRUPTS)
4307 for (; overflow; overflow--) {
4308 if (__perf_event_overflow(event, nmi, throttle,
4311 * We inhibit the overflow from happening when
4312 * hwc->interrupts == MAX_INTERRUPTS.
4320 static void perf_swevent_event(struct perf_event *event, u64 nr,
4321 int nmi, struct perf_sample_data *data,
4322 struct pt_regs *regs)
4324 struct hw_perf_event *hwc = &event->hw;
4326 local64_add(nr, &event->count);
4331 if (!hwc->sample_period)
4334 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
4335 return perf_swevent_overflow(event, 1, nmi, data, regs);
4337 if (local64_add_negative(nr, &hwc->period_left))
4340 perf_swevent_overflow(event, 0, nmi, data, regs);
4343 static int perf_exclude_event(struct perf_event *event,
4344 struct pt_regs *regs)
4346 if (event->hw.state & PERF_HES_STOPPED)
4350 if (event->attr.exclude_user && user_mode(regs))
4353 if (event->attr.exclude_kernel && !user_mode(regs))
4360 static int perf_swevent_match(struct perf_event *event,
4361 enum perf_type_id type,
4363 struct perf_sample_data *data,
4364 struct pt_regs *regs)
4366 if (event->attr.type != type)
4369 if (event->attr.config != event_id)
4372 if (perf_exclude_event(event, regs))
4378 static inline u64 swevent_hash(u64 type, u32 event_id)
4380 u64 val = event_id | (type << 32);
4382 return hash_64(val, SWEVENT_HLIST_BITS);
4385 static inline struct hlist_head *
4386 __find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
4388 u64 hash = swevent_hash(type, event_id);
4390 return &hlist->heads[hash];
4393 /* For the read side: events when they trigger */
4394 static inline struct hlist_head *
4395 find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
4397 struct swevent_hlist *hlist;
4399 hlist = rcu_dereference(swhash->swevent_hlist);
4403 return __find_swevent_head(hlist, type, event_id);
4406 /* For the event head insertion and removal in the hlist */
4407 static inline struct hlist_head *
4408 find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
4410 struct swevent_hlist *hlist;
4411 u32 event_id = event->attr.config;
4412 u64 type = event->attr.type;
4415 * Event scheduling is always serialized against hlist allocation
4416 * and release. Which makes the protected version suitable here.
4417 * The context lock guarantees that.
4419 hlist = rcu_dereference_protected(swhash->swevent_hlist,
4420 lockdep_is_held(&event->ctx->lock));
4424 return __find_swevent_head(hlist, type, event_id);
4427 static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
4429 struct perf_sample_data *data,
4430 struct pt_regs *regs)
4432 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
4433 struct perf_event *event;
4434 struct hlist_node *node;
4435 struct hlist_head *head;
4438 head = find_swevent_head_rcu(swhash, type, event_id);
4442 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
4443 if (perf_swevent_match(event, type, event_id, data, regs))
4444 perf_swevent_event(event, nr, nmi, data, regs);
4450 int perf_swevent_get_recursion_context(void)
4452 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
4454 return get_recursion_context(swhash->recursion);
4456 EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
4458 void inline perf_swevent_put_recursion_context(int rctx)
4460 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
4462 put_recursion_context(swhash->recursion, rctx);
4465 void __perf_sw_event(u32 event_id, u64 nr, int nmi,
4466 struct pt_regs *regs, u64 addr)
4468 struct perf_sample_data data;
4471 preempt_disable_notrace();
4472 rctx = perf_swevent_get_recursion_context();
4476 perf_sample_data_init(&data, addr);
4478 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, nmi, &data, regs);
4480 perf_swevent_put_recursion_context(rctx);
4481 preempt_enable_notrace();
4484 static void perf_swevent_read(struct perf_event *event)
4488 static int perf_swevent_add(struct perf_event *event, int flags)
4490 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
4491 struct hw_perf_event *hwc = &event->hw;
4492 struct hlist_head *head;
4494 if (hwc->sample_period) {
4495 hwc->last_period = hwc->sample_period;
4496 perf_swevent_set_period(event);
4499 hwc->state = !(flags & PERF_EF_START);
4501 head = find_swevent_head(swhash, event);
4502 if (WARN_ON_ONCE(!head))
4505 hlist_add_head_rcu(&event->hlist_entry, head);
4510 static void perf_swevent_del(struct perf_event *event, int flags)
4512 hlist_del_rcu(&event->hlist_entry);
4515 static void perf_swevent_start(struct perf_event *event, int flags)
4517 event->hw.state = 0;
4520 static void perf_swevent_stop(struct perf_event *event, int flags)
4522 event->hw.state = PERF_HES_STOPPED;
4525 /* Deref the hlist from the update side */
4526 static inline struct swevent_hlist *
4527 swevent_hlist_deref(struct swevent_htable *swhash)
4529 return rcu_dereference_protected(swhash->swevent_hlist,
4530 lockdep_is_held(&swhash->hlist_mutex));
4533 static void swevent_hlist_release_rcu(struct rcu_head *rcu_head)
4535 struct swevent_hlist *hlist;
4537 hlist = container_of(rcu_head, struct swevent_hlist, rcu_head);
4541 static void swevent_hlist_release(struct swevent_htable *swhash)
4543 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
4548 rcu_assign_pointer(swhash->swevent_hlist, NULL);
4549 call_rcu(&hlist->rcu_head, swevent_hlist_release_rcu);
4552 static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
4554 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
4556 mutex_lock(&swhash->hlist_mutex);
4558 if (!--swhash->hlist_refcount)
4559 swevent_hlist_release(swhash);
4561 mutex_unlock(&swhash->hlist_mutex);
4564 static void swevent_hlist_put(struct perf_event *event)
4568 if (event->cpu != -1) {
4569 swevent_hlist_put_cpu(event, event->cpu);
4573 for_each_possible_cpu(cpu)
4574 swevent_hlist_put_cpu(event, cpu);
4577 static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
4579 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
4582 mutex_lock(&swhash->hlist_mutex);
4584 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
4585 struct swevent_hlist *hlist;
4587 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
4592 rcu_assign_pointer(swhash->swevent_hlist, hlist);
4594 swhash->hlist_refcount++;
4596 mutex_unlock(&swhash->hlist_mutex);
4601 static int swevent_hlist_get(struct perf_event *event)
4604 int cpu, failed_cpu;
4606 if (event->cpu != -1)
4607 return swevent_hlist_get_cpu(event, event->cpu);
4610 for_each_possible_cpu(cpu) {
4611 err = swevent_hlist_get_cpu(event, cpu);
4621 for_each_possible_cpu(cpu) {
4622 if (cpu == failed_cpu)
4624 swevent_hlist_put_cpu(event, cpu);
4631 atomic_t perf_swevent_enabled[PERF_COUNT_SW_MAX];
4633 static void sw_perf_event_destroy(struct perf_event *event)
4635 u64 event_id = event->attr.config;
4637 WARN_ON(event->parent);
4639 jump_label_dec(&perf_swevent_enabled[event_id]);
4640 swevent_hlist_put(event);
4643 static int perf_swevent_init(struct perf_event *event)
4645 int event_id = event->attr.config;
4647 if (event->attr.type != PERF_TYPE_SOFTWARE)
4651 case PERF_COUNT_SW_CPU_CLOCK:
4652 case PERF_COUNT_SW_TASK_CLOCK:
4659 if (event_id > PERF_COUNT_SW_MAX)
4662 if (!event->parent) {
4665 err = swevent_hlist_get(event);
4669 jump_label_inc(&perf_swevent_enabled[event_id]);
4670 event->destroy = sw_perf_event_destroy;
4676 static struct pmu perf_swevent = {
4677 .task_ctx_nr = perf_sw_context,
4679 .event_init = perf_swevent_init,
4680 .add = perf_swevent_add,
4681 .del = perf_swevent_del,
4682 .start = perf_swevent_start,
4683 .stop = perf_swevent_stop,
4684 .read = perf_swevent_read,
4687 #ifdef CONFIG_EVENT_TRACING
4689 static int perf_tp_filter_match(struct perf_event *event,
4690 struct perf_sample_data *data)
4692 void *record = data->raw->data;
4694 if (likely(!event->filter) || filter_match_preds(event->filter, record))
4699 static int perf_tp_event_match(struct perf_event *event,
4700 struct perf_sample_data *data,
4701 struct pt_regs *regs)
4704 * All tracepoints are from kernel-space.
4706 if (event->attr.exclude_kernel)
4709 if (!perf_tp_filter_match(event, data))
4715 void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
4716 struct pt_regs *regs, struct hlist_head *head, int rctx)
4718 struct perf_sample_data data;
4719 struct perf_event *event;
4720 struct hlist_node *node;
4722 struct perf_raw_record raw = {
4727 perf_sample_data_init(&data, addr);
4730 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
4731 if (perf_tp_event_match(event, &data, regs))
4732 perf_swevent_event(event, count, 1, &data, regs);
4735 perf_swevent_put_recursion_context(rctx);
4737 EXPORT_SYMBOL_GPL(perf_tp_event);
4739 static void tp_perf_event_destroy(struct perf_event *event)
4741 perf_trace_destroy(event);
4744 static int perf_tp_event_init(struct perf_event *event)
4748 if (event->attr.type != PERF_TYPE_TRACEPOINT)
4752 * Raw tracepoint data is a severe data leak, only allow root to
4755 if ((event->attr.sample_type & PERF_SAMPLE_RAW) &&
4756 perf_paranoid_tracepoint_raw() &&
4757 !capable(CAP_SYS_ADMIN))
4760 err = perf_trace_init(event);
4764 event->destroy = tp_perf_event_destroy;
4769 static struct pmu perf_tracepoint = {
4770 .task_ctx_nr = perf_sw_context,
4772 .event_init = perf_tp_event_init,
4773 .add = perf_trace_add,
4774 .del = perf_trace_del,
4775 .start = perf_swevent_start,
4776 .stop = perf_swevent_stop,
4777 .read = perf_swevent_read,
4780 static inline void perf_tp_register(void)
4782 perf_pmu_register(&perf_tracepoint);
4785 static int perf_event_set_filter(struct perf_event *event, void __user *arg)
4790 if (event->attr.type != PERF_TYPE_TRACEPOINT)
4793 filter_str = strndup_user(arg, PAGE_SIZE);
4794 if (IS_ERR(filter_str))
4795 return PTR_ERR(filter_str);
4797 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
4803 static void perf_event_free_filter(struct perf_event *event)
4805 ftrace_profile_free_filter(event);
4810 static inline void perf_tp_register(void)
4814 static int perf_event_set_filter(struct perf_event *event, void __user *arg)
4819 static void perf_event_free_filter(struct perf_event *event)
4823 #endif /* CONFIG_EVENT_TRACING */
4825 #ifdef CONFIG_HAVE_HW_BREAKPOINT
4826 void perf_bp_event(struct perf_event *bp, void *data)
4828 struct perf_sample_data sample;
4829 struct pt_regs *regs = data;
4831 perf_sample_data_init(&sample, bp->attr.bp_addr);
4833 if (!bp->hw.state && !perf_exclude_event(bp, regs))
4834 perf_swevent_event(bp, 1, 1, &sample, regs);
4839 * hrtimer based swevent callback
4842 static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
4844 enum hrtimer_restart ret = HRTIMER_RESTART;
4845 struct perf_sample_data data;
4846 struct pt_regs *regs;
4847 struct perf_event *event;
4850 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
4851 event->pmu->read(event);
4853 perf_sample_data_init(&data, 0);
4854 data.period = event->hw.last_period;
4855 regs = get_irq_regs();
4857 if (regs && !perf_exclude_event(event, regs)) {
4858 if (!(event->attr.exclude_idle && current->pid == 0))
4859 if (perf_event_overflow(event, 0, &data, regs))
4860 ret = HRTIMER_NORESTART;
4863 period = max_t(u64, 10000, event->hw.sample_period);
4864 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
4869 static void perf_swevent_start_hrtimer(struct perf_event *event)
4871 struct hw_perf_event *hwc = &event->hw;
4873 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
4874 hwc->hrtimer.function = perf_swevent_hrtimer;
4875 if (hwc->sample_period) {
4876 s64 period = local64_read(&hwc->period_left);
4882 local64_set(&hwc->period_left, 0);
4884 period = max_t(u64, 10000, hwc->sample_period);
4886 __hrtimer_start_range_ns(&hwc->hrtimer,
4887 ns_to_ktime(period), 0,
4888 HRTIMER_MODE_REL_PINNED, 0);
4892 static void perf_swevent_cancel_hrtimer(struct perf_event *event)
4894 struct hw_perf_event *hwc = &event->hw;
4896 if (hwc->sample_period) {
4897 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
4898 local64_set(&hwc->period_left, ktime_to_ns(remaining));
4900 hrtimer_cancel(&hwc->hrtimer);
4905 * Software event: cpu wall time clock
4908 static void cpu_clock_event_update(struct perf_event *event)
4913 now = local_clock();
4914 prev = local64_xchg(&event->hw.prev_count, now);
4915 local64_add(now - prev, &event->count);
4918 static void cpu_clock_event_start(struct perf_event *event, int flags)
4920 local64_set(&event->hw.prev_count, local_clock());
4921 perf_swevent_start_hrtimer(event);
4924 static void cpu_clock_event_stop(struct perf_event *event, int flags)
4926 perf_swevent_cancel_hrtimer(event);
4927 cpu_clock_event_update(event);
4930 static int cpu_clock_event_add(struct perf_event *event, int flags)
4932 if (flags & PERF_EF_START)
4933 cpu_clock_event_start(event, flags);
4938 static void cpu_clock_event_del(struct perf_event *event, int flags)
4940 cpu_clock_event_stop(event, flags);
4943 static void cpu_clock_event_read(struct perf_event *event)
4945 cpu_clock_event_update(event);
4948 static int cpu_clock_event_init(struct perf_event *event)
4950 if (event->attr.type != PERF_TYPE_SOFTWARE)
4953 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
4959 static struct pmu perf_cpu_clock = {
4960 .task_ctx_nr = perf_sw_context,
4962 .event_init = cpu_clock_event_init,
4963 .add = cpu_clock_event_add,
4964 .del = cpu_clock_event_del,
4965 .start = cpu_clock_event_start,
4966 .stop = cpu_clock_event_stop,
4967 .read = cpu_clock_event_read,
4971 * Software event: task time clock
4974 static void task_clock_event_update(struct perf_event *event, u64 now)
4979 prev = local64_xchg(&event->hw.prev_count, now);
4981 local64_add(delta, &event->count);
4984 static void task_clock_event_start(struct perf_event *event, int flags)
4986 local64_set(&event->hw.prev_count, event->ctx->time);
4987 perf_swevent_start_hrtimer(event);
4990 static void task_clock_event_stop(struct perf_event *event, int flags)
4992 perf_swevent_cancel_hrtimer(event);
4993 task_clock_event_update(event, event->ctx->time);
4996 static int task_clock_event_add(struct perf_event *event, int flags)
4998 if (flags & PERF_EF_START)
4999 task_clock_event_start(event, flags);
5004 static void task_clock_event_del(struct perf_event *event, int flags)
5006 task_clock_event_stop(event, PERF_EF_UPDATE);
5009 static void task_clock_event_read(struct perf_event *event)
5014 update_context_time(event->ctx);
5015 time = event->ctx->time;
5017 u64 now = perf_clock();
5018 u64 delta = now - event->ctx->timestamp;
5019 time = event->ctx->time + delta;
5022 task_clock_event_update(event, time);
5025 static int task_clock_event_init(struct perf_event *event)
5027 if (event->attr.type != PERF_TYPE_SOFTWARE)
5030 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
5036 static struct pmu perf_task_clock = {
5037 .task_ctx_nr = perf_sw_context,
5039 .event_init = task_clock_event_init,
5040 .add = task_clock_event_add,
5041 .del = task_clock_event_del,
5042 .start = task_clock_event_start,
5043 .stop = task_clock_event_stop,
5044 .read = task_clock_event_read,
5047 static void perf_pmu_nop_void(struct pmu *pmu)
5051 static int perf_pmu_nop_int(struct pmu *pmu)
5056 static void perf_pmu_start_txn(struct pmu *pmu)
5058 perf_pmu_disable(pmu);
5061 static int perf_pmu_commit_txn(struct pmu *pmu)
5063 perf_pmu_enable(pmu);
5067 static void perf_pmu_cancel_txn(struct pmu *pmu)
5069 perf_pmu_enable(pmu);
5073 * Ensures all contexts with the same task_ctx_nr have the same
5074 * pmu_cpu_context too.
5076 static void *find_pmu_context(int ctxn)
5083 list_for_each_entry(pmu, &pmus, entry) {
5084 if (pmu->task_ctx_nr == ctxn)
5085 return pmu->pmu_cpu_context;
5091 static void free_pmu_context(void * __percpu cpu_context)
5095 mutex_lock(&pmus_lock);
5097 * Like a real lame refcount.
5099 list_for_each_entry(pmu, &pmus, entry) {
5100 if (pmu->pmu_cpu_context == cpu_context)
5104 free_percpu(cpu_context);
5106 mutex_unlock(&pmus_lock);
5109 int perf_pmu_register(struct pmu *pmu)
5113 mutex_lock(&pmus_lock);
5115 pmu->pmu_disable_count = alloc_percpu(int);
5116 if (!pmu->pmu_disable_count)
5119 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
5120 if (pmu->pmu_cpu_context)
5121 goto got_cpu_context;
5123 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
5124 if (!pmu->pmu_cpu_context)
5127 for_each_possible_cpu(cpu) {
5128 struct perf_cpu_context *cpuctx;
5130 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
5131 __perf_event_init_context(&cpuctx->ctx);
5132 cpuctx->ctx.type = cpu_context;
5133 cpuctx->ctx.pmu = pmu;
5134 cpuctx->jiffies_interval = 1;
5135 INIT_LIST_HEAD(&cpuctx->rotation_list);
5139 if (!pmu->start_txn) {
5140 if (pmu->pmu_enable) {
5142 * If we have pmu_enable/pmu_disable calls, install
5143 * transaction stubs that use that to try and batch
5144 * hardware accesses.
5146 pmu->start_txn = perf_pmu_start_txn;
5147 pmu->commit_txn = perf_pmu_commit_txn;
5148 pmu->cancel_txn = perf_pmu_cancel_txn;
5150 pmu->start_txn = perf_pmu_nop_void;
5151 pmu->commit_txn = perf_pmu_nop_int;
5152 pmu->cancel_txn = perf_pmu_nop_void;
5156 if (!pmu->pmu_enable) {
5157 pmu->pmu_enable = perf_pmu_nop_void;
5158 pmu->pmu_disable = perf_pmu_nop_void;
5161 list_add_rcu(&pmu->entry, &pmus);
5164 mutex_unlock(&pmus_lock);
5169 free_percpu(pmu->pmu_disable_count);
5173 void perf_pmu_unregister(struct pmu *pmu)
5175 mutex_lock(&pmus_lock);
5176 list_del_rcu(&pmu->entry);
5177 mutex_unlock(&pmus_lock);
5180 * We dereference the pmu list under both SRCU and regular RCU, so
5181 * synchronize against both of those.
5183 synchronize_srcu(&pmus_srcu);
5186 free_percpu(pmu->pmu_disable_count);
5187 free_pmu_context(pmu->pmu_cpu_context);
5190 struct pmu *perf_init_event(struct perf_event *event)
5192 struct pmu *pmu = NULL;
5195 idx = srcu_read_lock(&pmus_srcu);
5196 list_for_each_entry_rcu(pmu, &pmus, entry) {
5197 int ret = pmu->event_init(event);
5201 if (ret != -ENOENT) {
5206 pmu = ERR_PTR(-ENOENT);
5208 srcu_read_unlock(&pmus_srcu, idx);
5214 * Allocate and initialize a event structure
5216 static struct perf_event *
5217 perf_event_alloc(struct perf_event_attr *attr, int cpu,
5218 struct task_struct *task,
5219 struct perf_event *group_leader,
5220 struct perf_event *parent_event,
5221 perf_overflow_handler_t overflow_handler)
5224 struct perf_event *event;
5225 struct hw_perf_event *hwc;
5228 event = kzalloc(sizeof(*event), GFP_KERNEL);
5230 return ERR_PTR(-ENOMEM);
5233 * Single events are their own group leaders, with an
5234 * empty sibling list:
5237 group_leader = event;
5239 mutex_init(&event->child_mutex);
5240 INIT_LIST_HEAD(&event->child_list);
5242 INIT_LIST_HEAD(&event->group_entry);
5243 INIT_LIST_HEAD(&event->event_entry);
5244 INIT_LIST_HEAD(&event->sibling_list);
5245 init_waitqueue_head(&event->waitq);
5246 init_irq_work(&event->pending, perf_pending_event);
5248 mutex_init(&event->mmap_mutex);
5251 event->attr = *attr;
5252 event->group_leader = group_leader;
5256 event->parent = parent_event;
5258 event->ns = get_pid_ns(current->nsproxy->pid_ns);
5259 event->id = atomic64_inc_return(&perf_event_id);
5261 event->state = PERF_EVENT_STATE_INACTIVE;
5264 event->attach_state = PERF_ATTACH_TASK;
5265 #ifdef CONFIG_HAVE_HW_BREAKPOINT
5267 * hw_breakpoint is a bit difficult here..
5269 if (attr->type == PERF_TYPE_BREAKPOINT)
5270 event->hw.bp_target = task;
5274 if (!overflow_handler && parent_event)
5275 overflow_handler = parent_event->overflow_handler;
5277 event->overflow_handler = overflow_handler;
5280 event->state = PERF_EVENT_STATE_OFF;
5285 hwc->sample_period = attr->sample_period;
5286 if (attr->freq && attr->sample_freq)
5287 hwc->sample_period = 1;
5288 hwc->last_period = hwc->sample_period;
5290 local64_set(&hwc->period_left, hwc->sample_period);
5293 * we currently do not support PERF_FORMAT_GROUP on inherited events
5295 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
5298 pmu = perf_init_event(event);
5304 else if (IS_ERR(pmu))
5309 put_pid_ns(event->ns);
5311 return ERR_PTR(err);
5316 if (!event->parent) {
5317 if (event->attach_state & PERF_ATTACH_TASK)
5318 jump_label_inc(&perf_task_events);
5319 if (event->attr.mmap || event->attr.mmap_data)
5320 atomic_inc(&nr_mmap_events);
5321 if (event->attr.comm)
5322 atomic_inc(&nr_comm_events);
5323 if (event->attr.task)
5324 atomic_inc(&nr_task_events);
5325 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
5326 err = get_callchain_buffers();
5329 return ERR_PTR(err);
5337 static int perf_copy_attr(struct perf_event_attr __user *uattr,
5338 struct perf_event_attr *attr)
5343 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
5347 * zero the full structure, so that a short copy will be nice.
5349 memset(attr, 0, sizeof(*attr));
5351 ret = get_user(size, &uattr->size);
5355 if (size > PAGE_SIZE) /* silly large */
5358 if (!size) /* abi compat */
5359 size = PERF_ATTR_SIZE_VER0;
5361 if (size < PERF_ATTR_SIZE_VER0)
5365 * If we're handed a bigger struct than we know of,
5366 * ensure all the unknown bits are 0 - i.e. new
5367 * user-space does not rely on any kernel feature
5368 * extensions we dont know about yet.
5370 if (size > sizeof(*attr)) {
5371 unsigned char __user *addr;
5372 unsigned char __user *end;
5375 addr = (void __user *)uattr + sizeof(*attr);
5376 end = (void __user *)uattr + size;
5378 for (; addr < end; addr++) {
5379 ret = get_user(val, addr);
5385 size = sizeof(*attr);
5388 ret = copy_from_user(attr, uattr, size);
5393 * If the type exists, the corresponding creation will verify
5396 if (attr->type >= PERF_TYPE_MAX)
5399 if (attr->__reserved_1)
5402 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
5405 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
5412 put_user(sizeof(*attr), &uattr->size);
5418 perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
5420 struct perf_buffer *buffer = NULL, *old_buffer = NULL;
5426 /* don't allow circular references */
5427 if (event == output_event)
5431 * Don't allow cross-cpu buffers
5433 if (output_event->cpu != event->cpu)
5437 * If its not a per-cpu buffer, it must be the same task.
5439 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
5443 mutex_lock(&event->mmap_mutex);
5444 /* Can't redirect output if we've got an active mmap() */
5445 if (atomic_read(&event->mmap_count))
5449 /* get the buffer we want to redirect to */
5450 buffer = perf_buffer_get(output_event);
5455 old_buffer = event->buffer;
5456 rcu_assign_pointer(event->buffer, buffer);
5459 mutex_unlock(&event->mmap_mutex);
5462 perf_buffer_put(old_buffer);
5468 * sys_perf_event_open - open a performance event, associate it to a task/cpu
5470 * @attr_uptr: event_id type attributes for monitoring/sampling
5473 * @group_fd: group leader event fd
5475 SYSCALL_DEFINE5(perf_event_open,
5476 struct perf_event_attr __user *, attr_uptr,
5477 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
5479 struct perf_event *group_leader = NULL, *output_event = NULL;
5480 struct perf_event *event, *sibling;
5481 struct perf_event_attr attr;
5482 struct perf_event_context *ctx;
5483 struct file *event_file = NULL;
5484 struct file *group_file = NULL;
5485 struct task_struct *task = NULL;
5489 int fput_needed = 0;
5492 /* for future expandability... */
5493 if (flags & ~(PERF_FLAG_FD_NO_GROUP | PERF_FLAG_FD_OUTPUT))
5496 err = perf_copy_attr(attr_uptr, &attr);
5500 if (!attr.exclude_kernel) {
5501 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
5506 if (attr.sample_freq > sysctl_perf_event_sample_rate)
5510 event_fd = get_unused_fd_flags(O_RDWR);
5514 if (group_fd != -1) {
5515 group_leader = perf_fget_light(group_fd, &fput_needed);
5516 if (IS_ERR(group_leader)) {
5517 err = PTR_ERR(group_leader);
5520 group_file = group_leader->filp;
5521 if (flags & PERF_FLAG_FD_OUTPUT)
5522 output_event = group_leader;
5523 if (flags & PERF_FLAG_FD_NO_GROUP)
5524 group_leader = NULL;
5528 task = find_lively_task_by_vpid(pid);
5530 err = PTR_ERR(task);
5535 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL, NULL);
5536 if (IS_ERR(event)) {
5537 err = PTR_ERR(event);
5542 * Special case software events and allow them to be part of
5543 * any hardware group.
5548 (is_software_event(event) != is_software_event(group_leader))) {
5549 if (is_software_event(event)) {
5551 * If event and group_leader are not both a software
5552 * event, and event is, then group leader is not.
5554 * Allow the addition of software events to !software
5555 * groups, this is safe because software events never
5558 pmu = group_leader->pmu;
5559 } else if (is_software_event(group_leader) &&
5560 (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
5562 * In case the group is a pure software group, and we
5563 * try to add a hardware event, move the whole group to
5564 * the hardware context.
5571 * Get the target context (task or percpu):
5573 ctx = find_get_context(pmu, task, cpu);
5580 * Look up the group leader (we will attach this event to it):
5586 * Do not allow a recursive hierarchy (this new sibling
5587 * becoming part of another group-sibling):
5589 if (group_leader->group_leader != group_leader)
5592 * Do not allow to attach to a group in a different
5593 * task or CPU context:
5596 if (group_leader->ctx->type != ctx->type)
5599 if (group_leader->ctx != ctx)
5604 * Only a group leader can be exclusive or pinned
5606 if (attr.exclusive || attr.pinned)
5611 err = perf_event_set_output(event, output_event);
5616 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, O_RDWR);
5617 if (IS_ERR(event_file)) {
5618 err = PTR_ERR(event_file);
5623 struct perf_event_context *gctx = group_leader->ctx;
5625 mutex_lock(&gctx->mutex);
5626 perf_event_remove_from_context(group_leader);
5627 list_for_each_entry(sibling, &group_leader->sibling_list,
5629 perf_event_remove_from_context(sibling);
5632 mutex_unlock(&gctx->mutex);
5636 event->filp = event_file;
5637 WARN_ON_ONCE(ctx->parent_ctx);
5638 mutex_lock(&ctx->mutex);
5641 perf_install_in_context(ctx, group_leader, cpu);
5643 list_for_each_entry(sibling, &group_leader->sibling_list,
5645 perf_install_in_context(ctx, sibling, cpu);
5650 perf_install_in_context(ctx, event, cpu);
5652 mutex_unlock(&ctx->mutex);
5654 event->owner = current;
5655 get_task_struct(current);
5656 mutex_lock(¤t->perf_event_mutex);
5657 list_add_tail(&event->owner_entry, ¤t->perf_event_list);
5658 mutex_unlock(¤t->perf_event_mutex);
5661 * Drop the reference on the group_event after placing the
5662 * new event on the sibling_list. This ensures destruction
5663 * of the group leader will find the pointer to itself in
5664 * perf_group_detach().
5666 fput_light(group_file, fput_needed);
5667 fd_install(event_fd, event_file);
5676 put_task_struct(task);
5678 fput_light(group_file, fput_needed);
5680 put_unused_fd(event_fd);
5685 * perf_event_create_kernel_counter
5687 * @attr: attributes of the counter to create
5688 * @cpu: cpu in which the counter is bound
5689 * @task: task to profile (NULL for percpu)
5692 perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
5693 struct task_struct *task,
5694 perf_overflow_handler_t overflow_handler)
5696 struct perf_event_context *ctx;
5697 struct perf_event *event;
5701 * Get the target context (task or percpu):
5704 event = perf_event_alloc(attr, cpu, task, NULL, NULL, overflow_handler);
5705 if (IS_ERR(event)) {
5706 err = PTR_ERR(event);
5710 ctx = find_get_context(event->pmu, task, cpu);
5717 WARN_ON_ONCE(ctx->parent_ctx);
5718 mutex_lock(&ctx->mutex);
5719 perf_install_in_context(ctx, event, cpu);
5721 mutex_unlock(&ctx->mutex);
5723 event->owner = current;
5724 get_task_struct(current);
5725 mutex_lock(¤t->perf_event_mutex);
5726 list_add_tail(&event->owner_entry, ¤t->perf_event_list);
5727 mutex_unlock(¤t->perf_event_mutex);
5734 return ERR_PTR(err);
5736 EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
5738 static void sync_child_event(struct perf_event *child_event,
5739 struct task_struct *child)
5741 struct perf_event *parent_event = child_event->parent;
5744 if (child_event->attr.inherit_stat)
5745 perf_event_read_event(child_event, child);
5747 child_val = perf_event_count(child_event);
5750 * Add back the child's count to the parent's count:
5752 atomic64_add(child_val, &parent_event->child_count);
5753 atomic64_add(child_event->total_time_enabled,
5754 &parent_event->child_total_time_enabled);
5755 atomic64_add(child_event->total_time_running,
5756 &parent_event->child_total_time_running);
5759 * Remove this event from the parent's list
5761 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
5762 mutex_lock(&parent_event->child_mutex);
5763 list_del_init(&child_event->child_list);
5764 mutex_unlock(&parent_event->child_mutex);
5767 * Release the parent event, if this was the last
5770 fput(parent_event->filp);
5774 __perf_event_exit_task(struct perf_event *child_event,
5775 struct perf_event_context *child_ctx,
5776 struct task_struct *child)
5778 struct perf_event *parent_event;
5780 perf_event_remove_from_context(child_event);
5782 parent_event = child_event->parent;
5784 * It can happen that parent exits first, and has events
5785 * that are still around due to the child reference. These
5786 * events need to be zapped - but otherwise linger.
5789 sync_child_event(child_event, child);
5790 free_event(child_event);
5794 static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
5796 struct perf_event *child_event, *tmp;
5797 struct perf_event_context *child_ctx;
5798 unsigned long flags;
5800 if (likely(!child->perf_event_ctxp[ctxn])) {
5801 perf_event_task(child, NULL, 0);
5805 local_irq_save(flags);
5807 * We can't reschedule here because interrupts are disabled,
5808 * and either child is current or it is a task that can't be
5809 * scheduled, so we are now safe from rescheduling changing
5812 child_ctx = child->perf_event_ctxp[ctxn];
5813 task_ctx_sched_out(child_ctx, EVENT_ALL);
5816 * Take the context lock here so that if find_get_context is
5817 * reading child->perf_event_ctxp, we wait until it has
5818 * incremented the context's refcount before we do put_ctx below.
5820 raw_spin_lock(&child_ctx->lock);
5821 child->perf_event_ctxp[ctxn] = NULL;
5823 * If this context is a clone; unclone it so it can't get
5824 * swapped to another process while we're removing all
5825 * the events from it.
5827 unclone_ctx(child_ctx);
5828 update_context_time(child_ctx);
5829 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
5832 * Report the task dead after unscheduling the events so that we
5833 * won't get any samples after PERF_RECORD_EXIT. We can however still
5834 * get a few PERF_RECORD_READ events.
5836 perf_event_task(child, child_ctx, 0);
5839 * We can recurse on the same lock type through:
5841 * __perf_event_exit_task()
5842 * sync_child_event()
5843 * fput(parent_event->filp)
5845 * mutex_lock(&ctx->mutex)
5847 * But since its the parent context it won't be the same instance.
5849 mutex_lock(&child_ctx->mutex);
5852 list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
5854 __perf_event_exit_task(child_event, child_ctx, child);
5856 list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
5858 __perf_event_exit_task(child_event, child_ctx, child);
5861 * If the last event was a group event, it will have appended all
5862 * its siblings to the list, but we obtained 'tmp' before that which
5863 * will still point to the list head terminating the iteration.
5865 if (!list_empty(&child_ctx->pinned_groups) ||
5866 !list_empty(&child_ctx->flexible_groups))
5869 mutex_unlock(&child_ctx->mutex);
5875 * When a child task exits, feed back event values to parent events.
5877 void perf_event_exit_task(struct task_struct *child)
5881 for_each_task_context_nr(ctxn)
5882 perf_event_exit_task_context(child, ctxn);
5885 static void perf_free_event(struct perf_event *event,
5886 struct perf_event_context *ctx)
5888 struct perf_event *parent = event->parent;
5890 if (WARN_ON_ONCE(!parent))
5893 mutex_lock(&parent->child_mutex);
5894 list_del_init(&event->child_list);
5895 mutex_unlock(&parent->child_mutex);
5899 perf_group_detach(event);
5900 list_del_event(event, ctx);
5905 * free an unexposed, unused context as created by inheritance by
5906 * perf_event_init_task below, used by fork() in case of fail.
5908 void perf_event_free_task(struct task_struct *task)
5910 struct perf_event_context *ctx;
5911 struct perf_event *event, *tmp;
5914 for_each_task_context_nr(ctxn) {
5915 ctx = task->perf_event_ctxp[ctxn];
5919 mutex_lock(&ctx->mutex);
5921 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
5923 perf_free_event(event, ctx);
5925 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
5927 perf_free_event(event, ctx);
5929 if (!list_empty(&ctx->pinned_groups) ||
5930 !list_empty(&ctx->flexible_groups))
5933 mutex_unlock(&ctx->mutex);
5939 void perf_event_delayed_put(struct task_struct *task)
5943 for_each_task_context_nr(ctxn)
5944 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
5948 * inherit a event from parent task to child task:
5950 static struct perf_event *
5951 inherit_event(struct perf_event *parent_event,
5952 struct task_struct *parent,
5953 struct perf_event_context *parent_ctx,
5954 struct task_struct *child,
5955 struct perf_event *group_leader,
5956 struct perf_event_context *child_ctx)
5958 struct perf_event *child_event;
5959 unsigned long flags;
5962 * Instead of creating recursive hierarchies of events,
5963 * we link inherited events back to the original parent,
5964 * which has a filp for sure, which we use as the reference
5967 if (parent_event->parent)
5968 parent_event = parent_event->parent;
5970 child_event = perf_event_alloc(&parent_event->attr,
5973 group_leader, parent_event,
5975 if (IS_ERR(child_event))
5980 * Make the child state follow the state of the parent event,
5981 * not its attr.disabled bit. We hold the parent's mutex,
5982 * so we won't race with perf_event_{en, dis}able_family.
5984 if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
5985 child_event->state = PERF_EVENT_STATE_INACTIVE;
5987 child_event->state = PERF_EVENT_STATE_OFF;
5989 if (parent_event->attr.freq) {
5990 u64 sample_period = parent_event->hw.sample_period;
5991 struct hw_perf_event *hwc = &child_event->hw;
5993 hwc->sample_period = sample_period;
5994 hwc->last_period = sample_period;
5996 local64_set(&hwc->period_left, sample_period);
5999 child_event->ctx = child_ctx;
6000 child_event->overflow_handler = parent_event->overflow_handler;
6003 * Link it up in the child's context:
6005 raw_spin_lock_irqsave(&child_ctx->lock, flags);
6006 add_event_to_ctx(child_event, child_ctx);
6007 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
6010 * Get a reference to the parent filp - we will fput it
6011 * when the child event exits. This is safe to do because
6012 * we are in the parent and we know that the filp still
6013 * exists and has a nonzero count:
6015 atomic_long_inc(&parent_event->filp->f_count);
6018 * Link this into the parent event's child list
6020 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
6021 mutex_lock(&parent_event->child_mutex);
6022 list_add_tail(&child_event->child_list, &parent_event->child_list);
6023 mutex_unlock(&parent_event->child_mutex);
6028 static int inherit_group(struct perf_event *parent_event,
6029 struct task_struct *parent,
6030 struct perf_event_context *parent_ctx,
6031 struct task_struct *child,
6032 struct perf_event_context *child_ctx)
6034 struct perf_event *leader;
6035 struct perf_event *sub;
6036 struct perf_event *child_ctr;
6038 leader = inherit_event(parent_event, parent, parent_ctx,
6039 child, NULL, child_ctx);
6041 return PTR_ERR(leader);
6042 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
6043 child_ctr = inherit_event(sub, parent, parent_ctx,
6044 child, leader, child_ctx);
6045 if (IS_ERR(child_ctr))
6046 return PTR_ERR(child_ctr);
6052 inherit_task_group(struct perf_event *event, struct task_struct *parent,
6053 struct perf_event_context *parent_ctx,
6054 struct task_struct *child, int ctxn,
6058 struct perf_event_context *child_ctx;
6060 if (!event->attr.inherit) {
6065 child_ctx = child->perf_event_ctxp[ctxn];
6068 * This is executed from the parent task context, so
6069 * inherit events that have been marked for cloning.
6070 * First allocate and initialize a context for the
6074 child_ctx = alloc_perf_context(event->pmu, child);
6078 child->perf_event_ctxp[ctxn] = child_ctx;
6081 ret = inherit_group(event, parent, parent_ctx,
6091 * Initialize the perf_event context in task_struct
6093 int perf_event_init_context(struct task_struct *child, int ctxn)
6095 struct perf_event_context *child_ctx, *parent_ctx;
6096 struct perf_event_context *cloned_ctx;
6097 struct perf_event *event;
6098 struct task_struct *parent = current;
6099 int inherited_all = 1;
6102 child->perf_event_ctxp[ctxn] = NULL;
6104 mutex_init(&child->perf_event_mutex);
6105 INIT_LIST_HEAD(&child->perf_event_list);
6107 if (likely(!parent->perf_event_ctxp[ctxn]))
6111 * If the parent's context is a clone, pin it so it won't get
6114 parent_ctx = perf_pin_task_context(parent, ctxn);
6117 * No need to check if parent_ctx != NULL here; since we saw
6118 * it non-NULL earlier, the only reason for it to become NULL
6119 * is if we exit, and since we're currently in the middle of
6120 * a fork we can't be exiting at the same time.
6124 * Lock the parent list. No need to lock the child - not PID
6125 * hashed yet and not running, so nobody can access it.
6127 mutex_lock(&parent_ctx->mutex);
6130 * We dont have to disable NMIs - we are only looking at
6131 * the list, not manipulating it:
6133 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
6134 ret = inherit_task_group(event, parent, parent_ctx,
6135 child, ctxn, &inherited_all);
6140 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
6141 ret = inherit_task_group(event, parent, parent_ctx,
6142 child, ctxn, &inherited_all);
6147 child_ctx = child->perf_event_ctxp[ctxn];
6149 if (child_ctx && inherited_all) {
6151 * Mark the child context as a clone of the parent
6152 * context, or of whatever the parent is a clone of.
6153 * Note that if the parent is a clone, it could get
6154 * uncloned at any point, but that doesn't matter
6155 * because the list of events and the generation
6156 * count can't have changed since we took the mutex.
6158 cloned_ctx = rcu_dereference(parent_ctx->parent_ctx);
6160 child_ctx->parent_ctx = cloned_ctx;
6161 child_ctx->parent_gen = parent_ctx->parent_gen;
6163 child_ctx->parent_ctx = parent_ctx;
6164 child_ctx->parent_gen = parent_ctx->generation;
6166 get_ctx(child_ctx->parent_ctx);
6169 mutex_unlock(&parent_ctx->mutex);
6171 perf_unpin_context(parent_ctx);
6177 * Initialize the perf_event context in task_struct
6179 int perf_event_init_task(struct task_struct *child)
6183 for_each_task_context_nr(ctxn) {
6184 ret = perf_event_init_context(child, ctxn);
6192 static void __init perf_event_init_all_cpus(void)
6194 struct swevent_htable *swhash;
6197 for_each_possible_cpu(cpu) {
6198 swhash = &per_cpu(swevent_htable, cpu);
6199 mutex_init(&swhash->hlist_mutex);
6200 INIT_LIST_HEAD(&per_cpu(rotation_list, cpu));
6204 static void __cpuinit perf_event_init_cpu(int cpu)
6206 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
6208 mutex_lock(&swhash->hlist_mutex);
6209 if (swhash->hlist_refcount > 0) {
6210 struct swevent_hlist *hlist;
6212 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
6214 rcu_assign_pointer(swhash->swevent_hlist, hlist);
6216 mutex_unlock(&swhash->hlist_mutex);
6219 #ifdef CONFIG_HOTPLUG_CPU
6220 static void perf_pmu_rotate_stop(struct pmu *pmu)
6222 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
6224 WARN_ON(!irqs_disabled());
6226 list_del_init(&cpuctx->rotation_list);
6229 static void __perf_event_exit_context(void *__info)
6231 struct perf_event_context *ctx = __info;
6232 struct perf_event *event, *tmp;
6234 perf_pmu_rotate_stop(ctx->pmu);
6236 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
6237 __perf_event_remove_from_context(event);
6238 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, group_entry)
6239 __perf_event_remove_from_context(event);
6242 static void perf_event_exit_cpu_context(int cpu)
6244 struct perf_event_context *ctx;
6248 idx = srcu_read_lock(&pmus_srcu);
6249 list_for_each_entry_rcu(pmu, &pmus, entry) {
6250 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
6252 mutex_lock(&ctx->mutex);
6253 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
6254 mutex_unlock(&ctx->mutex);
6256 srcu_read_unlock(&pmus_srcu, idx);
6259 static void perf_event_exit_cpu(int cpu)
6261 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
6263 mutex_lock(&swhash->hlist_mutex);
6264 swevent_hlist_release(swhash);
6265 mutex_unlock(&swhash->hlist_mutex);
6267 perf_event_exit_cpu_context(cpu);
6270 static inline void perf_event_exit_cpu(int cpu) { }
6273 static int __cpuinit
6274 perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
6276 unsigned int cpu = (long)hcpu;
6278 switch (action & ~CPU_TASKS_FROZEN) {
6280 case CPU_UP_PREPARE:
6281 case CPU_DOWN_FAILED:
6282 perf_event_init_cpu(cpu);
6285 case CPU_UP_CANCELED:
6286 case CPU_DOWN_PREPARE:
6287 perf_event_exit_cpu(cpu);
6297 void __init perf_event_init(void)
6301 perf_event_init_all_cpus();
6302 init_srcu_struct(&pmus_srcu);
6303 perf_pmu_register(&perf_swevent);
6304 perf_pmu_register(&perf_cpu_clock);
6305 perf_pmu_register(&perf_task_clock);
6307 perf_cpu_notifier(perf_cpu_notify);
6309 ret = init_hw_breakpoint();
6310 WARN(ret, "hw_breakpoint initialization failed with: %d", ret);