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/idr.h>
17 #include <linux/file.h>
18 #include <linux/poll.h>
19 #include <linux/slab.h>
20 #include <linux/hash.h>
21 #include <linux/sysfs.h>
22 #include <linux/dcache.h>
23 #include <linux/percpu.h>
24 #include <linux/ptrace.h>
25 #include <linux/reboot.h>
26 #include <linux/vmstat.h>
27 #include <linux/vmalloc.h>
28 #include <linux/hardirq.h>
29 #include <linux/rculist.h>
30 #include <linux/uaccess.h>
31 #include <linux/syscalls.h>
32 #include <linux/anon_inodes.h>
33 #include <linux/kernel_stat.h>
34 #include <linux/perf_event.h>
35 #include <linux/ftrace_event.h>
36 #include <linux/hw_breakpoint.h>
38 #include <asm/irq_regs.h>
40 atomic_t perf_task_events __read_mostly;
41 static atomic_t nr_mmap_events __read_mostly;
42 static atomic_t nr_comm_events __read_mostly;
43 static atomic_t nr_task_events __read_mostly;
45 static LIST_HEAD(pmus);
46 static DEFINE_MUTEX(pmus_lock);
47 static struct srcu_struct pmus_srcu;
50 * perf event paranoia level:
51 * -1 - not paranoid at all
52 * 0 - disallow raw tracepoint access for unpriv
53 * 1 - disallow cpu events for unpriv
54 * 2 - disallow kernel profiling for unpriv
56 int sysctl_perf_event_paranoid __read_mostly = 1;
58 int sysctl_perf_event_mlock __read_mostly = 512; /* 'free' kb per user */
61 * max perf event sample rate
63 int sysctl_perf_event_sample_rate __read_mostly = 100000;
65 static atomic64_t perf_event_id;
67 void __weak perf_event_print_debug(void) { }
69 extern __weak const char *perf_pmu_name(void)
74 void perf_pmu_disable(struct pmu *pmu)
76 int *count = this_cpu_ptr(pmu->pmu_disable_count);
78 pmu->pmu_disable(pmu);
81 void perf_pmu_enable(struct pmu *pmu)
83 int *count = this_cpu_ptr(pmu->pmu_disable_count);
88 static DEFINE_PER_CPU(struct list_head, rotation_list);
91 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
92 * because they're strictly cpu affine and rotate_start is called with IRQs
93 * disabled, while rotate_context is called from IRQ context.
95 static void perf_pmu_rotate_start(struct pmu *pmu)
97 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
98 struct list_head *head = &__get_cpu_var(rotation_list);
100 WARN_ON(!irqs_disabled());
102 if (list_empty(&cpuctx->rotation_list))
103 list_add(&cpuctx->rotation_list, head);
106 static void get_ctx(struct perf_event_context *ctx)
108 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
111 static void free_ctx(struct rcu_head *head)
113 struct perf_event_context *ctx;
115 ctx = container_of(head, struct perf_event_context, rcu_head);
119 static void put_ctx(struct perf_event_context *ctx)
121 if (atomic_dec_and_test(&ctx->refcount)) {
123 put_ctx(ctx->parent_ctx);
125 put_task_struct(ctx->task);
126 call_rcu(&ctx->rcu_head, free_ctx);
130 static void unclone_ctx(struct perf_event_context *ctx)
132 if (ctx->parent_ctx) {
133 put_ctx(ctx->parent_ctx);
134 ctx->parent_ctx = NULL;
138 static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
141 * only top level events have the pid namespace they were created in
144 event = event->parent;
146 return task_tgid_nr_ns(p, event->ns);
149 static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
152 * only top level events have the pid namespace they were created in
155 event = event->parent;
157 return task_pid_nr_ns(p, event->ns);
161 * If we inherit events we want to return the parent event id
164 static u64 primary_event_id(struct perf_event *event)
169 id = event->parent->id;
175 * Get the perf_event_context for a task and lock it.
176 * This has to cope with with the fact that until it is locked,
177 * the context could get moved to another task.
179 static struct perf_event_context *
180 perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
182 struct perf_event_context *ctx;
186 ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
189 * If this context is a clone of another, it might
190 * get swapped for another underneath us by
191 * perf_event_task_sched_out, though the
192 * rcu_read_lock() protects us from any context
193 * getting freed. Lock the context and check if it
194 * got swapped before we could get the lock, and retry
195 * if so. If we locked the right context, then it
196 * can't get swapped on us any more.
198 raw_spin_lock_irqsave(&ctx->lock, *flags);
199 if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
200 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
204 if (!atomic_inc_not_zero(&ctx->refcount)) {
205 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
214 * Get the context for a task and increment its pin_count so it
215 * can't get swapped to another task. This also increments its
216 * reference count so that the context can't get freed.
218 static struct perf_event_context *
219 perf_pin_task_context(struct task_struct *task, int ctxn)
221 struct perf_event_context *ctx;
224 ctx = perf_lock_task_context(task, ctxn, &flags);
227 raw_spin_unlock_irqrestore(&ctx->lock, flags);
232 static void perf_unpin_context(struct perf_event_context *ctx)
236 raw_spin_lock_irqsave(&ctx->lock, flags);
238 raw_spin_unlock_irqrestore(&ctx->lock, flags);
242 static inline u64 perf_clock(void)
244 return local_clock();
248 * Update the record of the current time in a context.
250 static void update_context_time(struct perf_event_context *ctx)
252 u64 now = perf_clock();
254 ctx->time += now - ctx->timestamp;
255 ctx->timestamp = now;
259 * Update the total_time_enabled and total_time_running fields for a event.
261 static void update_event_times(struct perf_event *event)
263 struct perf_event_context *ctx = event->ctx;
266 if (event->state < PERF_EVENT_STATE_INACTIVE ||
267 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
273 run_end = event->tstamp_stopped;
275 event->total_time_enabled = run_end - event->tstamp_enabled;
277 if (event->state == PERF_EVENT_STATE_INACTIVE)
278 run_end = event->tstamp_stopped;
282 event->total_time_running = run_end - event->tstamp_running;
286 * Update total_time_enabled and total_time_running for all events in a group.
288 static void update_group_times(struct perf_event *leader)
290 struct perf_event *event;
292 update_event_times(leader);
293 list_for_each_entry(event, &leader->sibling_list, group_entry)
294 update_event_times(event);
297 static struct list_head *
298 ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
300 if (event->attr.pinned)
301 return &ctx->pinned_groups;
303 return &ctx->flexible_groups;
307 * Add a event from the lists for its context.
308 * Must be called with ctx->mutex and ctx->lock held.
311 list_add_event(struct perf_event *event, struct perf_event_context *ctx)
313 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
314 event->attach_state |= PERF_ATTACH_CONTEXT;
317 * If we're a stand alone event or group leader, we go to the context
318 * list, group events are kept attached to the group so that
319 * perf_group_detach can, at all times, locate all siblings.
321 if (event->group_leader == event) {
322 struct list_head *list;
324 if (is_software_event(event))
325 event->group_flags |= PERF_GROUP_SOFTWARE;
327 list = ctx_group_list(event, ctx);
328 list_add_tail(&event->group_entry, list);
331 list_add_rcu(&event->event_entry, &ctx->event_list);
333 perf_pmu_rotate_start(ctx->pmu);
335 if (event->attr.inherit_stat)
340 * Called at perf_event creation and when events are attached/detached from a
343 static void perf_event__read_size(struct perf_event *event)
345 int entry = sizeof(u64); /* value */
349 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
352 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
355 if (event->attr.read_format & PERF_FORMAT_ID)
356 entry += sizeof(u64);
358 if (event->attr.read_format & PERF_FORMAT_GROUP) {
359 nr += event->group_leader->nr_siblings;
364 event->read_size = size;
367 static void perf_event__header_size(struct perf_event *event)
369 struct perf_sample_data *data;
370 u64 sample_type = event->attr.sample_type;
373 perf_event__read_size(event);
375 if (sample_type & PERF_SAMPLE_IP)
376 size += sizeof(data->ip);
378 if (sample_type & PERF_SAMPLE_ADDR)
379 size += sizeof(data->addr);
381 if (sample_type & PERF_SAMPLE_PERIOD)
382 size += sizeof(data->period);
384 if (sample_type & PERF_SAMPLE_READ)
385 size += event->read_size;
387 event->header_size = size;
390 static void perf_event__id_header_size(struct perf_event *event)
392 struct perf_sample_data *data;
393 u64 sample_type = event->attr.sample_type;
396 if (sample_type & PERF_SAMPLE_TID)
397 size += sizeof(data->tid_entry);
399 if (sample_type & PERF_SAMPLE_TIME)
400 size += sizeof(data->time);
402 if (sample_type & PERF_SAMPLE_ID)
403 size += sizeof(data->id);
405 if (sample_type & PERF_SAMPLE_STREAM_ID)
406 size += sizeof(data->stream_id);
408 if (sample_type & PERF_SAMPLE_CPU)
409 size += sizeof(data->cpu_entry);
411 event->id_header_size = size;
414 static void perf_group_attach(struct perf_event *event)
416 struct perf_event *group_leader = event->group_leader, *pos;
419 * We can have double attach due to group movement in perf_event_open.
421 if (event->attach_state & PERF_ATTACH_GROUP)
424 event->attach_state |= PERF_ATTACH_GROUP;
426 if (group_leader == event)
429 if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
430 !is_software_event(event))
431 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
433 list_add_tail(&event->group_entry, &group_leader->sibling_list);
434 group_leader->nr_siblings++;
436 perf_event__header_size(group_leader);
438 list_for_each_entry(pos, &group_leader->sibling_list, group_entry)
439 perf_event__header_size(pos);
443 * Remove a event from the lists for its context.
444 * Must be called with ctx->mutex and ctx->lock held.
447 list_del_event(struct perf_event *event, struct perf_event_context *ctx)
450 * We can have double detach due to exit/hot-unplug + close.
452 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
455 event->attach_state &= ~PERF_ATTACH_CONTEXT;
458 if (event->attr.inherit_stat)
461 list_del_rcu(&event->event_entry);
463 if (event->group_leader == event)
464 list_del_init(&event->group_entry);
466 update_group_times(event);
469 * If event was in error state, then keep it
470 * that way, otherwise bogus counts will be
471 * returned on read(). The only way to get out
472 * of error state is by explicit re-enabling
475 if (event->state > PERF_EVENT_STATE_OFF)
476 event->state = PERF_EVENT_STATE_OFF;
479 static void perf_group_detach(struct perf_event *event)
481 struct perf_event *sibling, *tmp;
482 struct list_head *list = NULL;
485 * We can have double detach due to exit/hot-unplug + close.
487 if (!(event->attach_state & PERF_ATTACH_GROUP))
490 event->attach_state &= ~PERF_ATTACH_GROUP;
493 * If this is a sibling, remove it from its group.
495 if (event->group_leader != event) {
496 list_del_init(&event->group_entry);
497 event->group_leader->nr_siblings--;
501 if (!list_empty(&event->group_entry))
502 list = &event->group_entry;
505 * If this was a group event with sibling events then
506 * upgrade the siblings to singleton events by adding them
507 * to whatever list we are on.
509 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
511 list_move_tail(&sibling->group_entry, list);
512 sibling->group_leader = sibling;
514 /* Inherit group flags from the previous leader */
515 sibling->group_flags = event->group_flags;
519 perf_event__header_size(event->group_leader);
521 list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry)
522 perf_event__header_size(tmp);
526 event_filter_match(struct perf_event *event)
528 return event->cpu == -1 || event->cpu == smp_processor_id();
532 event_sched_out(struct perf_event *event,
533 struct perf_cpu_context *cpuctx,
534 struct perf_event_context *ctx)
538 * An event which could not be activated because of
539 * filter mismatch still needs to have its timings
540 * maintained, otherwise bogus information is return
541 * via read() for time_enabled, time_running:
543 if (event->state == PERF_EVENT_STATE_INACTIVE
544 && !event_filter_match(event)) {
545 delta = ctx->time - event->tstamp_stopped;
546 event->tstamp_running += delta;
547 event->tstamp_stopped = ctx->time;
550 if (event->state != PERF_EVENT_STATE_ACTIVE)
553 event->state = PERF_EVENT_STATE_INACTIVE;
554 if (event->pending_disable) {
555 event->pending_disable = 0;
556 event->state = PERF_EVENT_STATE_OFF;
558 event->tstamp_stopped = ctx->time;
559 event->pmu->del(event, 0);
562 if (!is_software_event(event))
563 cpuctx->active_oncpu--;
565 if (event->attr.exclusive || !cpuctx->active_oncpu)
566 cpuctx->exclusive = 0;
570 group_sched_out(struct perf_event *group_event,
571 struct perf_cpu_context *cpuctx,
572 struct perf_event_context *ctx)
574 struct perf_event *event;
575 int state = group_event->state;
577 event_sched_out(group_event, cpuctx, ctx);
580 * Schedule out siblings (if any):
582 list_for_each_entry(event, &group_event->sibling_list, group_entry)
583 event_sched_out(event, cpuctx, ctx);
585 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
586 cpuctx->exclusive = 0;
589 static inline struct perf_cpu_context *
590 __get_cpu_context(struct perf_event_context *ctx)
592 return this_cpu_ptr(ctx->pmu->pmu_cpu_context);
596 * Cross CPU call to remove a performance event
598 * We disable the event on the hardware level first. After that we
599 * remove it from the context list.
601 static void __perf_event_remove_from_context(void *info)
603 struct perf_event *event = info;
604 struct perf_event_context *ctx = event->ctx;
605 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
608 * If this is a task context, we need to check whether it is
609 * the current task context of this cpu. If not it has been
610 * scheduled out before the smp call arrived.
612 if (ctx->task && cpuctx->task_ctx != ctx)
615 raw_spin_lock(&ctx->lock);
617 event_sched_out(event, cpuctx, ctx);
619 list_del_event(event, ctx);
621 raw_spin_unlock(&ctx->lock);
626 * Remove the event from a task's (or a CPU's) list of events.
628 * Must be called with ctx->mutex held.
630 * CPU events are removed with a smp call. For task events we only
631 * call when the task is on a CPU.
633 * If event->ctx is a cloned context, callers must make sure that
634 * every task struct that event->ctx->task could possibly point to
635 * remains valid. This is OK when called from perf_release since
636 * that only calls us on the top-level context, which can't be a clone.
637 * When called from perf_event_exit_task, it's OK because the
638 * context has been detached from its task.
640 static void perf_event_remove_from_context(struct perf_event *event)
642 struct perf_event_context *ctx = event->ctx;
643 struct task_struct *task = ctx->task;
647 * Per cpu events are removed via an smp call and
648 * the removal is always successful.
650 smp_call_function_single(event->cpu,
651 __perf_event_remove_from_context,
657 task_oncpu_function_call(task, __perf_event_remove_from_context,
660 raw_spin_lock_irq(&ctx->lock);
662 * If the context is active we need to retry the smp call.
664 if (ctx->nr_active && !list_empty(&event->group_entry)) {
665 raw_spin_unlock_irq(&ctx->lock);
670 * The lock prevents that this context is scheduled in so we
671 * can remove the event safely, if the call above did not
674 if (!list_empty(&event->group_entry))
675 list_del_event(event, ctx);
676 raw_spin_unlock_irq(&ctx->lock);
680 * Cross CPU call to disable a performance event
682 static void __perf_event_disable(void *info)
684 struct perf_event *event = info;
685 struct perf_event_context *ctx = event->ctx;
686 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
689 * If this is a per-task event, need to check whether this
690 * event's task is the current task on this cpu.
692 if (ctx->task && cpuctx->task_ctx != ctx)
695 raw_spin_lock(&ctx->lock);
698 * If the event is on, turn it off.
699 * If it is in error state, leave it in error state.
701 if (event->state >= PERF_EVENT_STATE_INACTIVE) {
702 update_context_time(ctx);
703 update_group_times(event);
704 if (event == event->group_leader)
705 group_sched_out(event, cpuctx, ctx);
707 event_sched_out(event, cpuctx, ctx);
708 event->state = PERF_EVENT_STATE_OFF;
711 raw_spin_unlock(&ctx->lock);
717 * If event->ctx is a cloned context, callers must make sure that
718 * every task struct that event->ctx->task could possibly point to
719 * remains valid. This condition is satisifed when called through
720 * perf_event_for_each_child or perf_event_for_each because they
721 * hold the top-level event's child_mutex, so any descendant that
722 * goes to exit will block in sync_child_event.
723 * When called from perf_pending_event it's OK because event->ctx
724 * is the current context on this CPU and preemption is disabled,
725 * hence we can't get into perf_event_task_sched_out for this context.
727 void perf_event_disable(struct perf_event *event)
729 struct perf_event_context *ctx = event->ctx;
730 struct task_struct *task = ctx->task;
734 * Disable the event on the cpu that it's on
736 smp_call_function_single(event->cpu, __perf_event_disable,
742 task_oncpu_function_call(task, __perf_event_disable, event);
744 raw_spin_lock_irq(&ctx->lock);
746 * If the event is still active, we need to retry the cross-call.
748 if (event->state == PERF_EVENT_STATE_ACTIVE) {
749 raw_spin_unlock_irq(&ctx->lock);
754 * Since we have the lock this context can't be scheduled
755 * in, so we can change the state safely.
757 if (event->state == PERF_EVENT_STATE_INACTIVE) {
758 update_group_times(event);
759 event->state = PERF_EVENT_STATE_OFF;
762 raw_spin_unlock_irq(&ctx->lock);
766 event_sched_in(struct perf_event *event,
767 struct perf_cpu_context *cpuctx,
768 struct perf_event_context *ctx)
770 if (event->state <= PERF_EVENT_STATE_OFF)
773 event->state = PERF_EVENT_STATE_ACTIVE;
774 event->oncpu = smp_processor_id();
776 * The new state must be visible before we turn it on in the hardware:
780 if (event->pmu->add(event, PERF_EF_START)) {
781 event->state = PERF_EVENT_STATE_INACTIVE;
786 event->tstamp_running += ctx->time - event->tstamp_stopped;
788 event->shadow_ctx_time = ctx->time - ctx->timestamp;
790 if (!is_software_event(event))
791 cpuctx->active_oncpu++;
794 if (event->attr.exclusive)
795 cpuctx->exclusive = 1;
801 group_sched_in(struct perf_event *group_event,
802 struct perf_cpu_context *cpuctx,
803 struct perf_event_context *ctx)
805 struct perf_event *event, *partial_group = NULL;
806 struct pmu *pmu = group_event->pmu;
808 bool simulate = false;
810 if (group_event->state == PERF_EVENT_STATE_OFF)
815 if (event_sched_in(group_event, cpuctx, ctx)) {
816 pmu->cancel_txn(pmu);
821 * Schedule in siblings as one group (if any):
823 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
824 if (event_sched_in(event, cpuctx, ctx)) {
825 partial_group = event;
830 if (!pmu->commit_txn(pmu))
835 * Groups can be scheduled in as one unit only, so undo any
836 * partial group before returning:
837 * The events up to the failed event are scheduled out normally,
838 * tstamp_stopped will be updated.
840 * The failed events and the remaining siblings need to have
841 * their timings updated as if they had gone thru event_sched_in()
842 * and event_sched_out(). This is required to get consistent timings
843 * across the group. This also takes care of the case where the group
844 * could never be scheduled by ensuring tstamp_stopped is set to mark
845 * the time the event was actually stopped, such that time delta
846 * calculation in update_event_times() is correct.
848 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
849 if (event == partial_group)
853 event->tstamp_running += now - event->tstamp_stopped;
854 event->tstamp_stopped = now;
856 event_sched_out(event, cpuctx, ctx);
859 event_sched_out(group_event, cpuctx, ctx);
861 pmu->cancel_txn(pmu);
867 * Work out whether we can put this event group on the CPU now.
869 static int group_can_go_on(struct perf_event *event,
870 struct perf_cpu_context *cpuctx,
874 * Groups consisting entirely of software events can always go on.
876 if (event->group_flags & PERF_GROUP_SOFTWARE)
879 * If an exclusive group is already on, no other hardware
882 if (cpuctx->exclusive)
885 * If this group is exclusive and there are already
886 * events on the CPU, it can't go on.
888 if (event->attr.exclusive && cpuctx->active_oncpu)
891 * Otherwise, try to add it if all previous groups were able
897 static void add_event_to_ctx(struct perf_event *event,
898 struct perf_event_context *ctx)
900 list_add_event(event, ctx);
901 perf_group_attach(event);
902 event->tstamp_enabled = ctx->time;
903 event->tstamp_running = ctx->time;
904 event->tstamp_stopped = ctx->time;
908 * Cross CPU call to install and enable a performance event
910 * Must be called with ctx->mutex held
912 static void __perf_install_in_context(void *info)
914 struct perf_event *event = info;
915 struct perf_event_context *ctx = event->ctx;
916 struct perf_event *leader = event->group_leader;
917 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
921 * If this is a task context, we need to check whether it is
922 * the current task context of this cpu. If not it has been
923 * scheduled out before the smp call arrived.
924 * Or possibly this is the right context but it isn't
925 * on this cpu because it had no events.
927 if (ctx->task && cpuctx->task_ctx != ctx) {
928 if (cpuctx->task_ctx || ctx->task != current)
930 cpuctx->task_ctx = ctx;
933 raw_spin_lock(&ctx->lock);
935 update_context_time(ctx);
937 add_event_to_ctx(event, ctx);
939 if (event->cpu != -1 && event->cpu != smp_processor_id())
943 * Don't put the event on if it is disabled or if
944 * it is in a group and the group isn't on.
946 if (event->state != PERF_EVENT_STATE_INACTIVE ||
947 (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE))
951 * An exclusive event can't go on if there are already active
952 * hardware events, and no hardware event can go on if there
953 * is already an exclusive event on.
955 if (!group_can_go_on(event, cpuctx, 1))
958 err = event_sched_in(event, cpuctx, ctx);
962 * This event couldn't go on. If it is in a group
963 * then we have to pull the whole group off.
964 * If the event group is pinned then put it in error state.
967 group_sched_out(leader, cpuctx, ctx);
968 if (leader->attr.pinned) {
969 update_group_times(leader);
970 leader->state = PERF_EVENT_STATE_ERROR;
975 raw_spin_unlock(&ctx->lock);
979 * Attach a performance event to a context
981 * First we add the event to the list with the hardware enable bit
982 * in event->hw_config cleared.
984 * If the event is attached to a task which is on a CPU we use a smp
985 * call to enable it in the task context. The task might have been
986 * scheduled away, but we check this in the smp call again.
988 * Must be called with ctx->mutex held.
991 perf_install_in_context(struct perf_event_context *ctx,
992 struct perf_event *event,
995 struct task_struct *task = ctx->task;
1001 * Per cpu events are installed via an smp call and
1002 * the install is always successful.
1004 smp_call_function_single(cpu, __perf_install_in_context,
1010 task_oncpu_function_call(task, __perf_install_in_context,
1013 raw_spin_lock_irq(&ctx->lock);
1015 * we need to retry the smp call.
1017 if (ctx->is_active && list_empty(&event->group_entry)) {
1018 raw_spin_unlock_irq(&ctx->lock);
1023 * The lock prevents that this context is scheduled in so we
1024 * can add the event safely, if it the call above did not
1027 if (list_empty(&event->group_entry))
1028 add_event_to_ctx(event, ctx);
1029 raw_spin_unlock_irq(&ctx->lock);
1033 * Put a event into inactive state and update time fields.
1034 * Enabling the leader of a group effectively enables all
1035 * the group members that aren't explicitly disabled, so we
1036 * have to update their ->tstamp_enabled also.
1037 * Note: this works for group members as well as group leaders
1038 * since the non-leader members' sibling_lists will be empty.
1040 static void __perf_event_mark_enabled(struct perf_event *event,
1041 struct perf_event_context *ctx)
1043 struct perf_event *sub;
1045 event->state = PERF_EVENT_STATE_INACTIVE;
1046 event->tstamp_enabled = ctx->time - event->total_time_enabled;
1047 list_for_each_entry(sub, &event->sibling_list, group_entry) {
1048 if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
1049 sub->tstamp_enabled =
1050 ctx->time - sub->total_time_enabled;
1056 * Cross CPU call to enable a performance event
1058 static void __perf_event_enable(void *info)
1060 struct perf_event *event = info;
1061 struct perf_event_context *ctx = event->ctx;
1062 struct perf_event *leader = event->group_leader;
1063 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1067 * If this is a per-task event, need to check whether this
1068 * event's task is the current task on this cpu.
1070 if (ctx->task && cpuctx->task_ctx != ctx) {
1071 if (cpuctx->task_ctx || ctx->task != current)
1073 cpuctx->task_ctx = ctx;
1076 raw_spin_lock(&ctx->lock);
1078 update_context_time(ctx);
1080 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1082 __perf_event_mark_enabled(event, ctx);
1084 if (event->cpu != -1 && event->cpu != smp_processor_id())
1088 * If the event is in a group and isn't the group leader,
1089 * then don't put it on unless the group is on.
1091 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
1094 if (!group_can_go_on(event, cpuctx, 1)) {
1097 if (event == leader)
1098 err = group_sched_in(event, cpuctx, ctx);
1100 err = event_sched_in(event, cpuctx, ctx);
1105 * If this event can't go on and it's part of a
1106 * group, then the whole group has to come off.
1108 if (leader != event)
1109 group_sched_out(leader, cpuctx, ctx);
1110 if (leader->attr.pinned) {
1111 update_group_times(leader);
1112 leader->state = PERF_EVENT_STATE_ERROR;
1117 raw_spin_unlock(&ctx->lock);
1123 * If event->ctx is a cloned context, callers must make sure that
1124 * every task struct that event->ctx->task could possibly point to
1125 * remains valid. This condition is satisfied when called through
1126 * perf_event_for_each_child or perf_event_for_each as described
1127 * for perf_event_disable.
1129 void perf_event_enable(struct perf_event *event)
1131 struct perf_event_context *ctx = event->ctx;
1132 struct task_struct *task = ctx->task;
1136 * Enable the event on the cpu that it's on
1138 smp_call_function_single(event->cpu, __perf_event_enable,
1143 raw_spin_lock_irq(&ctx->lock);
1144 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1148 * If the event is in error state, clear that first.
1149 * That way, if we see the event in error state below, we
1150 * know that it has gone back into error state, as distinct
1151 * from the task having been scheduled away before the
1152 * cross-call arrived.
1154 if (event->state == PERF_EVENT_STATE_ERROR)
1155 event->state = PERF_EVENT_STATE_OFF;
1158 raw_spin_unlock_irq(&ctx->lock);
1159 task_oncpu_function_call(task, __perf_event_enable, event);
1161 raw_spin_lock_irq(&ctx->lock);
1164 * If the context is active and the event is still off,
1165 * we need to retry the cross-call.
1167 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF)
1171 * Since we have the lock this context can't be scheduled
1172 * in, so we can change the state safely.
1174 if (event->state == PERF_EVENT_STATE_OFF)
1175 __perf_event_mark_enabled(event, ctx);
1178 raw_spin_unlock_irq(&ctx->lock);
1181 static int perf_event_refresh(struct perf_event *event, int refresh)
1184 * not supported on inherited events
1186 if (event->attr.inherit || !is_sampling_event(event))
1189 atomic_add(refresh, &event->event_limit);
1190 perf_event_enable(event);
1196 EVENT_FLEXIBLE = 0x1,
1198 EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
1201 static void ctx_sched_out(struct perf_event_context *ctx,
1202 struct perf_cpu_context *cpuctx,
1203 enum event_type_t event_type)
1205 struct perf_event *event;
1207 raw_spin_lock(&ctx->lock);
1208 perf_pmu_disable(ctx->pmu);
1210 if (likely(!ctx->nr_events))
1212 update_context_time(ctx);
1214 if (!ctx->nr_active)
1217 if (event_type & EVENT_PINNED) {
1218 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
1219 group_sched_out(event, cpuctx, ctx);
1222 if (event_type & EVENT_FLEXIBLE) {
1223 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
1224 group_sched_out(event, cpuctx, ctx);
1227 perf_pmu_enable(ctx->pmu);
1228 raw_spin_unlock(&ctx->lock);
1232 * Test whether two contexts are equivalent, i.e. whether they
1233 * have both been cloned from the same version of the same context
1234 * and they both have the same number of enabled events.
1235 * If the number of enabled events is the same, then the set
1236 * of enabled events should be the same, because these are both
1237 * inherited contexts, therefore we can't access individual events
1238 * in them directly with an fd; we can only enable/disable all
1239 * events via prctl, or enable/disable all events in a family
1240 * via ioctl, which will have the same effect on both contexts.
1242 static int context_equiv(struct perf_event_context *ctx1,
1243 struct perf_event_context *ctx2)
1245 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
1246 && ctx1->parent_gen == ctx2->parent_gen
1247 && !ctx1->pin_count && !ctx2->pin_count;
1250 static void __perf_event_sync_stat(struct perf_event *event,
1251 struct perf_event *next_event)
1255 if (!event->attr.inherit_stat)
1259 * Update the event value, we cannot use perf_event_read()
1260 * because we're in the middle of a context switch and have IRQs
1261 * disabled, which upsets smp_call_function_single(), however
1262 * we know the event must be on the current CPU, therefore we
1263 * don't need to use it.
1265 switch (event->state) {
1266 case PERF_EVENT_STATE_ACTIVE:
1267 event->pmu->read(event);
1270 case PERF_EVENT_STATE_INACTIVE:
1271 update_event_times(event);
1279 * In order to keep per-task stats reliable we need to flip the event
1280 * values when we flip the contexts.
1282 value = local64_read(&next_event->count);
1283 value = local64_xchg(&event->count, value);
1284 local64_set(&next_event->count, value);
1286 swap(event->total_time_enabled, next_event->total_time_enabled);
1287 swap(event->total_time_running, next_event->total_time_running);
1290 * Since we swizzled the values, update the user visible data too.
1292 perf_event_update_userpage(event);
1293 perf_event_update_userpage(next_event);
1296 #define list_next_entry(pos, member) \
1297 list_entry(pos->member.next, typeof(*pos), member)
1299 static void perf_event_sync_stat(struct perf_event_context *ctx,
1300 struct perf_event_context *next_ctx)
1302 struct perf_event *event, *next_event;
1307 update_context_time(ctx);
1309 event = list_first_entry(&ctx->event_list,
1310 struct perf_event, event_entry);
1312 next_event = list_first_entry(&next_ctx->event_list,
1313 struct perf_event, event_entry);
1315 while (&event->event_entry != &ctx->event_list &&
1316 &next_event->event_entry != &next_ctx->event_list) {
1318 __perf_event_sync_stat(event, next_event);
1320 event = list_next_entry(event, event_entry);
1321 next_event = list_next_entry(next_event, event_entry);
1325 void perf_event_context_sched_out(struct task_struct *task, int ctxn,
1326 struct task_struct *next)
1328 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
1329 struct perf_event_context *next_ctx;
1330 struct perf_event_context *parent;
1331 struct perf_cpu_context *cpuctx;
1337 cpuctx = __get_cpu_context(ctx);
1338 if (!cpuctx->task_ctx)
1342 parent = rcu_dereference(ctx->parent_ctx);
1343 next_ctx = next->perf_event_ctxp[ctxn];
1344 if (parent && next_ctx &&
1345 rcu_dereference(next_ctx->parent_ctx) == parent) {
1347 * Looks like the two contexts are clones, so we might be
1348 * able to optimize the context switch. We lock both
1349 * contexts and check that they are clones under the
1350 * lock (including re-checking that neither has been
1351 * uncloned in the meantime). It doesn't matter which
1352 * order we take the locks because no other cpu could
1353 * be trying to lock both of these tasks.
1355 raw_spin_lock(&ctx->lock);
1356 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
1357 if (context_equiv(ctx, next_ctx)) {
1359 * XXX do we need a memory barrier of sorts
1360 * wrt to rcu_dereference() of perf_event_ctxp
1362 task->perf_event_ctxp[ctxn] = next_ctx;
1363 next->perf_event_ctxp[ctxn] = ctx;
1365 next_ctx->task = task;
1368 perf_event_sync_stat(ctx, next_ctx);
1370 raw_spin_unlock(&next_ctx->lock);
1371 raw_spin_unlock(&ctx->lock);
1376 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
1377 cpuctx->task_ctx = NULL;
1381 #define for_each_task_context_nr(ctxn) \
1382 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
1385 * Called from scheduler to remove the events of the current task,
1386 * with interrupts disabled.
1388 * We stop each event and update the event value in event->count.
1390 * This does not protect us against NMI, but disable()
1391 * sets the disabled bit in the control field of event _before_
1392 * accessing the event control register. If a NMI hits, then it will
1393 * not restart the event.
1395 void __perf_event_task_sched_out(struct task_struct *task,
1396 struct task_struct *next)
1400 for_each_task_context_nr(ctxn)
1401 perf_event_context_sched_out(task, ctxn, next);
1404 static void task_ctx_sched_out(struct perf_event_context *ctx,
1405 enum event_type_t event_type)
1407 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1409 if (!cpuctx->task_ctx)
1412 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
1415 ctx_sched_out(ctx, cpuctx, event_type);
1416 cpuctx->task_ctx = NULL;
1420 * Called with IRQs disabled
1422 static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
1423 enum event_type_t event_type)
1425 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
1429 ctx_pinned_sched_in(struct perf_event_context *ctx,
1430 struct perf_cpu_context *cpuctx)
1432 struct perf_event *event;
1434 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
1435 if (event->state <= PERF_EVENT_STATE_OFF)
1437 if (event->cpu != -1 && event->cpu != smp_processor_id())
1440 if (group_can_go_on(event, cpuctx, 1))
1441 group_sched_in(event, cpuctx, ctx);
1444 * If this pinned group hasn't been scheduled,
1445 * put it in error state.
1447 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1448 update_group_times(event);
1449 event->state = PERF_EVENT_STATE_ERROR;
1455 ctx_flexible_sched_in(struct perf_event_context *ctx,
1456 struct perf_cpu_context *cpuctx)
1458 struct perf_event *event;
1461 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
1462 /* Ignore events in OFF or ERROR state */
1463 if (event->state <= PERF_EVENT_STATE_OFF)
1466 * Listen to the 'cpu' scheduling filter constraint
1469 if (event->cpu != -1 && event->cpu != smp_processor_id())
1472 if (group_can_go_on(event, cpuctx, can_add_hw)) {
1473 if (group_sched_in(event, cpuctx, ctx))
1480 ctx_sched_in(struct perf_event_context *ctx,
1481 struct perf_cpu_context *cpuctx,
1482 enum event_type_t event_type)
1484 raw_spin_lock(&ctx->lock);
1486 if (likely(!ctx->nr_events))
1489 ctx->timestamp = perf_clock();
1492 * First go through the list and put on any pinned groups
1493 * in order to give them the best chance of going on.
1495 if (event_type & EVENT_PINNED)
1496 ctx_pinned_sched_in(ctx, cpuctx);
1498 /* Then walk through the lower prio flexible groups */
1499 if (event_type & EVENT_FLEXIBLE)
1500 ctx_flexible_sched_in(ctx, cpuctx);
1503 raw_spin_unlock(&ctx->lock);
1506 static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
1507 enum event_type_t event_type)
1509 struct perf_event_context *ctx = &cpuctx->ctx;
1511 ctx_sched_in(ctx, cpuctx, event_type);
1514 static void task_ctx_sched_in(struct perf_event_context *ctx,
1515 enum event_type_t event_type)
1517 struct perf_cpu_context *cpuctx;
1519 cpuctx = __get_cpu_context(ctx);
1520 if (cpuctx->task_ctx == ctx)
1523 ctx_sched_in(ctx, cpuctx, event_type);
1524 cpuctx->task_ctx = ctx;
1527 void perf_event_context_sched_in(struct perf_event_context *ctx)
1529 struct perf_cpu_context *cpuctx;
1531 cpuctx = __get_cpu_context(ctx);
1532 if (cpuctx->task_ctx == ctx)
1535 perf_pmu_disable(ctx->pmu);
1537 * We want to keep the following priority order:
1538 * cpu pinned (that don't need to move), task pinned,
1539 * cpu flexible, task flexible.
1541 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
1543 ctx_sched_in(ctx, cpuctx, EVENT_PINNED);
1544 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE);
1545 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE);
1547 cpuctx->task_ctx = ctx;
1550 * Since these rotations are per-cpu, we need to ensure the
1551 * cpu-context we got scheduled on is actually rotating.
1553 perf_pmu_rotate_start(ctx->pmu);
1554 perf_pmu_enable(ctx->pmu);
1558 * Called from scheduler to add the events of the current task
1559 * with interrupts disabled.
1561 * We restore the event value and then enable it.
1563 * This does not protect us against NMI, but enable()
1564 * sets the enabled bit in the control field of event _before_
1565 * accessing the event control register. If a NMI hits, then it will
1566 * keep the event running.
1568 void __perf_event_task_sched_in(struct task_struct *task)
1570 struct perf_event_context *ctx;
1573 for_each_task_context_nr(ctxn) {
1574 ctx = task->perf_event_ctxp[ctxn];
1578 perf_event_context_sched_in(ctx);
1582 #define MAX_INTERRUPTS (~0ULL)
1584 static void perf_log_throttle(struct perf_event *event, int enable);
1586 static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
1588 u64 frequency = event->attr.sample_freq;
1589 u64 sec = NSEC_PER_SEC;
1590 u64 divisor, dividend;
1592 int count_fls, nsec_fls, frequency_fls, sec_fls;
1594 count_fls = fls64(count);
1595 nsec_fls = fls64(nsec);
1596 frequency_fls = fls64(frequency);
1600 * We got @count in @nsec, with a target of sample_freq HZ
1601 * the target period becomes:
1604 * period = -------------------
1605 * @nsec * sample_freq
1610 * Reduce accuracy by one bit such that @a and @b converge
1611 * to a similar magnitude.
1613 #define REDUCE_FLS(a, b) \
1615 if (a##_fls > b##_fls) { \
1625 * Reduce accuracy until either term fits in a u64, then proceed with
1626 * the other, so that finally we can do a u64/u64 division.
1628 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
1629 REDUCE_FLS(nsec, frequency);
1630 REDUCE_FLS(sec, count);
1633 if (count_fls + sec_fls > 64) {
1634 divisor = nsec * frequency;
1636 while (count_fls + sec_fls > 64) {
1637 REDUCE_FLS(count, sec);
1641 dividend = count * sec;
1643 dividend = count * sec;
1645 while (nsec_fls + frequency_fls > 64) {
1646 REDUCE_FLS(nsec, frequency);
1650 divisor = nsec * frequency;
1656 return div64_u64(dividend, divisor);
1659 static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count)
1661 struct hw_perf_event *hwc = &event->hw;
1662 s64 period, sample_period;
1665 period = perf_calculate_period(event, nsec, count);
1667 delta = (s64)(period - hwc->sample_period);
1668 delta = (delta + 7) / 8; /* low pass filter */
1670 sample_period = hwc->sample_period + delta;
1675 hwc->sample_period = sample_period;
1677 if (local64_read(&hwc->period_left) > 8*sample_period) {
1678 event->pmu->stop(event, PERF_EF_UPDATE);
1679 local64_set(&hwc->period_left, 0);
1680 event->pmu->start(event, PERF_EF_RELOAD);
1684 static void perf_ctx_adjust_freq(struct perf_event_context *ctx, u64 period)
1686 struct perf_event *event;
1687 struct hw_perf_event *hwc;
1688 u64 interrupts, now;
1691 raw_spin_lock(&ctx->lock);
1692 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
1693 if (event->state != PERF_EVENT_STATE_ACTIVE)
1696 if (event->cpu != -1 && event->cpu != smp_processor_id())
1701 interrupts = hwc->interrupts;
1702 hwc->interrupts = 0;
1705 * unthrottle events on the tick
1707 if (interrupts == MAX_INTERRUPTS) {
1708 perf_log_throttle(event, 1);
1709 event->pmu->start(event, 0);
1712 if (!event->attr.freq || !event->attr.sample_freq)
1715 event->pmu->read(event);
1716 now = local64_read(&event->count);
1717 delta = now - hwc->freq_count_stamp;
1718 hwc->freq_count_stamp = now;
1721 perf_adjust_period(event, period, delta);
1723 raw_spin_unlock(&ctx->lock);
1727 * Round-robin a context's events:
1729 static void rotate_ctx(struct perf_event_context *ctx)
1731 raw_spin_lock(&ctx->lock);
1734 * Rotate the first entry last of non-pinned groups. Rotation might be
1735 * disabled by the inheritance code.
1737 if (!ctx->rotate_disable)
1738 list_rotate_left(&ctx->flexible_groups);
1740 raw_spin_unlock(&ctx->lock);
1744 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
1745 * because they're strictly cpu affine and rotate_start is called with IRQs
1746 * disabled, while rotate_context is called from IRQ context.
1748 static void perf_rotate_context(struct perf_cpu_context *cpuctx)
1750 u64 interval = (u64)cpuctx->jiffies_interval * TICK_NSEC;
1751 struct perf_event_context *ctx = NULL;
1752 int rotate = 0, remove = 1;
1754 if (cpuctx->ctx.nr_events) {
1756 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
1760 ctx = cpuctx->task_ctx;
1761 if (ctx && ctx->nr_events) {
1763 if (ctx->nr_events != ctx->nr_active)
1767 perf_pmu_disable(cpuctx->ctx.pmu);
1768 perf_ctx_adjust_freq(&cpuctx->ctx, interval);
1770 perf_ctx_adjust_freq(ctx, interval);
1775 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
1777 task_ctx_sched_out(ctx, EVENT_FLEXIBLE);
1779 rotate_ctx(&cpuctx->ctx);
1783 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE);
1785 task_ctx_sched_in(ctx, EVENT_FLEXIBLE);
1789 list_del_init(&cpuctx->rotation_list);
1791 perf_pmu_enable(cpuctx->ctx.pmu);
1794 void perf_event_task_tick(void)
1796 struct list_head *head = &__get_cpu_var(rotation_list);
1797 struct perf_cpu_context *cpuctx, *tmp;
1799 WARN_ON(!irqs_disabled());
1801 list_for_each_entry_safe(cpuctx, tmp, head, rotation_list) {
1802 if (cpuctx->jiffies_interval == 1 ||
1803 !(jiffies % cpuctx->jiffies_interval))
1804 perf_rotate_context(cpuctx);
1808 static int event_enable_on_exec(struct perf_event *event,
1809 struct perf_event_context *ctx)
1811 if (!event->attr.enable_on_exec)
1814 event->attr.enable_on_exec = 0;
1815 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1818 __perf_event_mark_enabled(event, ctx);
1824 * Enable all of a task's events that have been marked enable-on-exec.
1825 * This expects task == current.
1827 static void perf_event_enable_on_exec(struct perf_event_context *ctx)
1829 struct perf_event *event;
1830 unsigned long flags;
1834 local_irq_save(flags);
1835 if (!ctx || !ctx->nr_events)
1838 task_ctx_sched_out(ctx, EVENT_ALL);
1840 raw_spin_lock(&ctx->lock);
1842 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
1843 ret = event_enable_on_exec(event, ctx);
1848 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
1849 ret = event_enable_on_exec(event, ctx);
1855 * Unclone this context if we enabled any event.
1860 raw_spin_unlock(&ctx->lock);
1862 perf_event_context_sched_in(ctx);
1864 local_irq_restore(flags);
1868 * Cross CPU call to read the hardware event
1870 static void __perf_event_read(void *info)
1872 struct perf_event *event = info;
1873 struct perf_event_context *ctx = event->ctx;
1874 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1877 * If this is a task context, we need to check whether it is
1878 * the current task context of this cpu. If not it has been
1879 * scheduled out before the smp call arrived. In that case
1880 * event->count would have been updated to a recent sample
1881 * when the event was scheduled out.
1883 if (ctx->task && cpuctx->task_ctx != ctx)
1886 raw_spin_lock(&ctx->lock);
1887 update_context_time(ctx);
1888 update_event_times(event);
1889 raw_spin_unlock(&ctx->lock);
1891 event->pmu->read(event);
1894 static inline u64 perf_event_count(struct perf_event *event)
1896 return local64_read(&event->count) + atomic64_read(&event->child_count);
1899 static u64 perf_event_read(struct perf_event *event)
1902 * If event is enabled and currently active on a CPU, update the
1903 * value in the event structure:
1905 if (event->state == PERF_EVENT_STATE_ACTIVE) {
1906 smp_call_function_single(event->oncpu,
1907 __perf_event_read, event, 1);
1908 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
1909 struct perf_event_context *ctx = event->ctx;
1910 unsigned long flags;
1912 raw_spin_lock_irqsave(&ctx->lock, flags);
1914 * may read while context is not active
1915 * (e.g., thread is blocked), in that case
1916 * we cannot update context time
1919 update_context_time(ctx);
1920 update_event_times(event);
1921 raw_spin_unlock_irqrestore(&ctx->lock, flags);
1924 return perf_event_count(event);
1931 struct callchain_cpus_entries {
1932 struct rcu_head rcu_head;
1933 struct perf_callchain_entry *cpu_entries[0];
1936 static DEFINE_PER_CPU(int, callchain_recursion[PERF_NR_CONTEXTS]);
1937 static atomic_t nr_callchain_events;
1938 static DEFINE_MUTEX(callchain_mutex);
1939 struct callchain_cpus_entries *callchain_cpus_entries;
1942 __weak void perf_callchain_kernel(struct perf_callchain_entry *entry,
1943 struct pt_regs *regs)
1947 __weak void perf_callchain_user(struct perf_callchain_entry *entry,
1948 struct pt_regs *regs)
1952 static void release_callchain_buffers_rcu(struct rcu_head *head)
1954 struct callchain_cpus_entries *entries;
1957 entries = container_of(head, struct callchain_cpus_entries, rcu_head);
1959 for_each_possible_cpu(cpu)
1960 kfree(entries->cpu_entries[cpu]);
1965 static void release_callchain_buffers(void)
1967 struct callchain_cpus_entries *entries;
1969 entries = callchain_cpus_entries;
1970 rcu_assign_pointer(callchain_cpus_entries, NULL);
1971 call_rcu(&entries->rcu_head, release_callchain_buffers_rcu);
1974 static int alloc_callchain_buffers(void)
1978 struct callchain_cpus_entries *entries;
1981 * We can't use the percpu allocation API for data that can be
1982 * accessed from NMI. Use a temporary manual per cpu allocation
1983 * until that gets sorted out.
1985 size = sizeof(*entries) + sizeof(struct perf_callchain_entry *) *
1986 num_possible_cpus();
1988 entries = kzalloc(size, GFP_KERNEL);
1992 size = sizeof(struct perf_callchain_entry) * PERF_NR_CONTEXTS;
1994 for_each_possible_cpu(cpu) {
1995 entries->cpu_entries[cpu] = kmalloc_node(size, GFP_KERNEL,
1997 if (!entries->cpu_entries[cpu])
2001 rcu_assign_pointer(callchain_cpus_entries, entries);
2006 for_each_possible_cpu(cpu)
2007 kfree(entries->cpu_entries[cpu]);
2013 static int get_callchain_buffers(void)
2018 mutex_lock(&callchain_mutex);
2020 count = atomic_inc_return(&nr_callchain_events);
2021 if (WARN_ON_ONCE(count < 1)) {
2027 /* If the allocation failed, give up */
2028 if (!callchain_cpus_entries)
2033 err = alloc_callchain_buffers();
2035 release_callchain_buffers();
2037 mutex_unlock(&callchain_mutex);
2042 static void put_callchain_buffers(void)
2044 if (atomic_dec_and_mutex_lock(&nr_callchain_events, &callchain_mutex)) {
2045 release_callchain_buffers();
2046 mutex_unlock(&callchain_mutex);
2050 static int get_recursion_context(int *recursion)
2058 else if (in_softirq())
2063 if (recursion[rctx])
2072 static inline void put_recursion_context(int *recursion, int rctx)
2078 static struct perf_callchain_entry *get_callchain_entry(int *rctx)
2081 struct callchain_cpus_entries *entries;
2083 *rctx = get_recursion_context(__get_cpu_var(callchain_recursion));
2087 entries = rcu_dereference(callchain_cpus_entries);
2091 cpu = smp_processor_id();
2093 return &entries->cpu_entries[cpu][*rctx];
2097 put_callchain_entry(int rctx)
2099 put_recursion_context(__get_cpu_var(callchain_recursion), rctx);
2102 static struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
2105 struct perf_callchain_entry *entry;
2108 entry = get_callchain_entry(&rctx);
2117 if (!user_mode(regs)) {
2118 perf_callchain_store(entry, PERF_CONTEXT_KERNEL);
2119 perf_callchain_kernel(entry, regs);
2121 regs = task_pt_regs(current);
2127 perf_callchain_store(entry, PERF_CONTEXT_USER);
2128 perf_callchain_user(entry, regs);
2132 put_callchain_entry(rctx);
2138 * Initialize the perf_event context in a task_struct:
2140 static void __perf_event_init_context(struct perf_event_context *ctx)
2142 raw_spin_lock_init(&ctx->lock);
2143 mutex_init(&ctx->mutex);
2144 INIT_LIST_HEAD(&ctx->pinned_groups);
2145 INIT_LIST_HEAD(&ctx->flexible_groups);
2146 INIT_LIST_HEAD(&ctx->event_list);
2147 atomic_set(&ctx->refcount, 1);
2150 static struct perf_event_context *
2151 alloc_perf_context(struct pmu *pmu, struct task_struct *task)
2153 struct perf_event_context *ctx;
2155 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
2159 __perf_event_init_context(ctx);
2162 get_task_struct(task);
2169 static struct task_struct *
2170 find_lively_task_by_vpid(pid_t vpid)
2172 struct task_struct *task;
2179 task = find_task_by_vpid(vpid);
2181 get_task_struct(task);
2185 return ERR_PTR(-ESRCH);
2188 * Can't attach events to a dying task.
2191 if (task->flags & PF_EXITING)
2194 /* Reuse ptrace permission checks for now. */
2196 if (!ptrace_may_access(task, PTRACE_MODE_READ))
2201 put_task_struct(task);
2202 return ERR_PTR(err);
2206 static struct perf_event_context *
2207 find_get_context(struct pmu *pmu, struct task_struct *task, int cpu)
2209 struct perf_event_context *ctx;
2210 struct perf_cpu_context *cpuctx;
2211 unsigned long flags;
2214 if (!task && cpu != -1) {
2215 /* Must be root to operate on a CPU event: */
2216 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
2217 return ERR_PTR(-EACCES);
2219 if (cpu < 0 || cpu >= nr_cpumask_bits)
2220 return ERR_PTR(-EINVAL);
2223 * We could be clever and allow to attach a event to an
2224 * offline CPU and activate it when the CPU comes up, but
2227 if (!cpu_online(cpu))
2228 return ERR_PTR(-ENODEV);
2230 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
2238 ctxn = pmu->task_ctx_nr;
2243 ctx = perf_lock_task_context(task, ctxn, &flags);
2246 raw_spin_unlock_irqrestore(&ctx->lock, flags);
2250 ctx = alloc_perf_context(pmu, task);
2257 if (cmpxchg(&task->perf_event_ctxp[ctxn], NULL, ctx)) {
2259 * We raced with some other task; use
2260 * the context they set.
2262 put_task_struct(task);
2271 return ERR_PTR(err);
2274 static void perf_event_free_filter(struct perf_event *event);
2276 static void free_event_rcu(struct rcu_head *head)
2278 struct perf_event *event;
2280 event = container_of(head, struct perf_event, rcu_head);
2282 put_pid_ns(event->ns);
2283 perf_event_free_filter(event);
2287 static void perf_buffer_put(struct perf_buffer *buffer);
2289 static void free_event(struct perf_event *event)
2291 irq_work_sync(&event->pending);
2293 if (!event->parent) {
2294 if (event->attach_state & PERF_ATTACH_TASK)
2295 jump_label_dec(&perf_task_events);
2296 if (event->attr.mmap || event->attr.mmap_data)
2297 atomic_dec(&nr_mmap_events);
2298 if (event->attr.comm)
2299 atomic_dec(&nr_comm_events);
2300 if (event->attr.task)
2301 atomic_dec(&nr_task_events);
2302 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
2303 put_callchain_buffers();
2306 if (event->buffer) {
2307 perf_buffer_put(event->buffer);
2308 event->buffer = NULL;
2312 event->destroy(event);
2315 put_ctx(event->ctx);
2317 call_rcu(&event->rcu_head, free_event_rcu);
2320 int perf_event_release_kernel(struct perf_event *event)
2322 struct perf_event_context *ctx = event->ctx;
2325 * Remove from the PMU, can't get re-enabled since we got
2326 * here because the last ref went.
2328 perf_event_disable(event);
2330 WARN_ON_ONCE(ctx->parent_ctx);
2332 * There are two ways this annotation is useful:
2334 * 1) there is a lock recursion from perf_event_exit_task
2335 * see the comment there.
2337 * 2) there is a lock-inversion with mmap_sem through
2338 * perf_event_read_group(), which takes faults while
2339 * holding ctx->mutex, however this is called after
2340 * the last filedesc died, so there is no possibility
2341 * to trigger the AB-BA case.
2343 mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
2344 raw_spin_lock_irq(&ctx->lock);
2345 perf_group_detach(event);
2346 list_del_event(event, ctx);
2347 raw_spin_unlock_irq(&ctx->lock);
2348 mutex_unlock(&ctx->mutex);
2354 EXPORT_SYMBOL_GPL(perf_event_release_kernel);
2357 * Called when the last reference to the file is gone.
2359 static int perf_release(struct inode *inode, struct file *file)
2361 struct perf_event *event = file->private_data;
2362 struct task_struct *owner;
2364 file->private_data = NULL;
2367 owner = ACCESS_ONCE(event->owner);
2369 * Matches the smp_wmb() in perf_event_exit_task(). If we observe
2370 * !owner it means the list deletion is complete and we can indeed
2371 * free this event, otherwise we need to serialize on
2372 * owner->perf_event_mutex.
2374 smp_read_barrier_depends();
2377 * Since delayed_put_task_struct() also drops the last
2378 * task reference we can safely take a new reference
2379 * while holding the rcu_read_lock().
2381 get_task_struct(owner);
2386 mutex_lock(&owner->perf_event_mutex);
2388 * We have to re-check the event->owner field, if it is cleared
2389 * we raced with perf_event_exit_task(), acquiring the mutex
2390 * ensured they're done, and we can proceed with freeing the
2394 list_del_init(&event->owner_entry);
2395 mutex_unlock(&owner->perf_event_mutex);
2396 put_task_struct(owner);
2399 return perf_event_release_kernel(event);
2402 u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
2404 struct perf_event *child;
2410 mutex_lock(&event->child_mutex);
2411 total += perf_event_read(event);
2412 *enabled += event->total_time_enabled +
2413 atomic64_read(&event->child_total_time_enabled);
2414 *running += event->total_time_running +
2415 atomic64_read(&event->child_total_time_running);
2417 list_for_each_entry(child, &event->child_list, child_list) {
2418 total += perf_event_read(child);
2419 *enabled += child->total_time_enabled;
2420 *running += child->total_time_running;
2422 mutex_unlock(&event->child_mutex);
2426 EXPORT_SYMBOL_GPL(perf_event_read_value);
2428 static int perf_event_read_group(struct perf_event *event,
2429 u64 read_format, char __user *buf)
2431 struct perf_event *leader = event->group_leader, *sub;
2432 int n = 0, size = 0, ret = -EFAULT;
2433 struct perf_event_context *ctx = leader->ctx;
2435 u64 count, enabled, running;
2437 mutex_lock(&ctx->mutex);
2438 count = perf_event_read_value(leader, &enabled, &running);
2440 values[n++] = 1 + leader->nr_siblings;
2441 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2442 values[n++] = enabled;
2443 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2444 values[n++] = running;
2445 values[n++] = count;
2446 if (read_format & PERF_FORMAT_ID)
2447 values[n++] = primary_event_id(leader);
2449 size = n * sizeof(u64);
2451 if (copy_to_user(buf, values, size))
2456 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
2459 values[n++] = perf_event_read_value(sub, &enabled, &running);
2460 if (read_format & PERF_FORMAT_ID)
2461 values[n++] = primary_event_id(sub);
2463 size = n * sizeof(u64);
2465 if (copy_to_user(buf + ret, values, size)) {
2473 mutex_unlock(&ctx->mutex);
2478 static int perf_event_read_one(struct perf_event *event,
2479 u64 read_format, char __user *buf)
2481 u64 enabled, running;
2485 values[n++] = perf_event_read_value(event, &enabled, &running);
2486 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2487 values[n++] = enabled;
2488 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2489 values[n++] = running;
2490 if (read_format & PERF_FORMAT_ID)
2491 values[n++] = primary_event_id(event);
2493 if (copy_to_user(buf, values, n * sizeof(u64)))
2496 return n * sizeof(u64);
2500 * Read the performance event - simple non blocking version for now
2503 perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
2505 u64 read_format = event->attr.read_format;
2509 * Return end-of-file for a read on a event that is in
2510 * error state (i.e. because it was pinned but it couldn't be
2511 * scheduled on to the CPU at some point).
2513 if (event->state == PERF_EVENT_STATE_ERROR)
2516 if (count < event->read_size)
2519 WARN_ON_ONCE(event->ctx->parent_ctx);
2520 if (read_format & PERF_FORMAT_GROUP)
2521 ret = perf_event_read_group(event, read_format, buf);
2523 ret = perf_event_read_one(event, read_format, buf);
2529 perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
2531 struct perf_event *event = file->private_data;
2533 return perf_read_hw(event, buf, count);
2536 static unsigned int perf_poll(struct file *file, poll_table *wait)
2538 struct perf_event *event = file->private_data;
2539 struct perf_buffer *buffer;
2540 unsigned int events = POLL_HUP;
2543 buffer = rcu_dereference(event->buffer);
2545 events = atomic_xchg(&buffer->poll, 0);
2548 poll_wait(file, &event->waitq, wait);
2553 static void perf_event_reset(struct perf_event *event)
2555 (void)perf_event_read(event);
2556 local64_set(&event->count, 0);
2557 perf_event_update_userpage(event);
2561 * Holding the top-level event's child_mutex means that any
2562 * descendant process that has inherited this event will block
2563 * in sync_child_event if it goes to exit, thus satisfying the
2564 * task existence requirements of perf_event_enable/disable.
2566 static void perf_event_for_each_child(struct perf_event *event,
2567 void (*func)(struct perf_event *))
2569 struct perf_event *child;
2571 WARN_ON_ONCE(event->ctx->parent_ctx);
2572 mutex_lock(&event->child_mutex);
2574 list_for_each_entry(child, &event->child_list, child_list)
2576 mutex_unlock(&event->child_mutex);
2579 static void perf_event_for_each(struct perf_event *event,
2580 void (*func)(struct perf_event *))
2582 struct perf_event_context *ctx = event->ctx;
2583 struct perf_event *sibling;
2585 WARN_ON_ONCE(ctx->parent_ctx);
2586 mutex_lock(&ctx->mutex);
2587 event = event->group_leader;
2589 perf_event_for_each_child(event, func);
2591 list_for_each_entry(sibling, &event->sibling_list, group_entry)
2592 perf_event_for_each_child(event, func);
2593 mutex_unlock(&ctx->mutex);
2596 static int perf_event_period(struct perf_event *event, u64 __user *arg)
2598 struct perf_event_context *ctx = event->ctx;
2602 if (!is_sampling_event(event))
2605 if (copy_from_user(&value, arg, sizeof(value)))
2611 raw_spin_lock_irq(&ctx->lock);
2612 if (event->attr.freq) {
2613 if (value > sysctl_perf_event_sample_rate) {
2618 event->attr.sample_freq = value;
2620 event->attr.sample_period = value;
2621 event->hw.sample_period = value;
2624 raw_spin_unlock_irq(&ctx->lock);
2629 static const struct file_operations perf_fops;
2631 static struct perf_event *perf_fget_light(int fd, int *fput_needed)
2635 file = fget_light(fd, fput_needed);
2637 return ERR_PTR(-EBADF);
2639 if (file->f_op != &perf_fops) {
2640 fput_light(file, *fput_needed);
2642 return ERR_PTR(-EBADF);
2645 return file->private_data;
2648 static int perf_event_set_output(struct perf_event *event,
2649 struct perf_event *output_event);
2650 static int perf_event_set_filter(struct perf_event *event, void __user *arg);
2652 static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2654 struct perf_event *event = file->private_data;
2655 void (*func)(struct perf_event *);
2659 case PERF_EVENT_IOC_ENABLE:
2660 func = perf_event_enable;
2662 case PERF_EVENT_IOC_DISABLE:
2663 func = perf_event_disable;
2665 case PERF_EVENT_IOC_RESET:
2666 func = perf_event_reset;
2669 case PERF_EVENT_IOC_REFRESH:
2670 return perf_event_refresh(event, arg);
2672 case PERF_EVENT_IOC_PERIOD:
2673 return perf_event_period(event, (u64 __user *)arg);
2675 case PERF_EVENT_IOC_SET_OUTPUT:
2677 struct perf_event *output_event = NULL;
2678 int fput_needed = 0;
2682 output_event = perf_fget_light(arg, &fput_needed);
2683 if (IS_ERR(output_event))
2684 return PTR_ERR(output_event);
2687 ret = perf_event_set_output(event, output_event);
2689 fput_light(output_event->filp, fput_needed);
2694 case PERF_EVENT_IOC_SET_FILTER:
2695 return perf_event_set_filter(event, (void __user *)arg);
2701 if (flags & PERF_IOC_FLAG_GROUP)
2702 perf_event_for_each(event, func);
2704 perf_event_for_each_child(event, func);
2709 int perf_event_task_enable(void)
2711 struct perf_event *event;
2713 mutex_lock(¤t->perf_event_mutex);
2714 list_for_each_entry(event, ¤t->perf_event_list, owner_entry)
2715 perf_event_for_each_child(event, perf_event_enable);
2716 mutex_unlock(¤t->perf_event_mutex);
2721 int perf_event_task_disable(void)
2723 struct perf_event *event;
2725 mutex_lock(¤t->perf_event_mutex);
2726 list_for_each_entry(event, ¤t->perf_event_list, owner_entry)
2727 perf_event_for_each_child(event, perf_event_disable);
2728 mutex_unlock(¤t->perf_event_mutex);
2733 #ifndef PERF_EVENT_INDEX_OFFSET
2734 # define PERF_EVENT_INDEX_OFFSET 0
2737 static int perf_event_index(struct perf_event *event)
2739 if (event->hw.state & PERF_HES_STOPPED)
2742 if (event->state != PERF_EVENT_STATE_ACTIVE)
2745 return event->hw.idx + 1 - PERF_EVENT_INDEX_OFFSET;
2749 * Callers need to ensure there can be no nesting of this function, otherwise
2750 * the seqlock logic goes bad. We can not serialize this because the arch
2751 * code calls this from NMI context.
2753 void perf_event_update_userpage(struct perf_event *event)
2755 struct perf_event_mmap_page *userpg;
2756 struct perf_buffer *buffer;
2759 buffer = rcu_dereference(event->buffer);
2763 userpg = buffer->user_page;
2766 * Disable preemption so as to not let the corresponding user-space
2767 * spin too long if we get preempted.
2772 userpg->index = perf_event_index(event);
2773 userpg->offset = perf_event_count(event);
2774 if (event->state == PERF_EVENT_STATE_ACTIVE)
2775 userpg->offset -= local64_read(&event->hw.prev_count);
2777 userpg->time_enabled = event->total_time_enabled +
2778 atomic64_read(&event->child_total_time_enabled);
2780 userpg->time_running = event->total_time_running +
2781 atomic64_read(&event->child_total_time_running);
2790 static unsigned long perf_data_size(struct perf_buffer *buffer);
2793 perf_buffer_init(struct perf_buffer *buffer, long watermark, int flags)
2795 long max_size = perf_data_size(buffer);
2798 buffer->watermark = min(max_size, watermark);
2800 if (!buffer->watermark)
2801 buffer->watermark = max_size / 2;
2803 if (flags & PERF_BUFFER_WRITABLE)
2804 buffer->writable = 1;
2806 atomic_set(&buffer->refcount, 1);
2809 #ifndef CONFIG_PERF_USE_VMALLOC
2812 * Back perf_mmap() with regular GFP_KERNEL-0 pages.
2815 static struct page *
2816 perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
2818 if (pgoff > buffer->nr_pages)
2822 return virt_to_page(buffer->user_page);
2824 return virt_to_page(buffer->data_pages[pgoff - 1]);
2827 static void *perf_mmap_alloc_page(int cpu)
2832 node = (cpu == -1) ? cpu : cpu_to_node(cpu);
2833 page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
2837 return page_address(page);
2840 static struct perf_buffer *
2841 perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
2843 struct perf_buffer *buffer;
2847 size = sizeof(struct perf_buffer);
2848 size += nr_pages * sizeof(void *);
2850 buffer = kzalloc(size, GFP_KERNEL);
2854 buffer->user_page = perf_mmap_alloc_page(cpu);
2855 if (!buffer->user_page)
2856 goto fail_user_page;
2858 for (i = 0; i < nr_pages; i++) {
2859 buffer->data_pages[i] = perf_mmap_alloc_page(cpu);
2860 if (!buffer->data_pages[i])
2861 goto fail_data_pages;
2864 buffer->nr_pages = nr_pages;
2866 perf_buffer_init(buffer, watermark, flags);
2871 for (i--; i >= 0; i--)
2872 free_page((unsigned long)buffer->data_pages[i]);
2874 free_page((unsigned long)buffer->user_page);
2883 static void perf_mmap_free_page(unsigned long addr)
2885 struct page *page = virt_to_page((void *)addr);
2887 page->mapping = NULL;
2891 static void perf_buffer_free(struct perf_buffer *buffer)
2895 perf_mmap_free_page((unsigned long)buffer->user_page);
2896 for (i = 0; i < buffer->nr_pages; i++)
2897 perf_mmap_free_page((unsigned long)buffer->data_pages[i]);
2901 static inline int page_order(struct perf_buffer *buffer)
2909 * Back perf_mmap() with vmalloc memory.
2911 * Required for architectures that have d-cache aliasing issues.
2914 static inline int page_order(struct perf_buffer *buffer)
2916 return buffer->page_order;
2919 static struct page *
2920 perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
2922 if (pgoff > (1UL << page_order(buffer)))
2925 return vmalloc_to_page((void *)buffer->user_page + pgoff * PAGE_SIZE);
2928 static void perf_mmap_unmark_page(void *addr)
2930 struct page *page = vmalloc_to_page(addr);
2932 page->mapping = NULL;
2935 static void perf_buffer_free_work(struct work_struct *work)
2937 struct perf_buffer *buffer;
2941 buffer = container_of(work, struct perf_buffer, work);
2942 nr = 1 << page_order(buffer);
2944 base = buffer->user_page;
2945 for (i = 0; i < nr + 1; i++)
2946 perf_mmap_unmark_page(base + (i * PAGE_SIZE));
2952 static void perf_buffer_free(struct perf_buffer *buffer)
2954 schedule_work(&buffer->work);
2957 static struct perf_buffer *
2958 perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
2960 struct perf_buffer *buffer;
2964 size = sizeof(struct perf_buffer);
2965 size += sizeof(void *);
2967 buffer = kzalloc(size, GFP_KERNEL);
2971 INIT_WORK(&buffer->work, perf_buffer_free_work);
2973 all_buf = vmalloc_user((nr_pages + 1) * PAGE_SIZE);
2977 buffer->user_page = all_buf;
2978 buffer->data_pages[0] = all_buf + PAGE_SIZE;
2979 buffer->page_order = ilog2(nr_pages);
2980 buffer->nr_pages = 1;
2982 perf_buffer_init(buffer, watermark, flags);
2995 static unsigned long perf_data_size(struct perf_buffer *buffer)
2997 return buffer->nr_pages << (PAGE_SHIFT + page_order(buffer));
3000 static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
3002 struct perf_event *event = vma->vm_file->private_data;
3003 struct perf_buffer *buffer;
3004 int ret = VM_FAULT_SIGBUS;
3006 if (vmf->flags & FAULT_FLAG_MKWRITE) {
3007 if (vmf->pgoff == 0)
3013 buffer = rcu_dereference(event->buffer);
3017 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
3020 vmf->page = perf_mmap_to_page(buffer, vmf->pgoff);
3024 get_page(vmf->page);
3025 vmf->page->mapping = vma->vm_file->f_mapping;
3026 vmf->page->index = vmf->pgoff;
3035 static void perf_buffer_free_rcu(struct rcu_head *rcu_head)
3037 struct perf_buffer *buffer;
3039 buffer = container_of(rcu_head, struct perf_buffer, rcu_head);
3040 perf_buffer_free(buffer);
3043 static struct perf_buffer *perf_buffer_get(struct perf_event *event)
3045 struct perf_buffer *buffer;
3048 buffer = rcu_dereference(event->buffer);
3050 if (!atomic_inc_not_zero(&buffer->refcount))
3058 static void perf_buffer_put(struct perf_buffer *buffer)
3060 if (!atomic_dec_and_test(&buffer->refcount))
3063 call_rcu(&buffer->rcu_head, perf_buffer_free_rcu);
3066 static void perf_mmap_open(struct vm_area_struct *vma)
3068 struct perf_event *event = vma->vm_file->private_data;
3070 atomic_inc(&event->mmap_count);
3073 static void perf_mmap_close(struct vm_area_struct *vma)
3075 struct perf_event *event = vma->vm_file->private_data;
3077 if (atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) {
3078 unsigned long size = perf_data_size(event->buffer);
3079 struct user_struct *user = event->mmap_user;
3080 struct perf_buffer *buffer = event->buffer;
3082 atomic_long_sub((size >> PAGE_SHIFT) + 1, &user->locked_vm);
3083 vma->vm_mm->locked_vm -= event->mmap_locked;
3084 rcu_assign_pointer(event->buffer, NULL);
3085 mutex_unlock(&event->mmap_mutex);
3087 perf_buffer_put(buffer);
3092 static const struct vm_operations_struct perf_mmap_vmops = {
3093 .open = perf_mmap_open,
3094 .close = perf_mmap_close,
3095 .fault = perf_mmap_fault,
3096 .page_mkwrite = perf_mmap_fault,
3099 static int perf_mmap(struct file *file, struct vm_area_struct *vma)
3101 struct perf_event *event = file->private_data;
3102 unsigned long user_locked, user_lock_limit;
3103 struct user_struct *user = current_user();
3104 unsigned long locked, lock_limit;
3105 struct perf_buffer *buffer;
3106 unsigned long vma_size;
3107 unsigned long nr_pages;
3108 long user_extra, extra;
3109 int ret = 0, flags = 0;
3112 * Don't allow mmap() of inherited per-task counters. This would
3113 * create a performance issue due to all children writing to the
3116 if (event->cpu == -1 && event->attr.inherit)
3119 if (!(vma->vm_flags & VM_SHARED))
3122 vma_size = vma->vm_end - vma->vm_start;
3123 nr_pages = (vma_size / PAGE_SIZE) - 1;
3126 * If we have buffer pages ensure they're a power-of-two number, so we
3127 * can do bitmasks instead of modulo.
3129 if (nr_pages != 0 && !is_power_of_2(nr_pages))
3132 if (vma_size != PAGE_SIZE * (1 + nr_pages))
3135 if (vma->vm_pgoff != 0)
3138 WARN_ON_ONCE(event->ctx->parent_ctx);
3139 mutex_lock(&event->mmap_mutex);
3140 if (event->buffer) {
3141 if (event->buffer->nr_pages == nr_pages)
3142 atomic_inc(&event->buffer->refcount);
3148 user_extra = nr_pages + 1;
3149 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
3152 * Increase the limit linearly with more CPUs:
3154 user_lock_limit *= num_online_cpus();
3156 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
3159 if (user_locked > user_lock_limit)
3160 extra = user_locked - user_lock_limit;
3162 lock_limit = rlimit(RLIMIT_MEMLOCK);
3163 lock_limit >>= PAGE_SHIFT;
3164 locked = vma->vm_mm->locked_vm + extra;
3166 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
3167 !capable(CAP_IPC_LOCK)) {
3172 WARN_ON(event->buffer);
3174 if (vma->vm_flags & VM_WRITE)
3175 flags |= PERF_BUFFER_WRITABLE;
3177 buffer = perf_buffer_alloc(nr_pages, event->attr.wakeup_watermark,
3183 rcu_assign_pointer(event->buffer, buffer);
3185 atomic_long_add(user_extra, &user->locked_vm);
3186 event->mmap_locked = extra;
3187 event->mmap_user = get_current_user();
3188 vma->vm_mm->locked_vm += event->mmap_locked;
3192 atomic_inc(&event->mmap_count);
3193 mutex_unlock(&event->mmap_mutex);
3195 vma->vm_flags |= VM_RESERVED;
3196 vma->vm_ops = &perf_mmap_vmops;
3201 static int perf_fasync(int fd, struct file *filp, int on)
3203 struct inode *inode = filp->f_path.dentry->d_inode;
3204 struct perf_event *event = filp->private_data;
3207 mutex_lock(&inode->i_mutex);
3208 retval = fasync_helper(fd, filp, on, &event->fasync);
3209 mutex_unlock(&inode->i_mutex);
3217 static const struct file_operations perf_fops = {
3218 .llseek = no_llseek,
3219 .release = perf_release,
3222 .unlocked_ioctl = perf_ioctl,
3223 .compat_ioctl = perf_ioctl,
3225 .fasync = perf_fasync,
3231 * If there's data, ensure we set the poll() state and publish everything
3232 * to user-space before waking everybody up.
3235 void perf_event_wakeup(struct perf_event *event)
3237 wake_up_all(&event->waitq);
3239 if (event->pending_kill) {
3240 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
3241 event->pending_kill = 0;
3245 static void perf_pending_event(struct irq_work *entry)
3247 struct perf_event *event = container_of(entry,
3248 struct perf_event, pending);
3250 if (event->pending_disable) {
3251 event->pending_disable = 0;
3252 __perf_event_disable(event);
3255 if (event->pending_wakeup) {
3256 event->pending_wakeup = 0;
3257 perf_event_wakeup(event);
3262 * We assume there is only KVM supporting the callbacks.
3263 * Later on, we might change it to a list if there is
3264 * another virtualization implementation supporting the callbacks.
3266 struct perf_guest_info_callbacks *perf_guest_cbs;
3268 int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3270 perf_guest_cbs = cbs;
3273 EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
3275 int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3277 perf_guest_cbs = NULL;
3280 EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
3285 static bool perf_output_space(struct perf_buffer *buffer, unsigned long tail,
3286 unsigned long offset, unsigned long head)
3290 if (!buffer->writable)
3293 mask = perf_data_size(buffer) - 1;
3295 offset = (offset - tail) & mask;
3296 head = (head - tail) & mask;
3298 if ((int)(head - offset) < 0)
3304 static void perf_output_wakeup(struct perf_output_handle *handle)
3306 atomic_set(&handle->buffer->poll, POLL_IN);
3309 handle->event->pending_wakeup = 1;
3310 irq_work_queue(&handle->event->pending);
3312 perf_event_wakeup(handle->event);
3316 * We need to ensure a later event_id doesn't publish a head when a former
3317 * event isn't done writing. However since we need to deal with NMIs we
3318 * cannot fully serialize things.
3320 * We only publish the head (and generate a wakeup) when the outer-most
3323 static void perf_output_get_handle(struct perf_output_handle *handle)
3325 struct perf_buffer *buffer = handle->buffer;
3328 local_inc(&buffer->nest);
3329 handle->wakeup = local_read(&buffer->wakeup);
3332 static void perf_output_put_handle(struct perf_output_handle *handle)
3334 struct perf_buffer *buffer = handle->buffer;
3338 head = local_read(&buffer->head);
3341 * IRQ/NMI can happen here, which means we can miss a head update.
3344 if (!local_dec_and_test(&buffer->nest))
3348 * Publish the known good head. Rely on the full barrier implied
3349 * by atomic_dec_and_test() order the buffer->head read and this
3352 buffer->user_page->data_head = head;
3355 * Now check if we missed an update, rely on the (compiler)
3356 * barrier in atomic_dec_and_test() to re-read buffer->head.
3358 if (unlikely(head != local_read(&buffer->head))) {
3359 local_inc(&buffer->nest);
3363 if (handle->wakeup != local_read(&buffer->wakeup))
3364 perf_output_wakeup(handle);
3370 __always_inline void perf_output_copy(struct perf_output_handle *handle,
3371 const void *buf, unsigned int len)
3374 unsigned long size = min_t(unsigned long, handle->size, len);
3376 memcpy(handle->addr, buf, size);
3379 handle->addr += size;
3381 handle->size -= size;
3382 if (!handle->size) {
3383 struct perf_buffer *buffer = handle->buffer;
3386 handle->page &= buffer->nr_pages - 1;
3387 handle->addr = buffer->data_pages[handle->page];
3388 handle->size = PAGE_SIZE << page_order(buffer);
3393 static void __perf_event_header__init_id(struct perf_event_header *header,
3394 struct perf_sample_data *data,
3395 struct perf_event *event)
3397 u64 sample_type = event->attr.sample_type;
3399 data->type = sample_type;
3400 header->size += event->id_header_size;
3402 if (sample_type & PERF_SAMPLE_TID) {
3403 /* namespace issues */
3404 data->tid_entry.pid = perf_event_pid(event, current);
3405 data->tid_entry.tid = perf_event_tid(event, current);
3408 if (sample_type & PERF_SAMPLE_TIME)
3409 data->time = perf_clock();
3411 if (sample_type & PERF_SAMPLE_ID)
3412 data->id = primary_event_id(event);
3414 if (sample_type & PERF_SAMPLE_STREAM_ID)
3415 data->stream_id = event->id;
3417 if (sample_type & PERF_SAMPLE_CPU) {
3418 data->cpu_entry.cpu = raw_smp_processor_id();
3419 data->cpu_entry.reserved = 0;
3423 static void perf_event_header__init_id(struct perf_event_header *header,
3424 struct perf_sample_data *data,
3425 struct perf_event *event)
3427 if (event->attr.sample_id_all)
3428 __perf_event_header__init_id(header, data, event);
3431 static void __perf_event__output_id_sample(struct perf_output_handle *handle,
3432 struct perf_sample_data *data)
3434 u64 sample_type = data->type;
3436 if (sample_type & PERF_SAMPLE_TID)
3437 perf_output_put(handle, data->tid_entry);
3439 if (sample_type & PERF_SAMPLE_TIME)
3440 perf_output_put(handle, data->time);
3442 if (sample_type & PERF_SAMPLE_ID)
3443 perf_output_put(handle, data->id);
3445 if (sample_type & PERF_SAMPLE_STREAM_ID)
3446 perf_output_put(handle, data->stream_id);
3448 if (sample_type & PERF_SAMPLE_CPU)
3449 perf_output_put(handle, data->cpu_entry);
3452 static void perf_event__output_id_sample(struct perf_event *event,
3453 struct perf_output_handle *handle,
3454 struct perf_sample_data *sample)
3456 if (event->attr.sample_id_all)
3457 __perf_event__output_id_sample(handle, sample);
3460 int perf_output_begin(struct perf_output_handle *handle,
3461 struct perf_event *event, unsigned int size,
3462 int nmi, int sample)
3464 struct perf_buffer *buffer;
3465 unsigned long tail, offset, head;
3467 struct perf_sample_data sample_data;
3469 struct perf_event_header header;
3476 * For inherited events we send all the output towards the parent.
3479 event = event->parent;
3481 buffer = rcu_dereference(event->buffer);
3485 handle->buffer = buffer;
3486 handle->event = event;
3488 handle->sample = sample;
3490 if (!buffer->nr_pages)
3493 have_lost = local_read(&buffer->lost);
3495 lost_event.header.size = sizeof(lost_event);
3496 perf_event_header__init_id(&lost_event.header, &sample_data,
3498 size += lost_event.header.size;
3501 perf_output_get_handle(handle);
3505 * Userspace could choose to issue a mb() before updating the
3506 * tail pointer. So that all reads will be completed before the
3509 tail = ACCESS_ONCE(buffer->user_page->data_tail);
3511 offset = head = local_read(&buffer->head);
3513 if (unlikely(!perf_output_space(buffer, tail, offset, head)))
3515 } while (local_cmpxchg(&buffer->head, offset, head) != offset);
3517 if (head - local_read(&buffer->wakeup) > buffer->watermark)
3518 local_add(buffer->watermark, &buffer->wakeup);
3520 handle->page = offset >> (PAGE_SHIFT + page_order(buffer));
3521 handle->page &= buffer->nr_pages - 1;
3522 handle->size = offset & ((PAGE_SIZE << page_order(buffer)) - 1);
3523 handle->addr = buffer->data_pages[handle->page];
3524 handle->addr += handle->size;
3525 handle->size = (PAGE_SIZE << page_order(buffer)) - handle->size;
3528 lost_event.header.type = PERF_RECORD_LOST;
3529 lost_event.header.misc = 0;
3530 lost_event.id = event->id;
3531 lost_event.lost = local_xchg(&buffer->lost, 0);
3533 perf_output_put(handle, lost_event);
3534 perf_event__output_id_sample(event, handle, &sample_data);
3540 local_inc(&buffer->lost);
3541 perf_output_put_handle(handle);
3548 void perf_output_end(struct perf_output_handle *handle)
3550 struct perf_event *event = handle->event;
3551 struct perf_buffer *buffer = handle->buffer;
3553 int wakeup_events = event->attr.wakeup_events;
3555 if (handle->sample && wakeup_events) {
3556 int events = local_inc_return(&buffer->events);
3557 if (events >= wakeup_events) {
3558 local_sub(wakeup_events, &buffer->events);
3559 local_inc(&buffer->wakeup);
3563 perf_output_put_handle(handle);
3567 static void perf_output_read_one(struct perf_output_handle *handle,
3568 struct perf_event *event,
3569 u64 enabled, u64 running)
3571 u64 read_format = event->attr.read_format;
3575 values[n++] = perf_event_count(event);
3576 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
3577 values[n++] = enabled +
3578 atomic64_read(&event->child_total_time_enabled);
3580 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
3581 values[n++] = running +
3582 atomic64_read(&event->child_total_time_running);
3584 if (read_format & PERF_FORMAT_ID)
3585 values[n++] = primary_event_id(event);
3587 perf_output_copy(handle, values, n * sizeof(u64));
3591 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
3593 static void perf_output_read_group(struct perf_output_handle *handle,
3594 struct perf_event *event,
3595 u64 enabled, u64 running)
3597 struct perf_event *leader = event->group_leader, *sub;
3598 u64 read_format = event->attr.read_format;
3602 values[n++] = 1 + leader->nr_siblings;
3604 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3605 values[n++] = enabled;
3607 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3608 values[n++] = running;
3610 if (leader != event)
3611 leader->pmu->read(leader);
3613 values[n++] = perf_event_count(leader);
3614 if (read_format & PERF_FORMAT_ID)
3615 values[n++] = primary_event_id(leader);
3617 perf_output_copy(handle, values, n * sizeof(u64));
3619 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
3623 sub->pmu->read(sub);
3625 values[n++] = perf_event_count(sub);
3626 if (read_format & PERF_FORMAT_ID)
3627 values[n++] = primary_event_id(sub);
3629 perf_output_copy(handle, values, n * sizeof(u64));
3633 #define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
3634 PERF_FORMAT_TOTAL_TIME_RUNNING)
3636 static void perf_output_read(struct perf_output_handle *handle,
3637 struct perf_event *event)
3639 u64 enabled = 0, running = 0, now, ctx_time;
3640 u64 read_format = event->attr.read_format;
3643 * compute total_time_enabled, total_time_running
3644 * based on snapshot values taken when the event
3645 * was last scheduled in.
3647 * we cannot simply called update_context_time()
3648 * because of locking issue as we are called in
3651 if (read_format & PERF_FORMAT_TOTAL_TIMES) {
3653 ctx_time = event->shadow_ctx_time + now;
3654 enabled = ctx_time - event->tstamp_enabled;
3655 running = ctx_time - event->tstamp_running;
3658 if (event->attr.read_format & PERF_FORMAT_GROUP)
3659 perf_output_read_group(handle, event, enabled, running);
3661 perf_output_read_one(handle, event, enabled, running);
3664 void perf_output_sample(struct perf_output_handle *handle,
3665 struct perf_event_header *header,
3666 struct perf_sample_data *data,
3667 struct perf_event *event)
3669 u64 sample_type = data->type;
3671 perf_output_put(handle, *header);
3673 if (sample_type & PERF_SAMPLE_IP)
3674 perf_output_put(handle, data->ip);
3676 if (sample_type & PERF_SAMPLE_TID)
3677 perf_output_put(handle, data->tid_entry);
3679 if (sample_type & PERF_SAMPLE_TIME)
3680 perf_output_put(handle, data->time);
3682 if (sample_type & PERF_SAMPLE_ADDR)
3683 perf_output_put(handle, data->addr);
3685 if (sample_type & PERF_SAMPLE_ID)
3686 perf_output_put(handle, data->id);
3688 if (sample_type & PERF_SAMPLE_STREAM_ID)
3689 perf_output_put(handle, data->stream_id);
3691 if (sample_type & PERF_SAMPLE_CPU)
3692 perf_output_put(handle, data->cpu_entry);
3694 if (sample_type & PERF_SAMPLE_PERIOD)
3695 perf_output_put(handle, data->period);
3697 if (sample_type & PERF_SAMPLE_READ)
3698 perf_output_read(handle, event);
3700 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3701 if (data->callchain) {
3704 if (data->callchain)
3705 size += data->callchain->nr;
3707 size *= sizeof(u64);
3709 perf_output_copy(handle, data->callchain, size);
3712 perf_output_put(handle, nr);
3716 if (sample_type & PERF_SAMPLE_RAW) {
3718 perf_output_put(handle, data->raw->size);
3719 perf_output_copy(handle, data->raw->data,
3726 .size = sizeof(u32),
3729 perf_output_put(handle, raw);
3734 void perf_prepare_sample(struct perf_event_header *header,
3735 struct perf_sample_data *data,
3736 struct perf_event *event,
3737 struct pt_regs *regs)
3739 u64 sample_type = event->attr.sample_type;
3741 header->type = PERF_RECORD_SAMPLE;
3742 header->size = sizeof(*header) + event->header_size;
3745 header->misc |= perf_misc_flags(regs);
3747 __perf_event_header__init_id(header, data, event);
3749 if (sample_type & PERF_SAMPLE_IP)
3750 data->ip = perf_instruction_pointer(regs);
3752 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3755 data->callchain = perf_callchain(regs);
3757 if (data->callchain)
3758 size += data->callchain->nr;
3760 header->size += size * sizeof(u64);
3763 if (sample_type & PERF_SAMPLE_RAW) {
3764 int size = sizeof(u32);
3767 size += data->raw->size;
3769 size += sizeof(u32);
3771 WARN_ON_ONCE(size & (sizeof(u64)-1));
3772 header->size += size;
3776 static void perf_event_output(struct perf_event *event, int nmi,
3777 struct perf_sample_data *data,
3778 struct pt_regs *regs)
3780 struct perf_output_handle handle;
3781 struct perf_event_header header;
3783 /* protect the callchain buffers */
3786 perf_prepare_sample(&header, data, event, regs);
3788 if (perf_output_begin(&handle, event, header.size, nmi, 1))
3791 perf_output_sample(&handle, &header, data, event);
3793 perf_output_end(&handle);
3803 struct perf_read_event {
3804 struct perf_event_header header;
3811 perf_event_read_event(struct perf_event *event,
3812 struct task_struct *task)
3814 struct perf_output_handle handle;
3815 struct perf_sample_data sample;
3816 struct perf_read_event read_event = {
3818 .type = PERF_RECORD_READ,
3820 .size = sizeof(read_event) + event->read_size,
3822 .pid = perf_event_pid(event, task),
3823 .tid = perf_event_tid(event, task),
3827 perf_event_header__init_id(&read_event.header, &sample, event);
3828 ret = perf_output_begin(&handle, event, read_event.header.size, 0, 0);
3832 perf_output_put(&handle, read_event);
3833 perf_output_read(&handle, event);
3834 perf_event__output_id_sample(event, &handle, &sample);
3836 perf_output_end(&handle);
3840 * task tracking -- fork/exit
3842 * enabled by: attr.comm | attr.mmap | attr.mmap_data | attr.task
3845 struct perf_task_event {
3846 struct task_struct *task;
3847 struct perf_event_context *task_ctx;
3850 struct perf_event_header header;
3860 static void perf_event_task_output(struct perf_event *event,
3861 struct perf_task_event *task_event)
3863 struct perf_output_handle handle;
3864 struct perf_sample_data sample;
3865 struct task_struct *task = task_event->task;
3866 int ret, size = task_event->event_id.header.size;
3868 perf_event_header__init_id(&task_event->event_id.header, &sample, event);
3870 ret = perf_output_begin(&handle, event,
3871 task_event->event_id.header.size, 0, 0);
3875 task_event->event_id.pid = perf_event_pid(event, task);
3876 task_event->event_id.ppid = perf_event_pid(event, current);
3878 task_event->event_id.tid = perf_event_tid(event, task);
3879 task_event->event_id.ptid = perf_event_tid(event, current);
3881 perf_output_put(&handle, task_event->event_id);
3883 perf_event__output_id_sample(event, &handle, &sample);
3885 perf_output_end(&handle);
3887 task_event->event_id.header.size = size;
3890 static int perf_event_task_match(struct perf_event *event)
3892 if (event->state < PERF_EVENT_STATE_INACTIVE)
3895 if (event->cpu != -1 && event->cpu != smp_processor_id())
3898 if (event->attr.comm || event->attr.mmap ||
3899 event->attr.mmap_data || event->attr.task)
3905 static void perf_event_task_ctx(struct perf_event_context *ctx,
3906 struct perf_task_event *task_event)
3908 struct perf_event *event;
3910 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3911 if (perf_event_task_match(event))
3912 perf_event_task_output(event, task_event);
3916 static void perf_event_task_event(struct perf_task_event *task_event)
3918 struct perf_cpu_context *cpuctx;
3919 struct perf_event_context *ctx;
3924 list_for_each_entry_rcu(pmu, &pmus, entry) {
3925 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
3926 if (cpuctx->active_pmu != pmu)
3928 perf_event_task_ctx(&cpuctx->ctx, task_event);
3930 ctx = task_event->task_ctx;
3932 ctxn = pmu->task_ctx_nr;
3935 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
3938 perf_event_task_ctx(ctx, task_event);
3940 put_cpu_ptr(pmu->pmu_cpu_context);
3945 static void perf_event_task(struct task_struct *task,
3946 struct perf_event_context *task_ctx,
3949 struct perf_task_event task_event;
3951 if (!atomic_read(&nr_comm_events) &&
3952 !atomic_read(&nr_mmap_events) &&
3953 !atomic_read(&nr_task_events))
3956 task_event = (struct perf_task_event){
3958 .task_ctx = task_ctx,
3961 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
3963 .size = sizeof(task_event.event_id),
3969 .time = perf_clock(),
3973 perf_event_task_event(&task_event);
3976 void perf_event_fork(struct task_struct *task)
3978 perf_event_task(task, NULL, 1);
3985 struct perf_comm_event {
3986 struct task_struct *task;
3991 struct perf_event_header header;
3998 static void perf_event_comm_output(struct perf_event *event,
3999 struct perf_comm_event *comm_event)
4001 struct perf_output_handle handle;
4002 struct perf_sample_data sample;
4003 int size = comm_event->event_id.header.size;
4006 perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
4007 ret = perf_output_begin(&handle, event,
4008 comm_event->event_id.header.size, 0, 0);
4013 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
4014 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
4016 perf_output_put(&handle, comm_event->event_id);
4017 perf_output_copy(&handle, comm_event->comm,
4018 comm_event->comm_size);
4020 perf_event__output_id_sample(event, &handle, &sample);
4022 perf_output_end(&handle);
4024 comm_event->event_id.header.size = size;
4027 static int perf_event_comm_match(struct perf_event *event)
4029 if (event->state < PERF_EVENT_STATE_INACTIVE)
4032 if (event->cpu != -1 && event->cpu != smp_processor_id())
4035 if (event->attr.comm)
4041 static void perf_event_comm_ctx(struct perf_event_context *ctx,
4042 struct perf_comm_event *comm_event)
4044 struct perf_event *event;
4046 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4047 if (perf_event_comm_match(event))
4048 perf_event_comm_output(event, comm_event);
4052 static void perf_event_comm_event(struct perf_comm_event *comm_event)
4054 struct perf_cpu_context *cpuctx;
4055 struct perf_event_context *ctx;
4056 char comm[TASK_COMM_LEN];
4061 memset(comm, 0, sizeof(comm));
4062 strlcpy(comm, comm_event->task->comm, sizeof(comm));
4063 size = ALIGN(strlen(comm)+1, sizeof(u64));
4065 comm_event->comm = comm;
4066 comm_event->comm_size = size;
4068 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
4070 list_for_each_entry_rcu(pmu, &pmus, entry) {
4071 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
4072 if (cpuctx->active_pmu != pmu)
4074 perf_event_comm_ctx(&cpuctx->ctx, comm_event);
4076 ctxn = pmu->task_ctx_nr;
4080 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4082 perf_event_comm_ctx(ctx, comm_event);
4084 put_cpu_ptr(pmu->pmu_cpu_context);
4089 void perf_event_comm(struct task_struct *task)
4091 struct perf_comm_event comm_event;
4092 struct perf_event_context *ctx;
4095 for_each_task_context_nr(ctxn) {
4096 ctx = task->perf_event_ctxp[ctxn];
4100 perf_event_enable_on_exec(ctx);
4103 if (!atomic_read(&nr_comm_events))
4106 comm_event = (struct perf_comm_event){
4112 .type = PERF_RECORD_COMM,
4121 perf_event_comm_event(&comm_event);
4128 struct perf_mmap_event {
4129 struct vm_area_struct *vma;
4131 const char *file_name;
4135 struct perf_event_header header;
4145 static void perf_event_mmap_output(struct perf_event *event,
4146 struct perf_mmap_event *mmap_event)
4148 struct perf_output_handle handle;
4149 struct perf_sample_data sample;
4150 int size = mmap_event->event_id.header.size;
4153 perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
4154 ret = perf_output_begin(&handle, event,
4155 mmap_event->event_id.header.size, 0, 0);
4159 mmap_event->event_id.pid = perf_event_pid(event, current);
4160 mmap_event->event_id.tid = perf_event_tid(event, current);
4162 perf_output_put(&handle, mmap_event->event_id);
4163 perf_output_copy(&handle, mmap_event->file_name,
4164 mmap_event->file_size);
4166 perf_event__output_id_sample(event, &handle, &sample);
4168 perf_output_end(&handle);
4170 mmap_event->event_id.header.size = size;
4173 static int perf_event_mmap_match(struct perf_event *event,
4174 struct perf_mmap_event *mmap_event,
4177 if (event->state < PERF_EVENT_STATE_INACTIVE)
4180 if (event->cpu != -1 && event->cpu != smp_processor_id())
4183 if ((!executable && event->attr.mmap_data) ||
4184 (executable && event->attr.mmap))
4190 static void perf_event_mmap_ctx(struct perf_event_context *ctx,
4191 struct perf_mmap_event *mmap_event,
4194 struct perf_event *event;
4196 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4197 if (perf_event_mmap_match(event, mmap_event, executable))
4198 perf_event_mmap_output(event, mmap_event);
4202 static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
4204 struct perf_cpu_context *cpuctx;
4205 struct perf_event_context *ctx;
4206 struct vm_area_struct *vma = mmap_event->vma;
4207 struct file *file = vma->vm_file;
4215 memset(tmp, 0, sizeof(tmp));
4219 * d_path works from the end of the buffer backwards, so we
4220 * need to add enough zero bytes after the string to handle
4221 * the 64bit alignment we do later.
4223 buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
4225 name = strncpy(tmp, "//enomem", sizeof(tmp));
4228 name = d_path(&file->f_path, buf, PATH_MAX);
4230 name = strncpy(tmp, "//toolong", sizeof(tmp));
4234 if (arch_vma_name(mmap_event->vma)) {
4235 name = strncpy(tmp, arch_vma_name(mmap_event->vma),
4241 name = strncpy(tmp, "[vdso]", sizeof(tmp));
4243 } else if (vma->vm_start <= vma->vm_mm->start_brk &&
4244 vma->vm_end >= vma->vm_mm->brk) {
4245 name = strncpy(tmp, "[heap]", sizeof(tmp));
4247 } else if (vma->vm_start <= vma->vm_mm->start_stack &&
4248 vma->vm_end >= vma->vm_mm->start_stack) {
4249 name = strncpy(tmp, "[stack]", sizeof(tmp));
4253 name = strncpy(tmp, "//anon", sizeof(tmp));
4258 size = ALIGN(strlen(name)+1, sizeof(u64));
4260 mmap_event->file_name = name;
4261 mmap_event->file_size = size;
4263 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
4266 list_for_each_entry_rcu(pmu, &pmus, entry) {
4267 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
4268 if (cpuctx->active_pmu != pmu)
4270 perf_event_mmap_ctx(&cpuctx->ctx, mmap_event,
4271 vma->vm_flags & VM_EXEC);
4273 ctxn = pmu->task_ctx_nr;
4277 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4279 perf_event_mmap_ctx(ctx, mmap_event,
4280 vma->vm_flags & VM_EXEC);
4283 put_cpu_ptr(pmu->pmu_cpu_context);
4290 void perf_event_mmap(struct vm_area_struct *vma)
4292 struct perf_mmap_event mmap_event;
4294 if (!atomic_read(&nr_mmap_events))
4297 mmap_event = (struct perf_mmap_event){
4303 .type = PERF_RECORD_MMAP,
4304 .misc = PERF_RECORD_MISC_USER,
4309 .start = vma->vm_start,
4310 .len = vma->vm_end - vma->vm_start,
4311 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
4315 perf_event_mmap_event(&mmap_event);
4319 * IRQ throttle logging
4322 static void perf_log_throttle(struct perf_event *event, int enable)
4324 struct perf_output_handle handle;
4325 struct perf_sample_data sample;
4329 struct perf_event_header header;
4333 } throttle_event = {
4335 .type = PERF_RECORD_THROTTLE,
4337 .size = sizeof(throttle_event),
4339 .time = perf_clock(),
4340 .id = primary_event_id(event),
4341 .stream_id = event->id,
4345 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
4347 perf_event_header__init_id(&throttle_event.header, &sample, event);
4349 ret = perf_output_begin(&handle, event,
4350 throttle_event.header.size, 1, 0);
4354 perf_output_put(&handle, throttle_event);
4355 perf_event__output_id_sample(event, &handle, &sample);
4356 perf_output_end(&handle);
4360 * Generic event overflow handling, sampling.
4363 static int __perf_event_overflow(struct perf_event *event, int nmi,
4364 int throttle, struct perf_sample_data *data,
4365 struct pt_regs *regs)
4367 int events = atomic_read(&event->event_limit);
4368 struct hw_perf_event *hwc = &event->hw;
4372 * Non-sampling counters might still use the PMI to fold short
4373 * hardware counters, ignore those.
4375 if (unlikely(!is_sampling_event(event)))
4381 if (hwc->interrupts != MAX_INTERRUPTS) {
4383 if (HZ * hwc->interrupts >
4384 (u64)sysctl_perf_event_sample_rate) {
4385 hwc->interrupts = MAX_INTERRUPTS;
4386 perf_log_throttle(event, 0);
4391 * Keep re-disabling events even though on the previous
4392 * pass we disabled it - just in case we raced with a
4393 * sched-in and the event got enabled again:
4399 if (event->attr.freq) {
4400 u64 now = perf_clock();
4401 s64 delta = now - hwc->freq_time_stamp;
4403 hwc->freq_time_stamp = now;
4405 if (delta > 0 && delta < 2*TICK_NSEC)
4406 perf_adjust_period(event, delta, hwc->last_period);
4410 * XXX event_limit might not quite work as expected on inherited
4414 event->pending_kill = POLL_IN;
4415 if (events && atomic_dec_and_test(&event->event_limit)) {
4417 event->pending_kill = POLL_HUP;
4419 event->pending_disable = 1;
4420 irq_work_queue(&event->pending);
4422 perf_event_disable(event);
4425 if (event->overflow_handler)
4426 event->overflow_handler(event, nmi, data, regs);
4428 perf_event_output(event, nmi, data, regs);
4433 int perf_event_overflow(struct perf_event *event, int nmi,
4434 struct perf_sample_data *data,
4435 struct pt_regs *regs)
4437 return __perf_event_overflow(event, nmi, 1, data, regs);
4441 * Generic software event infrastructure
4444 struct swevent_htable {
4445 struct swevent_hlist *swevent_hlist;
4446 struct mutex hlist_mutex;
4449 /* Recursion avoidance in each contexts */
4450 int recursion[PERF_NR_CONTEXTS];
4453 static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
4456 * We directly increment event->count and keep a second value in
4457 * event->hw.period_left to count intervals. This period event
4458 * is kept in the range [-sample_period, 0] so that we can use the
4462 static u64 perf_swevent_set_period(struct perf_event *event)
4464 struct hw_perf_event *hwc = &event->hw;
4465 u64 period = hwc->last_period;
4469 hwc->last_period = hwc->sample_period;
4472 old = val = local64_read(&hwc->period_left);
4476 nr = div64_u64(period + val, period);
4477 offset = nr * period;
4479 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
4485 static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
4486 int nmi, struct perf_sample_data *data,
4487 struct pt_regs *regs)
4489 struct hw_perf_event *hwc = &event->hw;
4492 data->period = event->hw.last_period;
4494 overflow = perf_swevent_set_period(event);
4496 if (hwc->interrupts == MAX_INTERRUPTS)
4499 for (; overflow; overflow--) {
4500 if (__perf_event_overflow(event, nmi, throttle,
4503 * We inhibit the overflow from happening when
4504 * hwc->interrupts == MAX_INTERRUPTS.
4512 static void perf_swevent_event(struct perf_event *event, u64 nr,
4513 int nmi, struct perf_sample_data *data,
4514 struct pt_regs *regs)
4516 struct hw_perf_event *hwc = &event->hw;
4518 local64_add(nr, &event->count);
4523 if (!is_sampling_event(event))
4526 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
4527 return perf_swevent_overflow(event, 1, nmi, data, regs);
4529 if (local64_add_negative(nr, &hwc->period_left))
4532 perf_swevent_overflow(event, 0, nmi, data, regs);
4535 static int perf_exclude_event(struct perf_event *event,
4536 struct pt_regs *regs)
4538 if (event->hw.state & PERF_HES_STOPPED)
4542 if (event->attr.exclude_user && user_mode(regs))
4545 if (event->attr.exclude_kernel && !user_mode(regs))
4552 static int perf_swevent_match(struct perf_event *event,
4553 enum perf_type_id type,
4555 struct perf_sample_data *data,
4556 struct pt_regs *regs)
4558 if (event->attr.type != type)
4561 if (event->attr.config != event_id)
4564 if (perf_exclude_event(event, regs))
4570 static inline u64 swevent_hash(u64 type, u32 event_id)
4572 u64 val = event_id | (type << 32);
4574 return hash_64(val, SWEVENT_HLIST_BITS);
4577 static inline struct hlist_head *
4578 __find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
4580 u64 hash = swevent_hash(type, event_id);
4582 return &hlist->heads[hash];
4585 /* For the read side: events when they trigger */
4586 static inline struct hlist_head *
4587 find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
4589 struct swevent_hlist *hlist;
4591 hlist = rcu_dereference(swhash->swevent_hlist);
4595 return __find_swevent_head(hlist, type, event_id);
4598 /* For the event head insertion and removal in the hlist */
4599 static inline struct hlist_head *
4600 find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
4602 struct swevent_hlist *hlist;
4603 u32 event_id = event->attr.config;
4604 u64 type = event->attr.type;
4607 * Event scheduling is always serialized against hlist allocation
4608 * and release. Which makes the protected version suitable here.
4609 * The context lock guarantees that.
4611 hlist = rcu_dereference_protected(swhash->swevent_hlist,
4612 lockdep_is_held(&event->ctx->lock));
4616 return __find_swevent_head(hlist, type, event_id);
4619 static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
4621 struct perf_sample_data *data,
4622 struct pt_regs *regs)
4624 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
4625 struct perf_event *event;
4626 struct hlist_node *node;
4627 struct hlist_head *head;
4630 head = find_swevent_head_rcu(swhash, type, event_id);
4634 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
4635 if (perf_swevent_match(event, type, event_id, data, regs))
4636 perf_swevent_event(event, nr, nmi, data, regs);
4642 int perf_swevent_get_recursion_context(void)
4644 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
4646 return get_recursion_context(swhash->recursion);
4648 EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
4650 void inline perf_swevent_put_recursion_context(int rctx)
4652 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
4654 put_recursion_context(swhash->recursion, rctx);
4657 void __perf_sw_event(u32 event_id, u64 nr, int nmi,
4658 struct pt_regs *regs, u64 addr)
4660 struct perf_sample_data data;
4663 preempt_disable_notrace();
4664 rctx = perf_swevent_get_recursion_context();
4668 perf_sample_data_init(&data, addr);
4670 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, nmi, &data, regs);
4672 perf_swevent_put_recursion_context(rctx);
4673 preempt_enable_notrace();
4676 static void perf_swevent_read(struct perf_event *event)
4680 static int perf_swevent_add(struct perf_event *event, int flags)
4682 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
4683 struct hw_perf_event *hwc = &event->hw;
4684 struct hlist_head *head;
4686 if (is_sampling_event(event)) {
4687 hwc->last_period = hwc->sample_period;
4688 perf_swevent_set_period(event);
4691 hwc->state = !(flags & PERF_EF_START);
4693 head = find_swevent_head(swhash, event);
4694 if (WARN_ON_ONCE(!head))
4697 hlist_add_head_rcu(&event->hlist_entry, head);
4702 static void perf_swevent_del(struct perf_event *event, int flags)
4704 hlist_del_rcu(&event->hlist_entry);
4707 static void perf_swevent_start(struct perf_event *event, int flags)
4709 event->hw.state = 0;
4712 static void perf_swevent_stop(struct perf_event *event, int flags)
4714 event->hw.state = PERF_HES_STOPPED;
4717 /* Deref the hlist from the update side */
4718 static inline struct swevent_hlist *
4719 swevent_hlist_deref(struct swevent_htable *swhash)
4721 return rcu_dereference_protected(swhash->swevent_hlist,
4722 lockdep_is_held(&swhash->hlist_mutex));
4725 static void swevent_hlist_release_rcu(struct rcu_head *rcu_head)
4727 struct swevent_hlist *hlist;
4729 hlist = container_of(rcu_head, struct swevent_hlist, rcu_head);
4733 static void swevent_hlist_release(struct swevent_htable *swhash)
4735 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
4740 rcu_assign_pointer(swhash->swevent_hlist, NULL);
4741 call_rcu(&hlist->rcu_head, swevent_hlist_release_rcu);
4744 static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
4746 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
4748 mutex_lock(&swhash->hlist_mutex);
4750 if (!--swhash->hlist_refcount)
4751 swevent_hlist_release(swhash);
4753 mutex_unlock(&swhash->hlist_mutex);
4756 static void swevent_hlist_put(struct perf_event *event)
4760 if (event->cpu != -1) {
4761 swevent_hlist_put_cpu(event, event->cpu);
4765 for_each_possible_cpu(cpu)
4766 swevent_hlist_put_cpu(event, cpu);
4769 static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
4771 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
4774 mutex_lock(&swhash->hlist_mutex);
4776 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
4777 struct swevent_hlist *hlist;
4779 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
4784 rcu_assign_pointer(swhash->swevent_hlist, hlist);
4786 swhash->hlist_refcount++;
4788 mutex_unlock(&swhash->hlist_mutex);
4793 static int swevent_hlist_get(struct perf_event *event)
4796 int cpu, failed_cpu;
4798 if (event->cpu != -1)
4799 return swevent_hlist_get_cpu(event, event->cpu);
4802 for_each_possible_cpu(cpu) {
4803 err = swevent_hlist_get_cpu(event, cpu);
4813 for_each_possible_cpu(cpu) {
4814 if (cpu == failed_cpu)
4816 swevent_hlist_put_cpu(event, cpu);
4823 atomic_t perf_swevent_enabled[PERF_COUNT_SW_MAX];
4825 static void sw_perf_event_destroy(struct perf_event *event)
4827 u64 event_id = event->attr.config;
4829 WARN_ON(event->parent);
4831 jump_label_dec(&perf_swevent_enabled[event_id]);
4832 swevent_hlist_put(event);
4835 static int perf_swevent_init(struct perf_event *event)
4837 int event_id = event->attr.config;
4839 if (event->attr.type != PERF_TYPE_SOFTWARE)
4843 case PERF_COUNT_SW_CPU_CLOCK:
4844 case PERF_COUNT_SW_TASK_CLOCK:
4851 if (event_id >= PERF_COUNT_SW_MAX)
4854 if (!event->parent) {
4857 err = swevent_hlist_get(event);
4861 jump_label_inc(&perf_swevent_enabled[event_id]);
4862 event->destroy = sw_perf_event_destroy;
4868 static struct pmu perf_swevent = {
4869 .task_ctx_nr = perf_sw_context,
4871 .event_init = perf_swevent_init,
4872 .add = perf_swevent_add,
4873 .del = perf_swevent_del,
4874 .start = perf_swevent_start,
4875 .stop = perf_swevent_stop,
4876 .read = perf_swevent_read,
4879 #ifdef CONFIG_EVENT_TRACING
4881 static int perf_tp_filter_match(struct perf_event *event,
4882 struct perf_sample_data *data)
4884 void *record = data->raw->data;
4886 if (likely(!event->filter) || filter_match_preds(event->filter, record))
4891 static int perf_tp_event_match(struct perf_event *event,
4892 struct perf_sample_data *data,
4893 struct pt_regs *regs)
4896 * All tracepoints are from kernel-space.
4898 if (event->attr.exclude_kernel)
4901 if (!perf_tp_filter_match(event, data))
4907 void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
4908 struct pt_regs *regs, struct hlist_head *head, int rctx)
4910 struct perf_sample_data data;
4911 struct perf_event *event;
4912 struct hlist_node *node;
4914 struct perf_raw_record raw = {
4919 perf_sample_data_init(&data, addr);
4922 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
4923 if (perf_tp_event_match(event, &data, regs))
4924 perf_swevent_event(event, count, 1, &data, regs);
4927 perf_swevent_put_recursion_context(rctx);
4929 EXPORT_SYMBOL_GPL(perf_tp_event);
4931 static void tp_perf_event_destroy(struct perf_event *event)
4933 perf_trace_destroy(event);
4936 static int perf_tp_event_init(struct perf_event *event)
4940 if (event->attr.type != PERF_TYPE_TRACEPOINT)
4943 err = perf_trace_init(event);
4947 event->destroy = tp_perf_event_destroy;
4952 static struct pmu perf_tracepoint = {
4953 .task_ctx_nr = perf_sw_context,
4955 .event_init = perf_tp_event_init,
4956 .add = perf_trace_add,
4957 .del = perf_trace_del,
4958 .start = perf_swevent_start,
4959 .stop = perf_swevent_stop,
4960 .read = perf_swevent_read,
4963 static inline void perf_tp_register(void)
4965 perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
4968 static int perf_event_set_filter(struct perf_event *event, void __user *arg)
4973 if (event->attr.type != PERF_TYPE_TRACEPOINT)
4976 filter_str = strndup_user(arg, PAGE_SIZE);
4977 if (IS_ERR(filter_str))
4978 return PTR_ERR(filter_str);
4980 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
4986 static void perf_event_free_filter(struct perf_event *event)
4988 ftrace_profile_free_filter(event);
4993 static inline void perf_tp_register(void)
4997 static int perf_event_set_filter(struct perf_event *event, void __user *arg)
5002 static void perf_event_free_filter(struct perf_event *event)
5006 #endif /* CONFIG_EVENT_TRACING */
5008 #ifdef CONFIG_HAVE_HW_BREAKPOINT
5009 void perf_bp_event(struct perf_event *bp, void *data)
5011 struct perf_sample_data sample;
5012 struct pt_regs *regs = data;
5014 perf_sample_data_init(&sample, bp->attr.bp_addr);
5016 if (!bp->hw.state && !perf_exclude_event(bp, regs))
5017 perf_swevent_event(bp, 1, 1, &sample, regs);
5022 * hrtimer based swevent callback
5025 static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
5027 enum hrtimer_restart ret = HRTIMER_RESTART;
5028 struct perf_sample_data data;
5029 struct pt_regs *regs;
5030 struct perf_event *event;
5033 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
5034 event->pmu->read(event);
5036 perf_sample_data_init(&data, 0);
5037 data.period = event->hw.last_period;
5038 regs = get_irq_regs();
5040 if (regs && !perf_exclude_event(event, regs)) {
5041 if (!(event->attr.exclude_idle && current->pid == 0))
5042 if (perf_event_overflow(event, 0, &data, regs))
5043 ret = HRTIMER_NORESTART;
5046 period = max_t(u64, 10000, event->hw.sample_period);
5047 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
5052 static void perf_swevent_start_hrtimer(struct perf_event *event)
5054 struct hw_perf_event *hwc = &event->hw;
5057 if (!is_sampling_event(event))
5060 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
5061 hwc->hrtimer.function = perf_swevent_hrtimer;
5063 period = local64_read(&hwc->period_left);
5068 local64_set(&hwc->period_left, 0);
5070 period = max_t(u64, 10000, hwc->sample_period);
5072 __hrtimer_start_range_ns(&hwc->hrtimer,
5073 ns_to_ktime(period), 0,
5074 HRTIMER_MODE_REL_PINNED, 0);
5077 static void perf_swevent_cancel_hrtimer(struct perf_event *event)
5079 struct hw_perf_event *hwc = &event->hw;
5081 if (is_sampling_event(event)) {
5082 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
5083 local64_set(&hwc->period_left, ktime_to_ns(remaining));
5085 hrtimer_cancel(&hwc->hrtimer);
5090 * Software event: cpu wall time clock
5093 static void cpu_clock_event_update(struct perf_event *event)
5098 now = local_clock();
5099 prev = local64_xchg(&event->hw.prev_count, now);
5100 local64_add(now - prev, &event->count);
5103 static void cpu_clock_event_start(struct perf_event *event, int flags)
5105 local64_set(&event->hw.prev_count, local_clock());
5106 perf_swevent_start_hrtimer(event);
5109 static void cpu_clock_event_stop(struct perf_event *event, int flags)
5111 perf_swevent_cancel_hrtimer(event);
5112 cpu_clock_event_update(event);
5115 static int cpu_clock_event_add(struct perf_event *event, int flags)
5117 if (flags & PERF_EF_START)
5118 cpu_clock_event_start(event, flags);
5123 static void cpu_clock_event_del(struct perf_event *event, int flags)
5125 cpu_clock_event_stop(event, flags);
5128 static void cpu_clock_event_read(struct perf_event *event)
5130 cpu_clock_event_update(event);
5133 static int cpu_clock_event_init(struct perf_event *event)
5135 if (event->attr.type != PERF_TYPE_SOFTWARE)
5138 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
5144 static struct pmu perf_cpu_clock = {
5145 .task_ctx_nr = perf_sw_context,
5147 .event_init = cpu_clock_event_init,
5148 .add = cpu_clock_event_add,
5149 .del = cpu_clock_event_del,
5150 .start = cpu_clock_event_start,
5151 .stop = cpu_clock_event_stop,
5152 .read = cpu_clock_event_read,
5156 * Software event: task time clock
5159 static void task_clock_event_update(struct perf_event *event, u64 now)
5164 prev = local64_xchg(&event->hw.prev_count, now);
5166 local64_add(delta, &event->count);
5169 static void task_clock_event_start(struct perf_event *event, int flags)
5171 local64_set(&event->hw.prev_count, event->ctx->time);
5172 perf_swevent_start_hrtimer(event);
5175 static void task_clock_event_stop(struct perf_event *event, int flags)
5177 perf_swevent_cancel_hrtimer(event);
5178 task_clock_event_update(event, event->ctx->time);
5181 static int task_clock_event_add(struct perf_event *event, int flags)
5183 if (flags & PERF_EF_START)
5184 task_clock_event_start(event, flags);
5189 static void task_clock_event_del(struct perf_event *event, int flags)
5191 task_clock_event_stop(event, PERF_EF_UPDATE);
5194 static void task_clock_event_read(struct perf_event *event)
5199 update_context_time(event->ctx);
5200 time = event->ctx->time;
5202 u64 now = perf_clock();
5203 u64 delta = now - event->ctx->timestamp;
5204 time = event->ctx->time + delta;
5207 task_clock_event_update(event, time);
5210 static int task_clock_event_init(struct perf_event *event)
5212 if (event->attr.type != PERF_TYPE_SOFTWARE)
5215 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
5221 static struct pmu perf_task_clock = {
5222 .task_ctx_nr = perf_sw_context,
5224 .event_init = task_clock_event_init,
5225 .add = task_clock_event_add,
5226 .del = task_clock_event_del,
5227 .start = task_clock_event_start,
5228 .stop = task_clock_event_stop,
5229 .read = task_clock_event_read,
5232 static void perf_pmu_nop_void(struct pmu *pmu)
5236 static int perf_pmu_nop_int(struct pmu *pmu)
5241 static void perf_pmu_start_txn(struct pmu *pmu)
5243 perf_pmu_disable(pmu);
5246 static int perf_pmu_commit_txn(struct pmu *pmu)
5248 perf_pmu_enable(pmu);
5252 static void perf_pmu_cancel_txn(struct pmu *pmu)
5254 perf_pmu_enable(pmu);
5258 * Ensures all contexts with the same task_ctx_nr have the same
5259 * pmu_cpu_context too.
5261 static void *find_pmu_context(int ctxn)
5268 list_for_each_entry(pmu, &pmus, entry) {
5269 if (pmu->task_ctx_nr == ctxn)
5270 return pmu->pmu_cpu_context;
5276 static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
5280 for_each_possible_cpu(cpu) {
5281 struct perf_cpu_context *cpuctx;
5283 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
5285 if (cpuctx->active_pmu == old_pmu)
5286 cpuctx->active_pmu = pmu;
5290 static void free_pmu_context(struct pmu *pmu)
5294 mutex_lock(&pmus_lock);
5296 * Like a real lame refcount.
5298 list_for_each_entry(i, &pmus, entry) {
5299 if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
5300 update_pmu_context(i, pmu);
5305 free_percpu(pmu->pmu_cpu_context);
5307 mutex_unlock(&pmus_lock);
5309 static struct idr pmu_idr;
5311 int perf_pmu_register(struct pmu *pmu, char *name, int type)
5315 mutex_lock(&pmus_lock);
5317 pmu->pmu_disable_count = alloc_percpu(int);
5318 if (!pmu->pmu_disable_count)
5327 int err = idr_pre_get(&pmu_idr, GFP_KERNEL);
5331 err = idr_get_new_above(&pmu_idr, pmu, PERF_TYPE_MAX, &type);
5340 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
5341 if (pmu->pmu_cpu_context)
5342 goto got_cpu_context;
5344 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
5345 if (!pmu->pmu_cpu_context)
5348 for_each_possible_cpu(cpu) {
5349 struct perf_cpu_context *cpuctx;
5351 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
5352 __perf_event_init_context(&cpuctx->ctx);
5353 cpuctx->ctx.type = cpu_context;
5354 cpuctx->ctx.pmu = pmu;
5355 cpuctx->jiffies_interval = 1;
5356 INIT_LIST_HEAD(&cpuctx->rotation_list);
5357 cpuctx->active_pmu = pmu;
5361 if (!pmu->start_txn) {
5362 if (pmu->pmu_enable) {
5364 * If we have pmu_enable/pmu_disable calls, install
5365 * transaction stubs that use that to try and batch
5366 * hardware accesses.
5368 pmu->start_txn = perf_pmu_start_txn;
5369 pmu->commit_txn = perf_pmu_commit_txn;
5370 pmu->cancel_txn = perf_pmu_cancel_txn;
5372 pmu->start_txn = perf_pmu_nop_void;
5373 pmu->commit_txn = perf_pmu_nop_int;
5374 pmu->cancel_txn = perf_pmu_nop_void;
5378 if (!pmu->pmu_enable) {
5379 pmu->pmu_enable = perf_pmu_nop_void;
5380 pmu->pmu_disable = perf_pmu_nop_void;
5383 list_add_rcu(&pmu->entry, &pmus);
5386 mutex_unlock(&pmus_lock);
5391 if (pmu->type >= PERF_TYPE_MAX)
5392 idr_remove(&pmu_idr, pmu->type);
5395 free_percpu(pmu->pmu_disable_count);
5399 void perf_pmu_unregister(struct pmu *pmu)
5401 mutex_lock(&pmus_lock);
5402 list_del_rcu(&pmu->entry);
5403 mutex_unlock(&pmus_lock);
5406 * We dereference the pmu list under both SRCU and regular RCU, so
5407 * synchronize against both of those.
5409 synchronize_srcu(&pmus_srcu);
5412 free_percpu(pmu->pmu_disable_count);
5413 if (pmu->type >= PERF_TYPE_MAX)
5414 idr_remove(&pmu_idr, pmu->type);
5415 free_pmu_context(pmu);
5418 struct pmu *perf_init_event(struct perf_event *event)
5420 struct pmu *pmu = NULL;
5423 idx = srcu_read_lock(&pmus_srcu);
5426 pmu = idr_find(&pmu_idr, event->attr.type);
5431 list_for_each_entry_rcu(pmu, &pmus, entry) {
5432 int ret = pmu->event_init(event);
5436 if (ret != -ENOENT) {
5441 pmu = ERR_PTR(-ENOENT);
5443 srcu_read_unlock(&pmus_srcu, idx);
5449 * Allocate and initialize a event structure
5451 static struct perf_event *
5452 perf_event_alloc(struct perf_event_attr *attr, int cpu,
5453 struct task_struct *task,
5454 struct perf_event *group_leader,
5455 struct perf_event *parent_event,
5456 perf_overflow_handler_t overflow_handler)
5459 struct perf_event *event;
5460 struct hw_perf_event *hwc;
5463 event = kzalloc(sizeof(*event), GFP_KERNEL);
5465 return ERR_PTR(-ENOMEM);
5468 * Single events are their own group leaders, with an
5469 * empty sibling list:
5472 group_leader = event;
5474 mutex_init(&event->child_mutex);
5475 INIT_LIST_HEAD(&event->child_list);
5477 INIT_LIST_HEAD(&event->group_entry);
5478 INIT_LIST_HEAD(&event->event_entry);
5479 INIT_LIST_HEAD(&event->sibling_list);
5480 init_waitqueue_head(&event->waitq);
5481 init_irq_work(&event->pending, perf_pending_event);
5483 mutex_init(&event->mmap_mutex);
5486 event->attr = *attr;
5487 event->group_leader = group_leader;
5491 event->parent = parent_event;
5493 event->ns = get_pid_ns(current->nsproxy->pid_ns);
5494 event->id = atomic64_inc_return(&perf_event_id);
5496 event->state = PERF_EVENT_STATE_INACTIVE;
5499 event->attach_state = PERF_ATTACH_TASK;
5500 #ifdef CONFIG_HAVE_HW_BREAKPOINT
5502 * hw_breakpoint is a bit difficult here..
5504 if (attr->type == PERF_TYPE_BREAKPOINT)
5505 event->hw.bp_target = task;
5509 if (!overflow_handler && parent_event)
5510 overflow_handler = parent_event->overflow_handler;
5512 event->overflow_handler = overflow_handler;
5515 event->state = PERF_EVENT_STATE_OFF;
5520 hwc->sample_period = attr->sample_period;
5521 if (attr->freq && attr->sample_freq)
5522 hwc->sample_period = 1;
5523 hwc->last_period = hwc->sample_period;
5525 local64_set(&hwc->period_left, hwc->sample_period);
5528 * we currently do not support PERF_FORMAT_GROUP on inherited events
5530 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
5533 pmu = perf_init_event(event);
5539 else if (IS_ERR(pmu))
5544 put_pid_ns(event->ns);
5546 return ERR_PTR(err);
5551 if (!event->parent) {
5552 if (event->attach_state & PERF_ATTACH_TASK)
5553 jump_label_inc(&perf_task_events);
5554 if (event->attr.mmap || event->attr.mmap_data)
5555 atomic_inc(&nr_mmap_events);
5556 if (event->attr.comm)
5557 atomic_inc(&nr_comm_events);
5558 if (event->attr.task)
5559 atomic_inc(&nr_task_events);
5560 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
5561 err = get_callchain_buffers();
5564 return ERR_PTR(err);
5572 static int perf_copy_attr(struct perf_event_attr __user *uattr,
5573 struct perf_event_attr *attr)
5578 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
5582 * zero the full structure, so that a short copy will be nice.
5584 memset(attr, 0, sizeof(*attr));
5586 ret = get_user(size, &uattr->size);
5590 if (size > PAGE_SIZE) /* silly large */
5593 if (!size) /* abi compat */
5594 size = PERF_ATTR_SIZE_VER0;
5596 if (size < PERF_ATTR_SIZE_VER0)
5600 * If we're handed a bigger struct than we know of,
5601 * ensure all the unknown bits are 0 - i.e. new
5602 * user-space does not rely on any kernel feature
5603 * extensions we dont know about yet.
5605 if (size > sizeof(*attr)) {
5606 unsigned char __user *addr;
5607 unsigned char __user *end;
5610 addr = (void __user *)uattr + sizeof(*attr);
5611 end = (void __user *)uattr + size;
5613 for (; addr < end; addr++) {
5614 ret = get_user(val, addr);
5620 size = sizeof(*attr);
5623 ret = copy_from_user(attr, uattr, size);
5628 * If the type exists, the corresponding creation will verify
5631 if (attr->type >= PERF_TYPE_MAX)
5634 if (attr->__reserved_1)
5637 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
5640 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
5647 put_user(sizeof(*attr), &uattr->size);
5653 perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
5655 struct perf_buffer *buffer = NULL, *old_buffer = NULL;
5661 /* don't allow circular references */
5662 if (event == output_event)
5666 * Don't allow cross-cpu buffers
5668 if (output_event->cpu != event->cpu)
5672 * If its not a per-cpu buffer, it must be the same task.
5674 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
5678 mutex_lock(&event->mmap_mutex);
5679 /* Can't redirect output if we've got an active mmap() */
5680 if (atomic_read(&event->mmap_count))
5684 /* get the buffer we want to redirect to */
5685 buffer = perf_buffer_get(output_event);
5690 old_buffer = event->buffer;
5691 rcu_assign_pointer(event->buffer, buffer);
5694 mutex_unlock(&event->mmap_mutex);
5697 perf_buffer_put(old_buffer);
5703 * sys_perf_event_open - open a performance event, associate it to a task/cpu
5705 * @attr_uptr: event_id type attributes for monitoring/sampling
5708 * @group_fd: group leader event fd
5710 SYSCALL_DEFINE5(perf_event_open,
5711 struct perf_event_attr __user *, attr_uptr,
5712 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
5714 struct perf_event *group_leader = NULL, *output_event = NULL;
5715 struct perf_event *event, *sibling;
5716 struct perf_event_attr attr;
5717 struct perf_event_context *ctx;
5718 struct file *event_file = NULL;
5719 struct file *group_file = NULL;
5720 struct task_struct *task = NULL;
5724 int fput_needed = 0;
5727 /* for future expandability... */
5728 if (flags & ~(PERF_FLAG_FD_NO_GROUP | PERF_FLAG_FD_OUTPUT))
5731 err = perf_copy_attr(attr_uptr, &attr);
5735 if (!attr.exclude_kernel) {
5736 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
5741 if (attr.sample_freq > sysctl_perf_event_sample_rate)
5745 event_fd = get_unused_fd_flags(O_RDWR);
5749 if (group_fd != -1) {
5750 group_leader = perf_fget_light(group_fd, &fput_needed);
5751 if (IS_ERR(group_leader)) {
5752 err = PTR_ERR(group_leader);
5755 group_file = group_leader->filp;
5756 if (flags & PERF_FLAG_FD_OUTPUT)
5757 output_event = group_leader;
5758 if (flags & PERF_FLAG_FD_NO_GROUP)
5759 group_leader = NULL;
5763 task = find_lively_task_by_vpid(pid);
5765 err = PTR_ERR(task);
5770 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL, NULL);
5771 if (IS_ERR(event)) {
5772 err = PTR_ERR(event);
5777 * Special case software events and allow them to be part of
5778 * any hardware group.
5783 (is_software_event(event) != is_software_event(group_leader))) {
5784 if (is_software_event(event)) {
5786 * If event and group_leader are not both a software
5787 * event, and event is, then group leader is not.
5789 * Allow the addition of software events to !software
5790 * groups, this is safe because software events never
5793 pmu = group_leader->pmu;
5794 } else if (is_software_event(group_leader) &&
5795 (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
5797 * In case the group is a pure software group, and we
5798 * try to add a hardware event, move the whole group to
5799 * the hardware context.
5806 * Get the target context (task or percpu):
5808 ctx = find_get_context(pmu, task, cpu);
5815 * Look up the group leader (we will attach this event to it):
5821 * Do not allow a recursive hierarchy (this new sibling
5822 * becoming part of another group-sibling):
5824 if (group_leader->group_leader != group_leader)
5827 * Do not allow to attach to a group in a different
5828 * task or CPU context:
5831 if (group_leader->ctx->type != ctx->type)
5834 if (group_leader->ctx != ctx)
5839 * Only a group leader can be exclusive or pinned
5841 if (attr.exclusive || attr.pinned)
5846 err = perf_event_set_output(event, output_event);
5851 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, O_RDWR);
5852 if (IS_ERR(event_file)) {
5853 err = PTR_ERR(event_file);
5858 struct perf_event_context *gctx = group_leader->ctx;
5860 mutex_lock(&gctx->mutex);
5861 perf_event_remove_from_context(group_leader);
5862 list_for_each_entry(sibling, &group_leader->sibling_list,
5864 perf_event_remove_from_context(sibling);
5867 mutex_unlock(&gctx->mutex);
5871 event->filp = event_file;
5872 WARN_ON_ONCE(ctx->parent_ctx);
5873 mutex_lock(&ctx->mutex);
5876 perf_install_in_context(ctx, group_leader, cpu);
5878 list_for_each_entry(sibling, &group_leader->sibling_list,
5880 perf_install_in_context(ctx, sibling, cpu);
5885 perf_install_in_context(ctx, event, cpu);
5887 mutex_unlock(&ctx->mutex);
5889 event->owner = current;
5891 mutex_lock(¤t->perf_event_mutex);
5892 list_add_tail(&event->owner_entry, ¤t->perf_event_list);
5893 mutex_unlock(¤t->perf_event_mutex);
5896 * Precalculate sample_data sizes
5898 perf_event__header_size(event);
5899 perf_event__id_header_size(event);
5902 * Drop the reference on the group_event after placing the
5903 * new event on the sibling_list. This ensures destruction
5904 * of the group leader will find the pointer to itself in
5905 * perf_group_detach().
5907 fput_light(group_file, fput_needed);
5908 fd_install(event_fd, event_file);
5917 put_task_struct(task);
5919 fput_light(group_file, fput_needed);
5921 put_unused_fd(event_fd);
5926 * perf_event_create_kernel_counter
5928 * @attr: attributes of the counter to create
5929 * @cpu: cpu in which the counter is bound
5930 * @task: task to profile (NULL for percpu)
5933 perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
5934 struct task_struct *task,
5935 perf_overflow_handler_t overflow_handler)
5937 struct perf_event_context *ctx;
5938 struct perf_event *event;
5942 * Get the target context (task or percpu):
5945 event = perf_event_alloc(attr, cpu, task, NULL, NULL, overflow_handler);
5946 if (IS_ERR(event)) {
5947 err = PTR_ERR(event);
5951 ctx = find_get_context(event->pmu, task, cpu);
5958 WARN_ON_ONCE(ctx->parent_ctx);
5959 mutex_lock(&ctx->mutex);
5960 perf_install_in_context(ctx, event, cpu);
5962 mutex_unlock(&ctx->mutex);
5969 return ERR_PTR(err);
5971 EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
5973 static void sync_child_event(struct perf_event *child_event,
5974 struct task_struct *child)
5976 struct perf_event *parent_event = child_event->parent;
5979 if (child_event->attr.inherit_stat)
5980 perf_event_read_event(child_event, child);
5982 child_val = perf_event_count(child_event);
5985 * Add back the child's count to the parent's count:
5987 atomic64_add(child_val, &parent_event->child_count);
5988 atomic64_add(child_event->total_time_enabled,
5989 &parent_event->child_total_time_enabled);
5990 atomic64_add(child_event->total_time_running,
5991 &parent_event->child_total_time_running);
5994 * Remove this event from the parent's list
5996 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
5997 mutex_lock(&parent_event->child_mutex);
5998 list_del_init(&child_event->child_list);
5999 mutex_unlock(&parent_event->child_mutex);
6002 * Release the parent event, if this was the last
6005 fput(parent_event->filp);
6009 __perf_event_exit_task(struct perf_event *child_event,
6010 struct perf_event_context *child_ctx,
6011 struct task_struct *child)
6013 struct perf_event *parent_event;
6015 perf_event_remove_from_context(child_event);
6017 parent_event = child_event->parent;
6019 * It can happen that parent exits first, and has events
6020 * that are still around due to the child reference. These
6021 * events need to be zapped - but otherwise linger.
6024 sync_child_event(child_event, child);
6025 free_event(child_event);
6029 static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
6031 struct perf_event *child_event, *tmp;
6032 struct perf_event_context *child_ctx;
6033 unsigned long flags;
6035 if (likely(!child->perf_event_ctxp[ctxn])) {
6036 perf_event_task(child, NULL, 0);
6040 local_irq_save(flags);
6042 * We can't reschedule here because interrupts are disabled,
6043 * and either child is current or it is a task that can't be
6044 * scheduled, so we are now safe from rescheduling changing
6047 child_ctx = child->perf_event_ctxp[ctxn];
6048 task_ctx_sched_out(child_ctx, EVENT_ALL);
6051 * Take the context lock here so that if find_get_context is
6052 * reading child->perf_event_ctxp, we wait until it has
6053 * incremented the context's refcount before we do put_ctx below.
6055 raw_spin_lock(&child_ctx->lock);
6056 child->perf_event_ctxp[ctxn] = NULL;
6058 * If this context is a clone; unclone it so it can't get
6059 * swapped to another process while we're removing all
6060 * the events from it.
6062 unclone_ctx(child_ctx);
6063 update_context_time(child_ctx);
6064 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
6067 * Report the task dead after unscheduling the events so that we
6068 * won't get any samples after PERF_RECORD_EXIT. We can however still
6069 * get a few PERF_RECORD_READ events.
6071 perf_event_task(child, child_ctx, 0);
6074 * We can recurse on the same lock type through:
6076 * __perf_event_exit_task()
6077 * sync_child_event()
6078 * fput(parent_event->filp)
6080 * mutex_lock(&ctx->mutex)
6082 * But since its the parent context it won't be the same instance.
6084 mutex_lock(&child_ctx->mutex);
6087 list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
6089 __perf_event_exit_task(child_event, child_ctx, child);
6091 list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
6093 __perf_event_exit_task(child_event, child_ctx, child);
6096 * If the last event was a group event, it will have appended all
6097 * its siblings to the list, but we obtained 'tmp' before that which
6098 * will still point to the list head terminating the iteration.
6100 if (!list_empty(&child_ctx->pinned_groups) ||
6101 !list_empty(&child_ctx->flexible_groups))
6104 mutex_unlock(&child_ctx->mutex);
6110 * When a child task exits, feed back event values to parent events.
6112 void perf_event_exit_task(struct task_struct *child)
6114 struct perf_event *event, *tmp;
6117 mutex_lock(&child->perf_event_mutex);
6118 list_for_each_entry_safe(event, tmp, &child->perf_event_list,
6120 list_del_init(&event->owner_entry);
6123 * Ensure the list deletion is visible before we clear
6124 * the owner, closes a race against perf_release() where
6125 * we need to serialize on the owner->perf_event_mutex.
6128 event->owner = NULL;
6130 mutex_unlock(&child->perf_event_mutex);
6132 for_each_task_context_nr(ctxn)
6133 perf_event_exit_task_context(child, ctxn);
6136 static void perf_free_event(struct perf_event *event,
6137 struct perf_event_context *ctx)
6139 struct perf_event *parent = event->parent;
6141 if (WARN_ON_ONCE(!parent))
6144 mutex_lock(&parent->child_mutex);
6145 list_del_init(&event->child_list);
6146 mutex_unlock(&parent->child_mutex);
6150 perf_group_detach(event);
6151 list_del_event(event, ctx);
6156 * free an unexposed, unused context as created by inheritance by
6157 * perf_event_init_task below, used by fork() in case of fail.
6159 void perf_event_free_task(struct task_struct *task)
6161 struct perf_event_context *ctx;
6162 struct perf_event *event, *tmp;
6165 for_each_task_context_nr(ctxn) {
6166 ctx = task->perf_event_ctxp[ctxn];
6170 mutex_lock(&ctx->mutex);
6172 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
6174 perf_free_event(event, ctx);
6176 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
6178 perf_free_event(event, ctx);
6180 if (!list_empty(&ctx->pinned_groups) ||
6181 !list_empty(&ctx->flexible_groups))
6184 mutex_unlock(&ctx->mutex);
6190 void perf_event_delayed_put(struct task_struct *task)
6194 for_each_task_context_nr(ctxn)
6195 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
6199 * inherit a event from parent task to child task:
6201 static struct perf_event *
6202 inherit_event(struct perf_event *parent_event,
6203 struct task_struct *parent,
6204 struct perf_event_context *parent_ctx,
6205 struct task_struct *child,
6206 struct perf_event *group_leader,
6207 struct perf_event_context *child_ctx)
6209 struct perf_event *child_event;
6210 unsigned long flags;
6213 * Instead of creating recursive hierarchies of events,
6214 * we link inherited events back to the original parent,
6215 * which has a filp for sure, which we use as the reference
6218 if (parent_event->parent)
6219 parent_event = parent_event->parent;
6221 child_event = perf_event_alloc(&parent_event->attr,
6224 group_leader, parent_event,
6226 if (IS_ERR(child_event))
6231 * Make the child state follow the state of the parent event,
6232 * not its attr.disabled bit. We hold the parent's mutex,
6233 * so we won't race with perf_event_{en, dis}able_family.
6235 if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
6236 child_event->state = PERF_EVENT_STATE_INACTIVE;
6238 child_event->state = PERF_EVENT_STATE_OFF;
6240 if (parent_event->attr.freq) {
6241 u64 sample_period = parent_event->hw.sample_period;
6242 struct hw_perf_event *hwc = &child_event->hw;
6244 hwc->sample_period = sample_period;
6245 hwc->last_period = sample_period;
6247 local64_set(&hwc->period_left, sample_period);
6250 child_event->ctx = child_ctx;
6251 child_event->overflow_handler = parent_event->overflow_handler;
6254 * Precalculate sample_data sizes
6256 perf_event__header_size(child_event);
6257 perf_event__id_header_size(child_event);
6260 * Link it up in the child's context:
6262 raw_spin_lock_irqsave(&child_ctx->lock, flags);
6263 add_event_to_ctx(child_event, child_ctx);
6264 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
6267 * Get a reference to the parent filp - we will fput it
6268 * when the child event exits. This is safe to do because
6269 * we are in the parent and we know that the filp still
6270 * exists and has a nonzero count:
6272 atomic_long_inc(&parent_event->filp->f_count);
6275 * Link this into the parent event's child list
6277 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
6278 mutex_lock(&parent_event->child_mutex);
6279 list_add_tail(&child_event->child_list, &parent_event->child_list);
6280 mutex_unlock(&parent_event->child_mutex);
6285 static int inherit_group(struct perf_event *parent_event,
6286 struct task_struct *parent,
6287 struct perf_event_context *parent_ctx,
6288 struct task_struct *child,
6289 struct perf_event_context *child_ctx)
6291 struct perf_event *leader;
6292 struct perf_event *sub;
6293 struct perf_event *child_ctr;
6295 leader = inherit_event(parent_event, parent, parent_ctx,
6296 child, NULL, child_ctx);
6298 return PTR_ERR(leader);
6299 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
6300 child_ctr = inherit_event(sub, parent, parent_ctx,
6301 child, leader, child_ctx);
6302 if (IS_ERR(child_ctr))
6303 return PTR_ERR(child_ctr);
6309 inherit_task_group(struct perf_event *event, struct task_struct *parent,
6310 struct perf_event_context *parent_ctx,
6311 struct task_struct *child, int ctxn,
6315 struct perf_event_context *child_ctx;
6317 if (!event->attr.inherit) {
6322 child_ctx = child->perf_event_ctxp[ctxn];
6325 * This is executed from the parent task context, so
6326 * inherit events that have been marked for cloning.
6327 * First allocate and initialize a context for the
6331 child_ctx = alloc_perf_context(event->pmu, child);
6335 child->perf_event_ctxp[ctxn] = child_ctx;
6338 ret = inherit_group(event, parent, parent_ctx,
6348 * Initialize the perf_event context in task_struct
6350 int perf_event_init_context(struct task_struct *child, int ctxn)
6352 struct perf_event_context *child_ctx, *parent_ctx;
6353 struct perf_event_context *cloned_ctx;
6354 struct perf_event *event;
6355 struct task_struct *parent = current;
6356 int inherited_all = 1;
6357 unsigned long flags;
6360 child->perf_event_ctxp[ctxn] = NULL;
6362 mutex_init(&child->perf_event_mutex);
6363 INIT_LIST_HEAD(&child->perf_event_list);
6365 if (likely(!parent->perf_event_ctxp[ctxn]))
6369 * If the parent's context is a clone, pin it so it won't get
6372 parent_ctx = perf_pin_task_context(parent, ctxn);
6375 * No need to check if parent_ctx != NULL here; since we saw
6376 * it non-NULL earlier, the only reason for it to become NULL
6377 * is if we exit, and since we're currently in the middle of
6378 * a fork we can't be exiting at the same time.
6382 * Lock the parent list. No need to lock the child - not PID
6383 * hashed yet and not running, so nobody can access it.
6385 mutex_lock(&parent_ctx->mutex);
6388 * We dont have to disable NMIs - we are only looking at
6389 * the list, not manipulating it:
6391 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
6392 ret = inherit_task_group(event, parent, parent_ctx,
6393 child, ctxn, &inherited_all);
6399 * We can't hold ctx->lock when iterating the ->flexible_group list due
6400 * to allocations, but we need to prevent rotation because
6401 * rotate_ctx() will change the list from interrupt context.
6403 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
6404 parent_ctx->rotate_disable = 1;
6405 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
6407 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
6408 ret = inherit_task_group(event, parent, parent_ctx,
6409 child, ctxn, &inherited_all);
6414 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
6415 parent_ctx->rotate_disable = 0;
6416 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
6418 child_ctx = child->perf_event_ctxp[ctxn];
6420 if (child_ctx && inherited_all) {
6422 * Mark the child context as a clone of the parent
6423 * context, or of whatever the parent is a clone of.
6424 * Note that if the parent is a clone, it could get
6425 * uncloned at any point, but that doesn't matter
6426 * because the list of events and the generation
6427 * count can't have changed since we took the mutex.
6429 cloned_ctx = rcu_dereference(parent_ctx->parent_ctx);
6431 child_ctx->parent_ctx = cloned_ctx;
6432 child_ctx->parent_gen = parent_ctx->parent_gen;
6434 child_ctx->parent_ctx = parent_ctx;
6435 child_ctx->parent_gen = parent_ctx->generation;
6437 get_ctx(child_ctx->parent_ctx);
6440 mutex_unlock(&parent_ctx->mutex);
6442 perf_unpin_context(parent_ctx);
6448 * Initialize the perf_event context in task_struct
6450 int perf_event_init_task(struct task_struct *child)
6454 for_each_task_context_nr(ctxn) {
6455 ret = perf_event_init_context(child, ctxn);
6463 static void __init perf_event_init_all_cpus(void)
6465 struct swevent_htable *swhash;
6468 for_each_possible_cpu(cpu) {
6469 swhash = &per_cpu(swevent_htable, cpu);
6470 mutex_init(&swhash->hlist_mutex);
6471 INIT_LIST_HEAD(&per_cpu(rotation_list, cpu));
6475 static void __cpuinit perf_event_init_cpu(int cpu)
6477 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
6479 mutex_lock(&swhash->hlist_mutex);
6480 if (swhash->hlist_refcount > 0) {
6481 struct swevent_hlist *hlist;
6483 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
6485 rcu_assign_pointer(swhash->swevent_hlist, hlist);
6487 mutex_unlock(&swhash->hlist_mutex);
6490 #if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC
6491 static void perf_pmu_rotate_stop(struct pmu *pmu)
6493 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
6495 WARN_ON(!irqs_disabled());
6497 list_del_init(&cpuctx->rotation_list);
6500 static void __perf_event_exit_context(void *__info)
6502 struct perf_event_context *ctx = __info;
6503 struct perf_event *event, *tmp;
6505 perf_pmu_rotate_stop(ctx->pmu);
6507 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
6508 __perf_event_remove_from_context(event);
6509 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, group_entry)
6510 __perf_event_remove_from_context(event);
6513 static void perf_event_exit_cpu_context(int cpu)
6515 struct perf_event_context *ctx;
6519 idx = srcu_read_lock(&pmus_srcu);
6520 list_for_each_entry_rcu(pmu, &pmus, entry) {
6521 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
6523 mutex_lock(&ctx->mutex);
6524 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
6525 mutex_unlock(&ctx->mutex);
6527 srcu_read_unlock(&pmus_srcu, idx);
6530 static void perf_event_exit_cpu(int cpu)
6532 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
6534 mutex_lock(&swhash->hlist_mutex);
6535 swevent_hlist_release(swhash);
6536 mutex_unlock(&swhash->hlist_mutex);
6538 perf_event_exit_cpu_context(cpu);
6541 static inline void perf_event_exit_cpu(int cpu) { }
6545 perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
6549 for_each_online_cpu(cpu)
6550 perf_event_exit_cpu(cpu);
6556 * Run the perf reboot notifier at the very last possible moment so that
6557 * the generic watchdog code runs as long as possible.
6559 static struct notifier_block perf_reboot_notifier = {
6560 .notifier_call = perf_reboot,
6561 .priority = INT_MIN,
6564 static int __cpuinit
6565 perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
6567 unsigned int cpu = (long)hcpu;
6569 switch (action & ~CPU_TASKS_FROZEN) {
6571 case CPU_UP_PREPARE:
6572 case CPU_DOWN_FAILED:
6573 perf_event_init_cpu(cpu);
6576 case CPU_UP_CANCELED:
6577 case CPU_DOWN_PREPARE:
6578 perf_event_exit_cpu(cpu);
6588 void __init perf_event_init(void)
6594 perf_event_init_all_cpus();
6595 init_srcu_struct(&pmus_srcu);
6596 perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
6597 perf_pmu_register(&perf_cpu_clock, NULL, -1);
6598 perf_pmu_register(&perf_task_clock, NULL, -1);
6600 perf_cpu_notifier(perf_cpu_notify);
6601 register_reboot_notifier(&perf_reboot_notifier);
6603 ret = init_hw_breakpoint();
6604 WARN(ret, "hw_breakpoint initialization failed with: %d", ret);