2 * Performance events core code:
5 * Copyright (C) 2008-2009 Red Hat, Inc., Ingo Molnar
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/device.h>
28 #include <linux/vmalloc.h>
29 #include <linux/hardirq.h>
30 #include <linux/rculist.h>
31 #include <linux/uaccess.h>
32 #include <linux/syscalls.h>
33 #include <linux/anon_inodes.h>
34 #include <linux/kernel_stat.h>
35 #include <linux/perf_event.h>
36 #include <linux/ftrace_event.h>
37 #include <linux/hw_breakpoint.h>
39 #include <asm/irq_regs.h>
44 EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
47 atomic_t perf_task_events __read_mostly;
48 static atomic_t nr_mmap_events __read_mostly;
49 static atomic_t nr_comm_events __read_mostly;
50 static atomic_t nr_task_events __read_mostly;
52 static LIST_HEAD(pmus);
53 static DEFINE_MUTEX(pmus_lock);
54 static struct srcu_struct pmus_srcu;
57 * perf event paranoia level:
58 * -1 - not paranoid at all
59 * 0 - disallow raw tracepoint access for unpriv
60 * 1 - disallow cpu events for unpriv
61 * 2 - disallow kernel profiling for unpriv
63 int sysctl_perf_event_paranoid __read_mostly = 1;
65 int sysctl_perf_event_mlock __read_mostly = 512; /* 'free' kb per user */
68 * max perf event sample rate
70 int sysctl_perf_event_sample_rate __read_mostly = 100000;
72 static atomic64_t perf_event_id;
74 static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
75 enum event_type_t event_type);
77 static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
78 enum event_type_t event_type);
80 void __weak perf_event_print_debug(void) { }
82 extern __weak const char *perf_pmu_name(void)
87 static inline u64 perf_clock(void)
92 void perf_pmu_disable(struct pmu *pmu)
94 int *count = this_cpu_ptr(pmu->pmu_disable_count);
96 pmu->pmu_disable(pmu);
99 void perf_pmu_enable(struct pmu *pmu)
101 int *count = this_cpu_ptr(pmu->pmu_disable_count);
103 pmu->pmu_enable(pmu);
106 static DEFINE_PER_CPU(struct list_head, rotation_list);
109 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
110 * because they're strictly cpu affine and rotate_start is called with IRQs
111 * disabled, while rotate_context is called from IRQ context.
113 static void perf_pmu_rotate_start(struct pmu *pmu)
115 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
116 struct list_head *head = &__get_cpu_var(rotation_list);
118 WARN_ON(!irqs_disabled());
120 if (list_empty(&cpuctx->rotation_list))
121 list_add(&cpuctx->rotation_list, head);
124 static void get_ctx(struct perf_event_context *ctx)
126 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
129 static void free_ctx(struct rcu_head *head)
131 struct perf_event_context *ctx;
133 ctx = container_of(head, struct perf_event_context, rcu_head);
137 static void put_ctx(struct perf_event_context *ctx)
139 if (atomic_dec_and_test(&ctx->refcount)) {
141 put_ctx(ctx->parent_ctx);
143 put_task_struct(ctx->task);
144 call_rcu(&ctx->rcu_head, free_ctx);
148 static void unclone_ctx(struct perf_event_context *ctx)
150 if (ctx->parent_ctx) {
151 put_ctx(ctx->parent_ctx);
152 ctx->parent_ctx = NULL;
156 static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
159 * only top level events have the pid namespace they were created in
162 event = event->parent;
164 return task_tgid_nr_ns(p, event->ns);
167 static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
170 * only top level events have the pid namespace they were created in
173 event = event->parent;
175 return task_pid_nr_ns(p, event->ns);
179 * If we inherit events we want to return the parent event id
182 static u64 primary_event_id(struct perf_event *event)
187 id = event->parent->id;
193 * Get the perf_event_context for a task and lock it.
194 * This has to cope with with the fact that until it is locked,
195 * the context could get moved to another task.
197 static struct perf_event_context *
198 perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
200 struct perf_event_context *ctx;
204 ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
207 * If this context is a clone of another, it might
208 * get swapped for another underneath us by
209 * perf_event_task_sched_out, though the
210 * rcu_read_lock() protects us from any context
211 * getting freed. Lock the context and check if it
212 * got swapped before we could get the lock, and retry
213 * if so. If we locked the right context, then it
214 * can't get swapped on us any more.
216 raw_spin_lock_irqsave(&ctx->lock, *flags);
217 if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
218 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
222 if (!atomic_inc_not_zero(&ctx->refcount)) {
223 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
232 * Get the context for a task and increment its pin_count so it
233 * can't get swapped to another task. This also increments its
234 * reference count so that the context can't get freed.
236 static struct perf_event_context *
237 perf_pin_task_context(struct task_struct *task, int ctxn)
239 struct perf_event_context *ctx;
242 ctx = perf_lock_task_context(task, ctxn, &flags);
245 raw_spin_unlock_irqrestore(&ctx->lock, flags);
250 static void perf_unpin_context(struct perf_event_context *ctx)
254 raw_spin_lock_irqsave(&ctx->lock, flags);
256 raw_spin_unlock_irqrestore(&ctx->lock, flags);
261 * Update the record of the current time in a context.
263 static void update_context_time(struct perf_event_context *ctx)
265 u64 now = perf_clock();
267 ctx->time += now - ctx->timestamp;
268 ctx->timestamp = now;
271 static u64 perf_event_time(struct perf_event *event)
273 struct perf_event_context *ctx = event->ctx;
274 return ctx ? ctx->time : 0;
278 * Update the total_time_enabled and total_time_running fields for a event.
280 static void update_event_times(struct perf_event *event)
282 struct perf_event_context *ctx = event->ctx;
285 if (event->state < PERF_EVENT_STATE_INACTIVE ||
286 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
290 run_end = perf_event_time(event);
292 run_end = event->tstamp_stopped;
294 event->total_time_enabled = run_end - event->tstamp_enabled;
296 if (event->state == PERF_EVENT_STATE_INACTIVE)
297 run_end = event->tstamp_stopped;
299 run_end = perf_event_time(event);
301 event->total_time_running = run_end - event->tstamp_running;
305 * Update total_time_enabled and total_time_running for all events in a group.
307 static void update_group_times(struct perf_event *leader)
309 struct perf_event *event;
311 update_event_times(leader);
312 list_for_each_entry(event, &leader->sibling_list, group_entry)
313 update_event_times(event);
316 static struct list_head *
317 ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
319 if (event->attr.pinned)
320 return &ctx->pinned_groups;
322 return &ctx->flexible_groups;
326 * Add a event from the lists for its context.
327 * Must be called with ctx->mutex and ctx->lock held.
330 list_add_event(struct perf_event *event, struct perf_event_context *ctx)
332 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
333 event->attach_state |= PERF_ATTACH_CONTEXT;
336 * If we're a stand alone event or group leader, we go to the context
337 * list, group events are kept attached to the group so that
338 * perf_group_detach can, at all times, locate all siblings.
340 if (event->group_leader == event) {
341 struct list_head *list;
343 if (is_software_event(event))
344 event->group_flags |= PERF_GROUP_SOFTWARE;
346 list = ctx_group_list(event, ctx);
347 list_add_tail(&event->group_entry, list);
350 list_add_rcu(&event->event_entry, &ctx->event_list);
352 perf_pmu_rotate_start(ctx->pmu);
354 if (event->attr.inherit_stat)
359 * Called at perf_event creation and when events are attached/detached from a
362 static void perf_event__read_size(struct perf_event *event)
364 int entry = sizeof(u64); /* value */
368 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
371 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
374 if (event->attr.read_format & PERF_FORMAT_ID)
375 entry += sizeof(u64);
377 if (event->attr.read_format & PERF_FORMAT_GROUP) {
378 nr += event->group_leader->nr_siblings;
383 event->read_size = size;
386 static void perf_event__header_size(struct perf_event *event)
388 struct perf_sample_data *data;
389 u64 sample_type = event->attr.sample_type;
392 perf_event__read_size(event);
394 if (sample_type & PERF_SAMPLE_IP)
395 size += sizeof(data->ip);
397 if (sample_type & PERF_SAMPLE_ADDR)
398 size += sizeof(data->addr);
400 if (sample_type & PERF_SAMPLE_PERIOD)
401 size += sizeof(data->period);
403 if (sample_type & PERF_SAMPLE_READ)
404 size += event->read_size;
406 event->header_size = size;
409 static void perf_event__id_header_size(struct perf_event *event)
411 struct perf_sample_data *data;
412 u64 sample_type = event->attr.sample_type;
415 if (sample_type & PERF_SAMPLE_TID)
416 size += sizeof(data->tid_entry);
418 if (sample_type & PERF_SAMPLE_TIME)
419 size += sizeof(data->time);
421 if (sample_type & PERF_SAMPLE_ID)
422 size += sizeof(data->id);
424 if (sample_type & PERF_SAMPLE_STREAM_ID)
425 size += sizeof(data->stream_id);
427 if (sample_type & PERF_SAMPLE_CPU)
428 size += sizeof(data->cpu_entry);
430 event->id_header_size = size;
433 static void perf_group_attach(struct perf_event *event)
435 struct perf_event *group_leader = event->group_leader, *pos;
438 * We can have double attach due to group movement in perf_event_open.
440 if (event->attach_state & PERF_ATTACH_GROUP)
443 event->attach_state |= PERF_ATTACH_GROUP;
445 if (group_leader == event)
448 if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
449 !is_software_event(event))
450 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
452 list_add_tail(&event->group_entry, &group_leader->sibling_list);
453 group_leader->nr_siblings++;
455 perf_event__header_size(group_leader);
457 list_for_each_entry(pos, &group_leader->sibling_list, group_entry)
458 perf_event__header_size(pos);
462 * Remove a event from the lists for its context.
463 * Must be called with ctx->mutex and ctx->lock held.
466 list_del_event(struct perf_event *event, struct perf_event_context *ctx)
469 * We can have double detach due to exit/hot-unplug + close.
471 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
474 event->attach_state &= ~PERF_ATTACH_CONTEXT;
477 if (event->attr.inherit_stat)
480 list_del_rcu(&event->event_entry);
482 if (event->group_leader == event)
483 list_del_init(&event->group_entry);
485 update_group_times(event);
488 * If event was in error state, then keep it
489 * that way, otherwise bogus counts will be
490 * returned on read(). The only way to get out
491 * of error state is by explicit re-enabling
494 if (event->state > PERF_EVENT_STATE_OFF)
495 event->state = PERF_EVENT_STATE_OFF;
498 static void perf_group_detach(struct perf_event *event)
500 struct perf_event *sibling, *tmp;
501 struct list_head *list = NULL;
504 * We can have double detach due to exit/hot-unplug + close.
506 if (!(event->attach_state & PERF_ATTACH_GROUP))
509 event->attach_state &= ~PERF_ATTACH_GROUP;
512 * If this is a sibling, remove it from its group.
514 if (event->group_leader != event) {
515 list_del_init(&event->group_entry);
516 event->group_leader->nr_siblings--;
520 if (!list_empty(&event->group_entry))
521 list = &event->group_entry;
524 * If this was a group event with sibling events then
525 * upgrade the siblings to singleton events by adding them
526 * to whatever list we are on.
528 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
530 list_move_tail(&sibling->group_entry, list);
531 sibling->group_leader = sibling;
533 /* Inherit group flags from the previous leader */
534 sibling->group_flags = event->group_flags;
538 perf_event__header_size(event->group_leader);
540 list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry)
541 perf_event__header_size(tmp);
545 event_filter_match(struct perf_event *event)
547 return event->cpu == -1 || event->cpu == smp_processor_id();
551 event_sched_out(struct perf_event *event,
552 struct perf_cpu_context *cpuctx,
553 struct perf_event_context *ctx)
555 u64 tstamp = perf_event_time(event);
558 * An event which could not be activated because of
559 * filter mismatch still needs to have its timings
560 * maintained, otherwise bogus information is return
561 * via read() for time_enabled, time_running:
563 if (event->state == PERF_EVENT_STATE_INACTIVE
564 && !event_filter_match(event)) {
565 delta = ctx->time - event->tstamp_stopped;
566 event->tstamp_running += delta;
567 event->tstamp_stopped = tstamp;
570 if (event->state != PERF_EVENT_STATE_ACTIVE)
573 event->state = PERF_EVENT_STATE_INACTIVE;
574 if (event->pending_disable) {
575 event->pending_disable = 0;
576 event->state = PERF_EVENT_STATE_OFF;
578 event->tstamp_stopped = tstamp;
579 event->pmu->del(event, 0);
582 if (!is_software_event(event))
583 cpuctx->active_oncpu--;
585 if (event->attr.exclusive || !cpuctx->active_oncpu)
586 cpuctx->exclusive = 0;
590 group_sched_out(struct perf_event *group_event,
591 struct perf_cpu_context *cpuctx,
592 struct perf_event_context *ctx)
594 struct perf_event *event;
595 int state = group_event->state;
597 event_sched_out(group_event, cpuctx, ctx);
600 * Schedule out siblings (if any):
602 list_for_each_entry(event, &group_event->sibling_list, group_entry)
603 event_sched_out(event, cpuctx, ctx);
605 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
606 cpuctx->exclusive = 0;
609 static inline struct perf_cpu_context *
610 __get_cpu_context(struct perf_event_context *ctx)
612 return this_cpu_ptr(ctx->pmu->pmu_cpu_context);
616 * Cross CPU call to remove a performance event
618 * We disable the event on the hardware level first. After that we
619 * remove it from the context list.
621 static void __perf_event_remove_from_context(void *info)
623 struct perf_event *event = info;
624 struct perf_event_context *ctx = event->ctx;
625 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
628 * If this is a task context, we need to check whether it is
629 * the current task context of this cpu. If not it has been
630 * scheduled out before the smp call arrived.
632 if (ctx->task && cpuctx->task_ctx != ctx)
635 raw_spin_lock(&ctx->lock);
637 event_sched_out(event, cpuctx, ctx);
639 list_del_event(event, ctx);
641 raw_spin_unlock(&ctx->lock);
646 * Remove the event from a task's (or a CPU's) list of events.
648 * Must be called with ctx->mutex held.
650 * CPU events are removed with a smp call. For task events we only
651 * call when the task is on a CPU.
653 * If event->ctx is a cloned context, callers must make sure that
654 * every task struct that event->ctx->task could possibly point to
655 * remains valid. This is OK when called from perf_release since
656 * that only calls us on the top-level context, which can't be a clone.
657 * When called from perf_event_exit_task, it's OK because the
658 * context has been detached from its task.
660 static void perf_event_remove_from_context(struct perf_event *event)
662 struct perf_event_context *ctx = event->ctx;
663 struct task_struct *task = ctx->task;
667 * Per cpu events are removed via an smp call and
668 * the removal is always successful.
670 smp_call_function_single(event->cpu,
671 __perf_event_remove_from_context,
677 task_oncpu_function_call(task, __perf_event_remove_from_context,
680 raw_spin_lock_irq(&ctx->lock);
682 * If the context is active we need to retry the smp call.
684 if (ctx->nr_active && !list_empty(&event->group_entry)) {
685 raw_spin_unlock_irq(&ctx->lock);
690 * The lock prevents that this context is scheduled in so we
691 * can remove the event safely, if the call above did not
694 if (!list_empty(&event->group_entry))
695 list_del_event(event, ctx);
696 raw_spin_unlock_irq(&ctx->lock);
700 * Cross CPU call to disable a performance event
702 static void __perf_event_disable(void *info)
704 struct perf_event *event = info;
705 struct perf_event_context *ctx = event->ctx;
706 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
709 * If this is a per-task event, need to check whether this
710 * event's task is the current task on this cpu.
712 if (ctx->task && cpuctx->task_ctx != ctx)
715 raw_spin_lock(&ctx->lock);
718 * If the event is on, turn it off.
719 * If it is in error state, leave it in error state.
721 if (event->state >= PERF_EVENT_STATE_INACTIVE) {
722 update_context_time(ctx);
723 update_group_times(event);
724 if (event == event->group_leader)
725 group_sched_out(event, cpuctx, ctx);
727 event_sched_out(event, cpuctx, ctx);
728 event->state = PERF_EVENT_STATE_OFF;
731 raw_spin_unlock(&ctx->lock);
737 * If event->ctx is a cloned context, callers must make sure that
738 * every task struct that event->ctx->task could possibly point to
739 * remains valid. This condition is satisifed when called through
740 * perf_event_for_each_child or perf_event_for_each because they
741 * hold the top-level event's child_mutex, so any descendant that
742 * goes to exit will block in sync_child_event.
743 * When called from perf_pending_event it's OK because event->ctx
744 * is the current context on this CPU and preemption is disabled,
745 * hence we can't get into perf_event_task_sched_out for this context.
747 void perf_event_disable(struct perf_event *event)
749 struct perf_event_context *ctx = event->ctx;
750 struct task_struct *task = ctx->task;
754 * Disable the event on the cpu that it's on
756 smp_call_function_single(event->cpu, __perf_event_disable,
762 task_oncpu_function_call(task, __perf_event_disable, event);
764 raw_spin_lock_irq(&ctx->lock);
766 * If the event is still active, we need to retry the cross-call.
768 if (event->state == PERF_EVENT_STATE_ACTIVE) {
769 raw_spin_unlock_irq(&ctx->lock);
774 * Since we have the lock this context can't be scheduled
775 * in, so we can change the state safely.
777 if (event->state == PERF_EVENT_STATE_INACTIVE) {
778 update_group_times(event);
779 event->state = PERF_EVENT_STATE_OFF;
782 raw_spin_unlock_irq(&ctx->lock);
786 event_sched_in(struct perf_event *event,
787 struct perf_cpu_context *cpuctx,
788 struct perf_event_context *ctx)
790 u64 tstamp = perf_event_time(event);
792 if (event->state <= PERF_EVENT_STATE_OFF)
795 event->state = PERF_EVENT_STATE_ACTIVE;
796 event->oncpu = smp_processor_id();
798 * The new state must be visible before we turn it on in the hardware:
802 if (event->pmu->add(event, PERF_EF_START)) {
803 event->state = PERF_EVENT_STATE_INACTIVE;
808 event->tstamp_running += tstamp - event->tstamp_stopped;
810 event->shadow_ctx_time = tstamp - ctx->timestamp;
812 if (!is_software_event(event))
813 cpuctx->active_oncpu++;
816 if (event->attr.exclusive)
817 cpuctx->exclusive = 1;
823 group_sched_in(struct perf_event *group_event,
824 struct perf_cpu_context *cpuctx,
825 struct perf_event_context *ctx)
827 struct perf_event *event, *partial_group = NULL;
828 struct pmu *pmu = group_event->pmu;
830 bool simulate = false;
832 if (group_event->state == PERF_EVENT_STATE_OFF)
837 if (event_sched_in(group_event, cpuctx, ctx)) {
838 pmu->cancel_txn(pmu);
843 * Schedule in siblings as one group (if any):
845 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
846 if (event_sched_in(event, cpuctx, ctx)) {
847 partial_group = event;
852 if (!pmu->commit_txn(pmu))
857 * Groups can be scheduled in as one unit only, so undo any
858 * partial group before returning:
859 * The events up to the failed event are scheduled out normally,
860 * tstamp_stopped will be updated.
862 * The failed events and the remaining siblings need to have
863 * their timings updated as if they had gone thru event_sched_in()
864 * and event_sched_out(). This is required to get consistent timings
865 * across the group. This also takes care of the case where the group
866 * could never be scheduled by ensuring tstamp_stopped is set to mark
867 * the time the event was actually stopped, such that time delta
868 * calculation in update_event_times() is correct.
870 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
871 if (event == partial_group)
875 event->tstamp_running += now - event->tstamp_stopped;
876 event->tstamp_stopped = now;
878 event_sched_out(event, cpuctx, ctx);
881 event_sched_out(group_event, cpuctx, ctx);
883 pmu->cancel_txn(pmu);
889 * Work out whether we can put this event group on the CPU now.
891 static int group_can_go_on(struct perf_event *event,
892 struct perf_cpu_context *cpuctx,
896 * Groups consisting entirely of software events can always go on.
898 if (event->group_flags & PERF_GROUP_SOFTWARE)
901 * If an exclusive group is already on, no other hardware
904 if (cpuctx->exclusive)
907 * If this group is exclusive and there are already
908 * events on the CPU, it can't go on.
910 if (event->attr.exclusive && cpuctx->active_oncpu)
913 * Otherwise, try to add it if all previous groups were able
919 static void add_event_to_ctx(struct perf_event *event,
920 struct perf_event_context *ctx)
922 u64 tstamp = perf_event_time(event);
924 list_add_event(event, ctx);
925 perf_group_attach(event);
926 event->tstamp_enabled = tstamp;
927 event->tstamp_running = tstamp;
928 event->tstamp_stopped = tstamp;
932 * Cross CPU call to install and enable a performance event
934 * Must be called with ctx->mutex held
936 static void __perf_install_in_context(void *info)
938 struct perf_event *event = info;
939 struct perf_event_context *ctx = event->ctx;
940 struct perf_event *leader = event->group_leader;
941 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
945 * If this is a task context, we need to check whether it is
946 * the current task context of this cpu. If not it has been
947 * scheduled out before the smp call arrived.
948 * Or possibly this is the right context but it isn't
949 * on this cpu because it had no events.
951 if (ctx->task && cpuctx->task_ctx != ctx) {
952 if (cpuctx->task_ctx || ctx->task != current)
954 cpuctx->task_ctx = ctx;
957 raw_spin_lock(&ctx->lock);
959 update_context_time(ctx);
961 add_event_to_ctx(event, ctx);
963 if (!event_filter_match(event))
967 * Don't put the event on if it is disabled or if
968 * it is in a group and the group isn't on.
970 if (event->state != PERF_EVENT_STATE_INACTIVE ||
971 (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE))
975 * An exclusive event can't go on if there are already active
976 * hardware events, and no hardware event can go on if there
977 * is already an exclusive event on.
979 if (!group_can_go_on(event, cpuctx, 1))
982 err = event_sched_in(event, cpuctx, ctx);
986 * This event couldn't go on. If it is in a group
987 * then we have to pull the whole group off.
988 * If the event group is pinned then put it in error state.
991 group_sched_out(leader, cpuctx, ctx);
992 if (leader->attr.pinned) {
993 update_group_times(leader);
994 leader->state = PERF_EVENT_STATE_ERROR;
999 raw_spin_unlock(&ctx->lock);
1003 * Attach a performance event to a context
1005 * First we add the event to the list with the hardware enable bit
1006 * in event->hw_config cleared.
1008 * If the event is attached to a task which is on a CPU we use a smp
1009 * call to enable it in the task context. The task might have been
1010 * scheduled away, but we check this in the smp call again.
1012 * Must be called with ctx->mutex held.
1015 perf_install_in_context(struct perf_event_context *ctx,
1016 struct perf_event *event,
1019 struct task_struct *task = ctx->task;
1025 * Per cpu events are installed via an smp call and
1026 * the install is always successful.
1028 smp_call_function_single(cpu, __perf_install_in_context,
1034 task_oncpu_function_call(task, __perf_install_in_context,
1037 raw_spin_lock_irq(&ctx->lock);
1039 * we need to retry the smp call.
1041 if (ctx->is_active && list_empty(&event->group_entry)) {
1042 raw_spin_unlock_irq(&ctx->lock);
1047 * The lock prevents that this context is scheduled in so we
1048 * can add the event safely, if it the call above did not
1051 if (list_empty(&event->group_entry))
1052 add_event_to_ctx(event, ctx);
1053 raw_spin_unlock_irq(&ctx->lock);
1057 * Put a event into inactive state and update time fields.
1058 * Enabling the leader of a group effectively enables all
1059 * the group members that aren't explicitly disabled, so we
1060 * have to update their ->tstamp_enabled also.
1061 * Note: this works for group members as well as group leaders
1062 * since the non-leader members' sibling_lists will be empty.
1064 static void __perf_event_mark_enabled(struct perf_event *event,
1065 struct perf_event_context *ctx)
1067 struct perf_event *sub;
1068 u64 tstamp = perf_event_time(event);
1070 event->state = PERF_EVENT_STATE_INACTIVE;
1071 event->tstamp_enabled = tstamp - event->total_time_enabled;
1072 list_for_each_entry(sub, &event->sibling_list, group_entry) {
1073 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
1074 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
1079 * Cross CPU call to enable a performance event
1081 static void __perf_event_enable(void *info)
1083 struct perf_event *event = info;
1084 struct perf_event_context *ctx = event->ctx;
1085 struct perf_event *leader = event->group_leader;
1086 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1090 * If this is a per-task event, need to check whether this
1091 * event's task is the current task on this cpu.
1093 if (ctx->task && cpuctx->task_ctx != ctx) {
1094 if (cpuctx->task_ctx || ctx->task != current)
1096 cpuctx->task_ctx = ctx;
1099 raw_spin_lock(&ctx->lock);
1101 update_context_time(ctx);
1103 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1105 __perf_event_mark_enabled(event, ctx);
1107 if (!event_filter_match(event))
1111 * If the event is in a group and isn't the group leader,
1112 * then don't put it on unless the group is on.
1114 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
1117 if (!group_can_go_on(event, cpuctx, 1)) {
1120 if (event == leader)
1121 err = group_sched_in(event, cpuctx, ctx);
1123 err = event_sched_in(event, cpuctx, ctx);
1128 * If this event can't go on and it's part of a
1129 * group, then the whole group has to come off.
1131 if (leader != event)
1132 group_sched_out(leader, cpuctx, ctx);
1133 if (leader->attr.pinned) {
1134 update_group_times(leader);
1135 leader->state = PERF_EVENT_STATE_ERROR;
1140 raw_spin_unlock(&ctx->lock);
1146 * If event->ctx is a cloned context, callers must make sure that
1147 * every task struct that event->ctx->task could possibly point to
1148 * remains valid. This condition is satisfied when called through
1149 * perf_event_for_each_child or perf_event_for_each as described
1150 * for perf_event_disable.
1152 void perf_event_enable(struct perf_event *event)
1154 struct perf_event_context *ctx = event->ctx;
1155 struct task_struct *task = ctx->task;
1159 * Enable the event on the cpu that it's on
1161 smp_call_function_single(event->cpu, __perf_event_enable,
1166 raw_spin_lock_irq(&ctx->lock);
1167 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1171 * If the event is in error state, clear that first.
1172 * That way, if we see the event in error state below, we
1173 * know that it has gone back into error state, as distinct
1174 * from the task having been scheduled away before the
1175 * cross-call arrived.
1177 if (event->state == PERF_EVENT_STATE_ERROR)
1178 event->state = PERF_EVENT_STATE_OFF;
1181 raw_spin_unlock_irq(&ctx->lock);
1182 task_oncpu_function_call(task, __perf_event_enable, event);
1184 raw_spin_lock_irq(&ctx->lock);
1187 * If the context is active and the event is still off,
1188 * we need to retry the cross-call.
1190 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF)
1194 * Since we have the lock this context can't be scheduled
1195 * in, so we can change the state safely.
1197 if (event->state == PERF_EVENT_STATE_OFF)
1198 __perf_event_mark_enabled(event, ctx);
1201 raw_spin_unlock_irq(&ctx->lock);
1204 static int perf_event_refresh(struct perf_event *event, int refresh)
1207 * not supported on inherited events
1209 if (event->attr.inherit || !is_sampling_event(event))
1212 atomic_add(refresh, &event->event_limit);
1213 perf_event_enable(event);
1218 static void ctx_sched_out(struct perf_event_context *ctx,
1219 struct perf_cpu_context *cpuctx,
1220 enum event_type_t event_type)
1222 struct perf_event *event;
1224 raw_spin_lock(&ctx->lock);
1225 perf_pmu_disable(ctx->pmu);
1227 if (likely(!ctx->nr_events))
1229 update_context_time(ctx);
1231 if (!ctx->nr_active)
1234 if (event_type & EVENT_PINNED) {
1235 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
1236 group_sched_out(event, cpuctx, ctx);
1239 if (event_type & EVENT_FLEXIBLE) {
1240 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
1241 group_sched_out(event, cpuctx, ctx);
1244 perf_pmu_enable(ctx->pmu);
1245 raw_spin_unlock(&ctx->lock);
1249 * Test whether two contexts are equivalent, i.e. whether they
1250 * have both been cloned from the same version of the same context
1251 * and they both have the same number of enabled events.
1252 * If the number of enabled events is the same, then the set
1253 * of enabled events should be the same, because these are both
1254 * inherited contexts, therefore we can't access individual events
1255 * in them directly with an fd; we can only enable/disable all
1256 * events via prctl, or enable/disable all events in a family
1257 * via ioctl, which will have the same effect on both contexts.
1259 static int context_equiv(struct perf_event_context *ctx1,
1260 struct perf_event_context *ctx2)
1262 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
1263 && ctx1->parent_gen == ctx2->parent_gen
1264 && !ctx1->pin_count && !ctx2->pin_count;
1267 static void __perf_event_sync_stat(struct perf_event *event,
1268 struct perf_event *next_event)
1272 if (!event->attr.inherit_stat)
1276 * Update the event value, we cannot use perf_event_read()
1277 * because we're in the middle of a context switch and have IRQs
1278 * disabled, which upsets smp_call_function_single(), however
1279 * we know the event must be on the current CPU, therefore we
1280 * don't need to use it.
1282 switch (event->state) {
1283 case PERF_EVENT_STATE_ACTIVE:
1284 event->pmu->read(event);
1287 case PERF_EVENT_STATE_INACTIVE:
1288 update_event_times(event);
1296 * In order to keep per-task stats reliable we need to flip the event
1297 * values when we flip the contexts.
1299 value = local64_read(&next_event->count);
1300 value = local64_xchg(&event->count, value);
1301 local64_set(&next_event->count, value);
1303 swap(event->total_time_enabled, next_event->total_time_enabled);
1304 swap(event->total_time_running, next_event->total_time_running);
1307 * Since we swizzled the values, update the user visible data too.
1309 perf_event_update_userpage(event);
1310 perf_event_update_userpage(next_event);
1313 #define list_next_entry(pos, member) \
1314 list_entry(pos->member.next, typeof(*pos), member)
1316 static void perf_event_sync_stat(struct perf_event_context *ctx,
1317 struct perf_event_context *next_ctx)
1319 struct perf_event *event, *next_event;
1324 update_context_time(ctx);
1326 event = list_first_entry(&ctx->event_list,
1327 struct perf_event, event_entry);
1329 next_event = list_first_entry(&next_ctx->event_list,
1330 struct perf_event, event_entry);
1332 while (&event->event_entry != &ctx->event_list &&
1333 &next_event->event_entry != &next_ctx->event_list) {
1335 __perf_event_sync_stat(event, next_event);
1337 event = list_next_entry(event, event_entry);
1338 next_event = list_next_entry(next_event, event_entry);
1342 void perf_event_context_sched_out(struct task_struct *task, int ctxn,
1343 struct task_struct *next)
1345 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
1346 struct perf_event_context *next_ctx;
1347 struct perf_event_context *parent;
1348 struct perf_cpu_context *cpuctx;
1354 cpuctx = __get_cpu_context(ctx);
1355 if (!cpuctx->task_ctx)
1359 parent = rcu_dereference(ctx->parent_ctx);
1360 next_ctx = next->perf_event_ctxp[ctxn];
1361 if (parent && next_ctx &&
1362 rcu_dereference(next_ctx->parent_ctx) == parent) {
1364 * Looks like the two contexts are clones, so we might be
1365 * able to optimize the context switch. We lock both
1366 * contexts and check that they are clones under the
1367 * lock (including re-checking that neither has been
1368 * uncloned in the meantime). It doesn't matter which
1369 * order we take the locks because no other cpu could
1370 * be trying to lock both of these tasks.
1372 raw_spin_lock(&ctx->lock);
1373 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
1374 if (context_equiv(ctx, next_ctx)) {
1376 * XXX do we need a memory barrier of sorts
1377 * wrt to rcu_dereference() of perf_event_ctxp
1379 task->perf_event_ctxp[ctxn] = next_ctx;
1380 next->perf_event_ctxp[ctxn] = ctx;
1382 next_ctx->task = task;
1385 perf_event_sync_stat(ctx, next_ctx);
1387 raw_spin_unlock(&next_ctx->lock);
1388 raw_spin_unlock(&ctx->lock);
1393 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
1394 cpuctx->task_ctx = NULL;
1398 #define for_each_task_context_nr(ctxn) \
1399 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
1402 * Called from scheduler to remove the events of the current task,
1403 * with interrupts disabled.
1405 * We stop each event and update the event value in event->count.
1407 * This does not protect us against NMI, but disable()
1408 * sets the disabled bit in the control field of event _before_
1409 * accessing the event control register. If a NMI hits, then it will
1410 * not restart the event.
1412 void __perf_event_task_sched_out(struct task_struct *task,
1413 struct task_struct *next)
1417 for_each_task_context_nr(ctxn)
1418 perf_event_context_sched_out(task, ctxn, next);
1421 static void task_ctx_sched_out(struct perf_event_context *ctx,
1422 enum event_type_t event_type)
1424 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1426 if (!cpuctx->task_ctx)
1429 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
1432 ctx_sched_out(ctx, cpuctx, event_type);
1433 cpuctx->task_ctx = NULL;
1437 * Called with IRQs disabled
1439 static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
1440 enum event_type_t event_type)
1442 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
1446 ctx_pinned_sched_in(struct perf_event_context *ctx,
1447 struct perf_cpu_context *cpuctx)
1449 struct perf_event *event;
1451 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
1452 if (event->state <= PERF_EVENT_STATE_OFF)
1454 if (!event_filter_match(event))
1457 if (group_can_go_on(event, cpuctx, 1))
1458 group_sched_in(event, cpuctx, ctx);
1461 * If this pinned group hasn't been scheduled,
1462 * put it in error state.
1464 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1465 update_group_times(event);
1466 event->state = PERF_EVENT_STATE_ERROR;
1472 ctx_flexible_sched_in(struct perf_event_context *ctx,
1473 struct perf_cpu_context *cpuctx)
1475 struct perf_event *event;
1478 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
1479 /* Ignore events in OFF or ERROR state */
1480 if (event->state <= PERF_EVENT_STATE_OFF)
1483 * Listen to the 'cpu' scheduling filter constraint
1486 if (!event_filter_match(event))
1489 if (group_can_go_on(event, cpuctx, can_add_hw)) {
1490 if (group_sched_in(event, cpuctx, ctx))
1497 ctx_sched_in(struct perf_event_context *ctx,
1498 struct perf_cpu_context *cpuctx,
1499 enum event_type_t event_type)
1501 raw_spin_lock(&ctx->lock);
1503 if (likely(!ctx->nr_events))
1506 ctx->timestamp = perf_clock();
1509 * First go through the list and put on any pinned groups
1510 * in order to give them the best chance of going on.
1512 if (event_type & EVENT_PINNED)
1513 ctx_pinned_sched_in(ctx, cpuctx);
1515 /* Then walk through the lower prio flexible groups */
1516 if (event_type & EVENT_FLEXIBLE)
1517 ctx_flexible_sched_in(ctx, cpuctx);
1520 raw_spin_unlock(&ctx->lock);
1523 static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
1524 enum event_type_t event_type)
1526 struct perf_event_context *ctx = &cpuctx->ctx;
1528 ctx_sched_in(ctx, cpuctx, event_type);
1531 static void task_ctx_sched_in(struct perf_event_context *ctx,
1532 enum event_type_t event_type)
1534 struct perf_cpu_context *cpuctx;
1536 cpuctx = __get_cpu_context(ctx);
1537 if (cpuctx->task_ctx == ctx)
1540 ctx_sched_in(ctx, cpuctx, event_type);
1541 cpuctx->task_ctx = ctx;
1544 void perf_event_context_sched_in(struct perf_event_context *ctx)
1546 struct perf_cpu_context *cpuctx;
1548 cpuctx = __get_cpu_context(ctx);
1549 if (cpuctx->task_ctx == ctx)
1552 perf_pmu_disable(ctx->pmu);
1554 * We want to keep the following priority order:
1555 * cpu pinned (that don't need to move), task pinned,
1556 * cpu flexible, task flexible.
1558 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
1560 ctx_sched_in(ctx, cpuctx, EVENT_PINNED);
1561 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE);
1562 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE);
1564 cpuctx->task_ctx = ctx;
1567 * Since these rotations are per-cpu, we need to ensure the
1568 * cpu-context we got scheduled on is actually rotating.
1570 perf_pmu_rotate_start(ctx->pmu);
1571 perf_pmu_enable(ctx->pmu);
1575 * Called from scheduler to add the events of the current task
1576 * with interrupts disabled.
1578 * We restore the event value and then enable it.
1580 * This does not protect us against NMI, but enable()
1581 * sets the enabled bit in the control field of event _before_
1582 * accessing the event control register. If a NMI hits, then it will
1583 * keep the event running.
1585 void __perf_event_task_sched_in(struct task_struct *task)
1587 struct perf_event_context *ctx;
1590 for_each_task_context_nr(ctxn) {
1591 ctx = task->perf_event_ctxp[ctxn];
1595 perf_event_context_sched_in(ctx);
1599 #define MAX_INTERRUPTS (~0ULL)
1601 static void perf_log_throttle(struct perf_event *event, int enable);
1603 static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
1605 u64 frequency = event->attr.sample_freq;
1606 u64 sec = NSEC_PER_SEC;
1607 u64 divisor, dividend;
1609 int count_fls, nsec_fls, frequency_fls, sec_fls;
1611 count_fls = fls64(count);
1612 nsec_fls = fls64(nsec);
1613 frequency_fls = fls64(frequency);
1617 * We got @count in @nsec, with a target of sample_freq HZ
1618 * the target period becomes:
1621 * period = -------------------
1622 * @nsec * sample_freq
1627 * Reduce accuracy by one bit such that @a and @b converge
1628 * to a similar magnitude.
1630 #define REDUCE_FLS(a, b) \
1632 if (a##_fls > b##_fls) { \
1642 * Reduce accuracy until either term fits in a u64, then proceed with
1643 * the other, so that finally we can do a u64/u64 division.
1645 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
1646 REDUCE_FLS(nsec, frequency);
1647 REDUCE_FLS(sec, count);
1650 if (count_fls + sec_fls > 64) {
1651 divisor = nsec * frequency;
1653 while (count_fls + sec_fls > 64) {
1654 REDUCE_FLS(count, sec);
1658 dividend = count * sec;
1660 dividend = count * sec;
1662 while (nsec_fls + frequency_fls > 64) {
1663 REDUCE_FLS(nsec, frequency);
1667 divisor = nsec * frequency;
1673 return div64_u64(dividend, divisor);
1676 static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count)
1678 struct hw_perf_event *hwc = &event->hw;
1679 s64 period, sample_period;
1682 period = perf_calculate_period(event, nsec, count);
1684 delta = (s64)(period - hwc->sample_period);
1685 delta = (delta + 7) / 8; /* low pass filter */
1687 sample_period = hwc->sample_period + delta;
1692 hwc->sample_period = sample_period;
1694 if (local64_read(&hwc->period_left) > 8*sample_period) {
1695 event->pmu->stop(event, PERF_EF_UPDATE);
1696 local64_set(&hwc->period_left, 0);
1697 event->pmu->start(event, PERF_EF_RELOAD);
1701 static void perf_ctx_adjust_freq(struct perf_event_context *ctx, u64 period)
1703 struct perf_event *event;
1704 struct hw_perf_event *hwc;
1705 u64 interrupts, now;
1708 raw_spin_lock(&ctx->lock);
1709 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
1710 if (event->state != PERF_EVENT_STATE_ACTIVE)
1713 if (!event_filter_match(event))
1718 interrupts = hwc->interrupts;
1719 hwc->interrupts = 0;
1722 * unthrottle events on the tick
1724 if (interrupts == MAX_INTERRUPTS) {
1725 perf_log_throttle(event, 1);
1726 event->pmu->start(event, 0);
1729 if (!event->attr.freq || !event->attr.sample_freq)
1732 event->pmu->read(event);
1733 now = local64_read(&event->count);
1734 delta = now - hwc->freq_count_stamp;
1735 hwc->freq_count_stamp = now;
1738 perf_adjust_period(event, period, delta);
1740 raw_spin_unlock(&ctx->lock);
1744 * Round-robin a context's events:
1746 static void rotate_ctx(struct perf_event_context *ctx)
1748 raw_spin_lock(&ctx->lock);
1751 * Rotate the first entry last of non-pinned groups. Rotation might be
1752 * disabled by the inheritance code.
1754 if (!ctx->rotate_disable)
1755 list_rotate_left(&ctx->flexible_groups);
1757 raw_spin_unlock(&ctx->lock);
1761 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
1762 * because they're strictly cpu affine and rotate_start is called with IRQs
1763 * disabled, while rotate_context is called from IRQ context.
1765 static void perf_rotate_context(struct perf_cpu_context *cpuctx)
1767 u64 interval = (u64)cpuctx->jiffies_interval * TICK_NSEC;
1768 struct perf_event_context *ctx = NULL;
1769 int rotate = 0, remove = 1;
1771 if (cpuctx->ctx.nr_events) {
1773 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
1777 ctx = cpuctx->task_ctx;
1778 if (ctx && ctx->nr_events) {
1780 if (ctx->nr_events != ctx->nr_active)
1784 perf_pmu_disable(cpuctx->ctx.pmu);
1785 perf_ctx_adjust_freq(&cpuctx->ctx, interval);
1787 perf_ctx_adjust_freq(ctx, interval);
1792 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
1794 task_ctx_sched_out(ctx, EVENT_FLEXIBLE);
1796 rotate_ctx(&cpuctx->ctx);
1800 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE);
1802 task_ctx_sched_in(ctx, EVENT_FLEXIBLE);
1806 list_del_init(&cpuctx->rotation_list);
1808 perf_pmu_enable(cpuctx->ctx.pmu);
1811 void perf_event_task_tick(void)
1813 struct list_head *head = &__get_cpu_var(rotation_list);
1814 struct perf_cpu_context *cpuctx, *tmp;
1816 WARN_ON(!irqs_disabled());
1818 list_for_each_entry_safe(cpuctx, tmp, head, rotation_list) {
1819 if (cpuctx->jiffies_interval == 1 ||
1820 !(jiffies % cpuctx->jiffies_interval))
1821 perf_rotate_context(cpuctx);
1825 static int event_enable_on_exec(struct perf_event *event,
1826 struct perf_event_context *ctx)
1828 if (!event->attr.enable_on_exec)
1831 event->attr.enable_on_exec = 0;
1832 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1835 __perf_event_mark_enabled(event, ctx);
1841 * Enable all of a task's events that have been marked enable-on-exec.
1842 * This expects task == current.
1844 static void perf_event_enable_on_exec(struct perf_event_context *ctx)
1846 struct perf_event *event;
1847 unsigned long flags;
1851 local_irq_save(flags);
1852 if (!ctx || !ctx->nr_events)
1855 task_ctx_sched_out(ctx, EVENT_ALL);
1857 raw_spin_lock(&ctx->lock);
1859 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
1860 ret = event_enable_on_exec(event, ctx);
1865 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
1866 ret = event_enable_on_exec(event, ctx);
1872 * Unclone this context if we enabled any event.
1877 raw_spin_unlock(&ctx->lock);
1879 perf_event_context_sched_in(ctx);
1881 local_irq_restore(flags);
1885 * Cross CPU call to read the hardware event
1887 static void __perf_event_read(void *info)
1889 struct perf_event *event = info;
1890 struct perf_event_context *ctx = event->ctx;
1891 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1894 * If this is a task context, we need to check whether it is
1895 * the current task context of this cpu. If not it has been
1896 * scheduled out before the smp call arrived. In that case
1897 * event->count would have been updated to a recent sample
1898 * when the event was scheduled out.
1900 if (ctx->task && cpuctx->task_ctx != ctx)
1903 raw_spin_lock(&ctx->lock);
1904 update_context_time(ctx);
1905 update_event_times(event);
1906 raw_spin_unlock(&ctx->lock);
1908 event->pmu->read(event);
1911 static inline u64 perf_event_count(struct perf_event *event)
1913 return local64_read(&event->count) + atomic64_read(&event->child_count);
1916 static u64 perf_event_read(struct perf_event *event)
1919 * If event is enabled and currently active on a CPU, update the
1920 * value in the event structure:
1922 if (event->state == PERF_EVENT_STATE_ACTIVE) {
1923 smp_call_function_single(event->oncpu,
1924 __perf_event_read, event, 1);
1925 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
1926 struct perf_event_context *ctx = event->ctx;
1927 unsigned long flags;
1929 raw_spin_lock_irqsave(&ctx->lock, flags);
1931 * may read while context is not active
1932 * (e.g., thread is blocked), in that case
1933 * we cannot update context time
1936 update_context_time(ctx);
1937 update_event_times(event);
1938 raw_spin_unlock_irqrestore(&ctx->lock, flags);
1941 return perf_event_count(event);
1948 struct callchain_cpus_entries {
1949 struct rcu_head rcu_head;
1950 struct perf_callchain_entry *cpu_entries[0];
1953 static DEFINE_PER_CPU(int, callchain_recursion[PERF_NR_CONTEXTS]);
1954 static atomic_t nr_callchain_events;
1955 static DEFINE_MUTEX(callchain_mutex);
1956 struct callchain_cpus_entries *callchain_cpus_entries;
1959 __weak void perf_callchain_kernel(struct perf_callchain_entry *entry,
1960 struct pt_regs *regs)
1964 __weak void perf_callchain_user(struct perf_callchain_entry *entry,
1965 struct pt_regs *regs)
1969 static void release_callchain_buffers_rcu(struct rcu_head *head)
1971 struct callchain_cpus_entries *entries;
1974 entries = container_of(head, struct callchain_cpus_entries, rcu_head);
1976 for_each_possible_cpu(cpu)
1977 kfree(entries->cpu_entries[cpu]);
1982 static void release_callchain_buffers(void)
1984 struct callchain_cpus_entries *entries;
1986 entries = callchain_cpus_entries;
1987 rcu_assign_pointer(callchain_cpus_entries, NULL);
1988 call_rcu(&entries->rcu_head, release_callchain_buffers_rcu);
1991 static int alloc_callchain_buffers(void)
1995 struct callchain_cpus_entries *entries;
1998 * We can't use the percpu allocation API for data that can be
1999 * accessed from NMI. Use a temporary manual per cpu allocation
2000 * until that gets sorted out.
2002 size = offsetof(struct callchain_cpus_entries, cpu_entries[nr_cpu_ids]);
2004 entries = kzalloc(size, GFP_KERNEL);
2008 size = sizeof(struct perf_callchain_entry) * PERF_NR_CONTEXTS;
2010 for_each_possible_cpu(cpu) {
2011 entries->cpu_entries[cpu] = kmalloc_node(size, GFP_KERNEL,
2013 if (!entries->cpu_entries[cpu])
2017 rcu_assign_pointer(callchain_cpus_entries, entries);
2022 for_each_possible_cpu(cpu)
2023 kfree(entries->cpu_entries[cpu]);
2029 static int get_callchain_buffers(void)
2034 mutex_lock(&callchain_mutex);
2036 count = atomic_inc_return(&nr_callchain_events);
2037 if (WARN_ON_ONCE(count < 1)) {
2043 /* If the allocation failed, give up */
2044 if (!callchain_cpus_entries)
2049 err = alloc_callchain_buffers();
2051 release_callchain_buffers();
2053 mutex_unlock(&callchain_mutex);
2058 static void put_callchain_buffers(void)
2060 if (atomic_dec_and_mutex_lock(&nr_callchain_events, &callchain_mutex)) {
2061 release_callchain_buffers();
2062 mutex_unlock(&callchain_mutex);
2066 static int get_recursion_context(int *recursion)
2074 else if (in_softirq())
2079 if (recursion[rctx])
2088 static inline void put_recursion_context(int *recursion, int rctx)
2094 static struct perf_callchain_entry *get_callchain_entry(int *rctx)
2097 struct callchain_cpus_entries *entries;
2099 *rctx = get_recursion_context(__get_cpu_var(callchain_recursion));
2103 entries = rcu_dereference(callchain_cpus_entries);
2107 cpu = smp_processor_id();
2109 return &entries->cpu_entries[cpu][*rctx];
2113 put_callchain_entry(int rctx)
2115 put_recursion_context(__get_cpu_var(callchain_recursion), rctx);
2118 static struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
2121 struct perf_callchain_entry *entry;
2124 entry = get_callchain_entry(&rctx);
2133 if (!user_mode(regs)) {
2134 perf_callchain_store(entry, PERF_CONTEXT_KERNEL);
2135 perf_callchain_kernel(entry, regs);
2137 regs = task_pt_regs(current);
2143 perf_callchain_store(entry, PERF_CONTEXT_USER);
2144 perf_callchain_user(entry, regs);
2148 put_callchain_entry(rctx);
2154 * Initialize the perf_event context in a task_struct:
2156 static void __perf_event_init_context(struct perf_event_context *ctx)
2158 raw_spin_lock_init(&ctx->lock);
2159 mutex_init(&ctx->mutex);
2160 INIT_LIST_HEAD(&ctx->pinned_groups);
2161 INIT_LIST_HEAD(&ctx->flexible_groups);
2162 INIT_LIST_HEAD(&ctx->event_list);
2163 atomic_set(&ctx->refcount, 1);
2166 static struct perf_event_context *
2167 alloc_perf_context(struct pmu *pmu, struct task_struct *task)
2169 struct perf_event_context *ctx;
2171 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
2175 __perf_event_init_context(ctx);
2178 get_task_struct(task);
2185 static struct task_struct *
2186 find_lively_task_by_vpid(pid_t vpid)
2188 struct task_struct *task;
2195 task = find_task_by_vpid(vpid);
2197 get_task_struct(task);
2201 return ERR_PTR(-ESRCH);
2203 /* Reuse ptrace permission checks for now. */
2205 if (!ptrace_may_access(task, PTRACE_MODE_READ))
2210 put_task_struct(task);
2211 return ERR_PTR(err);
2215 static struct perf_event_context *
2216 find_get_context(struct pmu *pmu, struct task_struct *task, int cpu)
2218 struct perf_event_context *ctx;
2219 struct perf_cpu_context *cpuctx;
2220 unsigned long flags;
2224 /* Must be root to operate on a CPU event: */
2225 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
2226 return ERR_PTR(-EACCES);
2229 * We could be clever and allow to attach a event to an
2230 * offline CPU and activate it when the CPU comes up, but
2233 if (!cpu_online(cpu))
2234 return ERR_PTR(-ENODEV);
2236 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
2244 ctxn = pmu->task_ctx_nr;
2249 ctx = perf_lock_task_context(task, ctxn, &flags);
2252 raw_spin_unlock_irqrestore(&ctx->lock, flags);
2256 ctx = alloc_perf_context(pmu, task);
2264 mutex_lock(&task->perf_event_mutex);
2266 * If it has already passed perf_event_exit_task().
2267 * we must see PF_EXITING, it takes this mutex too.
2269 if (task->flags & PF_EXITING)
2271 else if (task->perf_event_ctxp[ctxn])
2274 rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
2275 mutex_unlock(&task->perf_event_mutex);
2277 if (unlikely(err)) {
2278 put_task_struct(task);
2290 return ERR_PTR(err);
2293 static void perf_event_free_filter(struct perf_event *event);
2295 static void free_event_rcu(struct rcu_head *head)
2297 struct perf_event *event;
2299 event = container_of(head, struct perf_event, rcu_head);
2301 put_pid_ns(event->ns);
2302 perf_event_free_filter(event);
2306 static void perf_buffer_put(struct perf_buffer *buffer);
2308 static void free_event(struct perf_event *event)
2310 irq_work_sync(&event->pending);
2312 if (!event->parent) {
2313 if (event->attach_state & PERF_ATTACH_TASK)
2314 jump_label_dec(&perf_task_events);
2315 if (event->attr.mmap || event->attr.mmap_data)
2316 atomic_dec(&nr_mmap_events);
2317 if (event->attr.comm)
2318 atomic_dec(&nr_comm_events);
2319 if (event->attr.task)
2320 atomic_dec(&nr_task_events);
2321 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
2322 put_callchain_buffers();
2325 if (event->buffer) {
2326 perf_buffer_put(event->buffer);
2327 event->buffer = NULL;
2331 event->destroy(event);
2334 put_ctx(event->ctx);
2336 call_rcu(&event->rcu_head, free_event_rcu);
2339 int perf_event_release_kernel(struct perf_event *event)
2341 struct perf_event_context *ctx = event->ctx;
2344 * Remove from the PMU, can't get re-enabled since we got
2345 * here because the last ref went.
2347 perf_event_disable(event);
2349 WARN_ON_ONCE(ctx->parent_ctx);
2351 * There are two ways this annotation is useful:
2353 * 1) there is a lock recursion from perf_event_exit_task
2354 * see the comment there.
2356 * 2) there is a lock-inversion with mmap_sem through
2357 * perf_event_read_group(), which takes faults while
2358 * holding ctx->mutex, however this is called after
2359 * the last filedesc died, so there is no possibility
2360 * to trigger the AB-BA case.
2362 mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
2363 raw_spin_lock_irq(&ctx->lock);
2364 perf_group_detach(event);
2365 list_del_event(event, ctx);
2366 raw_spin_unlock_irq(&ctx->lock);
2367 mutex_unlock(&ctx->mutex);
2373 EXPORT_SYMBOL_GPL(perf_event_release_kernel);
2376 * Called when the last reference to the file is gone.
2378 static int perf_release(struct inode *inode, struct file *file)
2380 struct perf_event *event = file->private_data;
2381 struct task_struct *owner;
2383 file->private_data = NULL;
2386 owner = ACCESS_ONCE(event->owner);
2388 * Matches the smp_wmb() in perf_event_exit_task(). If we observe
2389 * !owner it means the list deletion is complete and we can indeed
2390 * free this event, otherwise we need to serialize on
2391 * owner->perf_event_mutex.
2393 smp_read_barrier_depends();
2396 * Since delayed_put_task_struct() also drops the last
2397 * task reference we can safely take a new reference
2398 * while holding the rcu_read_lock().
2400 get_task_struct(owner);
2405 mutex_lock(&owner->perf_event_mutex);
2407 * We have to re-check the event->owner field, if it is cleared
2408 * we raced with perf_event_exit_task(), acquiring the mutex
2409 * ensured they're done, and we can proceed with freeing the
2413 list_del_init(&event->owner_entry);
2414 mutex_unlock(&owner->perf_event_mutex);
2415 put_task_struct(owner);
2418 return perf_event_release_kernel(event);
2421 u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
2423 struct perf_event *child;
2429 mutex_lock(&event->child_mutex);
2430 total += perf_event_read(event);
2431 *enabled += event->total_time_enabled +
2432 atomic64_read(&event->child_total_time_enabled);
2433 *running += event->total_time_running +
2434 atomic64_read(&event->child_total_time_running);
2436 list_for_each_entry(child, &event->child_list, child_list) {
2437 total += perf_event_read(child);
2438 *enabled += child->total_time_enabled;
2439 *running += child->total_time_running;
2441 mutex_unlock(&event->child_mutex);
2445 EXPORT_SYMBOL_GPL(perf_event_read_value);
2447 static int perf_event_read_group(struct perf_event *event,
2448 u64 read_format, char __user *buf)
2450 struct perf_event *leader = event->group_leader, *sub;
2451 int n = 0, size = 0, ret = -EFAULT;
2452 struct perf_event_context *ctx = leader->ctx;
2454 u64 count, enabled, running;
2456 mutex_lock(&ctx->mutex);
2457 count = perf_event_read_value(leader, &enabled, &running);
2459 values[n++] = 1 + leader->nr_siblings;
2460 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2461 values[n++] = enabled;
2462 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2463 values[n++] = running;
2464 values[n++] = count;
2465 if (read_format & PERF_FORMAT_ID)
2466 values[n++] = primary_event_id(leader);
2468 size = n * sizeof(u64);
2470 if (copy_to_user(buf, values, size))
2475 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
2478 values[n++] = perf_event_read_value(sub, &enabled, &running);
2479 if (read_format & PERF_FORMAT_ID)
2480 values[n++] = primary_event_id(sub);
2482 size = n * sizeof(u64);
2484 if (copy_to_user(buf + ret, values, size)) {
2492 mutex_unlock(&ctx->mutex);
2497 static int perf_event_read_one(struct perf_event *event,
2498 u64 read_format, char __user *buf)
2500 u64 enabled, running;
2504 values[n++] = perf_event_read_value(event, &enabled, &running);
2505 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2506 values[n++] = enabled;
2507 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2508 values[n++] = running;
2509 if (read_format & PERF_FORMAT_ID)
2510 values[n++] = primary_event_id(event);
2512 if (copy_to_user(buf, values, n * sizeof(u64)))
2515 return n * sizeof(u64);
2519 * Read the performance event - simple non blocking version for now
2522 perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
2524 u64 read_format = event->attr.read_format;
2528 * Return end-of-file for a read on a event that is in
2529 * error state (i.e. because it was pinned but it couldn't be
2530 * scheduled on to the CPU at some point).
2532 if (event->state == PERF_EVENT_STATE_ERROR)
2535 if (count < event->read_size)
2538 WARN_ON_ONCE(event->ctx->parent_ctx);
2539 if (read_format & PERF_FORMAT_GROUP)
2540 ret = perf_event_read_group(event, read_format, buf);
2542 ret = perf_event_read_one(event, read_format, buf);
2548 perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
2550 struct perf_event *event = file->private_data;
2552 return perf_read_hw(event, buf, count);
2555 static unsigned int perf_poll(struct file *file, poll_table *wait)
2557 struct perf_event *event = file->private_data;
2558 struct perf_buffer *buffer;
2559 unsigned int events = POLL_HUP;
2562 buffer = rcu_dereference(event->buffer);
2564 events = atomic_xchg(&buffer->poll, 0);
2567 poll_wait(file, &event->waitq, wait);
2572 static void perf_event_reset(struct perf_event *event)
2574 (void)perf_event_read(event);
2575 local64_set(&event->count, 0);
2576 perf_event_update_userpage(event);
2580 * Holding the top-level event's child_mutex means that any
2581 * descendant process that has inherited this event will block
2582 * in sync_child_event if it goes to exit, thus satisfying the
2583 * task existence requirements of perf_event_enable/disable.
2585 static void perf_event_for_each_child(struct perf_event *event,
2586 void (*func)(struct perf_event *))
2588 struct perf_event *child;
2590 WARN_ON_ONCE(event->ctx->parent_ctx);
2591 mutex_lock(&event->child_mutex);
2593 list_for_each_entry(child, &event->child_list, child_list)
2595 mutex_unlock(&event->child_mutex);
2598 static void perf_event_for_each(struct perf_event *event,
2599 void (*func)(struct perf_event *))
2601 struct perf_event_context *ctx = event->ctx;
2602 struct perf_event *sibling;
2604 WARN_ON_ONCE(ctx->parent_ctx);
2605 mutex_lock(&ctx->mutex);
2606 event = event->group_leader;
2608 perf_event_for_each_child(event, func);
2610 list_for_each_entry(sibling, &event->sibling_list, group_entry)
2611 perf_event_for_each_child(event, func);
2612 mutex_unlock(&ctx->mutex);
2615 static int perf_event_period(struct perf_event *event, u64 __user *arg)
2617 struct perf_event_context *ctx = event->ctx;
2621 if (!is_sampling_event(event))
2624 if (copy_from_user(&value, arg, sizeof(value)))
2630 raw_spin_lock_irq(&ctx->lock);
2631 if (event->attr.freq) {
2632 if (value > sysctl_perf_event_sample_rate) {
2637 event->attr.sample_freq = value;
2639 event->attr.sample_period = value;
2640 event->hw.sample_period = value;
2643 raw_spin_unlock_irq(&ctx->lock);
2648 static const struct file_operations perf_fops;
2650 static struct perf_event *perf_fget_light(int fd, int *fput_needed)
2654 file = fget_light(fd, fput_needed);
2656 return ERR_PTR(-EBADF);
2658 if (file->f_op != &perf_fops) {
2659 fput_light(file, *fput_needed);
2661 return ERR_PTR(-EBADF);
2664 return file->private_data;
2667 static int perf_event_set_output(struct perf_event *event,
2668 struct perf_event *output_event);
2669 static int perf_event_set_filter(struct perf_event *event, void __user *arg);
2671 static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2673 struct perf_event *event = file->private_data;
2674 void (*func)(struct perf_event *);
2678 case PERF_EVENT_IOC_ENABLE:
2679 func = perf_event_enable;
2681 case PERF_EVENT_IOC_DISABLE:
2682 func = perf_event_disable;
2684 case PERF_EVENT_IOC_RESET:
2685 func = perf_event_reset;
2688 case PERF_EVENT_IOC_REFRESH:
2689 return perf_event_refresh(event, arg);
2691 case PERF_EVENT_IOC_PERIOD:
2692 return perf_event_period(event, (u64 __user *)arg);
2694 case PERF_EVENT_IOC_SET_OUTPUT:
2696 struct perf_event *output_event = NULL;
2697 int fput_needed = 0;
2701 output_event = perf_fget_light(arg, &fput_needed);
2702 if (IS_ERR(output_event))
2703 return PTR_ERR(output_event);
2706 ret = perf_event_set_output(event, output_event);
2708 fput_light(output_event->filp, fput_needed);
2713 case PERF_EVENT_IOC_SET_FILTER:
2714 return perf_event_set_filter(event, (void __user *)arg);
2720 if (flags & PERF_IOC_FLAG_GROUP)
2721 perf_event_for_each(event, func);
2723 perf_event_for_each_child(event, func);
2728 int perf_event_task_enable(void)
2730 struct perf_event *event;
2732 mutex_lock(¤t->perf_event_mutex);
2733 list_for_each_entry(event, ¤t->perf_event_list, owner_entry)
2734 perf_event_for_each_child(event, perf_event_enable);
2735 mutex_unlock(¤t->perf_event_mutex);
2740 int perf_event_task_disable(void)
2742 struct perf_event *event;
2744 mutex_lock(¤t->perf_event_mutex);
2745 list_for_each_entry(event, ¤t->perf_event_list, owner_entry)
2746 perf_event_for_each_child(event, perf_event_disable);
2747 mutex_unlock(¤t->perf_event_mutex);
2752 #ifndef PERF_EVENT_INDEX_OFFSET
2753 # define PERF_EVENT_INDEX_OFFSET 0
2756 static int perf_event_index(struct perf_event *event)
2758 if (event->hw.state & PERF_HES_STOPPED)
2761 if (event->state != PERF_EVENT_STATE_ACTIVE)
2764 return event->hw.idx + 1 - PERF_EVENT_INDEX_OFFSET;
2768 * Callers need to ensure there can be no nesting of this function, otherwise
2769 * the seqlock logic goes bad. We can not serialize this because the arch
2770 * code calls this from NMI context.
2772 void perf_event_update_userpage(struct perf_event *event)
2774 struct perf_event_mmap_page *userpg;
2775 struct perf_buffer *buffer;
2778 buffer = rcu_dereference(event->buffer);
2782 userpg = buffer->user_page;
2785 * Disable preemption so as to not let the corresponding user-space
2786 * spin too long if we get preempted.
2791 userpg->index = perf_event_index(event);
2792 userpg->offset = perf_event_count(event);
2793 if (event->state == PERF_EVENT_STATE_ACTIVE)
2794 userpg->offset -= local64_read(&event->hw.prev_count);
2796 userpg->time_enabled = event->total_time_enabled +
2797 atomic64_read(&event->child_total_time_enabled);
2799 userpg->time_running = event->total_time_running +
2800 atomic64_read(&event->child_total_time_running);
2809 static unsigned long perf_data_size(struct perf_buffer *buffer);
2812 perf_buffer_init(struct perf_buffer *buffer, long watermark, int flags)
2814 long max_size = perf_data_size(buffer);
2817 buffer->watermark = min(max_size, watermark);
2819 if (!buffer->watermark)
2820 buffer->watermark = max_size / 2;
2822 if (flags & PERF_BUFFER_WRITABLE)
2823 buffer->writable = 1;
2825 atomic_set(&buffer->refcount, 1);
2828 #ifndef CONFIG_PERF_USE_VMALLOC
2831 * Back perf_mmap() with regular GFP_KERNEL-0 pages.
2834 static struct page *
2835 perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
2837 if (pgoff > buffer->nr_pages)
2841 return virt_to_page(buffer->user_page);
2843 return virt_to_page(buffer->data_pages[pgoff - 1]);
2846 static void *perf_mmap_alloc_page(int cpu)
2851 node = (cpu == -1) ? cpu : cpu_to_node(cpu);
2852 page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
2856 return page_address(page);
2859 static struct perf_buffer *
2860 perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
2862 struct perf_buffer *buffer;
2866 size = sizeof(struct perf_buffer);
2867 size += nr_pages * sizeof(void *);
2869 buffer = kzalloc(size, GFP_KERNEL);
2873 buffer->user_page = perf_mmap_alloc_page(cpu);
2874 if (!buffer->user_page)
2875 goto fail_user_page;
2877 for (i = 0; i < nr_pages; i++) {
2878 buffer->data_pages[i] = perf_mmap_alloc_page(cpu);
2879 if (!buffer->data_pages[i])
2880 goto fail_data_pages;
2883 buffer->nr_pages = nr_pages;
2885 perf_buffer_init(buffer, watermark, flags);
2890 for (i--; i >= 0; i--)
2891 free_page((unsigned long)buffer->data_pages[i]);
2893 free_page((unsigned long)buffer->user_page);
2902 static void perf_mmap_free_page(unsigned long addr)
2904 struct page *page = virt_to_page((void *)addr);
2906 page->mapping = NULL;
2910 static void perf_buffer_free(struct perf_buffer *buffer)
2914 perf_mmap_free_page((unsigned long)buffer->user_page);
2915 for (i = 0; i < buffer->nr_pages; i++)
2916 perf_mmap_free_page((unsigned long)buffer->data_pages[i]);
2920 static inline int page_order(struct perf_buffer *buffer)
2928 * Back perf_mmap() with vmalloc memory.
2930 * Required for architectures that have d-cache aliasing issues.
2933 static inline int page_order(struct perf_buffer *buffer)
2935 return buffer->page_order;
2938 static struct page *
2939 perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
2941 if (pgoff > (1UL << page_order(buffer)))
2944 return vmalloc_to_page((void *)buffer->user_page + pgoff * PAGE_SIZE);
2947 static void perf_mmap_unmark_page(void *addr)
2949 struct page *page = vmalloc_to_page(addr);
2951 page->mapping = NULL;
2954 static void perf_buffer_free_work(struct work_struct *work)
2956 struct perf_buffer *buffer;
2960 buffer = container_of(work, struct perf_buffer, work);
2961 nr = 1 << page_order(buffer);
2963 base = buffer->user_page;
2964 for (i = 0; i < nr + 1; i++)
2965 perf_mmap_unmark_page(base + (i * PAGE_SIZE));
2971 static void perf_buffer_free(struct perf_buffer *buffer)
2973 schedule_work(&buffer->work);
2976 static struct perf_buffer *
2977 perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
2979 struct perf_buffer *buffer;
2983 size = sizeof(struct perf_buffer);
2984 size += sizeof(void *);
2986 buffer = kzalloc(size, GFP_KERNEL);
2990 INIT_WORK(&buffer->work, perf_buffer_free_work);
2992 all_buf = vmalloc_user((nr_pages + 1) * PAGE_SIZE);
2996 buffer->user_page = all_buf;
2997 buffer->data_pages[0] = all_buf + PAGE_SIZE;
2998 buffer->page_order = ilog2(nr_pages);
2999 buffer->nr_pages = 1;
3001 perf_buffer_init(buffer, watermark, flags);
3014 static unsigned long perf_data_size(struct perf_buffer *buffer)
3016 return buffer->nr_pages << (PAGE_SHIFT + page_order(buffer));
3019 static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
3021 struct perf_event *event = vma->vm_file->private_data;
3022 struct perf_buffer *buffer;
3023 int ret = VM_FAULT_SIGBUS;
3025 if (vmf->flags & FAULT_FLAG_MKWRITE) {
3026 if (vmf->pgoff == 0)
3032 buffer = rcu_dereference(event->buffer);
3036 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
3039 vmf->page = perf_mmap_to_page(buffer, vmf->pgoff);
3043 get_page(vmf->page);
3044 vmf->page->mapping = vma->vm_file->f_mapping;
3045 vmf->page->index = vmf->pgoff;
3054 static void perf_buffer_free_rcu(struct rcu_head *rcu_head)
3056 struct perf_buffer *buffer;
3058 buffer = container_of(rcu_head, struct perf_buffer, rcu_head);
3059 perf_buffer_free(buffer);
3062 static struct perf_buffer *perf_buffer_get(struct perf_event *event)
3064 struct perf_buffer *buffer;
3067 buffer = rcu_dereference(event->buffer);
3069 if (!atomic_inc_not_zero(&buffer->refcount))
3077 static void perf_buffer_put(struct perf_buffer *buffer)
3079 if (!atomic_dec_and_test(&buffer->refcount))
3082 call_rcu(&buffer->rcu_head, perf_buffer_free_rcu);
3085 static void perf_mmap_open(struct vm_area_struct *vma)
3087 struct perf_event *event = vma->vm_file->private_data;
3089 atomic_inc(&event->mmap_count);
3092 static void perf_mmap_close(struct vm_area_struct *vma)
3094 struct perf_event *event = vma->vm_file->private_data;
3096 if (atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) {
3097 unsigned long size = perf_data_size(event->buffer);
3098 struct user_struct *user = event->mmap_user;
3099 struct perf_buffer *buffer = event->buffer;
3101 atomic_long_sub((size >> PAGE_SHIFT) + 1, &user->locked_vm);
3102 vma->vm_mm->locked_vm -= event->mmap_locked;
3103 rcu_assign_pointer(event->buffer, NULL);
3104 mutex_unlock(&event->mmap_mutex);
3106 perf_buffer_put(buffer);
3111 static const struct vm_operations_struct perf_mmap_vmops = {
3112 .open = perf_mmap_open,
3113 .close = perf_mmap_close,
3114 .fault = perf_mmap_fault,
3115 .page_mkwrite = perf_mmap_fault,
3118 static int perf_mmap(struct file *file, struct vm_area_struct *vma)
3120 struct perf_event *event = file->private_data;
3121 unsigned long user_locked, user_lock_limit;
3122 struct user_struct *user = current_user();
3123 unsigned long locked, lock_limit;
3124 struct perf_buffer *buffer;
3125 unsigned long vma_size;
3126 unsigned long nr_pages;
3127 long user_extra, extra;
3128 int ret = 0, flags = 0;
3131 * Don't allow mmap() of inherited per-task counters. This would
3132 * create a performance issue due to all children writing to the
3135 if (event->cpu == -1 && event->attr.inherit)
3138 if (!(vma->vm_flags & VM_SHARED))
3141 vma_size = vma->vm_end - vma->vm_start;
3142 nr_pages = (vma_size / PAGE_SIZE) - 1;
3145 * If we have buffer pages ensure they're a power-of-two number, so we
3146 * can do bitmasks instead of modulo.
3148 if (nr_pages != 0 && !is_power_of_2(nr_pages))
3151 if (vma_size != PAGE_SIZE * (1 + nr_pages))
3154 if (vma->vm_pgoff != 0)
3157 WARN_ON_ONCE(event->ctx->parent_ctx);
3158 mutex_lock(&event->mmap_mutex);
3159 if (event->buffer) {
3160 if (event->buffer->nr_pages == nr_pages)
3161 atomic_inc(&event->buffer->refcount);
3167 user_extra = nr_pages + 1;
3168 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
3171 * Increase the limit linearly with more CPUs:
3173 user_lock_limit *= num_online_cpus();
3175 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
3178 if (user_locked > user_lock_limit)
3179 extra = user_locked - user_lock_limit;
3181 lock_limit = rlimit(RLIMIT_MEMLOCK);
3182 lock_limit >>= PAGE_SHIFT;
3183 locked = vma->vm_mm->locked_vm + extra;
3185 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
3186 !capable(CAP_IPC_LOCK)) {
3191 WARN_ON(event->buffer);
3193 if (vma->vm_flags & VM_WRITE)
3194 flags |= PERF_BUFFER_WRITABLE;
3196 buffer = perf_buffer_alloc(nr_pages, event->attr.wakeup_watermark,
3202 rcu_assign_pointer(event->buffer, buffer);
3204 atomic_long_add(user_extra, &user->locked_vm);
3205 event->mmap_locked = extra;
3206 event->mmap_user = get_current_user();
3207 vma->vm_mm->locked_vm += event->mmap_locked;
3211 atomic_inc(&event->mmap_count);
3212 mutex_unlock(&event->mmap_mutex);
3214 vma->vm_flags |= VM_RESERVED;
3215 vma->vm_ops = &perf_mmap_vmops;
3220 static int perf_fasync(int fd, struct file *filp, int on)
3222 struct inode *inode = filp->f_path.dentry->d_inode;
3223 struct perf_event *event = filp->private_data;
3226 mutex_lock(&inode->i_mutex);
3227 retval = fasync_helper(fd, filp, on, &event->fasync);
3228 mutex_unlock(&inode->i_mutex);
3236 static const struct file_operations perf_fops = {
3237 .llseek = no_llseek,
3238 .release = perf_release,
3241 .unlocked_ioctl = perf_ioctl,
3242 .compat_ioctl = perf_ioctl,
3244 .fasync = perf_fasync,
3250 * If there's data, ensure we set the poll() state and publish everything
3251 * to user-space before waking everybody up.
3254 void perf_event_wakeup(struct perf_event *event)
3256 wake_up_all(&event->waitq);
3258 if (event->pending_kill) {
3259 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
3260 event->pending_kill = 0;
3264 static void perf_pending_event(struct irq_work *entry)
3266 struct perf_event *event = container_of(entry,
3267 struct perf_event, pending);
3269 if (event->pending_disable) {
3270 event->pending_disable = 0;
3271 __perf_event_disable(event);
3274 if (event->pending_wakeup) {
3275 event->pending_wakeup = 0;
3276 perf_event_wakeup(event);
3281 * We assume there is only KVM supporting the callbacks.
3282 * Later on, we might change it to a list if there is
3283 * another virtualization implementation supporting the callbacks.
3285 struct perf_guest_info_callbacks *perf_guest_cbs;
3287 int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3289 perf_guest_cbs = cbs;
3292 EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
3294 int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3296 perf_guest_cbs = NULL;
3299 EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
3304 static bool perf_output_space(struct perf_buffer *buffer, unsigned long tail,
3305 unsigned long offset, unsigned long head)
3309 if (!buffer->writable)
3312 mask = perf_data_size(buffer) - 1;
3314 offset = (offset - tail) & mask;
3315 head = (head - tail) & mask;
3317 if ((int)(head - offset) < 0)
3323 static void perf_output_wakeup(struct perf_output_handle *handle)
3325 atomic_set(&handle->buffer->poll, POLL_IN);
3328 handle->event->pending_wakeup = 1;
3329 irq_work_queue(&handle->event->pending);
3331 perf_event_wakeup(handle->event);
3335 * We need to ensure a later event_id doesn't publish a head when a former
3336 * event isn't done writing. However since we need to deal with NMIs we
3337 * cannot fully serialize things.
3339 * We only publish the head (and generate a wakeup) when the outer-most
3342 static void perf_output_get_handle(struct perf_output_handle *handle)
3344 struct perf_buffer *buffer = handle->buffer;
3347 local_inc(&buffer->nest);
3348 handle->wakeup = local_read(&buffer->wakeup);
3351 static void perf_output_put_handle(struct perf_output_handle *handle)
3353 struct perf_buffer *buffer = handle->buffer;
3357 head = local_read(&buffer->head);
3360 * IRQ/NMI can happen here, which means we can miss a head update.
3363 if (!local_dec_and_test(&buffer->nest))
3367 * Publish the known good head. Rely on the full barrier implied
3368 * by atomic_dec_and_test() order the buffer->head read and this
3371 buffer->user_page->data_head = head;
3374 * Now check if we missed an update, rely on the (compiler)
3375 * barrier in atomic_dec_and_test() to re-read buffer->head.
3377 if (unlikely(head != local_read(&buffer->head))) {
3378 local_inc(&buffer->nest);
3382 if (handle->wakeup != local_read(&buffer->wakeup))
3383 perf_output_wakeup(handle);
3389 __always_inline void perf_output_copy(struct perf_output_handle *handle,
3390 const void *buf, unsigned int len)
3393 unsigned long size = min_t(unsigned long, handle->size, len);
3395 memcpy(handle->addr, buf, size);
3398 handle->addr += size;
3400 handle->size -= size;
3401 if (!handle->size) {
3402 struct perf_buffer *buffer = handle->buffer;
3405 handle->page &= buffer->nr_pages - 1;
3406 handle->addr = buffer->data_pages[handle->page];
3407 handle->size = PAGE_SIZE << page_order(buffer);
3412 static void __perf_event_header__init_id(struct perf_event_header *header,
3413 struct perf_sample_data *data,
3414 struct perf_event *event)
3416 u64 sample_type = event->attr.sample_type;
3418 data->type = sample_type;
3419 header->size += event->id_header_size;
3421 if (sample_type & PERF_SAMPLE_TID) {
3422 /* namespace issues */
3423 data->tid_entry.pid = perf_event_pid(event, current);
3424 data->tid_entry.tid = perf_event_tid(event, current);
3427 if (sample_type & PERF_SAMPLE_TIME)
3428 data->time = perf_clock();
3430 if (sample_type & PERF_SAMPLE_ID)
3431 data->id = primary_event_id(event);
3433 if (sample_type & PERF_SAMPLE_STREAM_ID)
3434 data->stream_id = event->id;
3436 if (sample_type & PERF_SAMPLE_CPU) {
3437 data->cpu_entry.cpu = raw_smp_processor_id();
3438 data->cpu_entry.reserved = 0;
3442 static void perf_event_header__init_id(struct perf_event_header *header,
3443 struct perf_sample_data *data,
3444 struct perf_event *event)
3446 if (event->attr.sample_id_all)
3447 __perf_event_header__init_id(header, data, event);
3450 static void __perf_event__output_id_sample(struct perf_output_handle *handle,
3451 struct perf_sample_data *data)
3453 u64 sample_type = data->type;
3455 if (sample_type & PERF_SAMPLE_TID)
3456 perf_output_put(handle, data->tid_entry);
3458 if (sample_type & PERF_SAMPLE_TIME)
3459 perf_output_put(handle, data->time);
3461 if (sample_type & PERF_SAMPLE_ID)
3462 perf_output_put(handle, data->id);
3464 if (sample_type & PERF_SAMPLE_STREAM_ID)
3465 perf_output_put(handle, data->stream_id);
3467 if (sample_type & PERF_SAMPLE_CPU)
3468 perf_output_put(handle, data->cpu_entry);
3471 static void perf_event__output_id_sample(struct perf_event *event,
3472 struct perf_output_handle *handle,
3473 struct perf_sample_data *sample)
3475 if (event->attr.sample_id_all)
3476 __perf_event__output_id_sample(handle, sample);
3479 int perf_output_begin(struct perf_output_handle *handle,
3480 struct perf_event *event, unsigned int size,
3481 int nmi, int sample)
3483 struct perf_buffer *buffer;
3484 unsigned long tail, offset, head;
3486 struct perf_sample_data sample_data;
3488 struct perf_event_header header;
3495 * For inherited events we send all the output towards the parent.
3498 event = event->parent;
3500 buffer = rcu_dereference(event->buffer);
3504 handle->buffer = buffer;
3505 handle->event = event;
3507 handle->sample = sample;
3509 if (!buffer->nr_pages)
3512 have_lost = local_read(&buffer->lost);
3514 lost_event.header.size = sizeof(lost_event);
3515 perf_event_header__init_id(&lost_event.header, &sample_data,
3517 size += lost_event.header.size;
3520 perf_output_get_handle(handle);
3524 * Userspace could choose to issue a mb() before updating the
3525 * tail pointer. So that all reads will be completed before the
3528 tail = ACCESS_ONCE(buffer->user_page->data_tail);
3530 offset = head = local_read(&buffer->head);
3532 if (unlikely(!perf_output_space(buffer, tail, offset, head)))
3534 } while (local_cmpxchg(&buffer->head, offset, head) != offset);
3536 if (head - local_read(&buffer->wakeup) > buffer->watermark)
3537 local_add(buffer->watermark, &buffer->wakeup);
3539 handle->page = offset >> (PAGE_SHIFT + page_order(buffer));
3540 handle->page &= buffer->nr_pages - 1;
3541 handle->size = offset & ((PAGE_SIZE << page_order(buffer)) - 1);
3542 handle->addr = buffer->data_pages[handle->page];
3543 handle->addr += handle->size;
3544 handle->size = (PAGE_SIZE << page_order(buffer)) - handle->size;
3547 lost_event.header.type = PERF_RECORD_LOST;
3548 lost_event.header.misc = 0;
3549 lost_event.id = event->id;
3550 lost_event.lost = local_xchg(&buffer->lost, 0);
3552 perf_output_put(handle, lost_event);
3553 perf_event__output_id_sample(event, handle, &sample_data);
3559 local_inc(&buffer->lost);
3560 perf_output_put_handle(handle);
3567 void perf_output_end(struct perf_output_handle *handle)
3569 struct perf_event *event = handle->event;
3570 struct perf_buffer *buffer = handle->buffer;
3572 int wakeup_events = event->attr.wakeup_events;
3574 if (handle->sample && wakeup_events) {
3575 int events = local_inc_return(&buffer->events);
3576 if (events >= wakeup_events) {
3577 local_sub(wakeup_events, &buffer->events);
3578 local_inc(&buffer->wakeup);
3582 perf_output_put_handle(handle);
3586 static void perf_output_read_one(struct perf_output_handle *handle,
3587 struct perf_event *event,
3588 u64 enabled, u64 running)
3590 u64 read_format = event->attr.read_format;
3594 values[n++] = perf_event_count(event);
3595 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
3596 values[n++] = enabled +
3597 atomic64_read(&event->child_total_time_enabled);
3599 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
3600 values[n++] = running +
3601 atomic64_read(&event->child_total_time_running);
3603 if (read_format & PERF_FORMAT_ID)
3604 values[n++] = primary_event_id(event);
3606 perf_output_copy(handle, values, n * sizeof(u64));
3610 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
3612 static void perf_output_read_group(struct perf_output_handle *handle,
3613 struct perf_event *event,
3614 u64 enabled, u64 running)
3616 struct perf_event *leader = event->group_leader, *sub;
3617 u64 read_format = event->attr.read_format;
3621 values[n++] = 1 + leader->nr_siblings;
3623 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3624 values[n++] = enabled;
3626 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3627 values[n++] = running;
3629 if (leader != event)
3630 leader->pmu->read(leader);
3632 values[n++] = perf_event_count(leader);
3633 if (read_format & PERF_FORMAT_ID)
3634 values[n++] = primary_event_id(leader);
3636 perf_output_copy(handle, values, n * sizeof(u64));
3638 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
3642 sub->pmu->read(sub);
3644 values[n++] = perf_event_count(sub);
3645 if (read_format & PERF_FORMAT_ID)
3646 values[n++] = primary_event_id(sub);
3648 perf_output_copy(handle, values, n * sizeof(u64));
3652 #define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
3653 PERF_FORMAT_TOTAL_TIME_RUNNING)
3655 static void perf_output_read(struct perf_output_handle *handle,
3656 struct perf_event *event)
3658 u64 enabled = 0, running = 0, now, ctx_time;
3659 u64 read_format = event->attr.read_format;
3662 * compute total_time_enabled, total_time_running
3663 * based on snapshot values taken when the event
3664 * was last scheduled in.
3666 * we cannot simply called update_context_time()
3667 * because of locking issue as we are called in
3670 if (read_format & PERF_FORMAT_TOTAL_TIMES) {
3672 ctx_time = event->shadow_ctx_time + now;
3673 enabled = ctx_time - event->tstamp_enabled;
3674 running = ctx_time - event->tstamp_running;
3677 if (event->attr.read_format & PERF_FORMAT_GROUP)
3678 perf_output_read_group(handle, event, enabled, running);
3680 perf_output_read_one(handle, event, enabled, running);
3683 void perf_output_sample(struct perf_output_handle *handle,
3684 struct perf_event_header *header,
3685 struct perf_sample_data *data,
3686 struct perf_event *event)
3688 u64 sample_type = data->type;
3690 perf_output_put(handle, *header);
3692 if (sample_type & PERF_SAMPLE_IP)
3693 perf_output_put(handle, data->ip);
3695 if (sample_type & PERF_SAMPLE_TID)
3696 perf_output_put(handle, data->tid_entry);
3698 if (sample_type & PERF_SAMPLE_TIME)
3699 perf_output_put(handle, data->time);
3701 if (sample_type & PERF_SAMPLE_ADDR)
3702 perf_output_put(handle, data->addr);
3704 if (sample_type & PERF_SAMPLE_ID)
3705 perf_output_put(handle, data->id);
3707 if (sample_type & PERF_SAMPLE_STREAM_ID)
3708 perf_output_put(handle, data->stream_id);
3710 if (sample_type & PERF_SAMPLE_CPU)
3711 perf_output_put(handle, data->cpu_entry);
3713 if (sample_type & PERF_SAMPLE_PERIOD)
3714 perf_output_put(handle, data->period);
3716 if (sample_type & PERF_SAMPLE_READ)
3717 perf_output_read(handle, event);
3719 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3720 if (data->callchain) {
3723 if (data->callchain)
3724 size += data->callchain->nr;
3726 size *= sizeof(u64);
3728 perf_output_copy(handle, data->callchain, size);
3731 perf_output_put(handle, nr);
3735 if (sample_type & PERF_SAMPLE_RAW) {
3737 perf_output_put(handle, data->raw->size);
3738 perf_output_copy(handle, data->raw->data,
3745 .size = sizeof(u32),
3748 perf_output_put(handle, raw);
3753 void perf_prepare_sample(struct perf_event_header *header,
3754 struct perf_sample_data *data,
3755 struct perf_event *event,
3756 struct pt_regs *regs)
3758 u64 sample_type = event->attr.sample_type;
3760 header->type = PERF_RECORD_SAMPLE;
3761 header->size = sizeof(*header) + event->header_size;
3764 header->misc |= perf_misc_flags(regs);
3766 __perf_event_header__init_id(header, data, event);
3768 if (sample_type & PERF_SAMPLE_IP)
3769 data->ip = perf_instruction_pointer(regs);
3771 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3774 data->callchain = perf_callchain(regs);
3776 if (data->callchain)
3777 size += data->callchain->nr;
3779 header->size += size * sizeof(u64);
3782 if (sample_type & PERF_SAMPLE_RAW) {
3783 int size = sizeof(u32);
3786 size += data->raw->size;
3788 size += sizeof(u32);
3790 WARN_ON_ONCE(size & (sizeof(u64)-1));
3791 header->size += size;
3795 static void perf_event_output(struct perf_event *event, int nmi,
3796 struct perf_sample_data *data,
3797 struct pt_regs *regs)
3799 struct perf_output_handle handle;
3800 struct perf_event_header header;
3802 /* protect the callchain buffers */
3805 perf_prepare_sample(&header, data, event, regs);
3807 if (perf_output_begin(&handle, event, header.size, nmi, 1))
3810 perf_output_sample(&handle, &header, data, event);
3812 perf_output_end(&handle);
3822 struct perf_read_event {
3823 struct perf_event_header header;
3830 perf_event_read_event(struct perf_event *event,
3831 struct task_struct *task)
3833 struct perf_output_handle handle;
3834 struct perf_sample_data sample;
3835 struct perf_read_event read_event = {
3837 .type = PERF_RECORD_READ,
3839 .size = sizeof(read_event) + event->read_size,
3841 .pid = perf_event_pid(event, task),
3842 .tid = perf_event_tid(event, task),
3846 perf_event_header__init_id(&read_event.header, &sample, event);
3847 ret = perf_output_begin(&handle, event, read_event.header.size, 0, 0);
3851 perf_output_put(&handle, read_event);
3852 perf_output_read(&handle, event);
3853 perf_event__output_id_sample(event, &handle, &sample);
3855 perf_output_end(&handle);
3859 * task tracking -- fork/exit
3861 * enabled by: attr.comm | attr.mmap | attr.mmap_data | attr.task
3864 struct perf_task_event {
3865 struct task_struct *task;
3866 struct perf_event_context *task_ctx;
3869 struct perf_event_header header;
3879 static void perf_event_task_output(struct perf_event *event,
3880 struct perf_task_event *task_event)
3882 struct perf_output_handle handle;
3883 struct perf_sample_data sample;
3884 struct task_struct *task = task_event->task;
3885 int ret, size = task_event->event_id.header.size;
3887 perf_event_header__init_id(&task_event->event_id.header, &sample, event);
3889 ret = perf_output_begin(&handle, event,
3890 task_event->event_id.header.size, 0, 0);
3894 task_event->event_id.pid = perf_event_pid(event, task);
3895 task_event->event_id.ppid = perf_event_pid(event, current);
3897 task_event->event_id.tid = perf_event_tid(event, task);
3898 task_event->event_id.ptid = perf_event_tid(event, current);
3900 perf_output_put(&handle, task_event->event_id);
3902 perf_event__output_id_sample(event, &handle, &sample);
3904 perf_output_end(&handle);
3906 task_event->event_id.header.size = size;
3909 static int perf_event_task_match(struct perf_event *event)
3911 if (event->state < PERF_EVENT_STATE_INACTIVE)
3914 if (!event_filter_match(event))
3917 if (event->attr.comm || event->attr.mmap ||
3918 event->attr.mmap_data || event->attr.task)
3924 static void perf_event_task_ctx(struct perf_event_context *ctx,
3925 struct perf_task_event *task_event)
3927 struct perf_event *event;
3929 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3930 if (perf_event_task_match(event))
3931 perf_event_task_output(event, task_event);
3935 static void perf_event_task_event(struct perf_task_event *task_event)
3937 struct perf_cpu_context *cpuctx;
3938 struct perf_event_context *ctx;
3943 list_for_each_entry_rcu(pmu, &pmus, entry) {
3944 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
3945 if (cpuctx->active_pmu != pmu)
3947 perf_event_task_ctx(&cpuctx->ctx, task_event);
3949 ctx = task_event->task_ctx;
3951 ctxn = pmu->task_ctx_nr;
3954 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
3957 perf_event_task_ctx(ctx, task_event);
3959 put_cpu_ptr(pmu->pmu_cpu_context);
3964 static void perf_event_task(struct task_struct *task,
3965 struct perf_event_context *task_ctx,
3968 struct perf_task_event task_event;
3970 if (!atomic_read(&nr_comm_events) &&
3971 !atomic_read(&nr_mmap_events) &&
3972 !atomic_read(&nr_task_events))
3975 task_event = (struct perf_task_event){
3977 .task_ctx = task_ctx,
3980 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
3982 .size = sizeof(task_event.event_id),
3988 .time = perf_clock(),
3992 perf_event_task_event(&task_event);
3995 void perf_event_fork(struct task_struct *task)
3997 perf_event_task(task, NULL, 1);
4004 struct perf_comm_event {
4005 struct task_struct *task;
4010 struct perf_event_header header;
4017 static void perf_event_comm_output(struct perf_event *event,
4018 struct perf_comm_event *comm_event)
4020 struct perf_output_handle handle;
4021 struct perf_sample_data sample;
4022 int size = comm_event->event_id.header.size;
4025 perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
4026 ret = perf_output_begin(&handle, event,
4027 comm_event->event_id.header.size, 0, 0);
4032 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
4033 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
4035 perf_output_put(&handle, comm_event->event_id);
4036 perf_output_copy(&handle, comm_event->comm,
4037 comm_event->comm_size);
4039 perf_event__output_id_sample(event, &handle, &sample);
4041 perf_output_end(&handle);
4043 comm_event->event_id.header.size = size;
4046 static int perf_event_comm_match(struct perf_event *event)
4048 if (event->state < PERF_EVENT_STATE_INACTIVE)
4051 if (!event_filter_match(event))
4054 if (event->attr.comm)
4060 static void perf_event_comm_ctx(struct perf_event_context *ctx,
4061 struct perf_comm_event *comm_event)
4063 struct perf_event *event;
4065 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4066 if (perf_event_comm_match(event))
4067 perf_event_comm_output(event, comm_event);
4071 static void perf_event_comm_event(struct perf_comm_event *comm_event)
4073 struct perf_cpu_context *cpuctx;
4074 struct perf_event_context *ctx;
4075 char comm[TASK_COMM_LEN];
4080 memset(comm, 0, sizeof(comm));
4081 strlcpy(comm, comm_event->task->comm, sizeof(comm));
4082 size = ALIGN(strlen(comm)+1, sizeof(u64));
4084 comm_event->comm = comm;
4085 comm_event->comm_size = size;
4087 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
4089 list_for_each_entry_rcu(pmu, &pmus, entry) {
4090 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
4091 if (cpuctx->active_pmu != pmu)
4093 perf_event_comm_ctx(&cpuctx->ctx, comm_event);
4095 ctxn = pmu->task_ctx_nr;
4099 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4101 perf_event_comm_ctx(ctx, comm_event);
4103 put_cpu_ptr(pmu->pmu_cpu_context);
4108 void perf_event_comm(struct task_struct *task)
4110 struct perf_comm_event comm_event;
4111 struct perf_event_context *ctx;
4114 for_each_task_context_nr(ctxn) {
4115 ctx = task->perf_event_ctxp[ctxn];
4119 perf_event_enable_on_exec(ctx);
4122 if (!atomic_read(&nr_comm_events))
4125 comm_event = (struct perf_comm_event){
4131 .type = PERF_RECORD_COMM,
4140 perf_event_comm_event(&comm_event);
4147 struct perf_mmap_event {
4148 struct vm_area_struct *vma;
4150 const char *file_name;
4154 struct perf_event_header header;
4164 static void perf_event_mmap_output(struct perf_event *event,
4165 struct perf_mmap_event *mmap_event)
4167 struct perf_output_handle handle;
4168 struct perf_sample_data sample;
4169 int size = mmap_event->event_id.header.size;
4172 perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
4173 ret = perf_output_begin(&handle, event,
4174 mmap_event->event_id.header.size, 0, 0);
4178 mmap_event->event_id.pid = perf_event_pid(event, current);
4179 mmap_event->event_id.tid = perf_event_tid(event, current);
4181 perf_output_put(&handle, mmap_event->event_id);
4182 perf_output_copy(&handle, mmap_event->file_name,
4183 mmap_event->file_size);
4185 perf_event__output_id_sample(event, &handle, &sample);
4187 perf_output_end(&handle);
4189 mmap_event->event_id.header.size = size;
4192 static int perf_event_mmap_match(struct perf_event *event,
4193 struct perf_mmap_event *mmap_event,
4196 if (event->state < PERF_EVENT_STATE_INACTIVE)
4199 if (!event_filter_match(event))
4202 if ((!executable && event->attr.mmap_data) ||
4203 (executable && event->attr.mmap))
4209 static void perf_event_mmap_ctx(struct perf_event_context *ctx,
4210 struct perf_mmap_event *mmap_event,
4213 struct perf_event *event;
4215 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4216 if (perf_event_mmap_match(event, mmap_event, executable))
4217 perf_event_mmap_output(event, mmap_event);
4221 static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
4223 struct perf_cpu_context *cpuctx;
4224 struct perf_event_context *ctx;
4225 struct vm_area_struct *vma = mmap_event->vma;
4226 struct file *file = vma->vm_file;
4234 memset(tmp, 0, sizeof(tmp));
4238 * d_path works from the end of the buffer backwards, so we
4239 * need to add enough zero bytes after the string to handle
4240 * the 64bit alignment we do later.
4242 buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
4244 name = strncpy(tmp, "//enomem", sizeof(tmp));
4247 name = d_path(&file->f_path, buf, PATH_MAX);
4249 name = strncpy(tmp, "//toolong", sizeof(tmp));
4253 if (arch_vma_name(mmap_event->vma)) {
4254 name = strncpy(tmp, arch_vma_name(mmap_event->vma),
4260 name = strncpy(tmp, "[vdso]", sizeof(tmp));
4262 } else if (vma->vm_start <= vma->vm_mm->start_brk &&
4263 vma->vm_end >= vma->vm_mm->brk) {
4264 name = strncpy(tmp, "[heap]", sizeof(tmp));
4266 } else if (vma->vm_start <= vma->vm_mm->start_stack &&
4267 vma->vm_end >= vma->vm_mm->start_stack) {
4268 name = strncpy(tmp, "[stack]", sizeof(tmp));
4272 name = strncpy(tmp, "//anon", sizeof(tmp));
4277 size = ALIGN(strlen(name)+1, sizeof(u64));
4279 mmap_event->file_name = name;
4280 mmap_event->file_size = size;
4282 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
4285 list_for_each_entry_rcu(pmu, &pmus, entry) {
4286 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
4287 if (cpuctx->active_pmu != pmu)
4289 perf_event_mmap_ctx(&cpuctx->ctx, mmap_event,
4290 vma->vm_flags & VM_EXEC);
4292 ctxn = pmu->task_ctx_nr;
4296 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4298 perf_event_mmap_ctx(ctx, mmap_event,
4299 vma->vm_flags & VM_EXEC);
4302 put_cpu_ptr(pmu->pmu_cpu_context);
4309 void perf_event_mmap(struct vm_area_struct *vma)
4311 struct perf_mmap_event mmap_event;
4313 if (!atomic_read(&nr_mmap_events))
4316 mmap_event = (struct perf_mmap_event){
4322 .type = PERF_RECORD_MMAP,
4323 .misc = PERF_RECORD_MISC_USER,
4328 .start = vma->vm_start,
4329 .len = vma->vm_end - vma->vm_start,
4330 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
4334 perf_event_mmap_event(&mmap_event);
4338 * IRQ throttle logging
4341 static void perf_log_throttle(struct perf_event *event, int enable)
4343 struct perf_output_handle handle;
4344 struct perf_sample_data sample;
4348 struct perf_event_header header;
4352 } throttle_event = {
4354 .type = PERF_RECORD_THROTTLE,
4356 .size = sizeof(throttle_event),
4358 .time = perf_clock(),
4359 .id = primary_event_id(event),
4360 .stream_id = event->id,
4364 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
4366 perf_event_header__init_id(&throttle_event.header, &sample, event);
4368 ret = perf_output_begin(&handle, event,
4369 throttle_event.header.size, 1, 0);
4373 perf_output_put(&handle, throttle_event);
4374 perf_event__output_id_sample(event, &handle, &sample);
4375 perf_output_end(&handle);
4379 * Generic event overflow handling, sampling.
4382 static int __perf_event_overflow(struct perf_event *event, int nmi,
4383 int throttle, struct perf_sample_data *data,
4384 struct pt_regs *regs)
4386 int events = atomic_read(&event->event_limit);
4387 struct hw_perf_event *hwc = &event->hw;
4391 * Non-sampling counters might still use the PMI to fold short
4392 * hardware counters, ignore those.
4394 if (unlikely(!is_sampling_event(event)))
4400 if (hwc->interrupts != MAX_INTERRUPTS) {
4402 if (HZ * hwc->interrupts >
4403 (u64)sysctl_perf_event_sample_rate) {
4404 hwc->interrupts = MAX_INTERRUPTS;
4405 perf_log_throttle(event, 0);
4410 * Keep re-disabling events even though on the previous
4411 * pass we disabled it - just in case we raced with a
4412 * sched-in and the event got enabled again:
4418 if (event->attr.freq) {
4419 u64 now = perf_clock();
4420 s64 delta = now - hwc->freq_time_stamp;
4422 hwc->freq_time_stamp = now;
4424 if (delta > 0 && delta < 2*TICK_NSEC)
4425 perf_adjust_period(event, delta, hwc->last_period);
4429 * XXX event_limit might not quite work as expected on inherited
4433 event->pending_kill = POLL_IN;
4434 if (events && atomic_dec_and_test(&event->event_limit)) {
4436 event->pending_kill = POLL_HUP;
4438 event->pending_disable = 1;
4439 irq_work_queue(&event->pending);
4441 perf_event_disable(event);
4444 if (event->overflow_handler)
4445 event->overflow_handler(event, nmi, data, regs);
4447 perf_event_output(event, nmi, data, regs);
4452 int perf_event_overflow(struct perf_event *event, int nmi,
4453 struct perf_sample_data *data,
4454 struct pt_regs *regs)
4456 return __perf_event_overflow(event, nmi, 1, data, regs);
4460 * Generic software event infrastructure
4463 struct swevent_htable {
4464 struct swevent_hlist *swevent_hlist;
4465 struct mutex hlist_mutex;
4468 /* Recursion avoidance in each contexts */
4469 int recursion[PERF_NR_CONTEXTS];
4472 static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
4475 * We directly increment event->count and keep a second value in
4476 * event->hw.period_left to count intervals. This period event
4477 * is kept in the range [-sample_period, 0] so that we can use the
4481 static u64 perf_swevent_set_period(struct perf_event *event)
4483 struct hw_perf_event *hwc = &event->hw;
4484 u64 period = hwc->last_period;
4488 hwc->last_period = hwc->sample_period;
4491 old = val = local64_read(&hwc->period_left);
4495 nr = div64_u64(period + val, period);
4496 offset = nr * period;
4498 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
4504 static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
4505 int nmi, struct perf_sample_data *data,
4506 struct pt_regs *regs)
4508 struct hw_perf_event *hwc = &event->hw;
4511 data->period = event->hw.last_period;
4513 overflow = perf_swevent_set_period(event);
4515 if (hwc->interrupts == MAX_INTERRUPTS)
4518 for (; overflow; overflow--) {
4519 if (__perf_event_overflow(event, nmi, throttle,
4522 * We inhibit the overflow from happening when
4523 * hwc->interrupts == MAX_INTERRUPTS.
4531 static void perf_swevent_event(struct perf_event *event, u64 nr,
4532 int nmi, struct perf_sample_data *data,
4533 struct pt_regs *regs)
4535 struct hw_perf_event *hwc = &event->hw;
4537 local64_add(nr, &event->count);
4542 if (!is_sampling_event(event))
4545 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
4546 return perf_swevent_overflow(event, 1, nmi, data, regs);
4548 if (local64_add_negative(nr, &hwc->period_left))
4551 perf_swevent_overflow(event, 0, nmi, data, regs);
4554 static int perf_exclude_event(struct perf_event *event,
4555 struct pt_regs *regs)
4557 if (event->hw.state & PERF_HES_STOPPED)
4561 if (event->attr.exclude_user && user_mode(regs))
4564 if (event->attr.exclude_kernel && !user_mode(regs))
4571 static int perf_swevent_match(struct perf_event *event,
4572 enum perf_type_id type,
4574 struct perf_sample_data *data,
4575 struct pt_regs *regs)
4577 if (event->attr.type != type)
4580 if (event->attr.config != event_id)
4583 if (perf_exclude_event(event, regs))
4589 static inline u64 swevent_hash(u64 type, u32 event_id)
4591 u64 val = event_id | (type << 32);
4593 return hash_64(val, SWEVENT_HLIST_BITS);
4596 static inline struct hlist_head *
4597 __find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
4599 u64 hash = swevent_hash(type, event_id);
4601 return &hlist->heads[hash];
4604 /* For the read side: events when they trigger */
4605 static inline struct hlist_head *
4606 find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
4608 struct swevent_hlist *hlist;
4610 hlist = rcu_dereference(swhash->swevent_hlist);
4614 return __find_swevent_head(hlist, type, event_id);
4617 /* For the event head insertion and removal in the hlist */
4618 static inline struct hlist_head *
4619 find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
4621 struct swevent_hlist *hlist;
4622 u32 event_id = event->attr.config;
4623 u64 type = event->attr.type;
4626 * Event scheduling is always serialized against hlist allocation
4627 * and release. Which makes the protected version suitable here.
4628 * The context lock guarantees that.
4630 hlist = rcu_dereference_protected(swhash->swevent_hlist,
4631 lockdep_is_held(&event->ctx->lock));
4635 return __find_swevent_head(hlist, type, event_id);
4638 static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
4640 struct perf_sample_data *data,
4641 struct pt_regs *regs)
4643 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
4644 struct perf_event *event;
4645 struct hlist_node *node;
4646 struct hlist_head *head;
4649 head = find_swevent_head_rcu(swhash, type, event_id);
4653 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
4654 if (perf_swevent_match(event, type, event_id, data, regs))
4655 perf_swevent_event(event, nr, nmi, data, regs);
4661 int perf_swevent_get_recursion_context(void)
4663 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
4665 return get_recursion_context(swhash->recursion);
4667 EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
4669 inline void perf_swevent_put_recursion_context(int rctx)
4671 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
4673 put_recursion_context(swhash->recursion, rctx);
4676 void __perf_sw_event(u32 event_id, u64 nr, int nmi,
4677 struct pt_regs *regs, u64 addr)
4679 struct perf_sample_data data;
4682 preempt_disable_notrace();
4683 rctx = perf_swevent_get_recursion_context();
4687 perf_sample_data_init(&data, addr);
4689 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, nmi, &data, regs);
4691 perf_swevent_put_recursion_context(rctx);
4692 preempt_enable_notrace();
4695 static void perf_swevent_read(struct perf_event *event)
4699 static int perf_swevent_add(struct perf_event *event, int flags)
4701 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
4702 struct hw_perf_event *hwc = &event->hw;
4703 struct hlist_head *head;
4705 if (is_sampling_event(event)) {
4706 hwc->last_period = hwc->sample_period;
4707 perf_swevent_set_period(event);
4710 hwc->state = !(flags & PERF_EF_START);
4712 head = find_swevent_head(swhash, event);
4713 if (WARN_ON_ONCE(!head))
4716 hlist_add_head_rcu(&event->hlist_entry, head);
4721 static void perf_swevent_del(struct perf_event *event, int flags)
4723 hlist_del_rcu(&event->hlist_entry);
4726 static void perf_swevent_start(struct perf_event *event, int flags)
4728 event->hw.state = 0;
4731 static void perf_swevent_stop(struct perf_event *event, int flags)
4733 event->hw.state = PERF_HES_STOPPED;
4736 /* Deref the hlist from the update side */
4737 static inline struct swevent_hlist *
4738 swevent_hlist_deref(struct swevent_htable *swhash)
4740 return rcu_dereference_protected(swhash->swevent_hlist,
4741 lockdep_is_held(&swhash->hlist_mutex));
4744 static void swevent_hlist_release_rcu(struct rcu_head *rcu_head)
4746 struct swevent_hlist *hlist;
4748 hlist = container_of(rcu_head, struct swevent_hlist, rcu_head);
4752 static void swevent_hlist_release(struct swevent_htable *swhash)
4754 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
4759 rcu_assign_pointer(swhash->swevent_hlist, NULL);
4760 call_rcu(&hlist->rcu_head, swevent_hlist_release_rcu);
4763 static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
4765 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
4767 mutex_lock(&swhash->hlist_mutex);
4769 if (!--swhash->hlist_refcount)
4770 swevent_hlist_release(swhash);
4772 mutex_unlock(&swhash->hlist_mutex);
4775 static void swevent_hlist_put(struct perf_event *event)
4779 if (event->cpu != -1) {
4780 swevent_hlist_put_cpu(event, event->cpu);
4784 for_each_possible_cpu(cpu)
4785 swevent_hlist_put_cpu(event, cpu);
4788 static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
4790 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
4793 mutex_lock(&swhash->hlist_mutex);
4795 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
4796 struct swevent_hlist *hlist;
4798 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
4803 rcu_assign_pointer(swhash->swevent_hlist, hlist);
4805 swhash->hlist_refcount++;
4807 mutex_unlock(&swhash->hlist_mutex);
4812 static int swevent_hlist_get(struct perf_event *event)
4815 int cpu, failed_cpu;
4817 if (event->cpu != -1)
4818 return swevent_hlist_get_cpu(event, event->cpu);
4821 for_each_possible_cpu(cpu) {
4822 err = swevent_hlist_get_cpu(event, cpu);
4832 for_each_possible_cpu(cpu) {
4833 if (cpu == failed_cpu)
4835 swevent_hlist_put_cpu(event, cpu);
4842 atomic_t perf_swevent_enabled[PERF_COUNT_SW_MAX];
4844 static void sw_perf_event_destroy(struct perf_event *event)
4846 u64 event_id = event->attr.config;
4848 WARN_ON(event->parent);
4850 jump_label_dec(&perf_swevent_enabled[event_id]);
4851 swevent_hlist_put(event);
4854 static int perf_swevent_init(struct perf_event *event)
4856 int event_id = event->attr.config;
4858 if (event->attr.type != PERF_TYPE_SOFTWARE)
4862 case PERF_COUNT_SW_CPU_CLOCK:
4863 case PERF_COUNT_SW_TASK_CLOCK:
4870 if (event_id >= PERF_COUNT_SW_MAX)
4873 if (!event->parent) {
4876 err = swevent_hlist_get(event);
4880 jump_label_inc(&perf_swevent_enabled[event_id]);
4881 event->destroy = sw_perf_event_destroy;
4887 static struct pmu perf_swevent = {
4888 .task_ctx_nr = perf_sw_context,
4890 .event_init = perf_swevent_init,
4891 .add = perf_swevent_add,
4892 .del = perf_swevent_del,
4893 .start = perf_swevent_start,
4894 .stop = perf_swevent_stop,
4895 .read = perf_swevent_read,
4898 #ifdef CONFIG_EVENT_TRACING
4900 static int perf_tp_filter_match(struct perf_event *event,
4901 struct perf_sample_data *data)
4903 void *record = data->raw->data;
4905 if (likely(!event->filter) || filter_match_preds(event->filter, record))
4910 static int perf_tp_event_match(struct perf_event *event,
4911 struct perf_sample_data *data,
4912 struct pt_regs *regs)
4915 * All tracepoints are from kernel-space.
4917 if (event->attr.exclude_kernel)
4920 if (!perf_tp_filter_match(event, data))
4926 void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
4927 struct pt_regs *regs, struct hlist_head *head, int rctx)
4929 struct perf_sample_data data;
4930 struct perf_event *event;
4931 struct hlist_node *node;
4933 struct perf_raw_record raw = {
4938 perf_sample_data_init(&data, addr);
4941 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
4942 if (perf_tp_event_match(event, &data, regs))
4943 perf_swevent_event(event, count, 1, &data, regs);
4946 perf_swevent_put_recursion_context(rctx);
4948 EXPORT_SYMBOL_GPL(perf_tp_event);
4950 static void tp_perf_event_destroy(struct perf_event *event)
4952 perf_trace_destroy(event);
4955 static int perf_tp_event_init(struct perf_event *event)
4959 if (event->attr.type != PERF_TYPE_TRACEPOINT)
4962 err = perf_trace_init(event);
4966 event->destroy = tp_perf_event_destroy;
4971 static struct pmu perf_tracepoint = {
4972 .task_ctx_nr = perf_sw_context,
4974 .event_init = perf_tp_event_init,
4975 .add = perf_trace_add,
4976 .del = perf_trace_del,
4977 .start = perf_swevent_start,
4978 .stop = perf_swevent_stop,
4979 .read = perf_swevent_read,
4982 static inline void perf_tp_register(void)
4984 perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
4987 static int perf_event_set_filter(struct perf_event *event, void __user *arg)
4992 if (event->attr.type != PERF_TYPE_TRACEPOINT)
4995 filter_str = strndup_user(arg, PAGE_SIZE);
4996 if (IS_ERR(filter_str))
4997 return PTR_ERR(filter_str);
4999 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
5005 static void perf_event_free_filter(struct perf_event *event)
5007 ftrace_profile_free_filter(event);
5012 static inline void perf_tp_register(void)
5016 static int perf_event_set_filter(struct perf_event *event, void __user *arg)
5021 static void perf_event_free_filter(struct perf_event *event)
5025 #endif /* CONFIG_EVENT_TRACING */
5027 #ifdef CONFIG_HAVE_HW_BREAKPOINT
5028 void perf_bp_event(struct perf_event *bp, void *data)
5030 struct perf_sample_data sample;
5031 struct pt_regs *regs = data;
5033 perf_sample_data_init(&sample, bp->attr.bp_addr);
5035 if (!bp->hw.state && !perf_exclude_event(bp, regs))
5036 perf_swevent_event(bp, 1, 1, &sample, regs);
5041 * hrtimer based swevent callback
5044 static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
5046 enum hrtimer_restart ret = HRTIMER_RESTART;
5047 struct perf_sample_data data;
5048 struct pt_regs *regs;
5049 struct perf_event *event;
5052 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
5053 event->pmu->read(event);
5055 perf_sample_data_init(&data, 0);
5056 data.period = event->hw.last_period;
5057 regs = get_irq_regs();
5059 if (regs && !perf_exclude_event(event, regs)) {
5060 if (!(event->attr.exclude_idle && current->pid == 0))
5061 if (perf_event_overflow(event, 0, &data, regs))
5062 ret = HRTIMER_NORESTART;
5065 period = max_t(u64, 10000, event->hw.sample_period);
5066 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
5071 static void perf_swevent_start_hrtimer(struct perf_event *event)
5073 struct hw_perf_event *hwc = &event->hw;
5076 if (!is_sampling_event(event))
5079 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
5080 hwc->hrtimer.function = perf_swevent_hrtimer;
5082 period = local64_read(&hwc->period_left);
5087 local64_set(&hwc->period_left, 0);
5089 period = max_t(u64, 10000, hwc->sample_period);
5091 __hrtimer_start_range_ns(&hwc->hrtimer,
5092 ns_to_ktime(period), 0,
5093 HRTIMER_MODE_REL_PINNED, 0);
5096 static void perf_swevent_cancel_hrtimer(struct perf_event *event)
5098 struct hw_perf_event *hwc = &event->hw;
5100 if (is_sampling_event(event)) {
5101 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
5102 local64_set(&hwc->period_left, ktime_to_ns(remaining));
5104 hrtimer_cancel(&hwc->hrtimer);
5109 * Software event: cpu wall time clock
5112 static void cpu_clock_event_update(struct perf_event *event)
5117 now = local_clock();
5118 prev = local64_xchg(&event->hw.prev_count, now);
5119 local64_add(now - prev, &event->count);
5122 static void cpu_clock_event_start(struct perf_event *event, int flags)
5124 local64_set(&event->hw.prev_count, local_clock());
5125 perf_swevent_start_hrtimer(event);
5128 static void cpu_clock_event_stop(struct perf_event *event, int flags)
5130 perf_swevent_cancel_hrtimer(event);
5131 cpu_clock_event_update(event);
5134 static int cpu_clock_event_add(struct perf_event *event, int flags)
5136 if (flags & PERF_EF_START)
5137 cpu_clock_event_start(event, flags);
5142 static void cpu_clock_event_del(struct perf_event *event, int flags)
5144 cpu_clock_event_stop(event, flags);
5147 static void cpu_clock_event_read(struct perf_event *event)
5149 cpu_clock_event_update(event);
5152 static int cpu_clock_event_init(struct perf_event *event)
5154 if (event->attr.type != PERF_TYPE_SOFTWARE)
5157 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
5163 static struct pmu perf_cpu_clock = {
5164 .task_ctx_nr = perf_sw_context,
5166 .event_init = cpu_clock_event_init,
5167 .add = cpu_clock_event_add,
5168 .del = cpu_clock_event_del,
5169 .start = cpu_clock_event_start,
5170 .stop = cpu_clock_event_stop,
5171 .read = cpu_clock_event_read,
5175 * Software event: task time clock
5178 static void task_clock_event_update(struct perf_event *event, u64 now)
5183 prev = local64_xchg(&event->hw.prev_count, now);
5185 local64_add(delta, &event->count);
5188 static void task_clock_event_start(struct perf_event *event, int flags)
5190 local64_set(&event->hw.prev_count, event->ctx->time);
5191 perf_swevent_start_hrtimer(event);
5194 static void task_clock_event_stop(struct perf_event *event, int flags)
5196 perf_swevent_cancel_hrtimer(event);
5197 task_clock_event_update(event, event->ctx->time);
5200 static int task_clock_event_add(struct perf_event *event, int flags)
5202 if (flags & PERF_EF_START)
5203 task_clock_event_start(event, flags);
5208 static void task_clock_event_del(struct perf_event *event, int flags)
5210 task_clock_event_stop(event, PERF_EF_UPDATE);
5213 static void task_clock_event_read(struct perf_event *event)
5218 update_context_time(event->ctx);
5219 time = event->ctx->time;
5221 u64 now = perf_clock();
5222 u64 delta = now - event->ctx->timestamp;
5223 time = event->ctx->time + delta;
5226 task_clock_event_update(event, time);
5229 static int task_clock_event_init(struct perf_event *event)
5231 if (event->attr.type != PERF_TYPE_SOFTWARE)
5234 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
5240 static struct pmu perf_task_clock = {
5241 .task_ctx_nr = perf_sw_context,
5243 .event_init = task_clock_event_init,
5244 .add = task_clock_event_add,
5245 .del = task_clock_event_del,
5246 .start = task_clock_event_start,
5247 .stop = task_clock_event_stop,
5248 .read = task_clock_event_read,
5251 static void perf_pmu_nop_void(struct pmu *pmu)
5255 static int perf_pmu_nop_int(struct pmu *pmu)
5260 static void perf_pmu_start_txn(struct pmu *pmu)
5262 perf_pmu_disable(pmu);
5265 static int perf_pmu_commit_txn(struct pmu *pmu)
5267 perf_pmu_enable(pmu);
5271 static void perf_pmu_cancel_txn(struct pmu *pmu)
5273 perf_pmu_enable(pmu);
5277 * Ensures all contexts with the same task_ctx_nr have the same
5278 * pmu_cpu_context too.
5280 static void *find_pmu_context(int ctxn)
5287 list_for_each_entry(pmu, &pmus, entry) {
5288 if (pmu->task_ctx_nr == ctxn)
5289 return pmu->pmu_cpu_context;
5295 static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
5299 for_each_possible_cpu(cpu) {
5300 struct perf_cpu_context *cpuctx;
5302 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
5304 if (cpuctx->active_pmu == old_pmu)
5305 cpuctx->active_pmu = pmu;
5309 static void free_pmu_context(struct pmu *pmu)
5313 mutex_lock(&pmus_lock);
5315 * Like a real lame refcount.
5317 list_for_each_entry(i, &pmus, entry) {
5318 if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
5319 update_pmu_context(i, pmu);
5324 free_percpu(pmu->pmu_cpu_context);
5326 mutex_unlock(&pmus_lock);
5328 static struct idr pmu_idr;
5331 type_show(struct device *dev, struct device_attribute *attr, char *page)
5333 struct pmu *pmu = dev_get_drvdata(dev);
5335 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
5338 static struct device_attribute pmu_dev_attrs[] = {
5343 static int pmu_bus_running;
5344 static struct bus_type pmu_bus = {
5345 .name = "event_source",
5346 .dev_attrs = pmu_dev_attrs,
5349 static void pmu_dev_release(struct device *dev)
5354 static int pmu_dev_alloc(struct pmu *pmu)
5358 pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
5362 device_initialize(pmu->dev);
5363 ret = dev_set_name(pmu->dev, "%s", pmu->name);
5367 dev_set_drvdata(pmu->dev, pmu);
5368 pmu->dev->bus = &pmu_bus;
5369 pmu->dev->release = pmu_dev_release;
5370 ret = device_add(pmu->dev);
5378 put_device(pmu->dev);
5382 static struct lock_class_key cpuctx_mutex;
5384 int perf_pmu_register(struct pmu *pmu, char *name, int type)
5388 mutex_lock(&pmus_lock);
5390 pmu->pmu_disable_count = alloc_percpu(int);
5391 if (!pmu->pmu_disable_count)
5400 int err = idr_pre_get(&pmu_idr, GFP_KERNEL);
5404 err = idr_get_new_above(&pmu_idr, pmu, PERF_TYPE_MAX, &type);
5412 if (pmu_bus_running) {
5413 ret = pmu_dev_alloc(pmu);
5419 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
5420 if (pmu->pmu_cpu_context)
5421 goto got_cpu_context;
5423 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
5424 if (!pmu->pmu_cpu_context)
5427 for_each_possible_cpu(cpu) {
5428 struct perf_cpu_context *cpuctx;
5430 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
5431 __perf_event_init_context(&cpuctx->ctx);
5432 lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
5433 cpuctx->ctx.type = cpu_context;
5434 cpuctx->ctx.pmu = pmu;
5435 cpuctx->jiffies_interval = 1;
5436 INIT_LIST_HEAD(&cpuctx->rotation_list);
5437 cpuctx->active_pmu = pmu;
5441 if (!pmu->start_txn) {
5442 if (pmu->pmu_enable) {
5444 * If we have pmu_enable/pmu_disable calls, install
5445 * transaction stubs that use that to try and batch
5446 * hardware accesses.
5448 pmu->start_txn = perf_pmu_start_txn;
5449 pmu->commit_txn = perf_pmu_commit_txn;
5450 pmu->cancel_txn = perf_pmu_cancel_txn;
5452 pmu->start_txn = perf_pmu_nop_void;
5453 pmu->commit_txn = perf_pmu_nop_int;
5454 pmu->cancel_txn = perf_pmu_nop_void;
5458 if (!pmu->pmu_enable) {
5459 pmu->pmu_enable = perf_pmu_nop_void;
5460 pmu->pmu_disable = perf_pmu_nop_void;
5463 list_add_rcu(&pmu->entry, &pmus);
5466 mutex_unlock(&pmus_lock);
5471 device_del(pmu->dev);
5472 put_device(pmu->dev);
5475 if (pmu->type >= PERF_TYPE_MAX)
5476 idr_remove(&pmu_idr, pmu->type);
5479 free_percpu(pmu->pmu_disable_count);
5483 void perf_pmu_unregister(struct pmu *pmu)
5485 mutex_lock(&pmus_lock);
5486 list_del_rcu(&pmu->entry);
5487 mutex_unlock(&pmus_lock);
5490 * We dereference the pmu list under both SRCU and regular RCU, so
5491 * synchronize against both of those.
5493 synchronize_srcu(&pmus_srcu);
5496 free_percpu(pmu->pmu_disable_count);
5497 if (pmu->type >= PERF_TYPE_MAX)
5498 idr_remove(&pmu_idr, pmu->type);
5499 device_del(pmu->dev);
5500 put_device(pmu->dev);
5501 free_pmu_context(pmu);
5504 struct pmu *perf_init_event(struct perf_event *event)
5506 struct pmu *pmu = NULL;
5509 idx = srcu_read_lock(&pmus_srcu);
5512 pmu = idr_find(&pmu_idr, event->attr.type);
5517 list_for_each_entry_rcu(pmu, &pmus, entry) {
5518 int ret = pmu->event_init(event);
5522 if (ret != -ENOENT) {
5527 pmu = ERR_PTR(-ENOENT);
5529 srcu_read_unlock(&pmus_srcu, idx);
5535 * Allocate and initialize a event structure
5537 static struct perf_event *
5538 perf_event_alloc(struct perf_event_attr *attr, int cpu,
5539 struct task_struct *task,
5540 struct perf_event *group_leader,
5541 struct perf_event *parent_event,
5542 perf_overflow_handler_t overflow_handler)
5545 struct perf_event *event;
5546 struct hw_perf_event *hwc;
5549 if ((unsigned)cpu >= nr_cpu_ids) {
5550 if (!task || cpu != -1)
5551 return ERR_PTR(-EINVAL);
5554 event = kzalloc(sizeof(*event), GFP_KERNEL);
5556 return ERR_PTR(-ENOMEM);
5559 * Single events are their own group leaders, with an
5560 * empty sibling list:
5563 group_leader = event;
5565 mutex_init(&event->child_mutex);
5566 INIT_LIST_HEAD(&event->child_list);
5568 INIT_LIST_HEAD(&event->group_entry);
5569 INIT_LIST_HEAD(&event->event_entry);
5570 INIT_LIST_HEAD(&event->sibling_list);
5571 init_waitqueue_head(&event->waitq);
5572 init_irq_work(&event->pending, perf_pending_event);
5574 mutex_init(&event->mmap_mutex);
5577 event->attr = *attr;
5578 event->group_leader = group_leader;
5582 event->parent = parent_event;
5584 event->ns = get_pid_ns(current->nsproxy->pid_ns);
5585 event->id = atomic64_inc_return(&perf_event_id);
5587 event->state = PERF_EVENT_STATE_INACTIVE;
5590 event->attach_state = PERF_ATTACH_TASK;
5591 #ifdef CONFIG_HAVE_HW_BREAKPOINT
5593 * hw_breakpoint is a bit difficult here..
5595 if (attr->type == PERF_TYPE_BREAKPOINT)
5596 event->hw.bp_target = task;
5600 if (!overflow_handler && parent_event)
5601 overflow_handler = parent_event->overflow_handler;
5603 event->overflow_handler = overflow_handler;
5606 event->state = PERF_EVENT_STATE_OFF;
5611 hwc->sample_period = attr->sample_period;
5612 if (attr->freq && attr->sample_freq)
5613 hwc->sample_period = 1;
5614 hwc->last_period = hwc->sample_period;
5616 local64_set(&hwc->period_left, hwc->sample_period);
5619 * we currently do not support PERF_FORMAT_GROUP on inherited events
5621 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
5624 pmu = perf_init_event(event);
5630 else if (IS_ERR(pmu))
5635 put_pid_ns(event->ns);
5637 return ERR_PTR(err);
5642 if (!event->parent) {
5643 if (event->attach_state & PERF_ATTACH_TASK)
5644 jump_label_inc(&perf_task_events);
5645 if (event->attr.mmap || event->attr.mmap_data)
5646 atomic_inc(&nr_mmap_events);
5647 if (event->attr.comm)
5648 atomic_inc(&nr_comm_events);
5649 if (event->attr.task)
5650 atomic_inc(&nr_task_events);
5651 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
5652 err = get_callchain_buffers();
5655 return ERR_PTR(err);
5663 static int perf_copy_attr(struct perf_event_attr __user *uattr,
5664 struct perf_event_attr *attr)
5669 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
5673 * zero the full structure, so that a short copy will be nice.
5675 memset(attr, 0, sizeof(*attr));
5677 ret = get_user(size, &uattr->size);
5681 if (size > PAGE_SIZE) /* silly large */
5684 if (!size) /* abi compat */
5685 size = PERF_ATTR_SIZE_VER0;
5687 if (size < PERF_ATTR_SIZE_VER0)
5691 * If we're handed a bigger struct than we know of,
5692 * ensure all the unknown bits are 0 - i.e. new
5693 * user-space does not rely on any kernel feature
5694 * extensions we dont know about yet.
5696 if (size > sizeof(*attr)) {
5697 unsigned char __user *addr;
5698 unsigned char __user *end;
5701 addr = (void __user *)uattr + sizeof(*attr);
5702 end = (void __user *)uattr + size;
5704 for (; addr < end; addr++) {
5705 ret = get_user(val, addr);
5711 size = sizeof(*attr);
5714 ret = copy_from_user(attr, uattr, size);
5719 * If the type exists, the corresponding creation will verify
5722 if (attr->type >= PERF_TYPE_MAX)
5725 if (attr->__reserved_1)
5728 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
5731 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
5738 put_user(sizeof(*attr), &uattr->size);
5744 perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
5746 struct perf_buffer *buffer = NULL, *old_buffer = NULL;
5752 /* don't allow circular references */
5753 if (event == output_event)
5757 * Don't allow cross-cpu buffers
5759 if (output_event->cpu != event->cpu)
5763 * If its not a per-cpu buffer, it must be the same task.
5765 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
5769 mutex_lock(&event->mmap_mutex);
5770 /* Can't redirect output if we've got an active mmap() */
5771 if (atomic_read(&event->mmap_count))
5775 /* get the buffer we want to redirect to */
5776 buffer = perf_buffer_get(output_event);
5781 old_buffer = event->buffer;
5782 rcu_assign_pointer(event->buffer, buffer);
5785 mutex_unlock(&event->mmap_mutex);
5788 perf_buffer_put(old_buffer);
5794 * sys_perf_event_open - open a performance event, associate it to a task/cpu
5796 * @attr_uptr: event_id type attributes for monitoring/sampling
5799 * @group_fd: group leader event fd
5801 SYSCALL_DEFINE5(perf_event_open,
5802 struct perf_event_attr __user *, attr_uptr,
5803 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
5805 struct perf_event *group_leader = NULL, *output_event = NULL;
5806 struct perf_event *event, *sibling;
5807 struct perf_event_attr attr;
5808 struct perf_event_context *ctx;
5809 struct file *event_file = NULL;
5810 struct file *group_file = NULL;
5811 struct task_struct *task = NULL;
5815 int fput_needed = 0;
5818 /* for future expandability... */
5819 if (flags & ~(PERF_FLAG_FD_NO_GROUP | PERF_FLAG_FD_OUTPUT))
5822 err = perf_copy_attr(attr_uptr, &attr);
5826 if (!attr.exclude_kernel) {
5827 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
5832 if (attr.sample_freq > sysctl_perf_event_sample_rate)
5836 event_fd = get_unused_fd_flags(O_RDWR);
5840 if (group_fd != -1) {
5841 group_leader = perf_fget_light(group_fd, &fput_needed);
5842 if (IS_ERR(group_leader)) {
5843 err = PTR_ERR(group_leader);
5846 group_file = group_leader->filp;
5847 if (flags & PERF_FLAG_FD_OUTPUT)
5848 output_event = group_leader;
5849 if (flags & PERF_FLAG_FD_NO_GROUP)
5850 group_leader = NULL;
5854 task = find_lively_task_by_vpid(pid);
5856 err = PTR_ERR(task);
5861 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL, NULL);
5862 if (IS_ERR(event)) {
5863 err = PTR_ERR(event);
5868 * Special case software events and allow them to be part of
5869 * any hardware group.
5874 (is_software_event(event) != is_software_event(group_leader))) {
5875 if (is_software_event(event)) {
5877 * If event and group_leader are not both a software
5878 * event, and event is, then group leader is not.
5880 * Allow the addition of software events to !software
5881 * groups, this is safe because software events never
5884 pmu = group_leader->pmu;
5885 } else if (is_software_event(group_leader) &&
5886 (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
5888 * In case the group is a pure software group, and we
5889 * try to add a hardware event, move the whole group to
5890 * the hardware context.
5897 * Get the target context (task or percpu):
5899 ctx = find_get_context(pmu, task, cpu);
5906 * Look up the group leader (we will attach this event to it):
5912 * Do not allow a recursive hierarchy (this new sibling
5913 * becoming part of another group-sibling):
5915 if (group_leader->group_leader != group_leader)
5918 * Do not allow to attach to a group in a different
5919 * task or CPU context:
5922 if (group_leader->ctx->type != ctx->type)
5925 if (group_leader->ctx != ctx)
5930 * Only a group leader can be exclusive or pinned
5932 if (attr.exclusive || attr.pinned)
5937 err = perf_event_set_output(event, output_event);
5942 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, O_RDWR);
5943 if (IS_ERR(event_file)) {
5944 err = PTR_ERR(event_file);
5949 struct perf_event_context *gctx = group_leader->ctx;
5951 mutex_lock(&gctx->mutex);
5952 perf_event_remove_from_context(group_leader);
5953 list_for_each_entry(sibling, &group_leader->sibling_list,
5955 perf_event_remove_from_context(sibling);
5958 mutex_unlock(&gctx->mutex);
5962 event->filp = event_file;
5963 WARN_ON_ONCE(ctx->parent_ctx);
5964 mutex_lock(&ctx->mutex);
5967 perf_install_in_context(ctx, group_leader, cpu);
5969 list_for_each_entry(sibling, &group_leader->sibling_list,
5971 perf_install_in_context(ctx, sibling, cpu);
5976 perf_install_in_context(ctx, event, cpu);
5978 mutex_unlock(&ctx->mutex);
5980 event->owner = current;
5982 mutex_lock(¤t->perf_event_mutex);
5983 list_add_tail(&event->owner_entry, ¤t->perf_event_list);
5984 mutex_unlock(¤t->perf_event_mutex);
5987 * Precalculate sample_data sizes
5989 perf_event__header_size(event);
5990 perf_event__id_header_size(event);
5993 * Drop the reference on the group_event after placing the
5994 * new event on the sibling_list. This ensures destruction
5995 * of the group leader will find the pointer to itself in
5996 * perf_group_detach().
5998 fput_light(group_file, fput_needed);
5999 fd_install(event_fd, event_file);
6008 put_task_struct(task);
6010 fput_light(group_file, fput_needed);
6012 put_unused_fd(event_fd);
6017 * perf_event_create_kernel_counter
6019 * @attr: attributes of the counter to create
6020 * @cpu: cpu in which the counter is bound
6021 * @task: task to profile (NULL for percpu)
6024 perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
6025 struct task_struct *task,
6026 perf_overflow_handler_t overflow_handler)
6028 struct perf_event_context *ctx;
6029 struct perf_event *event;
6033 * Get the target context (task or percpu):
6036 event = perf_event_alloc(attr, cpu, task, NULL, NULL, overflow_handler);
6037 if (IS_ERR(event)) {
6038 err = PTR_ERR(event);
6042 ctx = find_get_context(event->pmu, task, cpu);
6049 WARN_ON_ONCE(ctx->parent_ctx);
6050 mutex_lock(&ctx->mutex);
6051 perf_install_in_context(ctx, event, cpu);
6053 mutex_unlock(&ctx->mutex);
6060 return ERR_PTR(err);
6062 EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
6064 static void sync_child_event(struct perf_event *child_event,
6065 struct task_struct *child)
6067 struct perf_event *parent_event = child_event->parent;
6070 if (child_event->attr.inherit_stat)
6071 perf_event_read_event(child_event, child);
6073 child_val = perf_event_count(child_event);
6076 * Add back the child's count to the parent's count:
6078 atomic64_add(child_val, &parent_event->child_count);
6079 atomic64_add(child_event->total_time_enabled,
6080 &parent_event->child_total_time_enabled);
6081 atomic64_add(child_event->total_time_running,
6082 &parent_event->child_total_time_running);
6085 * Remove this event from the parent's list
6087 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
6088 mutex_lock(&parent_event->child_mutex);
6089 list_del_init(&child_event->child_list);
6090 mutex_unlock(&parent_event->child_mutex);
6093 * Release the parent event, if this was the last
6096 fput(parent_event->filp);
6100 __perf_event_exit_task(struct perf_event *child_event,
6101 struct perf_event_context *child_ctx,
6102 struct task_struct *child)
6104 struct perf_event *parent_event;
6106 perf_event_remove_from_context(child_event);
6108 parent_event = child_event->parent;
6110 * It can happen that parent exits first, and has events
6111 * that are still around due to the child reference. These
6112 * events need to be zapped - but otherwise linger.
6115 sync_child_event(child_event, child);
6116 free_event(child_event);
6120 static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
6122 struct perf_event *child_event, *tmp;
6123 struct perf_event_context *child_ctx;
6124 unsigned long flags;
6126 if (likely(!child->perf_event_ctxp[ctxn])) {
6127 perf_event_task(child, NULL, 0);
6131 local_irq_save(flags);
6133 * We can't reschedule here because interrupts are disabled,
6134 * and either child is current or it is a task that can't be
6135 * scheduled, so we are now safe from rescheduling changing
6138 child_ctx = rcu_dereference_raw(child->perf_event_ctxp[ctxn]);
6139 task_ctx_sched_out(child_ctx, EVENT_ALL);
6142 * Take the context lock here so that if find_get_context is
6143 * reading child->perf_event_ctxp, we wait until it has
6144 * incremented the context's refcount before we do put_ctx below.
6146 raw_spin_lock(&child_ctx->lock);
6147 child->perf_event_ctxp[ctxn] = NULL;
6149 * If this context is a clone; unclone it so it can't get
6150 * swapped to another process while we're removing all
6151 * the events from it.
6153 unclone_ctx(child_ctx);
6154 update_context_time(child_ctx);
6155 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
6158 * Report the task dead after unscheduling the events so that we
6159 * won't get any samples after PERF_RECORD_EXIT. We can however still
6160 * get a few PERF_RECORD_READ events.
6162 perf_event_task(child, child_ctx, 0);
6165 * We can recurse on the same lock type through:
6167 * __perf_event_exit_task()
6168 * sync_child_event()
6169 * fput(parent_event->filp)
6171 * mutex_lock(&ctx->mutex)
6173 * But since its the parent context it won't be the same instance.
6175 mutex_lock(&child_ctx->mutex);
6178 list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
6180 __perf_event_exit_task(child_event, child_ctx, child);
6182 list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
6184 __perf_event_exit_task(child_event, child_ctx, child);
6187 * If the last event was a group event, it will have appended all
6188 * its siblings to the list, but we obtained 'tmp' before that which
6189 * will still point to the list head terminating the iteration.
6191 if (!list_empty(&child_ctx->pinned_groups) ||
6192 !list_empty(&child_ctx->flexible_groups))
6195 mutex_unlock(&child_ctx->mutex);
6201 * When a child task exits, feed back event values to parent events.
6203 void perf_event_exit_task(struct task_struct *child)
6205 struct perf_event *event, *tmp;
6208 mutex_lock(&child->perf_event_mutex);
6209 list_for_each_entry_safe(event, tmp, &child->perf_event_list,
6211 list_del_init(&event->owner_entry);
6214 * Ensure the list deletion is visible before we clear
6215 * the owner, closes a race against perf_release() where
6216 * we need to serialize on the owner->perf_event_mutex.
6219 event->owner = NULL;
6221 mutex_unlock(&child->perf_event_mutex);
6223 for_each_task_context_nr(ctxn)
6224 perf_event_exit_task_context(child, ctxn);
6227 static void perf_free_event(struct perf_event *event,
6228 struct perf_event_context *ctx)
6230 struct perf_event *parent = event->parent;
6232 if (WARN_ON_ONCE(!parent))
6235 mutex_lock(&parent->child_mutex);
6236 list_del_init(&event->child_list);
6237 mutex_unlock(&parent->child_mutex);
6241 perf_group_detach(event);
6242 list_del_event(event, ctx);
6247 * free an unexposed, unused context as created by inheritance by
6248 * perf_event_init_task below, used by fork() in case of fail.
6250 void perf_event_free_task(struct task_struct *task)
6252 struct perf_event_context *ctx;
6253 struct perf_event *event, *tmp;
6256 for_each_task_context_nr(ctxn) {
6257 ctx = task->perf_event_ctxp[ctxn];
6261 mutex_lock(&ctx->mutex);
6263 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
6265 perf_free_event(event, ctx);
6267 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
6269 perf_free_event(event, ctx);
6271 if (!list_empty(&ctx->pinned_groups) ||
6272 !list_empty(&ctx->flexible_groups))
6275 mutex_unlock(&ctx->mutex);
6281 void perf_event_delayed_put(struct task_struct *task)
6285 for_each_task_context_nr(ctxn)
6286 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
6290 * inherit a event from parent task to child task:
6292 static struct perf_event *
6293 inherit_event(struct perf_event *parent_event,
6294 struct task_struct *parent,
6295 struct perf_event_context *parent_ctx,
6296 struct task_struct *child,
6297 struct perf_event *group_leader,
6298 struct perf_event_context *child_ctx)
6300 struct perf_event *child_event;
6301 unsigned long flags;
6304 * Instead of creating recursive hierarchies of events,
6305 * we link inherited events back to the original parent,
6306 * which has a filp for sure, which we use as the reference
6309 if (parent_event->parent)
6310 parent_event = parent_event->parent;
6312 child_event = perf_event_alloc(&parent_event->attr,
6315 group_leader, parent_event,
6317 if (IS_ERR(child_event))
6322 * Make the child state follow the state of the parent event,
6323 * not its attr.disabled bit. We hold the parent's mutex,
6324 * so we won't race with perf_event_{en, dis}able_family.
6326 if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
6327 child_event->state = PERF_EVENT_STATE_INACTIVE;
6329 child_event->state = PERF_EVENT_STATE_OFF;
6331 if (parent_event->attr.freq) {
6332 u64 sample_period = parent_event->hw.sample_period;
6333 struct hw_perf_event *hwc = &child_event->hw;
6335 hwc->sample_period = sample_period;
6336 hwc->last_period = sample_period;
6338 local64_set(&hwc->period_left, sample_period);
6341 child_event->ctx = child_ctx;
6342 child_event->overflow_handler = parent_event->overflow_handler;
6345 * Precalculate sample_data sizes
6347 perf_event__header_size(child_event);
6348 perf_event__id_header_size(child_event);
6351 * Link it up in the child's context:
6353 raw_spin_lock_irqsave(&child_ctx->lock, flags);
6354 add_event_to_ctx(child_event, child_ctx);
6355 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
6358 * Get a reference to the parent filp - we will fput it
6359 * when the child event exits. This is safe to do because
6360 * we are in the parent and we know that the filp still
6361 * exists and has a nonzero count:
6363 atomic_long_inc(&parent_event->filp->f_count);
6366 * Link this into the parent event's child list
6368 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
6369 mutex_lock(&parent_event->child_mutex);
6370 list_add_tail(&child_event->child_list, &parent_event->child_list);
6371 mutex_unlock(&parent_event->child_mutex);
6376 static int inherit_group(struct perf_event *parent_event,
6377 struct task_struct *parent,
6378 struct perf_event_context *parent_ctx,
6379 struct task_struct *child,
6380 struct perf_event_context *child_ctx)
6382 struct perf_event *leader;
6383 struct perf_event *sub;
6384 struct perf_event *child_ctr;
6386 leader = inherit_event(parent_event, parent, parent_ctx,
6387 child, NULL, child_ctx);
6389 return PTR_ERR(leader);
6390 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
6391 child_ctr = inherit_event(sub, parent, parent_ctx,
6392 child, leader, child_ctx);
6393 if (IS_ERR(child_ctr))
6394 return PTR_ERR(child_ctr);
6400 inherit_task_group(struct perf_event *event, struct task_struct *parent,
6401 struct perf_event_context *parent_ctx,
6402 struct task_struct *child, int ctxn,
6406 struct perf_event_context *child_ctx;
6408 if (!event->attr.inherit) {
6413 child_ctx = child->perf_event_ctxp[ctxn];
6416 * This is executed from the parent task context, so
6417 * inherit events that have been marked for cloning.
6418 * First allocate and initialize a context for the
6422 child_ctx = alloc_perf_context(event->pmu, child);
6426 child->perf_event_ctxp[ctxn] = child_ctx;
6429 ret = inherit_group(event, parent, parent_ctx,
6439 * Initialize the perf_event context in task_struct
6441 int perf_event_init_context(struct task_struct *child, int ctxn)
6443 struct perf_event_context *child_ctx, *parent_ctx;
6444 struct perf_event_context *cloned_ctx;
6445 struct perf_event *event;
6446 struct task_struct *parent = current;
6447 int inherited_all = 1;
6448 unsigned long flags;
6451 if (likely(!parent->perf_event_ctxp[ctxn]))
6455 * If the parent's context is a clone, pin it so it won't get
6458 parent_ctx = perf_pin_task_context(parent, ctxn);
6461 * No need to check if parent_ctx != NULL here; since we saw
6462 * it non-NULL earlier, the only reason for it to become NULL
6463 * is if we exit, and since we're currently in the middle of
6464 * a fork we can't be exiting at the same time.
6468 * Lock the parent list. No need to lock the child - not PID
6469 * hashed yet and not running, so nobody can access it.
6471 mutex_lock(&parent_ctx->mutex);
6474 * We dont have to disable NMIs - we are only looking at
6475 * the list, not manipulating it:
6477 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
6478 ret = inherit_task_group(event, parent, parent_ctx,
6479 child, ctxn, &inherited_all);
6485 * We can't hold ctx->lock when iterating the ->flexible_group list due
6486 * to allocations, but we need to prevent rotation because
6487 * rotate_ctx() will change the list from interrupt context.
6489 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
6490 parent_ctx->rotate_disable = 1;
6491 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
6493 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
6494 ret = inherit_task_group(event, parent, parent_ctx,
6495 child, ctxn, &inherited_all);
6500 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
6501 parent_ctx->rotate_disable = 0;
6503 child_ctx = child->perf_event_ctxp[ctxn];
6505 if (child_ctx && inherited_all) {
6507 * Mark the child context as a clone of the parent
6508 * context, or of whatever the parent is a clone of.
6510 * Note that if the parent is a clone, the holding of
6511 * parent_ctx->lock avoids it from being uncloned.
6513 cloned_ctx = parent_ctx->parent_ctx;
6515 child_ctx->parent_ctx = cloned_ctx;
6516 child_ctx->parent_gen = parent_ctx->parent_gen;
6518 child_ctx->parent_ctx = parent_ctx;
6519 child_ctx->parent_gen = parent_ctx->generation;
6521 get_ctx(child_ctx->parent_ctx);
6524 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
6525 mutex_unlock(&parent_ctx->mutex);
6527 perf_unpin_context(parent_ctx);
6533 * Initialize the perf_event context in task_struct
6535 int perf_event_init_task(struct task_struct *child)
6539 memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
6540 mutex_init(&child->perf_event_mutex);
6541 INIT_LIST_HEAD(&child->perf_event_list);
6543 for_each_task_context_nr(ctxn) {
6544 ret = perf_event_init_context(child, ctxn);
6552 static void __init perf_event_init_all_cpus(void)
6554 struct swevent_htable *swhash;
6557 for_each_possible_cpu(cpu) {
6558 swhash = &per_cpu(swevent_htable, cpu);
6559 mutex_init(&swhash->hlist_mutex);
6560 INIT_LIST_HEAD(&per_cpu(rotation_list, cpu));
6564 static void __cpuinit perf_event_init_cpu(int cpu)
6566 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
6568 mutex_lock(&swhash->hlist_mutex);
6569 if (swhash->hlist_refcount > 0) {
6570 struct swevent_hlist *hlist;
6572 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
6574 rcu_assign_pointer(swhash->swevent_hlist, hlist);
6576 mutex_unlock(&swhash->hlist_mutex);
6579 #if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC
6580 static void perf_pmu_rotate_stop(struct pmu *pmu)
6582 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
6584 WARN_ON(!irqs_disabled());
6586 list_del_init(&cpuctx->rotation_list);
6589 static void __perf_event_exit_context(void *__info)
6591 struct perf_event_context *ctx = __info;
6592 struct perf_event *event, *tmp;
6594 perf_pmu_rotate_stop(ctx->pmu);
6596 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
6597 __perf_event_remove_from_context(event);
6598 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, group_entry)
6599 __perf_event_remove_from_context(event);
6602 static void perf_event_exit_cpu_context(int cpu)
6604 struct perf_event_context *ctx;
6608 idx = srcu_read_lock(&pmus_srcu);
6609 list_for_each_entry_rcu(pmu, &pmus, entry) {
6610 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
6612 mutex_lock(&ctx->mutex);
6613 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
6614 mutex_unlock(&ctx->mutex);
6616 srcu_read_unlock(&pmus_srcu, idx);
6619 static void perf_event_exit_cpu(int cpu)
6621 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
6623 mutex_lock(&swhash->hlist_mutex);
6624 swevent_hlist_release(swhash);
6625 mutex_unlock(&swhash->hlist_mutex);
6627 perf_event_exit_cpu_context(cpu);
6630 static inline void perf_event_exit_cpu(int cpu) { }
6634 perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
6638 for_each_online_cpu(cpu)
6639 perf_event_exit_cpu(cpu);
6645 * Run the perf reboot notifier at the very last possible moment so that
6646 * the generic watchdog code runs as long as possible.
6648 static struct notifier_block perf_reboot_notifier = {
6649 .notifier_call = perf_reboot,
6650 .priority = INT_MIN,
6653 static int __cpuinit
6654 perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
6656 unsigned int cpu = (long)hcpu;
6658 switch (action & ~CPU_TASKS_FROZEN) {
6660 case CPU_UP_PREPARE:
6661 case CPU_DOWN_FAILED:
6662 perf_event_init_cpu(cpu);
6665 case CPU_UP_CANCELED:
6666 case CPU_DOWN_PREPARE:
6667 perf_event_exit_cpu(cpu);
6677 void __init perf_event_init(void)
6683 perf_event_init_all_cpus();
6684 init_srcu_struct(&pmus_srcu);
6685 perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
6686 perf_pmu_register(&perf_cpu_clock, NULL, -1);
6687 perf_pmu_register(&perf_task_clock, NULL, -1);
6689 perf_cpu_notifier(perf_cpu_notify);
6690 register_reboot_notifier(&perf_reboot_notifier);
6692 ret = init_hw_breakpoint();
6693 WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
6696 static int __init perf_event_sysfs_init(void)
6701 mutex_lock(&pmus_lock);
6703 ret = bus_register(&pmu_bus);
6707 list_for_each_entry(pmu, &pmus, entry) {
6708 if (!pmu->name || pmu->type < 0)
6711 ret = pmu_dev_alloc(pmu);
6712 WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
6714 pmu_bus_running = 1;
6718 mutex_unlock(&pmus_lock);
6722 device_initcall(perf_event_sysfs_init);