2 * Performance events core code:
4 * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
5 * Copyright (C) 2008-2009 Red Hat, Inc., Ingo Molnar
6 * Copyright (C) 2008-2009 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
7 * Copyright © 2009 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
9 * For licensing details see kernel-base/COPYING
14 #include <linux/cpu.h>
15 #include <linux/smp.h>
16 #include <linux/idr.h>
17 #include <linux/file.h>
18 #include <linux/poll.h>
19 #include <linux/slab.h>
20 #include <linux/hash.h>
21 #include <linux/sysfs.h>
22 #include <linux/dcache.h>
23 #include <linux/percpu.h>
24 #include <linux/ptrace.h>
25 #include <linux/reboot.h>
26 #include <linux/vmstat.h>
27 #include <linux/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>
41 struct remote_function_call {
42 struct task_struct *p;
43 int (*func)(void *info);
48 static void remote_function(void *data)
50 struct remote_function_call *tfc = data;
51 struct task_struct *p = tfc->p;
55 if (task_cpu(p) != smp_processor_id() || !task_curr(p))
59 tfc->ret = tfc->func(tfc->info);
63 * task_function_call - call a function on the cpu on which a task runs
64 * @p: the task to evaluate
65 * @func: the function to be called
66 * @info: the function call argument
68 * Calls the function @func when the task is currently running. This might
69 * be on the current CPU, which just calls the function directly
71 * returns: @func return value, or
72 * -ESRCH - when the process isn't running
73 * -EAGAIN - when the process moved away
76 task_function_call(struct task_struct *p, int (*func) (void *info), void *info)
78 struct remote_function_call data = {
82 .ret = -ESRCH, /* No such (running) process */
86 smp_call_function_single(task_cpu(p), remote_function, &data, 1);
92 * cpu_function_call - call a function on the cpu
93 * @func: the function to be called
94 * @info: the function call argument
96 * Calls the function @func on the remote cpu.
98 * returns: @func return value or -ENXIO when the cpu is offline
100 static int cpu_function_call(int cpu, int (*func) (void *info), void *info)
102 struct remote_function_call data = {
106 .ret = -ENXIO, /* No such CPU */
109 smp_call_function_single(cpu, remote_function, &data, 1);
115 EVENT_FLEXIBLE = 0x1,
117 EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
120 atomic_t perf_task_events __read_mostly;
121 static atomic_t nr_mmap_events __read_mostly;
122 static atomic_t nr_comm_events __read_mostly;
123 static atomic_t nr_task_events __read_mostly;
125 static LIST_HEAD(pmus);
126 static DEFINE_MUTEX(pmus_lock);
127 static struct srcu_struct pmus_srcu;
130 * perf event paranoia level:
131 * -1 - not paranoid at all
132 * 0 - disallow raw tracepoint access for unpriv
133 * 1 - disallow cpu events for unpriv
134 * 2 - disallow kernel profiling for unpriv
136 int sysctl_perf_event_paranoid __read_mostly = 1;
138 int sysctl_perf_event_mlock __read_mostly = 512; /* 'free' kb per user */
141 * max perf event sample rate
143 int sysctl_perf_event_sample_rate __read_mostly = 100000;
145 static atomic64_t perf_event_id;
147 static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
148 enum event_type_t event_type);
150 static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
151 enum event_type_t event_type);
153 void __weak perf_event_print_debug(void) { }
155 extern __weak const char *perf_pmu_name(void)
160 static inline u64 perf_clock(void)
162 return local_clock();
165 void perf_pmu_disable(struct pmu *pmu)
167 int *count = this_cpu_ptr(pmu->pmu_disable_count);
169 pmu->pmu_disable(pmu);
172 void perf_pmu_enable(struct pmu *pmu)
174 int *count = this_cpu_ptr(pmu->pmu_disable_count);
176 pmu->pmu_enable(pmu);
179 static DEFINE_PER_CPU(struct list_head, rotation_list);
182 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
183 * because they're strictly cpu affine and rotate_start is called with IRQs
184 * disabled, while rotate_context is called from IRQ context.
186 static void perf_pmu_rotate_start(struct pmu *pmu)
188 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
189 struct list_head *head = &__get_cpu_var(rotation_list);
191 WARN_ON(!irqs_disabled());
193 if (list_empty(&cpuctx->rotation_list))
194 list_add(&cpuctx->rotation_list, head);
197 static void get_ctx(struct perf_event_context *ctx)
199 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
202 static void free_ctx(struct rcu_head *head)
204 struct perf_event_context *ctx;
206 ctx = container_of(head, struct perf_event_context, rcu_head);
210 static void put_ctx(struct perf_event_context *ctx)
212 if (atomic_dec_and_test(&ctx->refcount)) {
214 put_ctx(ctx->parent_ctx);
216 put_task_struct(ctx->task);
217 call_rcu(&ctx->rcu_head, free_ctx);
221 static void unclone_ctx(struct perf_event_context *ctx)
223 if (ctx->parent_ctx) {
224 put_ctx(ctx->parent_ctx);
225 ctx->parent_ctx = NULL;
229 static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
232 * only top level events have the pid namespace they were created in
235 event = event->parent;
237 return task_tgid_nr_ns(p, event->ns);
240 static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
243 * only top level events have the pid namespace they were created in
246 event = event->parent;
248 return task_pid_nr_ns(p, event->ns);
252 * If we inherit events we want to return the parent event id
255 static u64 primary_event_id(struct perf_event *event)
260 id = event->parent->id;
266 * Get the perf_event_context for a task and lock it.
267 * This has to cope with with the fact that until it is locked,
268 * the context could get moved to another task.
270 static struct perf_event_context *
271 perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
273 struct perf_event_context *ctx;
277 ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
280 * If this context is a clone of another, it might
281 * get swapped for another underneath us by
282 * perf_event_task_sched_out, though the
283 * rcu_read_lock() protects us from any context
284 * getting freed. Lock the context and check if it
285 * got swapped before we could get the lock, and retry
286 * if so. If we locked the right context, then it
287 * can't get swapped on us any more.
289 raw_spin_lock_irqsave(&ctx->lock, *flags);
290 if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
291 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
295 if (!atomic_inc_not_zero(&ctx->refcount)) {
296 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
305 * Get the context for a task and increment its pin_count so it
306 * can't get swapped to another task. This also increments its
307 * reference count so that the context can't get freed.
309 static struct perf_event_context *
310 perf_pin_task_context(struct task_struct *task, int ctxn)
312 struct perf_event_context *ctx;
315 ctx = perf_lock_task_context(task, ctxn, &flags);
318 raw_spin_unlock_irqrestore(&ctx->lock, flags);
323 static void perf_unpin_context(struct perf_event_context *ctx)
327 raw_spin_lock_irqsave(&ctx->lock, flags);
329 raw_spin_unlock_irqrestore(&ctx->lock, flags);
333 * Update the record of the current time in a context.
335 static void update_context_time(struct perf_event_context *ctx)
337 u64 now = perf_clock();
339 ctx->time += now - ctx->timestamp;
340 ctx->timestamp = now;
343 static u64 perf_event_time(struct perf_event *event)
345 struct perf_event_context *ctx = event->ctx;
346 return ctx ? ctx->time : 0;
350 * Update the total_time_enabled and total_time_running fields for a event.
352 static void update_event_times(struct perf_event *event)
354 struct perf_event_context *ctx = event->ctx;
357 if (event->state < PERF_EVENT_STATE_INACTIVE ||
358 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
362 run_end = perf_event_time(event);
364 run_end = event->tstamp_stopped;
366 event->total_time_enabled = run_end - event->tstamp_enabled;
368 if (event->state == PERF_EVENT_STATE_INACTIVE)
369 run_end = event->tstamp_stopped;
371 run_end = perf_event_time(event);
373 event->total_time_running = run_end - event->tstamp_running;
377 * Update total_time_enabled and total_time_running for all events in a group.
379 static void update_group_times(struct perf_event *leader)
381 struct perf_event *event;
383 update_event_times(leader);
384 list_for_each_entry(event, &leader->sibling_list, group_entry)
385 update_event_times(event);
388 static struct list_head *
389 ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
391 if (event->attr.pinned)
392 return &ctx->pinned_groups;
394 return &ctx->flexible_groups;
398 * Add a event from the lists for its context.
399 * Must be called with ctx->mutex and ctx->lock held.
402 list_add_event(struct perf_event *event, struct perf_event_context *ctx)
404 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
405 event->attach_state |= PERF_ATTACH_CONTEXT;
408 * If we're a stand alone event or group leader, we go to the context
409 * list, group events are kept attached to the group so that
410 * perf_group_detach can, at all times, locate all siblings.
412 if (event->group_leader == event) {
413 struct list_head *list;
415 if (is_software_event(event))
416 event->group_flags |= PERF_GROUP_SOFTWARE;
418 list = ctx_group_list(event, ctx);
419 list_add_tail(&event->group_entry, list);
422 list_add_rcu(&event->event_entry, &ctx->event_list);
424 perf_pmu_rotate_start(ctx->pmu);
426 if (event->attr.inherit_stat)
431 * Called at perf_event creation and when events are attached/detached from a
434 static void perf_event__read_size(struct perf_event *event)
436 int entry = sizeof(u64); /* value */
440 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
443 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
446 if (event->attr.read_format & PERF_FORMAT_ID)
447 entry += sizeof(u64);
449 if (event->attr.read_format & PERF_FORMAT_GROUP) {
450 nr += event->group_leader->nr_siblings;
455 event->read_size = size;
458 static void perf_event__header_size(struct perf_event *event)
460 struct perf_sample_data *data;
461 u64 sample_type = event->attr.sample_type;
464 perf_event__read_size(event);
466 if (sample_type & PERF_SAMPLE_IP)
467 size += sizeof(data->ip);
469 if (sample_type & PERF_SAMPLE_ADDR)
470 size += sizeof(data->addr);
472 if (sample_type & PERF_SAMPLE_PERIOD)
473 size += sizeof(data->period);
475 if (sample_type & PERF_SAMPLE_READ)
476 size += event->read_size;
478 event->header_size = size;
481 static void perf_event__id_header_size(struct perf_event *event)
483 struct perf_sample_data *data;
484 u64 sample_type = event->attr.sample_type;
487 if (sample_type & PERF_SAMPLE_TID)
488 size += sizeof(data->tid_entry);
490 if (sample_type & PERF_SAMPLE_TIME)
491 size += sizeof(data->time);
493 if (sample_type & PERF_SAMPLE_ID)
494 size += sizeof(data->id);
496 if (sample_type & PERF_SAMPLE_STREAM_ID)
497 size += sizeof(data->stream_id);
499 if (sample_type & PERF_SAMPLE_CPU)
500 size += sizeof(data->cpu_entry);
502 event->id_header_size = size;
505 static void perf_group_attach(struct perf_event *event)
507 struct perf_event *group_leader = event->group_leader, *pos;
510 * We can have double attach due to group movement in perf_event_open.
512 if (event->attach_state & PERF_ATTACH_GROUP)
515 event->attach_state |= PERF_ATTACH_GROUP;
517 if (group_leader == event)
520 if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
521 !is_software_event(event))
522 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
524 list_add_tail(&event->group_entry, &group_leader->sibling_list);
525 group_leader->nr_siblings++;
527 perf_event__header_size(group_leader);
529 list_for_each_entry(pos, &group_leader->sibling_list, group_entry)
530 perf_event__header_size(pos);
534 * Remove a event from the lists for its context.
535 * Must be called with ctx->mutex and ctx->lock held.
538 list_del_event(struct perf_event *event, struct perf_event_context *ctx)
541 * We can have double detach due to exit/hot-unplug + close.
543 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
546 event->attach_state &= ~PERF_ATTACH_CONTEXT;
549 if (event->attr.inherit_stat)
552 list_del_rcu(&event->event_entry);
554 if (event->group_leader == event)
555 list_del_init(&event->group_entry);
557 update_group_times(event);
560 * If event was in error state, then keep it
561 * that way, otherwise bogus counts will be
562 * returned on read(). The only way to get out
563 * of error state is by explicit re-enabling
566 if (event->state > PERF_EVENT_STATE_OFF)
567 event->state = PERF_EVENT_STATE_OFF;
570 static void perf_group_detach(struct perf_event *event)
572 struct perf_event *sibling, *tmp;
573 struct list_head *list = NULL;
576 * We can have double detach due to exit/hot-unplug + close.
578 if (!(event->attach_state & PERF_ATTACH_GROUP))
581 event->attach_state &= ~PERF_ATTACH_GROUP;
584 * If this is a sibling, remove it from its group.
586 if (event->group_leader != event) {
587 list_del_init(&event->group_entry);
588 event->group_leader->nr_siblings--;
592 if (!list_empty(&event->group_entry))
593 list = &event->group_entry;
596 * If this was a group event with sibling events then
597 * upgrade the siblings to singleton events by adding them
598 * to whatever list we are on.
600 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
602 list_move_tail(&sibling->group_entry, list);
603 sibling->group_leader = sibling;
605 /* Inherit group flags from the previous leader */
606 sibling->group_flags = event->group_flags;
610 perf_event__header_size(event->group_leader);
612 list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry)
613 perf_event__header_size(tmp);
617 event_filter_match(struct perf_event *event)
619 return event->cpu == -1 || event->cpu == smp_processor_id();
623 event_sched_out(struct perf_event *event,
624 struct perf_cpu_context *cpuctx,
625 struct perf_event_context *ctx)
627 u64 tstamp = perf_event_time(event);
630 * An event which could not be activated because of
631 * filter mismatch still needs to have its timings
632 * maintained, otherwise bogus information is return
633 * via read() for time_enabled, time_running:
635 if (event->state == PERF_EVENT_STATE_INACTIVE
636 && !event_filter_match(event)) {
637 delta = ctx->time - event->tstamp_stopped;
638 event->tstamp_running += delta;
639 event->tstamp_stopped = tstamp;
642 if (event->state != PERF_EVENT_STATE_ACTIVE)
645 event->state = PERF_EVENT_STATE_INACTIVE;
646 if (event->pending_disable) {
647 event->pending_disable = 0;
648 event->state = PERF_EVENT_STATE_OFF;
650 event->tstamp_stopped = tstamp;
651 event->pmu->del(event, 0);
654 if (!is_software_event(event))
655 cpuctx->active_oncpu--;
657 if (event->attr.exclusive || !cpuctx->active_oncpu)
658 cpuctx->exclusive = 0;
662 group_sched_out(struct perf_event *group_event,
663 struct perf_cpu_context *cpuctx,
664 struct perf_event_context *ctx)
666 struct perf_event *event;
667 int state = group_event->state;
669 event_sched_out(group_event, cpuctx, ctx);
672 * Schedule out siblings (if any):
674 list_for_each_entry(event, &group_event->sibling_list, group_entry)
675 event_sched_out(event, cpuctx, ctx);
677 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
678 cpuctx->exclusive = 0;
681 static inline struct perf_cpu_context *
682 __get_cpu_context(struct perf_event_context *ctx)
684 return this_cpu_ptr(ctx->pmu->pmu_cpu_context);
688 * Cross CPU call to remove a performance event
690 * We disable the event on the hardware level first. After that we
691 * remove it from the context list.
693 static int __perf_remove_from_context(void *info)
695 struct perf_event *event = info;
696 struct perf_event_context *ctx = event->ctx;
697 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
699 raw_spin_lock(&ctx->lock);
700 event_sched_out(event, cpuctx, ctx);
701 list_del_event(event, ctx);
702 raw_spin_unlock(&ctx->lock);
709 * Remove the event from a task's (or a CPU's) list of events.
711 * CPU events are removed with a smp call. For task events we only
712 * call when the task is on a CPU.
714 * If event->ctx is a cloned context, callers must make sure that
715 * every task struct that event->ctx->task could possibly point to
716 * remains valid. This is OK when called from perf_release since
717 * that only calls us on the top-level context, which can't be a clone.
718 * When called from perf_event_exit_task, it's OK because the
719 * context has been detached from its task.
721 static void perf_remove_from_context(struct perf_event *event)
723 struct perf_event_context *ctx = event->ctx;
724 struct task_struct *task = ctx->task;
726 lockdep_assert_held(&ctx->mutex);
730 * Per cpu events are removed via an smp call and
731 * the removal is always successful.
733 cpu_function_call(event->cpu, __perf_remove_from_context, event);
738 if (!task_function_call(task, __perf_remove_from_context, event))
741 raw_spin_lock_irq(&ctx->lock);
743 * If we failed to find a running task, but find the context active now
744 * that we've acquired the ctx->lock, retry.
746 if (ctx->is_active) {
747 raw_spin_unlock_irq(&ctx->lock);
752 * Since the task isn't running, its safe to remove the event, us
753 * holding the ctx->lock ensures the task won't get scheduled in.
755 list_del_event(event, ctx);
756 raw_spin_unlock_irq(&ctx->lock);
760 * Cross CPU call to disable a performance event
762 static int __perf_event_disable(void *info)
764 struct perf_event *event = info;
765 struct perf_event_context *ctx = event->ctx;
766 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
769 * If this is a per-task event, need to check whether this
770 * event's task is the current task on this cpu.
772 * Can trigger due to concurrent perf_event_context_sched_out()
773 * flipping contexts around.
775 if (ctx->task && cpuctx->task_ctx != ctx)
778 raw_spin_lock(&ctx->lock);
781 * If the event is on, turn it off.
782 * If it is in error state, leave it in error state.
784 if (event->state >= PERF_EVENT_STATE_INACTIVE) {
785 update_context_time(ctx);
786 update_group_times(event);
787 if (event == event->group_leader)
788 group_sched_out(event, cpuctx, ctx);
790 event_sched_out(event, cpuctx, ctx);
791 event->state = PERF_EVENT_STATE_OFF;
794 raw_spin_unlock(&ctx->lock);
802 * If event->ctx is a cloned context, callers must make sure that
803 * every task struct that event->ctx->task could possibly point to
804 * remains valid. This condition is satisifed when called through
805 * perf_event_for_each_child or perf_event_for_each because they
806 * hold the top-level event's child_mutex, so any descendant that
807 * goes to exit will block in sync_child_event.
808 * When called from perf_pending_event it's OK because event->ctx
809 * is the current context on this CPU and preemption is disabled,
810 * hence we can't get into perf_event_task_sched_out for this context.
812 void perf_event_disable(struct perf_event *event)
814 struct perf_event_context *ctx = event->ctx;
815 struct task_struct *task = ctx->task;
819 * Disable the event on the cpu that it's on
821 cpu_function_call(event->cpu, __perf_event_disable, event);
826 if (!task_function_call(task, __perf_event_disable, event))
829 raw_spin_lock_irq(&ctx->lock);
831 * If the event is still active, we need to retry the cross-call.
833 if (event->state == PERF_EVENT_STATE_ACTIVE) {
834 raw_spin_unlock_irq(&ctx->lock);
836 * Reload the task pointer, it might have been changed by
837 * a concurrent perf_event_context_sched_out().
844 * Since we have the lock this context can't be scheduled
845 * in, so we can change the state safely.
847 if (event->state == PERF_EVENT_STATE_INACTIVE) {
848 update_group_times(event);
849 event->state = PERF_EVENT_STATE_OFF;
851 raw_spin_unlock_irq(&ctx->lock);
854 #define MAX_INTERRUPTS (~0ULL)
856 static void perf_log_throttle(struct perf_event *event, int enable);
859 event_sched_in(struct perf_event *event,
860 struct perf_cpu_context *cpuctx,
861 struct perf_event_context *ctx)
863 u64 tstamp = perf_event_time(event);
865 if (event->state <= PERF_EVENT_STATE_OFF)
868 event->state = PERF_EVENT_STATE_ACTIVE;
869 event->oncpu = smp_processor_id();
872 * Unthrottle events, since we scheduled we might have missed several
873 * ticks already, also for a heavily scheduling task there is little
874 * guarantee it'll get a tick in a timely manner.
876 if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
877 perf_log_throttle(event, 1);
878 event->hw.interrupts = 0;
882 * The new state must be visible before we turn it on in the hardware:
886 if (event->pmu->add(event, PERF_EF_START)) {
887 event->state = PERF_EVENT_STATE_INACTIVE;
892 event->tstamp_running += tstamp - event->tstamp_stopped;
894 event->shadow_ctx_time = tstamp - ctx->timestamp;
896 if (!is_software_event(event))
897 cpuctx->active_oncpu++;
900 if (event->attr.exclusive)
901 cpuctx->exclusive = 1;
907 group_sched_in(struct perf_event *group_event,
908 struct perf_cpu_context *cpuctx,
909 struct perf_event_context *ctx)
911 struct perf_event *event, *partial_group = NULL;
912 struct pmu *pmu = group_event->pmu;
914 bool simulate = false;
916 if (group_event->state == PERF_EVENT_STATE_OFF)
921 if (event_sched_in(group_event, cpuctx, ctx)) {
922 pmu->cancel_txn(pmu);
927 * Schedule in siblings as one group (if any):
929 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
930 if (event_sched_in(event, cpuctx, ctx)) {
931 partial_group = event;
936 if (!pmu->commit_txn(pmu))
941 * Groups can be scheduled in as one unit only, so undo any
942 * partial group before returning:
943 * The events up to the failed event are scheduled out normally,
944 * tstamp_stopped will be updated.
946 * The failed events and the remaining siblings need to have
947 * their timings updated as if they had gone thru event_sched_in()
948 * and event_sched_out(). This is required to get consistent timings
949 * across the group. This also takes care of the case where the group
950 * could never be scheduled by ensuring tstamp_stopped is set to mark
951 * the time the event was actually stopped, such that time delta
952 * calculation in update_event_times() is correct.
954 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
955 if (event == partial_group)
959 event->tstamp_running += now - event->tstamp_stopped;
960 event->tstamp_stopped = now;
962 event_sched_out(event, cpuctx, ctx);
965 event_sched_out(group_event, cpuctx, ctx);
967 pmu->cancel_txn(pmu);
973 * Work out whether we can put this event group on the CPU now.
975 static int group_can_go_on(struct perf_event *event,
976 struct perf_cpu_context *cpuctx,
980 * Groups consisting entirely of software events can always go on.
982 if (event->group_flags & PERF_GROUP_SOFTWARE)
985 * If an exclusive group is already on, no other hardware
988 if (cpuctx->exclusive)
991 * If this group is exclusive and there are already
992 * events on the CPU, it can't go on.
994 if (event->attr.exclusive && cpuctx->active_oncpu)
997 * Otherwise, try to add it if all previous groups were able
1003 static void add_event_to_ctx(struct perf_event *event,
1004 struct perf_event_context *ctx)
1006 u64 tstamp = perf_event_time(event);
1008 list_add_event(event, ctx);
1009 perf_group_attach(event);
1010 event->tstamp_enabled = tstamp;
1011 event->tstamp_running = tstamp;
1012 event->tstamp_stopped = tstamp;
1015 static void perf_event_context_sched_in(struct perf_event_context *ctx);
1018 * Cross CPU call to install and enable a performance event
1020 * Must be called with ctx->mutex held
1022 static int __perf_install_in_context(void *info)
1024 struct perf_event *event = info;
1025 struct perf_event_context *ctx = event->ctx;
1026 struct perf_event *leader = event->group_leader;
1027 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1031 * In case we're installing a new context to an already running task,
1032 * could also happen before perf_event_task_sched_in() on architectures
1033 * which do context switches with IRQs enabled.
1035 if (ctx->task && !cpuctx->task_ctx)
1036 perf_event_context_sched_in(ctx);
1038 raw_spin_lock(&ctx->lock);
1040 update_context_time(ctx);
1042 add_event_to_ctx(event, ctx);
1044 if (!event_filter_match(event))
1048 * Don't put the event on if it is disabled or if
1049 * it is in a group and the group isn't on.
1051 if (event->state != PERF_EVENT_STATE_INACTIVE ||
1052 (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE))
1056 * An exclusive event can't go on if there are already active
1057 * hardware events, and no hardware event can go on if there
1058 * is already an exclusive event on.
1060 if (!group_can_go_on(event, cpuctx, 1))
1063 err = event_sched_in(event, cpuctx, ctx);
1067 * This event couldn't go on. If it is in a group
1068 * then we have to pull the whole group off.
1069 * If the event group is pinned then put it in error state.
1071 if (leader != event)
1072 group_sched_out(leader, cpuctx, ctx);
1073 if (leader->attr.pinned) {
1074 update_group_times(leader);
1075 leader->state = PERF_EVENT_STATE_ERROR;
1080 raw_spin_unlock(&ctx->lock);
1086 * Attach a performance event to a context
1088 * First we add the event to the list with the hardware enable bit
1089 * in event->hw_config cleared.
1091 * If the event is attached to a task which is on a CPU we use a smp
1092 * call to enable it in the task context. The task might have been
1093 * scheduled away, but we check this in the smp call again.
1096 perf_install_in_context(struct perf_event_context *ctx,
1097 struct perf_event *event,
1100 struct task_struct *task = ctx->task;
1102 lockdep_assert_held(&ctx->mutex);
1108 * Per cpu events are installed via an smp call and
1109 * the install is always successful.
1111 cpu_function_call(cpu, __perf_install_in_context, event);
1116 if (!task_function_call(task, __perf_install_in_context, event))
1119 raw_spin_lock_irq(&ctx->lock);
1121 * If we failed to find a running task, but find the context active now
1122 * that we've acquired the ctx->lock, retry.
1124 if (ctx->is_active) {
1125 raw_spin_unlock_irq(&ctx->lock);
1130 * Since the task isn't running, its safe to add the event, us holding
1131 * the ctx->lock ensures the task won't get scheduled in.
1133 add_event_to_ctx(event, ctx);
1134 raw_spin_unlock_irq(&ctx->lock);
1138 * Put a event into inactive state and update time fields.
1139 * Enabling the leader of a group effectively enables all
1140 * the group members that aren't explicitly disabled, so we
1141 * have to update their ->tstamp_enabled also.
1142 * Note: this works for group members as well as group leaders
1143 * since the non-leader members' sibling_lists will be empty.
1145 static void __perf_event_mark_enabled(struct perf_event *event,
1146 struct perf_event_context *ctx)
1148 struct perf_event *sub;
1149 u64 tstamp = perf_event_time(event);
1151 event->state = PERF_EVENT_STATE_INACTIVE;
1152 event->tstamp_enabled = tstamp - event->total_time_enabled;
1153 list_for_each_entry(sub, &event->sibling_list, group_entry) {
1154 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
1155 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
1160 * Cross CPU call to enable a performance event
1162 static int __perf_event_enable(void *info)
1164 struct perf_event *event = info;
1165 struct perf_event_context *ctx = event->ctx;
1166 struct perf_event *leader = event->group_leader;
1167 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1170 if (WARN_ON_ONCE(!ctx->is_active))
1173 raw_spin_lock(&ctx->lock);
1174 update_context_time(ctx);
1176 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1178 __perf_event_mark_enabled(event, ctx);
1180 if (!event_filter_match(event))
1184 * If the event is in a group and isn't the group leader,
1185 * then don't put it on unless the group is on.
1187 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
1190 if (!group_can_go_on(event, cpuctx, 1)) {
1193 if (event == leader)
1194 err = group_sched_in(event, cpuctx, ctx);
1196 err = event_sched_in(event, cpuctx, ctx);
1201 * If this event can't go on and it's part of a
1202 * group, then the whole group has to come off.
1204 if (leader != event)
1205 group_sched_out(leader, cpuctx, ctx);
1206 if (leader->attr.pinned) {
1207 update_group_times(leader);
1208 leader->state = PERF_EVENT_STATE_ERROR;
1213 raw_spin_unlock(&ctx->lock);
1221 * If event->ctx is a cloned context, callers must make sure that
1222 * every task struct that event->ctx->task could possibly point to
1223 * remains valid. This condition is satisfied when called through
1224 * perf_event_for_each_child or perf_event_for_each as described
1225 * for perf_event_disable.
1227 void perf_event_enable(struct perf_event *event)
1229 struct perf_event_context *ctx = event->ctx;
1230 struct task_struct *task = ctx->task;
1234 * Enable the event on the cpu that it's on
1236 cpu_function_call(event->cpu, __perf_event_enable, event);
1240 raw_spin_lock_irq(&ctx->lock);
1241 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1245 * If the event is in error state, clear that first.
1246 * That way, if we see the event in error state below, we
1247 * know that it has gone back into error state, as distinct
1248 * from the task having been scheduled away before the
1249 * cross-call arrived.
1251 if (event->state == PERF_EVENT_STATE_ERROR)
1252 event->state = PERF_EVENT_STATE_OFF;
1255 if (!ctx->is_active) {
1256 __perf_event_mark_enabled(event, ctx);
1260 raw_spin_unlock_irq(&ctx->lock);
1262 if (!task_function_call(task, __perf_event_enable, event))
1265 raw_spin_lock_irq(&ctx->lock);
1268 * If the context is active and the event is still off,
1269 * we need to retry the cross-call.
1271 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF) {
1273 * task could have been flipped by a concurrent
1274 * perf_event_context_sched_out()
1281 raw_spin_unlock_irq(&ctx->lock);
1284 static int perf_event_refresh(struct perf_event *event, int refresh)
1287 * not supported on inherited events
1289 if (event->attr.inherit || !is_sampling_event(event))
1292 atomic_add(refresh, &event->event_limit);
1293 perf_event_enable(event);
1298 static void ctx_sched_out(struct perf_event_context *ctx,
1299 struct perf_cpu_context *cpuctx,
1300 enum event_type_t event_type)
1302 struct perf_event *event;
1304 raw_spin_lock(&ctx->lock);
1305 perf_pmu_disable(ctx->pmu);
1307 if (likely(!ctx->nr_events))
1309 update_context_time(ctx);
1311 if (!ctx->nr_active)
1314 if (event_type & EVENT_PINNED) {
1315 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
1316 group_sched_out(event, cpuctx, ctx);
1319 if (event_type & EVENT_FLEXIBLE) {
1320 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
1321 group_sched_out(event, cpuctx, ctx);
1324 perf_pmu_enable(ctx->pmu);
1325 raw_spin_unlock(&ctx->lock);
1329 * Test whether two contexts are equivalent, i.e. whether they
1330 * have both been cloned from the same version of the same context
1331 * and they both have the same number of enabled events.
1332 * If the number of enabled events is the same, then the set
1333 * of enabled events should be the same, because these are both
1334 * inherited contexts, therefore we can't access individual events
1335 * in them directly with an fd; we can only enable/disable all
1336 * events via prctl, or enable/disable all events in a family
1337 * via ioctl, which will have the same effect on both contexts.
1339 static int context_equiv(struct perf_event_context *ctx1,
1340 struct perf_event_context *ctx2)
1342 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
1343 && ctx1->parent_gen == ctx2->parent_gen
1344 && !ctx1->pin_count && !ctx2->pin_count;
1347 static void __perf_event_sync_stat(struct perf_event *event,
1348 struct perf_event *next_event)
1352 if (!event->attr.inherit_stat)
1356 * Update the event value, we cannot use perf_event_read()
1357 * because we're in the middle of a context switch and have IRQs
1358 * disabled, which upsets smp_call_function_single(), however
1359 * we know the event must be on the current CPU, therefore we
1360 * don't need to use it.
1362 switch (event->state) {
1363 case PERF_EVENT_STATE_ACTIVE:
1364 event->pmu->read(event);
1367 case PERF_EVENT_STATE_INACTIVE:
1368 update_event_times(event);
1376 * In order to keep per-task stats reliable we need to flip the event
1377 * values when we flip the contexts.
1379 value = local64_read(&next_event->count);
1380 value = local64_xchg(&event->count, value);
1381 local64_set(&next_event->count, value);
1383 swap(event->total_time_enabled, next_event->total_time_enabled);
1384 swap(event->total_time_running, next_event->total_time_running);
1387 * Since we swizzled the values, update the user visible data too.
1389 perf_event_update_userpage(event);
1390 perf_event_update_userpage(next_event);
1393 #define list_next_entry(pos, member) \
1394 list_entry(pos->member.next, typeof(*pos), member)
1396 static void perf_event_sync_stat(struct perf_event_context *ctx,
1397 struct perf_event_context *next_ctx)
1399 struct perf_event *event, *next_event;
1404 update_context_time(ctx);
1406 event = list_first_entry(&ctx->event_list,
1407 struct perf_event, event_entry);
1409 next_event = list_first_entry(&next_ctx->event_list,
1410 struct perf_event, event_entry);
1412 while (&event->event_entry != &ctx->event_list &&
1413 &next_event->event_entry != &next_ctx->event_list) {
1415 __perf_event_sync_stat(event, next_event);
1417 event = list_next_entry(event, event_entry);
1418 next_event = list_next_entry(next_event, event_entry);
1422 static void perf_event_context_sched_out(struct task_struct *task, int ctxn,
1423 struct task_struct *next)
1425 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
1426 struct perf_event_context *next_ctx;
1427 struct perf_event_context *parent;
1428 struct perf_cpu_context *cpuctx;
1434 cpuctx = __get_cpu_context(ctx);
1435 if (!cpuctx->task_ctx)
1439 parent = rcu_dereference(ctx->parent_ctx);
1440 next_ctx = next->perf_event_ctxp[ctxn];
1441 if (parent && next_ctx &&
1442 rcu_dereference(next_ctx->parent_ctx) == parent) {
1444 * Looks like the two contexts are clones, so we might be
1445 * able to optimize the context switch. We lock both
1446 * contexts and check that they are clones under the
1447 * lock (including re-checking that neither has been
1448 * uncloned in the meantime). It doesn't matter which
1449 * order we take the locks because no other cpu could
1450 * be trying to lock both of these tasks.
1452 raw_spin_lock(&ctx->lock);
1453 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
1454 if (context_equiv(ctx, next_ctx)) {
1456 * XXX do we need a memory barrier of sorts
1457 * wrt to rcu_dereference() of perf_event_ctxp
1459 task->perf_event_ctxp[ctxn] = next_ctx;
1460 next->perf_event_ctxp[ctxn] = ctx;
1462 next_ctx->task = task;
1465 perf_event_sync_stat(ctx, next_ctx);
1467 raw_spin_unlock(&next_ctx->lock);
1468 raw_spin_unlock(&ctx->lock);
1473 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
1474 cpuctx->task_ctx = NULL;
1478 #define for_each_task_context_nr(ctxn) \
1479 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
1482 * Called from scheduler to remove the events of the current task,
1483 * with interrupts disabled.
1485 * We stop each event and update the event value in event->count.
1487 * This does not protect us against NMI, but disable()
1488 * sets the disabled bit in the control field of event _before_
1489 * accessing the event control register. If a NMI hits, then it will
1490 * not restart the event.
1492 void __perf_event_task_sched_out(struct task_struct *task,
1493 struct task_struct *next)
1497 for_each_task_context_nr(ctxn)
1498 perf_event_context_sched_out(task, ctxn, next);
1501 static void task_ctx_sched_out(struct perf_event_context *ctx,
1502 enum event_type_t event_type)
1504 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1506 if (!cpuctx->task_ctx)
1509 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
1512 ctx_sched_out(ctx, cpuctx, event_type);
1513 cpuctx->task_ctx = NULL;
1517 * Called with IRQs disabled
1519 static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
1520 enum event_type_t event_type)
1522 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
1526 ctx_pinned_sched_in(struct perf_event_context *ctx,
1527 struct perf_cpu_context *cpuctx)
1529 struct perf_event *event;
1531 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
1532 if (event->state <= PERF_EVENT_STATE_OFF)
1534 if (!event_filter_match(event))
1537 if (group_can_go_on(event, cpuctx, 1))
1538 group_sched_in(event, cpuctx, ctx);
1541 * If this pinned group hasn't been scheduled,
1542 * put it in error state.
1544 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1545 update_group_times(event);
1546 event->state = PERF_EVENT_STATE_ERROR;
1552 ctx_flexible_sched_in(struct perf_event_context *ctx,
1553 struct perf_cpu_context *cpuctx)
1555 struct perf_event *event;
1558 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
1559 /* Ignore events in OFF or ERROR state */
1560 if (event->state <= PERF_EVENT_STATE_OFF)
1563 * Listen to the 'cpu' scheduling filter constraint
1566 if (!event_filter_match(event))
1569 if (group_can_go_on(event, cpuctx, can_add_hw)) {
1570 if (group_sched_in(event, cpuctx, ctx))
1577 ctx_sched_in(struct perf_event_context *ctx,
1578 struct perf_cpu_context *cpuctx,
1579 enum event_type_t event_type)
1581 raw_spin_lock(&ctx->lock);
1583 if (likely(!ctx->nr_events))
1586 ctx->timestamp = perf_clock();
1589 * First go through the list and put on any pinned groups
1590 * in order to give them the best chance of going on.
1592 if (event_type & EVENT_PINNED)
1593 ctx_pinned_sched_in(ctx, cpuctx);
1595 /* Then walk through the lower prio flexible groups */
1596 if (event_type & EVENT_FLEXIBLE)
1597 ctx_flexible_sched_in(ctx, cpuctx);
1600 raw_spin_unlock(&ctx->lock);
1603 static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
1604 enum event_type_t event_type)
1606 struct perf_event_context *ctx = &cpuctx->ctx;
1608 ctx_sched_in(ctx, cpuctx, event_type);
1611 static void task_ctx_sched_in(struct perf_event_context *ctx,
1612 enum event_type_t event_type)
1614 struct perf_cpu_context *cpuctx;
1616 cpuctx = __get_cpu_context(ctx);
1617 if (cpuctx->task_ctx == ctx)
1620 ctx_sched_in(ctx, cpuctx, event_type);
1621 cpuctx->task_ctx = ctx;
1624 static void perf_event_context_sched_in(struct perf_event_context *ctx)
1626 struct perf_cpu_context *cpuctx;
1628 cpuctx = __get_cpu_context(ctx);
1629 if (cpuctx->task_ctx == ctx)
1632 perf_pmu_disable(ctx->pmu);
1634 * We want to keep the following priority order:
1635 * cpu pinned (that don't need to move), task pinned,
1636 * cpu flexible, task flexible.
1638 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
1640 ctx_sched_in(ctx, cpuctx, EVENT_PINNED);
1641 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE);
1642 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE);
1644 cpuctx->task_ctx = ctx;
1647 * Since these rotations are per-cpu, we need to ensure the
1648 * cpu-context we got scheduled on is actually rotating.
1650 perf_pmu_rotate_start(ctx->pmu);
1651 perf_pmu_enable(ctx->pmu);
1655 * Called from scheduler to add the events of the current task
1656 * with interrupts disabled.
1658 * We restore the event value and then enable it.
1660 * This does not protect us against NMI, but enable()
1661 * sets the enabled bit in the control field of event _before_
1662 * accessing the event control register. If a NMI hits, then it will
1663 * keep the event running.
1665 void __perf_event_task_sched_in(struct task_struct *task)
1667 struct perf_event_context *ctx;
1670 for_each_task_context_nr(ctxn) {
1671 ctx = task->perf_event_ctxp[ctxn];
1675 perf_event_context_sched_in(ctx);
1679 static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
1681 u64 frequency = event->attr.sample_freq;
1682 u64 sec = NSEC_PER_SEC;
1683 u64 divisor, dividend;
1685 int count_fls, nsec_fls, frequency_fls, sec_fls;
1687 count_fls = fls64(count);
1688 nsec_fls = fls64(nsec);
1689 frequency_fls = fls64(frequency);
1693 * We got @count in @nsec, with a target of sample_freq HZ
1694 * the target period becomes:
1697 * period = -------------------
1698 * @nsec * sample_freq
1703 * Reduce accuracy by one bit such that @a and @b converge
1704 * to a similar magnitude.
1706 #define REDUCE_FLS(a, b) \
1708 if (a##_fls > b##_fls) { \
1718 * Reduce accuracy until either term fits in a u64, then proceed with
1719 * the other, so that finally we can do a u64/u64 division.
1721 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
1722 REDUCE_FLS(nsec, frequency);
1723 REDUCE_FLS(sec, count);
1726 if (count_fls + sec_fls > 64) {
1727 divisor = nsec * frequency;
1729 while (count_fls + sec_fls > 64) {
1730 REDUCE_FLS(count, sec);
1734 dividend = count * sec;
1736 dividend = count * sec;
1738 while (nsec_fls + frequency_fls > 64) {
1739 REDUCE_FLS(nsec, frequency);
1743 divisor = nsec * frequency;
1749 return div64_u64(dividend, divisor);
1752 static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count)
1754 struct hw_perf_event *hwc = &event->hw;
1755 s64 period, sample_period;
1758 period = perf_calculate_period(event, nsec, count);
1760 delta = (s64)(period - hwc->sample_period);
1761 delta = (delta + 7) / 8; /* low pass filter */
1763 sample_period = hwc->sample_period + delta;
1768 hwc->sample_period = sample_period;
1770 if (local64_read(&hwc->period_left) > 8*sample_period) {
1771 event->pmu->stop(event, PERF_EF_UPDATE);
1772 local64_set(&hwc->period_left, 0);
1773 event->pmu->start(event, PERF_EF_RELOAD);
1777 static void perf_ctx_adjust_freq(struct perf_event_context *ctx, u64 period)
1779 struct perf_event *event;
1780 struct hw_perf_event *hwc;
1781 u64 interrupts, now;
1784 raw_spin_lock(&ctx->lock);
1785 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
1786 if (event->state != PERF_EVENT_STATE_ACTIVE)
1789 if (!event_filter_match(event))
1794 interrupts = hwc->interrupts;
1795 hwc->interrupts = 0;
1798 * unthrottle events on the tick
1800 if (interrupts == MAX_INTERRUPTS) {
1801 perf_log_throttle(event, 1);
1802 event->pmu->start(event, 0);
1805 if (!event->attr.freq || !event->attr.sample_freq)
1808 event->pmu->read(event);
1809 now = local64_read(&event->count);
1810 delta = now - hwc->freq_count_stamp;
1811 hwc->freq_count_stamp = now;
1814 perf_adjust_period(event, period, delta);
1816 raw_spin_unlock(&ctx->lock);
1820 * Round-robin a context's events:
1822 static void rotate_ctx(struct perf_event_context *ctx)
1824 raw_spin_lock(&ctx->lock);
1827 * Rotate the first entry last of non-pinned groups. Rotation might be
1828 * disabled by the inheritance code.
1830 if (!ctx->rotate_disable)
1831 list_rotate_left(&ctx->flexible_groups);
1833 raw_spin_unlock(&ctx->lock);
1837 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
1838 * because they're strictly cpu affine and rotate_start is called with IRQs
1839 * disabled, while rotate_context is called from IRQ context.
1841 static void perf_rotate_context(struct perf_cpu_context *cpuctx)
1843 u64 interval = (u64)cpuctx->jiffies_interval * TICK_NSEC;
1844 struct perf_event_context *ctx = NULL;
1845 int rotate = 0, remove = 1;
1847 if (cpuctx->ctx.nr_events) {
1849 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
1853 ctx = cpuctx->task_ctx;
1854 if (ctx && ctx->nr_events) {
1856 if (ctx->nr_events != ctx->nr_active)
1860 perf_pmu_disable(cpuctx->ctx.pmu);
1861 perf_ctx_adjust_freq(&cpuctx->ctx, interval);
1863 perf_ctx_adjust_freq(ctx, interval);
1868 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
1870 task_ctx_sched_out(ctx, EVENT_FLEXIBLE);
1872 rotate_ctx(&cpuctx->ctx);
1876 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE);
1878 task_ctx_sched_in(ctx, EVENT_FLEXIBLE);
1882 list_del_init(&cpuctx->rotation_list);
1884 perf_pmu_enable(cpuctx->ctx.pmu);
1887 void perf_event_task_tick(void)
1889 struct list_head *head = &__get_cpu_var(rotation_list);
1890 struct perf_cpu_context *cpuctx, *tmp;
1892 WARN_ON(!irqs_disabled());
1894 list_for_each_entry_safe(cpuctx, tmp, head, rotation_list) {
1895 if (cpuctx->jiffies_interval == 1 ||
1896 !(jiffies % cpuctx->jiffies_interval))
1897 perf_rotate_context(cpuctx);
1901 static int event_enable_on_exec(struct perf_event *event,
1902 struct perf_event_context *ctx)
1904 if (!event->attr.enable_on_exec)
1907 event->attr.enable_on_exec = 0;
1908 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1911 __perf_event_mark_enabled(event, ctx);
1917 * Enable all of a task's events that have been marked enable-on-exec.
1918 * This expects task == current.
1920 static void perf_event_enable_on_exec(struct perf_event_context *ctx)
1922 struct perf_event *event;
1923 unsigned long flags;
1927 local_irq_save(flags);
1928 if (!ctx || !ctx->nr_events)
1931 task_ctx_sched_out(ctx, EVENT_ALL);
1933 raw_spin_lock(&ctx->lock);
1935 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
1936 ret = event_enable_on_exec(event, ctx);
1941 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
1942 ret = event_enable_on_exec(event, ctx);
1948 * Unclone this context if we enabled any event.
1953 raw_spin_unlock(&ctx->lock);
1955 perf_event_context_sched_in(ctx);
1957 local_irq_restore(flags);
1961 * Cross CPU call to read the hardware event
1963 static void __perf_event_read(void *info)
1965 struct perf_event *event = info;
1966 struct perf_event_context *ctx = event->ctx;
1967 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1970 * If this is a task context, we need to check whether it is
1971 * the current task context of this cpu. If not it has been
1972 * scheduled out before the smp call arrived. In that case
1973 * event->count would have been updated to a recent sample
1974 * when the event was scheduled out.
1976 if (ctx->task && cpuctx->task_ctx != ctx)
1979 raw_spin_lock(&ctx->lock);
1981 update_context_time(ctx);
1982 update_event_times(event);
1983 if (event->state == PERF_EVENT_STATE_ACTIVE)
1984 event->pmu->read(event);
1985 raw_spin_unlock(&ctx->lock);
1988 static inline u64 perf_event_count(struct perf_event *event)
1990 return local64_read(&event->count) + atomic64_read(&event->child_count);
1993 static u64 perf_event_read(struct perf_event *event)
1996 * If event is enabled and currently active on a CPU, update the
1997 * value in the event structure:
1999 if (event->state == PERF_EVENT_STATE_ACTIVE) {
2000 smp_call_function_single(event->oncpu,
2001 __perf_event_read, event, 1);
2002 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
2003 struct perf_event_context *ctx = event->ctx;
2004 unsigned long flags;
2006 raw_spin_lock_irqsave(&ctx->lock, flags);
2008 * may read while context is not active
2009 * (e.g., thread is blocked), in that case
2010 * we cannot update context time
2013 update_context_time(ctx);
2014 update_event_times(event);
2015 raw_spin_unlock_irqrestore(&ctx->lock, flags);
2018 return perf_event_count(event);
2025 struct callchain_cpus_entries {
2026 struct rcu_head rcu_head;
2027 struct perf_callchain_entry *cpu_entries[0];
2030 static DEFINE_PER_CPU(int, callchain_recursion[PERF_NR_CONTEXTS]);
2031 static atomic_t nr_callchain_events;
2032 static DEFINE_MUTEX(callchain_mutex);
2033 struct callchain_cpus_entries *callchain_cpus_entries;
2036 __weak void perf_callchain_kernel(struct perf_callchain_entry *entry,
2037 struct pt_regs *regs)
2041 __weak void perf_callchain_user(struct perf_callchain_entry *entry,
2042 struct pt_regs *regs)
2046 static void release_callchain_buffers_rcu(struct rcu_head *head)
2048 struct callchain_cpus_entries *entries;
2051 entries = container_of(head, struct callchain_cpus_entries, rcu_head);
2053 for_each_possible_cpu(cpu)
2054 kfree(entries->cpu_entries[cpu]);
2059 static void release_callchain_buffers(void)
2061 struct callchain_cpus_entries *entries;
2063 entries = callchain_cpus_entries;
2064 rcu_assign_pointer(callchain_cpus_entries, NULL);
2065 call_rcu(&entries->rcu_head, release_callchain_buffers_rcu);
2068 static int alloc_callchain_buffers(void)
2072 struct callchain_cpus_entries *entries;
2075 * We can't use the percpu allocation API for data that can be
2076 * accessed from NMI. Use a temporary manual per cpu allocation
2077 * until that gets sorted out.
2079 size = offsetof(struct callchain_cpus_entries, cpu_entries[nr_cpu_ids]);
2081 entries = kzalloc(size, GFP_KERNEL);
2085 size = sizeof(struct perf_callchain_entry) * PERF_NR_CONTEXTS;
2087 for_each_possible_cpu(cpu) {
2088 entries->cpu_entries[cpu] = kmalloc_node(size, GFP_KERNEL,
2090 if (!entries->cpu_entries[cpu])
2094 rcu_assign_pointer(callchain_cpus_entries, entries);
2099 for_each_possible_cpu(cpu)
2100 kfree(entries->cpu_entries[cpu]);
2106 static int get_callchain_buffers(void)
2111 mutex_lock(&callchain_mutex);
2113 count = atomic_inc_return(&nr_callchain_events);
2114 if (WARN_ON_ONCE(count < 1)) {
2120 /* If the allocation failed, give up */
2121 if (!callchain_cpus_entries)
2126 err = alloc_callchain_buffers();
2128 release_callchain_buffers();
2130 mutex_unlock(&callchain_mutex);
2135 static void put_callchain_buffers(void)
2137 if (atomic_dec_and_mutex_lock(&nr_callchain_events, &callchain_mutex)) {
2138 release_callchain_buffers();
2139 mutex_unlock(&callchain_mutex);
2143 static int get_recursion_context(int *recursion)
2151 else if (in_softirq())
2156 if (recursion[rctx])
2165 static inline void put_recursion_context(int *recursion, int rctx)
2171 static struct perf_callchain_entry *get_callchain_entry(int *rctx)
2174 struct callchain_cpus_entries *entries;
2176 *rctx = get_recursion_context(__get_cpu_var(callchain_recursion));
2180 entries = rcu_dereference(callchain_cpus_entries);
2184 cpu = smp_processor_id();
2186 return &entries->cpu_entries[cpu][*rctx];
2190 put_callchain_entry(int rctx)
2192 put_recursion_context(__get_cpu_var(callchain_recursion), rctx);
2195 static struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
2198 struct perf_callchain_entry *entry;
2201 entry = get_callchain_entry(&rctx);
2210 if (!user_mode(regs)) {
2211 perf_callchain_store(entry, PERF_CONTEXT_KERNEL);
2212 perf_callchain_kernel(entry, regs);
2214 regs = task_pt_regs(current);
2220 perf_callchain_store(entry, PERF_CONTEXT_USER);
2221 perf_callchain_user(entry, regs);
2225 put_callchain_entry(rctx);
2231 * Initialize the perf_event context in a task_struct:
2233 static void __perf_event_init_context(struct perf_event_context *ctx)
2235 raw_spin_lock_init(&ctx->lock);
2236 mutex_init(&ctx->mutex);
2237 INIT_LIST_HEAD(&ctx->pinned_groups);
2238 INIT_LIST_HEAD(&ctx->flexible_groups);
2239 INIT_LIST_HEAD(&ctx->event_list);
2240 atomic_set(&ctx->refcount, 1);
2243 static struct perf_event_context *
2244 alloc_perf_context(struct pmu *pmu, struct task_struct *task)
2246 struct perf_event_context *ctx;
2248 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
2252 __perf_event_init_context(ctx);
2255 get_task_struct(task);
2262 static struct task_struct *
2263 find_lively_task_by_vpid(pid_t vpid)
2265 struct task_struct *task;
2272 task = find_task_by_vpid(vpid);
2274 get_task_struct(task);
2278 return ERR_PTR(-ESRCH);
2280 /* Reuse ptrace permission checks for now. */
2282 if (!ptrace_may_access(task, PTRACE_MODE_READ))
2287 put_task_struct(task);
2288 return ERR_PTR(err);
2293 * Returns a matching context with refcount and pincount.
2295 static struct perf_event_context *
2296 find_get_context(struct pmu *pmu, struct task_struct *task, int cpu)
2298 struct perf_event_context *ctx;
2299 struct perf_cpu_context *cpuctx;
2300 unsigned long flags;
2304 /* Must be root to operate on a CPU event: */
2305 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
2306 return ERR_PTR(-EACCES);
2309 * We could be clever and allow to attach a event to an
2310 * offline CPU and activate it when the CPU comes up, but
2313 if (!cpu_online(cpu))
2314 return ERR_PTR(-ENODEV);
2316 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
2325 ctxn = pmu->task_ctx_nr;
2330 ctx = perf_lock_task_context(task, ctxn, &flags);
2334 raw_spin_unlock_irqrestore(&ctx->lock, flags);
2338 ctx = alloc_perf_context(pmu, task);
2346 mutex_lock(&task->perf_event_mutex);
2348 * If it has already passed perf_event_exit_task().
2349 * we must see PF_EXITING, it takes this mutex too.
2351 if (task->flags & PF_EXITING)
2353 else if (task->perf_event_ctxp[ctxn])
2357 rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
2359 mutex_unlock(&task->perf_event_mutex);
2361 if (unlikely(err)) {
2362 put_task_struct(task);
2374 return ERR_PTR(err);
2377 static void perf_event_free_filter(struct perf_event *event);
2379 static void free_event_rcu(struct rcu_head *head)
2381 struct perf_event *event;
2383 event = container_of(head, struct perf_event, rcu_head);
2385 put_pid_ns(event->ns);
2386 perf_event_free_filter(event);
2390 static void perf_buffer_put(struct perf_buffer *buffer);
2392 static void free_event(struct perf_event *event)
2394 irq_work_sync(&event->pending);
2396 if (!event->parent) {
2397 if (event->attach_state & PERF_ATTACH_TASK)
2398 jump_label_dec(&perf_task_events);
2399 if (event->attr.mmap || event->attr.mmap_data)
2400 atomic_dec(&nr_mmap_events);
2401 if (event->attr.comm)
2402 atomic_dec(&nr_comm_events);
2403 if (event->attr.task)
2404 atomic_dec(&nr_task_events);
2405 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
2406 put_callchain_buffers();
2409 if (event->buffer) {
2410 perf_buffer_put(event->buffer);
2411 event->buffer = NULL;
2415 event->destroy(event);
2418 put_ctx(event->ctx);
2420 call_rcu(&event->rcu_head, free_event_rcu);
2423 int perf_event_release_kernel(struct perf_event *event)
2425 struct perf_event_context *ctx = event->ctx;
2428 * Remove from the PMU, can't get re-enabled since we got
2429 * here because the last ref went.
2431 perf_event_disable(event);
2433 WARN_ON_ONCE(ctx->parent_ctx);
2435 * There are two ways this annotation is useful:
2437 * 1) there is a lock recursion from perf_event_exit_task
2438 * see the comment there.
2440 * 2) there is a lock-inversion with mmap_sem through
2441 * perf_event_read_group(), which takes faults while
2442 * holding ctx->mutex, however this is called after
2443 * the last filedesc died, so there is no possibility
2444 * to trigger the AB-BA case.
2446 mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
2447 raw_spin_lock_irq(&ctx->lock);
2448 perf_group_detach(event);
2449 list_del_event(event, ctx);
2450 raw_spin_unlock_irq(&ctx->lock);
2451 mutex_unlock(&ctx->mutex);
2457 EXPORT_SYMBOL_GPL(perf_event_release_kernel);
2460 * Called when the last reference to the file is gone.
2462 static int perf_release(struct inode *inode, struct file *file)
2464 struct perf_event *event = file->private_data;
2465 struct task_struct *owner;
2467 file->private_data = NULL;
2470 owner = ACCESS_ONCE(event->owner);
2472 * Matches the smp_wmb() in perf_event_exit_task(). If we observe
2473 * !owner it means the list deletion is complete and we can indeed
2474 * free this event, otherwise we need to serialize on
2475 * owner->perf_event_mutex.
2477 smp_read_barrier_depends();
2480 * Since delayed_put_task_struct() also drops the last
2481 * task reference we can safely take a new reference
2482 * while holding the rcu_read_lock().
2484 get_task_struct(owner);
2489 mutex_lock(&owner->perf_event_mutex);
2491 * We have to re-check the event->owner field, if it is cleared
2492 * we raced with perf_event_exit_task(), acquiring the mutex
2493 * ensured they're done, and we can proceed with freeing the
2497 list_del_init(&event->owner_entry);
2498 mutex_unlock(&owner->perf_event_mutex);
2499 put_task_struct(owner);
2502 return perf_event_release_kernel(event);
2505 u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
2507 struct perf_event *child;
2513 mutex_lock(&event->child_mutex);
2514 total += perf_event_read(event);
2515 *enabled += event->total_time_enabled +
2516 atomic64_read(&event->child_total_time_enabled);
2517 *running += event->total_time_running +
2518 atomic64_read(&event->child_total_time_running);
2520 list_for_each_entry(child, &event->child_list, child_list) {
2521 total += perf_event_read(child);
2522 *enabled += child->total_time_enabled;
2523 *running += child->total_time_running;
2525 mutex_unlock(&event->child_mutex);
2529 EXPORT_SYMBOL_GPL(perf_event_read_value);
2531 static int perf_event_read_group(struct perf_event *event,
2532 u64 read_format, char __user *buf)
2534 struct perf_event *leader = event->group_leader, *sub;
2535 int n = 0, size = 0, ret = -EFAULT;
2536 struct perf_event_context *ctx = leader->ctx;
2538 u64 count, enabled, running;
2540 mutex_lock(&ctx->mutex);
2541 count = perf_event_read_value(leader, &enabled, &running);
2543 values[n++] = 1 + leader->nr_siblings;
2544 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2545 values[n++] = enabled;
2546 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2547 values[n++] = running;
2548 values[n++] = count;
2549 if (read_format & PERF_FORMAT_ID)
2550 values[n++] = primary_event_id(leader);
2552 size = n * sizeof(u64);
2554 if (copy_to_user(buf, values, size))
2559 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
2562 values[n++] = perf_event_read_value(sub, &enabled, &running);
2563 if (read_format & PERF_FORMAT_ID)
2564 values[n++] = primary_event_id(sub);
2566 size = n * sizeof(u64);
2568 if (copy_to_user(buf + ret, values, size)) {
2576 mutex_unlock(&ctx->mutex);
2581 static int perf_event_read_one(struct perf_event *event,
2582 u64 read_format, char __user *buf)
2584 u64 enabled, running;
2588 values[n++] = perf_event_read_value(event, &enabled, &running);
2589 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2590 values[n++] = enabled;
2591 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2592 values[n++] = running;
2593 if (read_format & PERF_FORMAT_ID)
2594 values[n++] = primary_event_id(event);
2596 if (copy_to_user(buf, values, n * sizeof(u64)))
2599 return n * sizeof(u64);
2603 * Read the performance event - simple non blocking version for now
2606 perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
2608 u64 read_format = event->attr.read_format;
2612 * Return end-of-file for a read on a event that is in
2613 * error state (i.e. because it was pinned but it couldn't be
2614 * scheduled on to the CPU at some point).
2616 if (event->state == PERF_EVENT_STATE_ERROR)
2619 if (count < event->read_size)
2622 WARN_ON_ONCE(event->ctx->parent_ctx);
2623 if (read_format & PERF_FORMAT_GROUP)
2624 ret = perf_event_read_group(event, read_format, buf);
2626 ret = perf_event_read_one(event, read_format, buf);
2632 perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
2634 struct perf_event *event = file->private_data;
2636 return perf_read_hw(event, buf, count);
2639 static unsigned int perf_poll(struct file *file, poll_table *wait)
2641 struct perf_event *event = file->private_data;
2642 struct perf_buffer *buffer;
2643 unsigned int events = POLL_HUP;
2646 buffer = rcu_dereference(event->buffer);
2648 events = atomic_xchg(&buffer->poll, 0);
2651 poll_wait(file, &event->waitq, wait);
2656 static void perf_event_reset(struct perf_event *event)
2658 (void)perf_event_read(event);
2659 local64_set(&event->count, 0);
2660 perf_event_update_userpage(event);
2664 * Holding the top-level event's child_mutex means that any
2665 * descendant process that has inherited this event will block
2666 * in sync_child_event if it goes to exit, thus satisfying the
2667 * task existence requirements of perf_event_enable/disable.
2669 static void perf_event_for_each_child(struct perf_event *event,
2670 void (*func)(struct perf_event *))
2672 struct perf_event *child;
2674 WARN_ON_ONCE(event->ctx->parent_ctx);
2675 mutex_lock(&event->child_mutex);
2677 list_for_each_entry(child, &event->child_list, child_list)
2679 mutex_unlock(&event->child_mutex);
2682 static void perf_event_for_each(struct perf_event *event,
2683 void (*func)(struct perf_event *))
2685 struct perf_event_context *ctx = event->ctx;
2686 struct perf_event *sibling;
2688 WARN_ON_ONCE(ctx->parent_ctx);
2689 mutex_lock(&ctx->mutex);
2690 event = event->group_leader;
2692 perf_event_for_each_child(event, func);
2694 list_for_each_entry(sibling, &event->sibling_list, group_entry)
2695 perf_event_for_each_child(event, func);
2696 mutex_unlock(&ctx->mutex);
2699 static int perf_event_period(struct perf_event *event, u64 __user *arg)
2701 struct perf_event_context *ctx = event->ctx;
2705 if (!is_sampling_event(event))
2708 if (copy_from_user(&value, arg, sizeof(value)))
2714 raw_spin_lock_irq(&ctx->lock);
2715 if (event->attr.freq) {
2716 if (value > sysctl_perf_event_sample_rate) {
2721 event->attr.sample_freq = value;
2723 event->attr.sample_period = value;
2724 event->hw.sample_period = value;
2727 raw_spin_unlock_irq(&ctx->lock);
2732 static const struct file_operations perf_fops;
2734 static struct perf_event *perf_fget_light(int fd, int *fput_needed)
2738 file = fget_light(fd, fput_needed);
2740 return ERR_PTR(-EBADF);
2742 if (file->f_op != &perf_fops) {
2743 fput_light(file, *fput_needed);
2745 return ERR_PTR(-EBADF);
2748 return file->private_data;
2751 static int perf_event_set_output(struct perf_event *event,
2752 struct perf_event *output_event);
2753 static int perf_event_set_filter(struct perf_event *event, void __user *arg);
2755 static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2757 struct perf_event *event = file->private_data;
2758 void (*func)(struct perf_event *);
2762 case PERF_EVENT_IOC_ENABLE:
2763 func = perf_event_enable;
2765 case PERF_EVENT_IOC_DISABLE:
2766 func = perf_event_disable;
2768 case PERF_EVENT_IOC_RESET:
2769 func = perf_event_reset;
2772 case PERF_EVENT_IOC_REFRESH:
2773 return perf_event_refresh(event, arg);
2775 case PERF_EVENT_IOC_PERIOD:
2776 return perf_event_period(event, (u64 __user *)arg);
2778 case PERF_EVENT_IOC_SET_OUTPUT:
2780 struct perf_event *output_event = NULL;
2781 int fput_needed = 0;
2785 output_event = perf_fget_light(arg, &fput_needed);
2786 if (IS_ERR(output_event))
2787 return PTR_ERR(output_event);
2790 ret = perf_event_set_output(event, output_event);
2792 fput_light(output_event->filp, fput_needed);
2797 case PERF_EVENT_IOC_SET_FILTER:
2798 return perf_event_set_filter(event, (void __user *)arg);
2804 if (flags & PERF_IOC_FLAG_GROUP)
2805 perf_event_for_each(event, func);
2807 perf_event_for_each_child(event, func);
2812 int perf_event_task_enable(void)
2814 struct perf_event *event;
2816 mutex_lock(¤t->perf_event_mutex);
2817 list_for_each_entry(event, ¤t->perf_event_list, owner_entry)
2818 perf_event_for_each_child(event, perf_event_enable);
2819 mutex_unlock(¤t->perf_event_mutex);
2824 int perf_event_task_disable(void)
2826 struct perf_event *event;
2828 mutex_lock(¤t->perf_event_mutex);
2829 list_for_each_entry(event, ¤t->perf_event_list, owner_entry)
2830 perf_event_for_each_child(event, perf_event_disable);
2831 mutex_unlock(¤t->perf_event_mutex);
2836 #ifndef PERF_EVENT_INDEX_OFFSET
2837 # define PERF_EVENT_INDEX_OFFSET 0
2840 static int perf_event_index(struct perf_event *event)
2842 if (event->hw.state & PERF_HES_STOPPED)
2845 if (event->state != PERF_EVENT_STATE_ACTIVE)
2848 return event->hw.idx + 1 - PERF_EVENT_INDEX_OFFSET;
2852 * Callers need to ensure there can be no nesting of this function, otherwise
2853 * the seqlock logic goes bad. We can not serialize this because the arch
2854 * code calls this from NMI context.
2856 void perf_event_update_userpage(struct perf_event *event)
2858 struct perf_event_mmap_page *userpg;
2859 struct perf_buffer *buffer;
2862 buffer = rcu_dereference(event->buffer);
2866 userpg = buffer->user_page;
2869 * Disable preemption so as to not let the corresponding user-space
2870 * spin too long if we get preempted.
2875 userpg->index = perf_event_index(event);
2876 userpg->offset = perf_event_count(event);
2877 if (event->state == PERF_EVENT_STATE_ACTIVE)
2878 userpg->offset -= local64_read(&event->hw.prev_count);
2880 userpg->time_enabled = event->total_time_enabled +
2881 atomic64_read(&event->child_total_time_enabled);
2883 userpg->time_running = event->total_time_running +
2884 atomic64_read(&event->child_total_time_running);
2893 static unsigned long perf_data_size(struct perf_buffer *buffer);
2896 perf_buffer_init(struct perf_buffer *buffer, long watermark, int flags)
2898 long max_size = perf_data_size(buffer);
2901 buffer->watermark = min(max_size, watermark);
2903 if (!buffer->watermark)
2904 buffer->watermark = max_size / 2;
2906 if (flags & PERF_BUFFER_WRITABLE)
2907 buffer->writable = 1;
2909 atomic_set(&buffer->refcount, 1);
2912 #ifndef CONFIG_PERF_USE_VMALLOC
2915 * Back perf_mmap() with regular GFP_KERNEL-0 pages.
2918 static struct page *
2919 perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
2921 if (pgoff > buffer->nr_pages)
2925 return virt_to_page(buffer->user_page);
2927 return virt_to_page(buffer->data_pages[pgoff - 1]);
2930 static void *perf_mmap_alloc_page(int cpu)
2935 node = (cpu == -1) ? cpu : cpu_to_node(cpu);
2936 page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
2940 return page_address(page);
2943 static struct perf_buffer *
2944 perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
2946 struct perf_buffer *buffer;
2950 size = sizeof(struct perf_buffer);
2951 size += nr_pages * sizeof(void *);
2953 buffer = kzalloc(size, GFP_KERNEL);
2957 buffer->user_page = perf_mmap_alloc_page(cpu);
2958 if (!buffer->user_page)
2959 goto fail_user_page;
2961 for (i = 0; i < nr_pages; i++) {
2962 buffer->data_pages[i] = perf_mmap_alloc_page(cpu);
2963 if (!buffer->data_pages[i])
2964 goto fail_data_pages;
2967 buffer->nr_pages = nr_pages;
2969 perf_buffer_init(buffer, watermark, flags);
2974 for (i--; i >= 0; i--)
2975 free_page((unsigned long)buffer->data_pages[i]);
2977 free_page((unsigned long)buffer->user_page);
2986 static void perf_mmap_free_page(unsigned long addr)
2988 struct page *page = virt_to_page((void *)addr);
2990 page->mapping = NULL;
2994 static void perf_buffer_free(struct perf_buffer *buffer)
2998 perf_mmap_free_page((unsigned long)buffer->user_page);
2999 for (i = 0; i < buffer->nr_pages; i++)
3000 perf_mmap_free_page((unsigned long)buffer->data_pages[i]);
3004 static inline int page_order(struct perf_buffer *buffer)
3012 * Back perf_mmap() with vmalloc memory.
3014 * Required for architectures that have d-cache aliasing issues.
3017 static inline int page_order(struct perf_buffer *buffer)
3019 return buffer->page_order;
3022 static struct page *
3023 perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
3025 if (pgoff > (1UL << page_order(buffer)))
3028 return vmalloc_to_page((void *)buffer->user_page + pgoff * PAGE_SIZE);
3031 static void perf_mmap_unmark_page(void *addr)
3033 struct page *page = vmalloc_to_page(addr);
3035 page->mapping = NULL;
3038 static void perf_buffer_free_work(struct work_struct *work)
3040 struct perf_buffer *buffer;
3044 buffer = container_of(work, struct perf_buffer, work);
3045 nr = 1 << page_order(buffer);
3047 base = buffer->user_page;
3048 for (i = 0; i < nr + 1; i++)
3049 perf_mmap_unmark_page(base + (i * PAGE_SIZE));
3055 static void perf_buffer_free(struct perf_buffer *buffer)
3057 schedule_work(&buffer->work);
3060 static struct perf_buffer *
3061 perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
3063 struct perf_buffer *buffer;
3067 size = sizeof(struct perf_buffer);
3068 size += sizeof(void *);
3070 buffer = kzalloc(size, GFP_KERNEL);
3074 INIT_WORK(&buffer->work, perf_buffer_free_work);
3076 all_buf = vmalloc_user((nr_pages + 1) * PAGE_SIZE);
3080 buffer->user_page = all_buf;
3081 buffer->data_pages[0] = all_buf + PAGE_SIZE;
3082 buffer->page_order = ilog2(nr_pages);
3083 buffer->nr_pages = 1;
3085 perf_buffer_init(buffer, watermark, flags);
3098 static unsigned long perf_data_size(struct perf_buffer *buffer)
3100 return buffer->nr_pages << (PAGE_SHIFT + page_order(buffer));
3103 static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
3105 struct perf_event *event = vma->vm_file->private_data;
3106 struct perf_buffer *buffer;
3107 int ret = VM_FAULT_SIGBUS;
3109 if (vmf->flags & FAULT_FLAG_MKWRITE) {
3110 if (vmf->pgoff == 0)
3116 buffer = rcu_dereference(event->buffer);
3120 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
3123 vmf->page = perf_mmap_to_page(buffer, vmf->pgoff);
3127 get_page(vmf->page);
3128 vmf->page->mapping = vma->vm_file->f_mapping;
3129 vmf->page->index = vmf->pgoff;
3138 static void perf_buffer_free_rcu(struct rcu_head *rcu_head)
3140 struct perf_buffer *buffer;
3142 buffer = container_of(rcu_head, struct perf_buffer, rcu_head);
3143 perf_buffer_free(buffer);
3146 static struct perf_buffer *perf_buffer_get(struct perf_event *event)
3148 struct perf_buffer *buffer;
3151 buffer = rcu_dereference(event->buffer);
3153 if (!atomic_inc_not_zero(&buffer->refcount))
3161 static void perf_buffer_put(struct perf_buffer *buffer)
3163 if (!atomic_dec_and_test(&buffer->refcount))
3166 call_rcu(&buffer->rcu_head, perf_buffer_free_rcu);
3169 static void perf_mmap_open(struct vm_area_struct *vma)
3171 struct perf_event *event = vma->vm_file->private_data;
3173 atomic_inc(&event->mmap_count);
3176 static void perf_mmap_close(struct vm_area_struct *vma)
3178 struct perf_event *event = vma->vm_file->private_data;
3180 if (atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) {
3181 unsigned long size = perf_data_size(event->buffer);
3182 struct user_struct *user = event->mmap_user;
3183 struct perf_buffer *buffer = event->buffer;
3185 atomic_long_sub((size >> PAGE_SHIFT) + 1, &user->locked_vm);
3186 vma->vm_mm->locked_vm -= event->mmap_locked;
3187 rcu_assign_pointer(event->buffer, NULL);
3188 mutex_unlock(&event->mmap_mutex);
3190 perf_buffer_put(buffer);
3195 static const struct vm_operations_struct perf_mmap_vmops = {
3196 .open = perf_mmap_open,
3197 .close = perf_mmap_close,
3198 .fault = perf_mmap_fault,
3199 .page_mkwrite = perf_mmap_fault,
3202 static int perf_mmap(struct file *file, struct vm_area_struct *vma)
3204 struct perf_event *event = file->private_data;
3205 unsigned long user_locked, user_lock_limit;
3206 struct user_struct *user = current_user();
3207 unsigned long locked, lock_limit;
3208 struct perf_buffer *buffer;
3209 unsigned long vma_size;
3210 unsigned long nr_pages;
3211 long user_extra, extra;
3212 int ret = 0, flags = 0;
3215 * Don't allow mmap() of inherited per-task counters. This would
3216 * create a performance issue due to all children writing to the
3219 if (event->cpu == -1 && event->attr.inherit)
3222 if (!(vma->vm_flags & VM_SHARED))
3225 vma_size = vma->vm_end - vma->vm_start;
3226 nr_pages = (vma_size / PAGE_SIZE) - 1;
3229 * If we have buffer pages ensure they're a power-of-two number, so we
3230 * can do bitmasks instead of modulo.
3232 if (nr_pages != 0 && !is_power_of_2(nr_pages))
3235 if (vma_size != PAGE_SIZE * (1 + nr_pages))
3238 if (vma->vm_pgoff != 0)
3241 WARN_ON_ONCE(event->ctx->parent_ctx);
3242 mutex_lock(&event->mmap_mutex);
3243 if (event->buffer) {
3244 if (event->buffer->nr_pages == nr_pages)
3245 atomic_inc(&event->buffer->refcount);
3251 user_extra = nr_pages + 1;
3252 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
3255 * Increase the limit linearly with more CPUs:
3257 user_lock_limit *= num_online_cpus();
3259 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
3262 if (user_locked > user_lock_limit)
3263 extra = user_locked - user_lock_limit;
3265 lock_limit = rlimit(RLIMIT_MEMLOCK);
3266 lock_limit >>= PAGE_SHIFT;
3267 locked = vma->vm_mm->locked_vm + extra;
3269 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
3270 !capable(CAP_IPC_LOCK)) {
3275 WARN_ON(event->buffer);
3277 if (vma->vm_flags & VM_WRITE)
3278 flags |= PERF_BUFFER_WRITABLE;
3280 buffer = perf_buffer_alloc(nr_pages, event->attr.wakeup_watermark,
3286 rcu_assign_pointer(event->buffer, buffer);
3288 atomic_long_add(user_extra, &user->locked_vm);
3289 event->mmap_locked = extra;
3290 event->mmap_user = get_current_user();
3291 vma->vm_mm->locked_vm += event->mmap_locked;
3295 atomic_inc(&event->mmap_count);
3296 mutex_unlock(&event->mmap_mutex);
3298 vma->vm_flags |= VM_RESERVED;
3299 vma->vm_ops = &perf_mmap_vmops;
3304 static int perf_fasync(int fd, struct file *filp, int on)
3306 struct inode *inode = filp->f_path.dentry->d_inode;
3307 struct perf_event *event = filp->private_data;
3310 mutex_lock(&inode->i_mutex);
3311 retval = fasync_helper(fd, filp, on, &event->fasync);
3312 mutex_unlock(&inode->i_mutex);
3320 static const struct file_operations perf_fops = {
3321 .llseek = no_llseek,
3322 .release = perf_release,
3325 .unlocked_ioctl = perf_ioctl,
3326 .compat_ioctl = perf_ioctl,
3328 .fasync = perf_fasync,
3334 * If there's data, ensure we set the poll() state and publish everything
3335 * to user-space before waking everybody up.
3338 void perf_event_wakeup(struct perf_event *event)
3340 wake_up_all(&event->waitq);
3342 if (event->pending_kill) {
3343 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
3344 event->pending_kill = 0;
3348 static void perf_pending_event(struct irq_work *entry)
3350 struct perf_event *event = container_of(entry,
3351 struct perf_event, pending);
3353 if (event->pending_disable) {
3354 event->pending_disable = 0;
3355 __perf_event_disable(event);
3358 if (event->pending_wakeup) {
3359 event->pending_wakeup = 0;
3360 perf_event_wakeup(event);
3365 * We assume there is only KVM supporting the callbacks.
3366 * Later on, we might change it to a list if there is
3367 * another virtualization implementation supporting the callbacks.
3369 struct perf_guest_info_callbacks *perf_guest_cbs;
3371 int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3373 perf_guest_cbs = cbs;
3376 EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
3378 int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3380 perf_guest_cbs = NULL;
3383 EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
3388 static bool perf_output_space(struct perf_buffer *buffer, unsigned long tail,
3389 unsigned long offset, unsigned long head)
3393 if (!buffer->writable)
3396 mask = perf_data_size(buffer) - 1;
3398 offset = (offset - tail) & mask;
3399 head = (head - tail) & mask;
3401 if ((int)(head - offset) < 0)
3407 static void perf_output_wakeup(struct perf_output_handle *handle)
3409 atomic_set(&handle->buffer->poll, POLL_IN);
3412 handle->event->pending_wakeup = 1;
3413 irq_work_queue(&handle->event->pending);
3415 perf_event_wakeup(handle->event);
3419 * We need to ensure a later event_id doesn't publish a head when a former
3420 * event isn't done writing. However since we need to deal with NMIs we
3421 * cannot fully serialize things.
3423 * We only publish the head (and generate a wakeup) when the outer-most
3426 static void perf_output_get_handle(struct perf_output_handle *handle)
3428 struct perf_buffer *buffer = handle->buffer;
3431 local_inc(&buffer->nest);
3432 handle->wakeup = local_read(&buffer->wakeup);
3435 static void perf_output_put_handle(struct perf_output_handle *handle)
3437 struct perf_buffer *buffer = handle->buffer;
3441 head = local_read(&buffer->head);
3444 * IRQ/NMI can happen here, which means we can miss a head update.
3447 if (!local_dec_and_test(&buffer->nest))
3451 * Publish the known good head. Rely on the full barrier implied
3452 * by atomic_dec_and_test() order the buffer->head read and this
3455 buffer->user_page->data_head = head;
3458 * Now check if we missed an update, rely on the (compiler)
3459 * barrier in atomic_dec_and_test() to re-read buffer->head.
3461 if (unlikely(head != local_read(&buffer->head))) {
3462 local_inc(&buffer->nest);
3466 if (handle->wakeup != local_read(&buffer->wakeup))
3467 perf_output_wakeup(handle);
3473 __always_inline void perf_output_copy(struct perf_output_handle *handle,
3474 const void *buf, unsigned int len)
3477 unsigned long size = min_t(unsigned long, handle->size, len);
3479 memcpy(handle->addr, buf, size);
3482 handle->addr += size;
3484 handle->size -= size;
3485 if (!handle->size) {
3486 struct perf_buffer *buffer = handle->buffer;
3489 handle->page &= buffer->nr_pages - 1;
3490 handle->addr = buffer->data_pages[handle->page];
3491 handle->size = PAGE_SIZE << page_order(buffer);
3496 static void __perf_event_header__init_id(struct perf_event_header *header,
3497 struct perf_sample_data *data,
3498 struct perf_event *event)
3500 u64 sample_type = event->attr.sample_type;
3502 data->type = sample_type;
3503 header->size += event->id_header_size;
3505 if (sample_type & PERF_SAMPLE_TID) {
3506 /* namespace issues */
3507 data->tid_entry.pid = perf_event_pid(event, current);
3508 data->tid_entry.tid = perf_event_tid(event, current);
3511 if (sample_type & PERF_SAMPLE_TIME)
3512 data->time = perf_clock();
3514 if (sample_type & PERF_SAMPLE_ID)
3515 data->id = primary_event_id(event);
3517 if (sample_type & PERF_SAMPLE_STREAM_ID)
3518 data->stream_id = event->id;
3520 if (sample_type & PERF_SAMPLE_CPU) {
3521 data->cpu_entry.cpu = raw_smp_processor_id();
3522 data->cpu_entry.reserved = 0;
3526 static void perf_event_header__init_id(struct perf_event_header *header,
3527 struct perf_sample_data *data,
3528 struct perf_event *event)
3530 if (event->attr.sample_id_all)
3531 __perf_event_header__init_id(header, data, event);
3534 static void __perf_event__output_id_sample(struct perf_output_handle *handle,
3535 struct perf_sample_data *data)
3537 u64 sample_type = data->type;
3539 if (sample_type & PERF_SAMPLE_TID)
3540 perf_output_put(handle, data->tid_entry);
3542 if (sample_type & PERF_SAMPLE_TIME)
3543 perf_output_put(handle, data->time);
3545 if (sample_type & PERF_SAMPLE_ID)
3546 perf_output_put(handle, data->id);
3548 if (sample_type & PERF_SAMPLE_STREAM_ID)
3549 perf_output_put(handle, data->stream_id);
3551 if (sample_type & PERF_SAMPLE_CPU)
3552 perf_output_put(handle, data->cpu_entry);
3555 static void perf_event__output_id_sample(struct perf_event *event,
3556 struct perf_output_handle *handle,
3557 struct perf_sample_data *sample)
3559 if (event->attr.sample_id_all)
3560 __perf_event__output_id_sample(handle, sample);
3563 int perf_output_begin(struct perf_output_handle *handle,
3564 struct perf_event *event, unsigned int size,
3565 int nmi, int sample)
3567 struct perf_buffer *buffer;
3568 unsigned long tail, offset, head;
3570 struct perf_sample_data sample_data;
3572 struct perf_event_header header;
3579 * For inherited events we send all the output towards the parent.
3582 event = event->parent;
3584 buffer = rcu_dereference(event->buffer);
3588 handle->buffer = buffer;
3589 handle->event = event;
3591 handle->sample = sample;
3593 if (!buffer->nr_pages)
3596 have_lost = local_read(&buffer->lost);
3598 lost_event.header.size = sizeof(lost_event);
3599 perf_event_header__init_id(&lost_event.header, &sample_data,
3601 size += lost_event.header.size;
3604 perf_output_get_handle(handle);
3608 * Userspace could choose to issue a mb() before updating the
3609 * tail pointer. So that all reads will be completed before the
3612 tail = ACCESS_ONCE(buffer->user_page->data_tail);
3614 offset = head = local_read(&buffer->head);
3616 if (unlikely(!perf_output_space(buffer, tail, offset, head)))
3618 } while (local_cmpxchg(&buffer->head, offset, head) != offset);
3620 if (head - local_read(&buffer->wakeup) > buffer->watermark)
3621 local_add(buffer->watermark, &buffer->wakeup);
3623 handle->page = offset >> (PAGE_SHIFT + page_order(buffer));
3624 handle->page &= buffer->nr_pages - 1;
3625 handle->size = offset & ((PAGE_SIZE << page_order(buffer)) - 1);
3626 handle->addr = buffer->data_pages[handle->page];
3627 handle->addr += handle->size;
3628 handle->size = (PAGE_SIZE << page_order(buffer)) - handle->size;
3631 lost_event.header.type = PERF_RECORD_LOST;
3632 lost_event.header.misc = 0;
3633 lost_event.id = event->id;
3634 lost_event.lost = local_xchg(&buffer->lost, 0);
3636 perf_output_put(handle, lost_event);
3637 perf_event__output_id_sample(event, handle, &sample_data);
3643 local_inc(&buffer->lost);
3644 perf_output_put_handle(handle);
3651 void perf_output_end(struct perf_output_handle *handle)
3653 struct perf_event *event = handle->event;
3654 struct perf_buffer *buffer = handle->buffer;
3656 int wakeup_events = event->attr.wakeup_events;
3658 if (handle->sample && wakeup_events) {
3659 int events = local_inc_return(&buffer->events);
3660 if (events >= wakeup_events) {
3661 local_sub(wakeup_events, &buffer->events);
3662 local_inc(&buffer->wakeup);
3666 perf_output_put_handle(handle);
3670 static void perf_output_read_one(struct perf_output_handle *handle,
3671 struct perf_event *event,
3672 u64 enabled, u64 running)
3674 u64 read_format = event->attr.read_format;
3678 values[n++] = perf_event_count(event);
3679 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
3680 values[n++] = enabled +
3681 atomic64_read(&event->child_total_time_enabled);
3683 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
3684 values[n++] = running +
3685 atomic64_read(&event->child_total_time_running);
3687 if (read_format & PERF_FORMAT_ID)
3688 values[n++] = primary_event_id(event);
3690 perf_output_copy(handle, values, n * sizeof(u64));
3694 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
3696 static void perf_output_read_group(struct perf_output_handle *handle,
3697 struct perf_event *event,
3698 u64 enabled, u64 running)
3700 struct perf_event *leader = event->group_leader, *sub;
3701 u64 read_format = event->attr.read_format;
3705 values[n++] = 1 + leader->nr_siblings;
3707 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3708 values[n++] = enabled;
3710 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3711 values[n++] = running;
3713 if (leader != event)
3714 leader->pmu->read(leader);
3716 values[n++] = perf_event_count(leader);
3717 if (read_format & PERF_FORMAT_ID)
3718 values[n++] = primary_event_id(leader);
3720 perf_output_copy(handle, values, n * sizeof(u64));
3722 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
3726 sub->pmu->read(sub);
3728 values[n++] = perf_event_count(sub);
3729 if (read_format & PERF_FORMAT_ID)
3730 values[n++] = primary_event_id(sub);
3732 perf_output_copy(handle, values, n * sizeof(u64));
3736 #define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
3737 PERF_FORMAT_TOTAL_TIME_RUNNING)
3739 static void perf_output_read(struct perf_output_handle *handle,
3740 struct perf_event *event)
3742 u64 enabled = 0, running = 0, now, ctx_time;
3743 u64 read_format = event->attr.read_format;
3746 * compute total_time_enabled, total_time_running
3747 * based on snapshot values taken when the event
3748 * was last scheduled in.
3750 * we cannot simply called update_context_time()
3751 * because of locking issue as we are called in
3754 if (read_format & PERF_FORMAT_TOTAL_TIMES) {
3756 ctx_time = event->shadow_ctx_time + now;
3757 enabled = ctx_time - event->tstamp_enabled;
3758 running = ctx_time - event->tstamp_running;
3761 if (event->attr.read_format & PERF_FORMAT_GROUP)
3762 perf_output_read_group(handle, event, enabled, running);
3764 perf_output_read_one(handle, event, enabled, running);
3767 void perf_output_sample(struct perf_output_handle *handle,
3768 struct perf_event_header *header,
3769 struct perf_sample_data *data,
3770 struct perf_event *event)
3772 u64 sample_type = data->type;
3774 perf_output_put(handle, *header);
3776 if (sample_type & PERF_SAMPLE_IP)
3777 perf_output_put(handle, data->ip);
3779 if (sample_type & PERF_SAMPLE_TID)
3780 perf_output_put(handle, data->tid_entry);
3782 if (sample_type & PERF_SAMPLE_TIME)
3783 perf_output_put(handle, data->time);
3785 if (sample_type & PERF_SAMPLE_ADDR)
3786 perf_output_put(handle, data->addr);
3788 if (sample_type & PERF_SAMPLE_ID)
3789 perf_output_put(handle, data->id);
3791 if (sample_type & PERF_SAMPLE_STREAM_ID)
3792 perf_output_put(handle, data->stream_id);
3794 if (sample_type & PERF_SAMPLE_CPU)
3795 perf_output_put(handle, data->cpu_entry);
3797 if (sample_type & PERF_SAMPLE_PERIOD)
3798 perf_output_put(handle, data->period);
3800 if (sample_type & PERF_SAMPLE_READ)
3801 perf_output_read(handle, event);
3803 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3804 if (data->callchain) {
3807 if (data->callchain)
3808 size += data->callchain->nr;
3810 size *= sizeof(u64);
3812 perf_output_copy(handle, data->callchain, size);
3815 perf_output_put(handle, nr);
3819 if (sample_type & PERF_SAMPLE_RAW) {
3821 perf_output_put(handle, data->raw->size);
3822 perf_output_copy(handle, data->raw->data,
3829 .size = sizeof(u32),
3832 perf_output_put(handle, raw);
3837 void perf_prepare_sample(struct perf_event_header *header,
3838 struct perf_sample_data *data,
3839 struct perf_event *event,
3840 struct pt_regs *regs)
3842 u64 sample_type = event->attr.sample_type;
3844 header->type = PERF_RECORD_SAMPLE;
3845 header->size = sizeof(*header) + event->header_size;
3848 header->misc |= perf_misc_flags(regs);
3850 __perf_event_header__init_id(header, data, event);
3852 if (sample_type & PERF_SAMPLE_IP)
3853 data->ip = perf_instruction_pointer(regs);
3855 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3858 data->callchain = perf_callchain(regs);
3860 if (data->callchain)
3861 size += data->callchain->nr;
3863 header->size += size * sizeof(u64);
3866 if (sample_type & PERF_SAMPLE_RAW) {
3867 int size = sizeof(u32);
3870 size += data->raw->size;
3872 size += sizeof(u32);
3874 WARN_ON_ONCE(size & (sizeof(u64)-1));
3875 header->size += size;
3879 static void perf_event_output(struct perf_event *event, int nmi,
3880 struct perf_sample_data *data,
3881 struct pt_regs *regs)
3883 struct perf_output_handle handle;
3884 struct perf_event_header header;
3886 /* protect the callchain buffers */
3889 perf_prepare_sample(&header, data, event, regs);
3891 if (perf_output_begin(&handle, event, header.size, nmi, 1))
3894 perf_output_sample(&handle, &header, data, event);
3896 perf_output_end(&handle);
3906 struct perf_read_event {
3907 struct perf_event_header header;
3914 perf_event_read_event(struct perf_event *event,
3915 struct task_struct *task)
3917 struct perf_output_handle handle;
3918 struct perf_sample_data sample;
3919 struct perf_read_event read_event = {
3921 .type = PERF_RECORD_READ,
3923 .size = sizeof(read_event) + event->read_size,
3925 .pid = perf_event_pid(event, task),
3926 .tid = perf_event_tid(event, task),
3930 perf_event_header__init_id(&read_event.header, &sample, event);
3931 ret = perf_output_begin(&handle, event, read_event.header.size, 0, 0);
3935 perf_output_put(&handle, read_event);
3936 perf_output_read(&handle, event);
3937 perf_event__output_id_sample(event, &handle, &sample);
3939 perf_output_end(&handle);
3943 * task tracking -- fork/exit
3945 * enabled by: attr.comm | attr.mmap | attr.mmap_data | attr.task
3948 struct perf_task_event {
3949 struct task_struct *task;
3950 struct perf_event_context *task_ctx;
3953 struct perf_event_header header;
3963 static void perf_event_task_output(struct perf_event *event,
3964 struct perf_task_event *task_event)
3966 struct perf_output_handle handle;
3967 struct perf_sample_data sample;
3968 struct task_struct *task = task_event->task;
3969 int ret, size = task_event->event_id.header.size;
3971 perf_event_header__init_id(&task_event->event_id.header, &sample, event);
3973 ret = perf_output_begin(&handle, event,
3974 task_event->event_id.header.size, 0, 0);
3978 task_event->event_id.pid = perf_event_pid(event, task);
3979 task_event->event_id.ppid = perf_event_pid(event, current);
3981 task_event->event_id.tid = perf_event_tid(event, task);
3982 task_event->event_id.ptid = perf_event_tid(event, current);
3984 perf_output_put(&handle, task_event->event_id);
3986 perf_event__output_id_sample(event, &handle, &sample);
3988 perf_output_end(&handle);
3990 task_event->event_id.header.size = size;
3993 static int perf_event_task_match(struct perf_event *event)
3995 if (event->state < PERF_EVENT_STATE_INACTIVE)
3998 if (!event_filter_match(event))
4001 if (event->attr.comm || event->attr.mmap ||
4002 event->attr.mmap_data || event->attr.task)
4008 static void perf_event_task_ctx(struct perf_event_context *ctx,
4009 struct perf_task_event *task_event)
4011 struct perf_event *event;
4013 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4014 if (perf_event_task_match(event))
4015 perf_event_task_output(event, task_event);
4019 static void perf_event_task_event(struct perf_task_event *task_event)
4021 struct perf_cpu_context *cpuctx;
4022 struct perf_event_context *ctx;
4027 list_for_each_entry_rcu(pmu, &pmus, entry) {
4028 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
4029 if (cpuctx->active_pmu != pmu)
4031 perf_event_task_ctx(&cpuctx->ctx, task_event);
4033 ctx = task_event->task_ctx;
4035 ctxn = pmu->task_ctx_nr;
4038 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4041 perf_event_task_ctx(ctx, task_event);
4043 put_cpu_ptr(pmu->pmu_cpu_context);
4048 static void perf_event_task(struct task_struct *task,
4049 struct perf_event_context *task_ctx,
4052 struct perf_task_event task_event;
4054 if (!atomic_read(&nr_comm_events) &&
4055 !atomic_read(&nr_mmap_events) &&
4056 !atomic_read(&nr_task_events))
4059 task_event = (struct perf_task_event){
4061 .task_ctx = task_ctx,
4064 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
4066 .size = sizeof(task_event.event_id),
4072 .time = perf_clock(),
4076 perf_event_task_event(&task_event);
4079 void perf_event_fork(struct task_struct *task)
4081 perf_event_task(task, NULL, 1);
4088 struct perf_comm_event {
4089 struct task_struct *task;
4094 struct perf_event_header header;
4101 static void perf_event_comm_output(struct perf_event *event,
4102 struct perf_comm_event *comm_event)
4104 struct perf_output_handle handle;
4105 struct perf_sample_data sample;
4106 int size = comm_event->event_id.header.size;
4109 perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
4110 ret = perf_output_begin(&handle, event,
4111 comm_event->event_id.header.size, 0, 0);
4116 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
4117 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
4119 perf_output_put(&handle, comm_event->event_id);
4120 perf_output_copy(&handle, comm_event->comm,
4121 comm_event->comm_size);
4123 perf_event__output_id_sample(event, &handle, &sample);
4125 perf_output_end(&handle);
4127 comm_event->event_id.header.size = size;
4130 static int perf_event_comm_match(struct perf_event *event)
4132 if (event->state < PERF_EVENT_STATE_INACTIVE)
4135 if (!event_filter_match(event))
4138 if (event->attr.comm)
4144 static void perf_event_comm_ctx(struct perf_event_context *ctx,
4145 struct perf_comm_event *comm_event)
4147 struct perf_event *event;
4149 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4150 if (perf_event_comm_match(event))
4151 perf_event_comm_output(event, comm_event);
4155 static void perf_event_comm_event(struct perf_comm_event *comm_event)
4157 struct perf_cpu_context *cpuctx;
4158 struct perf_event_context *ctx;
4159 char comm[TASK_COMM_LEN];
4164 memset(comm, 0, sizeof(comm));
4165 strlcpy(comm, comm_event->task->comm, sizeof(comm));
4166 size = ALIGN(strlen(comm)+1, sizeof(u64));
4168 comm_event->comm = comm;
4169 comm_event->comm_size = size;
4171 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
4173 list_for_each_entry_rcu(pmu, &pmus, entry) {
4174 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
4175 if (cpuctx->active_pmu != pmu)
4177 perf_event_comm_ctx(&cpuctx->ctx, comm_event);
4179 ctxn = pmu->task_ctx_nr;
4183 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4185 perf_event_comm_ctx(ctx, comm_event);
4187 put_cpu_ptr(pmu->pmu_cpu_context);
4192 void perf_event_comm(struct task_struct *task)
4194 struct perf_comm_event comm_event;
4195 struct perf_event_context *ctx;
4198 for_each_task_context_nr(ctxn) {
4199 ctx = task->perf_event_ctxp[ctxn];
4203 perf_event_enable_on_exec(ctx);
4206 if (!atomic_read(&nr_comm_events))
4209 comm_event = (struct perf_comm_event){
4215 .type = PERF_RECORD_COMM,
4224 perf_event_comm_event(&comm_event);
4231 struct perf_mmap_event {
4232 struct vm_area_struct *vma;
4234 const char *file_name;
4238 struct perf_event_header header;
4248 static void perf_event_mmap_output(struct perf_event *event,
4249 struct perf_mmap_event *mmap_event)
4251 struct perf_output_handle handle;
4252 struct perf_sample_data sample;
4253 int size = mmap_event->event_id.header.size;
4256 perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
4257 ret = perf_output_begin(&handle, event,
4258 mmap_event->event_id.header.size, 0, 0);
4262 mmap_event->event_id.pid = perf_event_pid(event, current);
4263 mmap_event->event_id.tid = perf_event_tid(event, current);
4265 perf_output_put(&handle, mmap_event->event_id);
4266 perf_output_copy(&handle, mmap_event->file_name,
4267 mmap_event->file_size);
4269 perf_event__output_id_sample(event, &handle, &sample);
4271 perf_output_end(&handle);
4273 mmap_event->event_id.header.size = size;
4276 static int perf_event_mmap_match(struct perf_event *event,
4277 struct perf_mmap_event *mmap_event,
4280 if (event->state < PERF_EVENT_STATE_INACTIVE)
4283 if (!event_filter_match(event))
4286 if ((!executable && event->attr.mmap_data) ||
4287 (executable && event->attr.mmap))
4293 static void perf_event_mmap_ctx(struct perf_event_context *ctx,
4294 struct perf_mmap_event *mmap_event,
4297 struct perf_event *event;
4299 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4300 if (perf_event_mmap_match(event, mmap_event, executable))
4301 perf_event_mmap_output(event, mmap_event);
4305 static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
4307 struct perf_cpu_context *cpuctx;
4308 struct perf_event_context *ctx;
4309 struct vm_area_struct *vma = mmap_event->vma;
4310 struct file *file = vma->vm_file;
4318 memset(tmp, 0, sizeof(tmp));
4322 * d_path works from the end of the buffer backwards, so we
4323 * need to add enough zero bytes after the string to handle
4324 * the 64bit alignment we do later.
4326 buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
4328 name = strncpy(tmp, "//enomem", sizeof(tmp));
4331 name = d_path(&file->f_path, buf, PATH_MAX);
4333 name = strncpy(tmp, "//toolong", sizeof(tmp));
4337 if (arch_vma_name(mmap_event->vma)) {
4338 name = strncpy(tmp, arch_vma_name(mmap_event->vma),
4344 name = strncpy(tmp, "[vdso]", sizeof(tmp));
4346 } else if (vma->vm_start <= vma->vm_mm->start_brk &&
4347 vma->vm_end >= vma->vm_mm->brk) {
4348 name = strncpy(tmp, "[heap]", sizeof(tmp));
4350 } else if (vma->vm_start <= vma->vm_mm->start_stack &&
4351 vma->vm_end >= vma->vm_mm->start_stack) {
4352 name = strncpy(tmp, "[stack]", sizeof(tmp));
4356 name = strncpy(tmp, "//anon", sizeof(tmp));
4361 size = ALIGN(strlen(name)+1, sizeof(u64));
4363 mmap_event->file_name = name;
4364 mmap_event->file_size = size;
4366 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
4369 list_for_each_entry_rcu(pmu, &pmus, entry) {
4370 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
4371 if (cpuctx->active_pmu != pmu)
4373 perf_event_mmap_ctx(&cpuctx->ctx, mmap_event,
4374 vma->vm_flags & VM_EXEC);
4376 ctxn = pmu->task_ctx_nr;
4380 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4382 perf_event_mmap_ctx(ctx, mmap_event,
4383 vma->vm_flags & VM_EXEC);
4386 put_cpu_ptr(pmu->pmu_cpu_context);
4393 void perf_event_mmap(struct vm_area_struct *vma)
4395 struct perf_mmap_event mmap_event;
4397 if (!atomic_read(&nr_mmap_events))
4400 mmap_event = (struct perf_mmap_event){
4406 .type = PERF_RECORD_MMAP,
4407 .misc = PERF_RECORD_MISC_USER,
4412 .start = vma->vm_start,
4413 .len = vma->vm_end - vma->vm_start,
4414 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
4418 perf_event_mmap_event(&mmap_event);
4422 * IRQ throttle logging
4425 static void perf_log_throttle(struct perf_event *event, int enable)
4427 struct perf_output_handle handle;
4428 struct perf_sample_data sample;
4432 struct perf_event_header header;
4436 } throttle_event = {
4438 .type = PERF_RECORD_THROTTLE,
4440 .size = sizeof(throttle_event),
4442 .time = perf_clock(),
4443 .id = primary_event_id(event),
4444 .stream_id = event->id,
4448 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
4450 perf_event_header__init_id(&throttle_event.header, &sample, event);
4452 ret = perf_output_begin(&handle, event,
4453 throttle_event.header.size, 1, 0);
4457 perf_output_put(&handle, throttle_event);
4458 perf_event__output_id_sample(event, &handle, &sample);
4459 perf_output_end(&handle);
4463 * Generic event overflow handling, sampling.
4466 static int __perf_event_overflow(struct perf_event *event, int nmi,
4467 int throttle, struct perf_sample_data *data,
4468 struct pt_regs *regs)
4470 int events = atomic_read(&event->event_limit);
4471 struct hw_perf_event *hwc = &event->hw;
4475 * Non-sampling counters might still use the PMI to fold short
4476 * hardware counters, ignore those.
4478 if (unlikely(!is_sampling_event(event)))
4484 if (hwc->interrupts != MAX_INTERRUPTS) {
4486 if (HZ * hwc->interrupts >
4487 (u64)sysctl_perf_event_sample_rate) {
4488 hwc->interrupts = MAX_INTERRUPTS;
4489 perf_log_throttle(event, 0);
4494 * Keep re-disabling events even though on the previous
4495 * pass we disabled it - just in case we raced with a
4496 * sched-in and the event got enabled again:
4502 if (event->attr.freq) {
4503 u64 now = perf_clock();
4504 s64 delta = now - hwc->freq_time_stamp;
4506 hwc->freq_time_stamp = now;
4508 if (delta > 0 && delta < 2*TICK_NSEC)
4509 perf_adjust_period(event, delta, hwc->last_period);
4513 * XXX event_limit might not quite work as expected on inherited
4517 event->pending_kill = POLL_IN;
4518 if (events && atomic_dec_and_test(&event->event_limit)) {
4520 event->pending_kill = POLL_HUP;
4522 event->pending_disable = 1;
4523 irq_work_queue(&event->pending);
4525 perf_event_disable(event);
4528 if (event->overflow_handler)
4529 event->overflow_handler(event, nmi, data, regs);
4531 perf_event_output(event, nmi, data, regs);
4536 int perf_event_overflow(struct perf_event *event, int nmi,
4537 struct perf_sample_data *data,
4538 struct pt_regs *regs)
4540 return __perf_event_overflow(event, nmi, 1, data, regs);
4544 * Generic software event infrastructure
4547 struct swevent_htable {
4548 struct swevent_hlist *swevent_hlist;
4549 struct mutex hlist_mutex;
4552 /* Recursion avoidance in each contexts */
4553 int recursion[PERF_NR_CONTEXTS];
4556 static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
4559 * We directly increment event->count and keep a second value in
4560 * event->hw.period_left to count intervals. This period event
4561 * is kept in the range [-sample_period, 0] so that we can use the
4565 static u64 perf_swevent_set_period(struct perf_event *event)
4567 struct hw_perf_event *hwc = &event->hw;
4568 u64 period = hwc->last_period;
4572 hwc->last_period = hwc->sample_period;
4575 old = val = local64_read(&hwc->period_left);
4579 nr = div64_u64(period + val, period);
4580 offset = nr * period;
4582 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
4588 static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
4589 int nmi, struct perf_sample_data *data,
4590 struct pt_regs *regs)
4592 struct hw_perf_event *hwc = &event->hw;
4595 data->period = event->hw.last_period;
4597 overflow = perf_swevent_set_period(event);
4599 if (hwc->interrupts == MAX_INTERRUPTS)
4602 for (; overflow; overflow--) {
4603 if (__perf_event_overflow(event, nmi, throttle,
4606 * We inhibit the overflow from happening when
4607 * hwc->interrupts == MAX_INTERRUPTS.
4615 static void perf_swevent_event(struct perf_event *event, u64 nr,
4616 int nmi, struct perf_sample_data *data,
4617 struct pt_regs *regs)
4619 struct hw_perf_event *hwc = &event->hw;
4621 local64_add(nr, &event->count);
4626 if (!is_sampling_event(event))
4629 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
4630 return perf_swevent_overflow(event, 1, nmi, data, regs);
4632 if (local64_add_negative(nr, &hwc->period_left))
4635 perf_swevent_overflow(event, 0, nmi, data, regs);
4638 static int perf_exclude_event(struct perf_event *event,
4639 struct pt_regs *regs)
4641 if (event->hw.state & PERF_HES_STOPPED)
4645 if (event->attr.exclude_user && user_mode(regs))
4648 if (event->attr.exclude_kernel && !user_mode(regs))
4655 static int perf_swevent_match(struct perf_event *event,
4656 enum perf_type_id type,
4658 struct perf_sample_data *data,
4659 struct pt_regs *regs)
4661 if (event->attr.type != type)
4664 if (event->attr.config != event_id)
4667 if (perf_exclude_event(event, regs))
4673 static inline u64 swevent_hash(u64 type, u32 event_id)
4675 u64 val = event_id | (type << 32);
4677 return hash_64(val, SWEVENT_HLIST_BITS);
4680 static inline struct hlist_head *
4681 __find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
4683 u64 hash = swevent_hash(type, event_id);
4685 return &hlist->heads[hash];
4688 /* For the read side: events when they trigger */
4689 static inline struct hlist_head *
4690 find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
4692 struct swevent_hlist *hlist;
4694 hlist = rcu_dereference(swhash->swevent_hlist);
4698 return __find_swevent_head(hlist, type, event_id);
4701 /* For the event head insertion and removal in the hlist */
4702 static inline struct hlist_head *
4703 find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
4705 struct swevent_hlist *hlist;
4706 u32 event_id = event->attr.config;
4707 u64 type = event->attr.type;
4710 * Event scheduling is always serialized against hlist allocation
4711 * and release. Which makes the protected version suitable here.
4712 * The context lock guarantees that.
4714 hlist = rcu_dereference_protected(swhash->swevent_hlist,
4715 lockdep_is_held(&event->ctx->lock));
4719 return __find_swevent_head(hlist, type, event_id);
4722 static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
4724 struct perf_sample_data *data,
4725 struct pt_regs *regs)
4727 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
4728 struct perf_event *event;
4729 struct hlist_node *node;
4730 struct hlist_head *head;
4733 head = find_swevent_head_rcu(swhash, type, event_id);
4737 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
4738 if (perf_swevent_match(event, type, event_id, data, regs))
4739 perf_swevent_event(event, nr, nmi, data, regs);
4745 int perf_swevent_get_recursion_context(void)
4747 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
4749 return get_recursion_context(swhash->recursion);
4751 EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
4753 inline void perf_swevent_put_recursion_context(int rctx)
4755 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
4757 put_recursion_context(swhash->recursion, rctx);
4760 void __perf_sw_event(u32 event_id, u64 nr, int nmi,
4761 struct pt_regs *regs, u64 addr)
4763 struct perf_sample_data data;
4766 preempt_disable_notrace();
4767 rctx = perf_swevent_get_recursion_context();
4771 perf_sample_data_init(&data, addr);
4773 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, nmi, &data, regs);
4775 perf_swevent_put_recursion_context(rctx);
4776 preempt_enable_notrace();
4779 static void perf_swevent_read(struct perf_event *event)
4783 static int perf_swevent_add(struct perf_event *event, int flags)
4785 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
4786 struct hw_perf_event *hwc = &event->hw;
4787 struct hlist_head *head;
4789 if (is_sampling_event(event)) {
4790 hwc->last_period = hwc->sample_period;
4791 perf_swevent_set_period(event);
4794 hwc->state = !(flags & PERF_EF_START);
4796 head = find_swevent_head(swhash, event);
4797 if (WARN_ON_ONCE(!head))
4800 hlist_add_head_rcu(&event->hlist_entry, head);
4805 static void perf_swevent_del(struct perf_event *event, int flags)
4807 hlist_del_rcu(&event->hlist_entry);
4810 static void perf_swevent_start(struct perf_event *event, int flags)
4812 event->hw.state = 0;
4815 static void perf_swevent_stop(struct perf_event *event, int flags)
4817 event->hw.state = PERF_HES_STOPPED;
4820 /* Deref the hlist from the update side */
4821 static inline struct swevent_hlist *
4822 swevent_hlist_deref(struct swevent_htable *swhash)
4824 return rcu_dereference_protected(swhash->swevent_hlist,
4825 lockdep_is_held(&swhash->hlist_mutex));
4828 static void swevent_hlist_release_rcu(struct rcu_head *rcu_head)
4830 struct swevent_hlist *hlist;
4832 hlist = container_of(rcu_head, struct swevent_hlist, rcu_head);
4836 static void swevent_hlist_release(struct swevent_htable *swhash)
4838 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
4843 rcu_assign_pointer(swhash->swevent_hlist, NULL);
4844 call_rcu(&hlist->rcu_head, swevent_hlist_release_rcu);
4847 static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
4849 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
4851 mutex_lock(&swhash->hlist_mutex);
4853 if (!--swhash->hlist_refcount)
4854 swevent_hlist_release(swhash);
4856 mutex_unlock(&swhash->hlist_mutex);
4859 static void swevent_hlist_put(struct perf_event *event)
4863 if (event->cpu != -1) {
4864 swevent_hlist_put_cpu(event, event->cpu);
4868 for_each_possible_cpu(cpu)
4869 swevent_hlist_put_cpu(event, cpu);
4872 static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
4874 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
4877 mutex_lock(&swhash->hlist_mutex);
4879 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
4880 struct swevent_hlist *hlist;
4882 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
4887 rcu_assign_pointer(swhash->swevent_hlist, hlist);
4889 swhash->hlist_refcount++;
4891 mutex_unlock(&swhash->hlist_mutex);
4896 static int swevent_hlist_get(struct perf_event *event)
4899 int cpu, failed_cpu;
4901 if (event->cpu != -1)
4902 return swevent_hlist_get_cpu(event, event->cpu);
4905 for_each_possible_cpu(cpu) {
4906 err = swevent_hlist_get_cpu(event, cpu);
4916 for_each_possible_cpu(cpu) {
4917 if (cpu == failed_cpu)
4919 swevent_hlist_put_cpu(event, cpu);
4926 atomic_t perf_swevent_enabled[PERF_COUNT_SW_MAX];
4928 static void sw_perf_event_destroy(struct perf_event *event)
4930 u64 event_id = event->attr.config;
4932 WARN_ON(event->parent);
4934 jump_label_dec(&perf_swevent_enabled[event_id]);
4935 swevent_hlist_put(event);
4938 static int perf_swevent_init(struct perf_event *event)
4940 int event_id = event->attr.config;
4942 if (event->attr.type != PERF_TYPE_SOFTWARE)
4946 case PERF_COUNT_SW_CPU_CLOCK:
4947 case PERF_COUNT_SW_TASK_CLOCK:
4954 if (event_id >= PERF_COUNT_SW_MAX)
4957 if (!event->parent) {
4960 err = swevent_hlist_get(event);
4964 jump_label_inc(&perf_swevent_enabled[event_id]);
4965 event->destroy = sw_perf_event_destroy;
4971 static struct pmu perf_swevent = {
4972 .task_ctx_nr = perf_sw_context,
4974 .event_init = perf_swevent_init,
4975 .add = perf_swevent_add,
4976 .del = perf_swevent_del,
4977 .start = perf_swevent_start,
4978 .stop = perf_swevent_stop,
4979 .read = perf_swevent_read,
4982 #ifdef CONFIG_EVENT_TRACING
4984 static int perf_tp_filter_match(struct perf_event *event,
4985 struct perf_sample_data *data)
4987 void *record = data->raw->data;
4989 if (likely(!event->filter) || filter_match_preds(event->filter, record))
4994 static int perf_tp_event_match(struct perf_event *event,
4995 struct perf_sample_data *data,
4996 struct pt_regs *regs)
4999 * All tracepoints are from kernel-space.
5001 if (event->attr.exclude_kernel)
5004 if (!perf_tp_filter_match(event, data))
5010 void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
5011 struct pt_regs *regs, struct hlist_head *head, int rctx)
5013 struct perf_sample_data data;
5014 struct perf_event *event;
5015 struct hlist_node *node;
5017 struct perf_raw_record raw = {
5022 perf_sample_data_init(&data, addr);
5025 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
5026 if (perf_tp_event_match(event, &data, regs))
5027 perf_swevent_event(event, count, 1, &data, regs);
5030 perf_swevent_put_recursion_context(rctx);
5032 EXPORT_SYMBOL_GPL(perf_tp_event);
5034 static void tp_perf_event_destroy(struct perf_event *event)
5036 perf_trace_destroy(event);
5039 static int perf_tp_event_init(struct perf_event *event)
5043 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5046 err = perf_trace_init(event);
5050 event->destroy = tp_perf_event_destroy;
5055 static struct pmu perf_tracepoint = {
5056 .task_ctx_nr = perf_sw_context,
5058 .event_init = perf_tp_event_init,
5059 .add = perf_trace_add,
5060 .del = perf_trace_del,
5061 .start = perf_swevent_start,
5062 .stop = perf_swevent_stop,
5063 .read = perf_swevent_read,
5066 static inline void perf_tp_register(void)
5068 perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
5071 static int perf_event_set_filter(struct perf_event *event, void __user *arg)
5076 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5079 filter_str = strndup_user(arg, PAGE_SIZE);
5080 if (IS_ERR(filter_str))
5081 return PTR_ERR(filter_str);
5083 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
5089 static void perf_event_free_filter(struct perf_event *event)
5091 ftrace_profile_free_filter(event);
5096 static inline void perf_tp_register(void)
5100 static int perf_event_set_filter(struct perf_event *event, void __user *arg)
5105 static void perf_event_free_filter(struct perf_event *event)
5109 #endif /* CONFIG_EVENT_TRACING */
5111 #ifdef CONFIG_HAVE_HW_BREAKPOINT
5112 void perf_bp_event(struct perf_event *bp, void *data)
5114 struct perf_sample_data sample;
5115 struct pt_regs *regs = data;
5117 perf_sample_data_init(&sample, bp->attr.bp_addr);
5119 if (!bp->hw.state && !perf_exclude_event(bp, regs))
5120 perf_swevent_event(bp, 1, 1, &sample, regs);
5125 * hrtimer based swevent callback
5128 static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
5130 enum hrtimer_restart ret = HRTIMER_RESTART;
5131 struct perf_sample_data data;
5132 struct pt_regs *regs;
5133 struct perf_event *event;
5136 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
5137 event->pmu->read(event);
5139 perf_sample_data_init(&data, 0);
5140 data.period = event->hw.last_period;
5141 regs = get_irq_regs();
5143 if (regs && !perf_exclude_event(event, regs)) {
5144 if (!(event->attr.exclude_idle && current->pid == 0))
5145 if (perf_event_overflow(event, 0, &data, regs))
5146 ret = HRTIMER_NORESTART;
5149 period = max_t(u64, 10000, event->hw.sample_period);
5150 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
5155 static void perf_swevent_start_hrtimer(struct perf_event *event)
5157 struct hw_perf_event *hwc = &event->hw;
5160 if (!is_sampling_event(event))
5163 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
5164 hwc->hrtimer.function = perf_swevent_hrtimer;
5166 period = local64_read(&hwc->period_left);
5171 local64_set(&hwc->period_left, 0);
5173 period = max_t(u64, 10000, hwc->sample_period);
5175 __hrtimer_start_range_ns(&hwc->hrtimer,
5176 ns_to_ktime(period), 0,
5177 HRTIMER_MODE_REL_PINNED, 0);
5180 static void perf_swevent_cancel_hrtimer(struct perf_event *event)
5182 struct hw_perf_event *hwc = &event->hw;
5184 if (is_sampling_event(event)) {
5185 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
5186 local64_set(&hwc->period_left, ktime_to_ns(remaining));
5188 hrtimer_cancel(&hwc->hrtimer);
5193 * Software event: cpu wall time clock
5196 static void cpu_clock_event_update(struct perf_event *event)
5201 now = local_clock();
5202 prev = local64_xchg(&event->hw.prev_count, now);
5203 local64_add(now - prev, &event->count);
5206 static void cpu_clock_event_start(struct perf_event *event, int flags)
5208 local64_set(&event->hw.prev_count, local_clock());
5209 perf_swevent_start_hrtimer(event);
5212 static void cpu_clock_event_stop(struct perf_event *event, int flags)
5214 perf_swevent_cancel_hrtimer(event);
5215 cpu_clock_event_update(event);
5218 static int cpu_clock_event_add(struct perf_event *event, int flags)
5220 if (flags & PERF_EF_START)
5221 cpu_clock_event_start(event, flags);
5226 static void cpu_clock_event_del(struct perf_event *event, int flags)
5228 cpu_clock_event_stop(event, flags);
5231 static void cpu_clock_event_read(struct perf_event *event)
5233 cpu_clock_event_update(event);
5236 static int cpu_clock_event_init(struct perf_event *event)
5238 if (event->attr.type != PERF_TYPE_SOFTWARE)
5241 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
5247 static struct pmu perf_cpu_clock = {
5248 .task_ctx_nr = perf_sw_context,
5250 .event_init = cpu_clock_event_init,
5251 .add = cpu_clock_event_add,
5252 .del = cpu_clock_event_del,
5253 .start = cpu_clock_event_start,
5254 .stop = cpu_clock_event_stop,
5255 .read = cpu_clock_event_read,
5259 * Software event: task time clock
5262 static void task_clock_event_update(struct perf_event *event, u64 now)
5267 prev = local64_xchg(&event->hw.prev_count, now);
5269 local64_add(delta, &event->count);
5272 static void task_clock_event_start(struct perf_event *event, int flags)
5274 local64_set(&event->hw.prev_count, event->ctx->time);
5275 perf_swevent_start_hrtimer(event);
5278 static void task_clock_event_stop(struct perf_event *event, int flags)
5280 perf_swevent_cancel_hrtimer(event);
5281 task_clock_event_update(event, event->ctx->time);
5284 static int task_clock_event_add(struct perf_event *event, int flags)
5286 if (flags & PERF_EF_START)
5287 task_clock_event_start(event, flags);
5292 static void task_clock_event_del(struct perf_event *event, int flags)
5294 task_clock_event_stop(event, PERF_EF_UPDATE);
5297 static void task_clock_event_read(struct perf_event *event)
5302 update_context_time(event->ctx);
5303 time = event->ctx->time;
5305 u64 now = perf_clock();
5306 u64 delta = now - event->ctx->timestamp;
5307 time = event->ctx->time + delta;
5310 task_clock_event_update(event, time);
5313 static int task_clock_event_init(struct perf_event *event)
5315 if (event->attr.type != PERF_TYPE_SOFTWARE)
5318 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
5324 static struct pmu perf_task_clock = {
5325 .task_ctx_nr = perf_sw_context,
5327 .event_init = task_clock_event_init,
5328 .add = task_clock_event_add,
5329 .del = task_clock_event_del,
5330 .start = task_clock_event_start,
5331 .stop = task_clock_event_stop,
5332 .read = task_clock_event_read,
5335 static void perf_pmu_nop_void(struct pmu *pmu)
5339 static int perf_pmu_nop_int(struct pmu *pmu)
5344 static void perf_pmu_start_txn(struct pmu *pmu)
5346 perf_pmu_disable(pmu);
5349 static int perf_pmu_commit_txn(struct pmu *pmu)
5351 perf_pmu_enable(pmu);
5355 static void perf_pmu_cancel_txn(struct pmu *pmu)
5357 perf_pmu_enable(pmu);
5361 * Ensures all contexts with the same task_ctx_nr have the same
5362 * pmu_cpu_context too.
5364 static void *find_pmu_context(int ctxn)
5371 list_for_each_entry(pmu, &pmus, entry) {
5372 if (pmu->task_ctx_nr == ctxn)
5373 return pmu->pmu_cpu_context;
5379 static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
5383 for_each_possible_cpu(cpu) {
5384 struct perf_cpu_context *cpuctx;
5386 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
5388 if (cpuctx->active_pmu == old_pmu)
5389 cpuctx->active_pmu = pmu;
5393 static void free_pmu_context(struct pmu *pmu)
5397 mutex_lock(&pmus_lock);
5399 * Like a real lame refcount.
5401 list_for_each_entry(i, &pmus, entry) {
5402 if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
5403 update_pmu_context(i, pmu);
5408 free_percpu(pmu->pmu_cpu_context);
5410 mutex_unlock(&pmus_lock);
5412 static struct idr pmu_idr;
5415 type_show(struct device *dev, struct device_attribute *attr, char *page)
5417 struct pmu *pmu = dev_get_drvdata(dev);
5419 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
5422 static struct device_attribute pmu_dev_attrs[] = {
5427 static int pmu_bus_running;
5428 static struct bus_type pmu_bus = {
5429 .name = "event_source",
5430 .dev_attrs = pmu_dev_attrs,
5433 static void pmu_dev_release(struct device *dev)
5438 static int pmu_dev_alloc(struct pmu *pmu)
5442 pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
5446 device_initialize(pmu->dev);
5447 ret = dev_set_name(pmu->dev, "%s", pmu->name);
5451 dev_set_drvdata(pmu->dev, pmu);
5452 pmu->dev->bus = &pmu_bus;
5453 pmu->dev->release = pmu_dev_release;
5454 ret = device_add(pmu->dev);
5462 put_device(pmu->dev);
5466 static struct lock_class_key cpuctx_mutex;
5468 int perf_pmu_register(struct pmu *pmu, char *name, int type)
5472 mutex_lock(&pmus_lock);
5474 pmu->pmu_disable_count = alloc_percpu(int);
5475 if (!pmu->pmu_disable_count)
5484 int err = idr_pre_get(&pmu_idr, GFP_KERNEL);
5488 err = idr_get_new_above(&pmu_idr, pmu, PERF_TYPE_MAX, &type);
5496 if (pmu_bus_running) {
5497 ret = pmu_dev_alloc(pmu);
5503 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
5504 if (pmu->pmu_cpu_context)
5505 goto got_cpu_context;
5507 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
5508 if (!pmu->pmu_cpu_context)
5511 for_each_possible_cpu(cpu) {
5512 struct perf_cpu_context *cpuctx;
5514 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
5515 __perf_event_init_context(&cpuctx->ctx);
5516 lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
5517 cpuctx->ctx.type = cpu_context;
5518 cpuctx->ctx.pmu = pmu;
5519 cpuctx->jiffies_interval = 1;
5520 INIT_LIST_HEAD(&cpuctx->rotation_list);
5521 cpuctx->active_pmu = pmu;
5525 if (!pmu->start_txn) {
5526 if (pmu->pmu_enable) {
5528 * If we have pmu_enable/pmu_disable calls, install
5529 * transaction stubs that use that to try and batch
5530 * hardware accesses.
5532 pmu->start_txn = perf_pmu_start_txn;
5533 pmu->commit_txn = perf_pmu_commit_txn;
5534 pmu->cancel_txn = perf_pmu_cancel_txn;
5536 pmu->start_txn = perf_pmu_nop_void;
5537 pmu->commit_txn = perf_pmu_nop_int;
5538 pmu->cancel_txn = perf_pmu_nop_void;
5542 if (!pmu->pmu_enable) {
5543 pmu->pmu_enable = perf_pmu_nop_void;
5544 pmu->pmu_disable = perf_pmu_nop_void;
5547 list_add_rcu(&pmu->entry, &pmus);
5550 mutex_unlock(&pmus_lock);
5555 device_del(pmu->dev);
5556 put_device(pmu->dev);
5559 if (pmu->type >= PERF_TYPE_MAX)
5560 idr_remove(&pmu_idr, pmu->type);
5563 free_percpu(pmu->pmu_disable_count);
5567 void perf_pmu_unregister(struct pmu *pmu)
5569 mutex_lock(&pmus_lock);
5570 list_del_rcu(&pmu->entry);
5571 mutex_unlock(&pmus_lock);
5574 * We dereference the pmu list under both SRCU and regular RCU, so
5575 * synchronize against both of those.
5577 synchronize_srcu(&pmus_srcu);
5580 free_percpu(pmu->pmu_disable_count);
5581 if (pmu->type >= PERF_TYPE_MAX)
5582 idr_remove(&pmu_idr, pmu->type);
5583 device_del(pmu->dev);
5584 put_device(pmu->dev);
5585 free_pmu_context(pmu);
5588 struct pmu *perf_init_event(struct perf_event *event)
5590 struct pmu *pmu = NULL;
5593 idx = srcu_read_lock(&pmus_srcu);
5596 pmu = idr_find(&pmu_idr, event->attr.type);
5601 list_for_each_entry_rcu(pmu, &pmus, entry) {
5602 int ret = pmu->event_init(event);
5606 if (ret != -ENOENT) {
5611 pmu = ERR_PTR(-ENOENT);
5613 srcu_read_unlock(&pmus_srcu, idx);
5619 * Allocate and initialize a event structure
5621 static struct perf_event *
5622 perf_event_alloc(struct perf_event_attr *attr, int cpu,
5623 struct task_struct *task,
5624 struct perf_event *group_leader,
5625 struct perf_event *parent_event,
5626 perf_overflow_handler_t overflow_handler)
5629 struct perf_event *event;
5630 struct hw_perf_event *hwc;
5633 if ((unsigned)cpu >= nr_cpu_ids) {
5634 if (!task || cpu != -1)
5635 return ERR_PTR(-EINVAL);
5638 event = kzalloc(sizeof(*event), GFP_KERNEL);
5640 return ERR_PTR(-ENOMEM);
5643 * Single events are their own group leaders, with an
5644 * empty sibling list:
5647 group_leader = event;
5649 mutex_init(&event->child_mutex);
5650 INIT_LIST_HEAD(&event->child_list);
5652 INIT_LIST_HEAD(&event->group_entry);
5653 INIT_LIST_HEAD(&event->event_entry);
5654 INIT_LIST_HEAD(&event->sibling_list);
5655 init_waitqueue_head(&event->waitq);
5656 init_irq_work(&event->pending, perf_pending_event);
5658 mutex_init(&event->mmap_mutex);
5661 event->attr = *attr;
5662 event->group_leader = group_leader;
5666 event->parent = parent_event;
5668 event->ns = get_pid_ns(current->nsproxy->pid_ns);
5669 event->id = atomic64_inc_return(&perf_event_id);
5671 event->state = PERF_EVENT_STATE_INACTIVE;
5674 event->attach_state = PERF_ATTACH_TASK;
5675 #ifdef CONFIG_HAVE_HW_BREAKPOINT
5677 * hw_breakpoint is a bit difficult here..
5679 if (attr->type == PERF_TYPE_BREAKPOINT)
5680 event->hw.bp_target = task;
5684 if (!overflow_handler && parent_event)
5685 overflow_handler = parent_event->overflow_handler;
5687 event->overflow_handler = overflow_handler;
5690 event->state = PERF_EVENT_STATE_OFF;
5695 hwc->sample_period = attr->sample_period;
5696 if (attr->freq && attr->sample_freq)
5697 hwc->sample_period = 1;
5698 hwc->last_period = hwc->sample_period;
5700 local64_set(&hwc->period_left, hwc->sample_period);
5703 * we currently do not support PERF_FORMAT_GROUP on inherited events
5705 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
5708 pmu = perf_init_event(event);
5714 else if (IS_ERR(pmu))
5719 put_pid_ns(event->ns);
5721 return ERR_PTR(err);
5726 if (!event->parent) {
5727 if (event->attach_state & PERF_ATTACH_TASK)
5728 jump_label_inc(&perf_task_events);
5729 if (event->attr.mmap || event->attr.mmap_data)
5730 atomic_inc(&nr_mmap_events);
5731 if (event->attr.comm)
5732 atomic_inc(&nr_comm_events);
5733 if (event->attr.task)
5734 atomic_inc(&nr_task_events);
5735 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
5736 err = get_callchain_buffers();
5739 return ERR_PTR(err);
5747 static int perf_copy_attr(struct perf_event_attr __user *uattr,
5748 struct perf_event_attr *attr)
5753 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
5757 * zero the full structure, so that a short copy will be nice.
5759 memset(attr, 0, sizeof(*attr));
5761 ret = get_user(size, &uattr->size);
5765 if (size > PAGE_SIZE) /* silly large */
5768 if (!size) /* abi compat */
5769 size = PERF_ATTR_SIZE_VER0;
5771 if (size < PERF_ATTR_SIZE_VER0)
5775 * If we're handed a bigger struct than we know of,
5776 * ensure all the unknown bits are 0 - i.e. new
5777 * user-space does not rely on any kernel feature
5778 * extensions we dont know about yet.
5780 if (size > sizeof(*attr)) {
5781 unsigned char __user *addr;
5782 unsigned char __user *end;
5785 addr = (void __user *)uattr + sizeof(*attr);
5786 end = (void __user *)uattr + size;
5788 for (; addr < end; addr++) {
5789 ret = get_user(val, addr);
5795 size = sizeof(*attr);
5798 ret = copy_from_user(attr, uattr, size);
5803 * If the type exists, the corresponding creation will verify
5806 if (attr->type >= PERF_TYPE_MAX)
5809 if (attr->__reserved_1)
5812 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
5815 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
5822 put_user(sizeof(*attr), &uattr->size);
5828 perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
5830 struct perf_buffer *buffer = NULL, *old_buffer = NULL;
5836 /* don't allow circular references */
5837 if (event == output_event)
5841 * Don't allow cross-cpu buffers
5843 if (output_event->cpu != event->cpu)
5847 * If its not a per-cpu buffer, it must be the same task.
5849 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
5853 mutex_lock(&event->mmap_mutex);
5854 /* Can't redirect output if we've got an active mmap() */
5855 if (atomic_read(&event->mmap_count))
5859 /* get the buffer we want to redirect to */
5860 buffer = perf_buffer_get(output_event);
5865 old_buffer = event->buffer;
5866 rcu_assign_pointer(event->buffer, buffer);
5869 mutex_unlock(&event->mmap_mutex);
5872 perf_buffer_put(old_buffer);
5878 * sys_perf_event_open - open a performance event, associate it to a task/cpu
5880 * @attr_uptr: event_id type attributes for monitoring/sampling
5883 * @group_fd: group leader event fd
5885 SYSCALL_DEFINE5(perf_event_open,
5886 struct perf_event_attr __user *, attr_uptr,
5887 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
5889 struct perf_event *group_leader = NULL, *output_event = NULL;
5890 struct perf_event *event, *sibling;
5891 struct perf_event_attr attr;
5892 struct perf_event_context *ctx;
5893 struct file *event_file = NULL;
5894 struct file *group_file = NULL;
5895 struct task_struct *task = NULL;
5899 int fput_needed = 0;
5902 /* for future expandability... */
5903 if (flags & ~(PERF_FLAG_FD_NO_GROUP | PERF_FLAG_FD_OUTPUT))
5906 err = perf_copy_attr(attr_uptr, &attr);
5910 if (!attr.exclude_kernel) {
5911 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
5916 if (attr.sample_freq > sysctl_perf_event_sample_rate)
5920 event_fd = get_unused_fd_flags(O_RDWR);
5924 if (group_fd != -1) {
5925 group_leader = perf_fget_light(group_fd, &fput_needed);
5926 if (IS_ERR(group_leader)) {
5927 err = PTR_ERR(group_leader);
5930 group_file = group_leader->filp;
5931 if (flags & PERF_FLAG_FD_OUTPUT)
5932 output_event = group_leader;
5933 if (flags & PERF_FLAG_FD_NO_GROUP)
5934 group_leader = NULL;
5938 task = find_lively_task_by_vpid(pid);
5940 err = PTR_ERR(task);
5945 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL, NULL);
5946 if (IS_ERR(event)) {
5947 err = PTR_ERR(event);
5952 * Special case software events and allow them to be part of
5953 * any hardware group.
5958 (is_software_event(event) != is_software_event(group_leader))) {
5959 if (is_software_event(event)) {
5961 * If event and group_leader are not both a software
5962 * event, and event is, then group leader is not.
5964 * Allow the addition of software events to !software
5965 * groups, this is safe because software events never
5968 pmu = group_leader->pmu;
5969 } else if (is_software_event(group_leader) &&
5970 (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
5972 * In case the group is a pure software group, and we
5973 * try to add a hardware event, move the whole group to
5974 * the hardware context.
5981 * Get the target context (task or percpu):
5983 ctx = find_get_context(pmu, task, cpu);
5990 * Look up the group leader (we will attach this event to it):
5996 * Do not allow a recursive hierarchy (this new sibling
5997 * becoming part of another group-sibling):
5999 if (group_leader->group_leader != group_leader)
6002 * Do not allow to attach to a group in a different
6003 * task or CPU context:
6006 if (group_leader->ctx->type != ctx->type)
6009 if (group_leader->ctx != ctx)
6014 * Only a group leader can be exclusive or pinned
6016 if (attr.exclusive || attr.pinned)
6021 err = perf_event_set_output(event, output_event);
6026 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, O_RDWR);
6027 if (IS_ERR(event_file)) {
6028 err = PTR_ERR(event_file);
6033 struct perf_event_context *gctx = group_leader->ctx;
6035 mutex_lock(&gctx->mutex);
6036 perf_remove_from_context(group_leader);
6037 list_for_each_entry(sibling, &group_leader->sibling_list,
6039 perf_remove_from_context(sibling);
6042 mutex_unlock(&gctx->mutex);
6046 event->filp = event_file;
6047 WARN_ON_ONCE(ctx->parent_ctx);
6048 mutex_lock(&ctx->mutex);
6051 perf_install_in_context(ctx, group_leader, cpu);
6053 list_for_each_entry(sibling, &group_leader->sibling_list,
6055 perf_install_in_context(ctx, sibling, cpu);
6060 perf_install_in_context(ctx, event, cpu);
6062 perf_unpin_context(ctx);
6063 mutex_unlock(&ctx->mutex);
6065 event->owner = current;
6067 mutex_lock(¤t->perf_event_mutex);
6068 list_add_tail(&event->owner_entry, ¤t->perf_event_list);
6069 mutex_unlock(¤t->perf_event_mutex);
6072 * Precalculate sample_data sizes
6074 perf_event__header_size(event);
6075 perf_event__id_header_size(event);
6078 * Drop the reference on the group_event after placing the
6079 * new event on the sibling_list. This ensures destruction
6080 * of the group leader will find the pointer to itself in
6081 * perf_group_detach().
6083 fput_light(group_file, fput_needed);
6084 fd_install(event_fd, event_file);
6088 perf_unpin_context(ctx);
6094 put_task_struct(task);
6096 fput_light(group_file, fput_needed);
6098 put_unused_fd(event_fd);
6103 * perf_event_create_kernel_counter
6105 * @attr: attributes of the counter to create
6106 * @cpu: cpu in which the counter is bound
6107 * @task: task to profile (NULL for percpu)
6110 perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
6111 struct task_struct *task,
6112 perf_overflow_handler_t overflow_handler)
6114 struct perf_event_context *ctx;
6115 struct perf_event *event;
6119 * Get the target context (task or percpu):
6122 event = perf_event_alloc(attr, cpu, task, NULL, NULL, overflow_handler);
6123 if (IS_ERR(event)) {
6124 err = PTR_ERR(event);
6128 ctx = find_get_context(event->pmu, task, cpu);
6135 WARN_ON_ONCE(ctx->parent_ctx);
6136 mutex_lock(&ctx->mutex);
6137 perf_install_in_context(ctx, event, cpu);
6139 perf_unpin_context(ctx);
6140 mutex_unlock(&ctx->mutex);
6147 return ERR_PTR(err);
6149 EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
6151 static void sync_child_event(struct perf_event *child_event,
6152 struct task_struct *child)
6154 struct perf_event *parent_event = child_event->parent;
6157 if (child_event->attr.inherit_stat)
6158 perf_event_read_event(child_event, child);
6160 child_val = perf_event_count(child_event);
6163 * Add back the child's count to the parent's count:
6165 atomic64_add(child_val, &parent_event->child_count);
6166 atomic64_add(child_event->total_time_enabled,
6167 &parent_event->child_total_time_enabled);
6168 atomic64_add(child_event->total_time_running,
6169 &parent_event->child_total_time_running);
6172 * Remove this event from the parent's list
6174 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
6175 mutex_lock(&parent_event->child_mutex);
6176 list_del_init(&child_event->child_list);
6177 mutex_unlock(&parent_event->child_mutex);
6180 * Release the parent event, if this was the last
6183 fput(parent_event->filp);
6187 __perf_event_exit_task(struct perf_event *child_event,
6188 struct perf_event_context *child_ctx,
6189 struct task_struct *child)
6191 struct perf_event *parent_event;
6193 perf_remove_from_context(child_event);
6195 parent_event = child_event->parent;
6197 * It can happen that parent exits first, and has events
6198 * that are still around due to the child reference. These
6199 * events need to be zapped - but otherwise linger.
6202 sync_child_event(child_event, child);
6203 free_event(child_event);
6207 static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
6209 struct perf_event *child_event, *tmp;
6210 struct perf_event_context *child_ctx;
6211 unsigned long flags;
6213 if (likely(!child->perf_event_ctxp[ctxn])) {
6214 perf_event_task(child, NULL, 0);
6218 local_irq_save(flags);
6220 * We can't reschedule here because interrupts are disabled,
6221 * and either child is current or it is a task that can't be
6222 * scheduled, so we are now safe from rescheduling changing
6225 child_ctx = rcu_dereference_raw(child->perf_event_ctxp[ctxn]);
6226 task_ctx_sched_out(child_ctx, EVENT_ALL);
6229 * Take the context lock here so that if find_get_context is
6230 * reading child->perf_event_ctxp, we wait until it has
6231 * incremented the context's refcount before we do put_ctx below.
6233 raw_spin_lock(&child_ctx->lock);
6234 child->perf_event_ctxp[ctxn] = NULL;
6236 * If this context is a clone; unclone it so it can't get
6237 * swapped to another process while we're removing all
6238 * the events from it.
6240 unclone_ctx(child_ctx);
6241 update_context_time(child_ctx);
6242 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
6245 * Report the task dead after unscheduling the events so that we
6246 * won't get any samples after PERF_RECORD_EXIT. We can however still
6247 * get a few PERF_RECORD_READ events.
6249 perf_event_task(child, child_ctx, 0);
6252 * We can recurse on the same lock type through:
6254 * __perf_event_exit_task()
6255 * sync_child_event()
6256 * fput(parent_event->filp)
6258 * mutex_lock(&ctx->mutex)
6260 * But since its the parent context it won't be the same instance.
6262 mutex_lock(&child_ctx->mutex);
6265 list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
6267 __perf_event_exit_task(child_event, child_ctx, child);
6269 list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
6271 __perf_event_exit_task(child_event, child_ctx, child);
6274 * If the last event was a group event, it will have appended all
6275 * its siblings to the list, but we obtained 'tmp' before that which
6276 * will still point to the list head terminating the iteration.
6278 if (!list_empty(&child_ctx->pinned_groups) ||
6279 !list_empty(&child_ctx->flexible_groups))
6282 mutex_unlock(&child_ctx->mutex);
6288 * When a child task exits, feed back event values to parent events.
6290 void perf_event_exit_task(struct task_struct *child)
6292 struct perf_event *event, *tmp;
6295 mutex_lock(&child->perf_event_mutex);
6296 list_for_each_entry_safe(event, tmp, &child->perf_event_list,
6298 list_del_init(&event->owner_entry);
6301 * Ensure the list deletion is visible before we clear
6302 * the owner, closes a race against perf_release() where
6303 * we need to serialize on the owner->perf_event_mutex.
6306 event->owner = NULL;
6308 mutex_unlock(&child->perf_event_mutex);
6310 for_each_task_context_nr(ctxn)
6311 perf_event_exit_task_context(child, ctxn);
6314 static void perf_free_event(struct perf_event *event,
6315 struct perf_event_context *ctx)
6317 struct perf_event *parent = event->parent;
6319 if (WARN_ON_ONCE(!parent))
6322 mutex_lock(&parent->child_mutex);
6323 list_del_init(&event->child_list);
6324 mutex_unlock(&parent->child_mutex);
6328 perf_group_detach(event);
6329 list_del_event(event, ctx);
6334 * free an unexposed, unused context as created by inheritance by
6335 * perf_event_init_task below, used by fork() in case of fail.
6337 void perf_event_free_task(struct task_struct *task)
6339 struct perf_event_context *ctx;
6340 struct perf_event *event, *tmp;
6343 for_each_task_context_nr(ctxn) {
6344 ctx = task->perf_event_ctxp[ctxn];
6348 mutex_lock(&ctx->mutex);
6350 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
6352 perf_free_event(event, ctx);
6354 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
6356 perf_free_event(event, ctx);
6358 if (!list_empty(&ctx->pinned_groups) ||
6359 !list_empty(&ctx->flexible_groups))
6362 mutex_unlock(&ctx->mutex);
6368 void perf_event_delayed_put(struct task_struct *task)
6372 for_each_task_context_nr(ctxn)
6373 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
6377 * inherit a event from parent task to child task:
6379 static struct perf_event *
6380 inherit_event(struct perf_event *parent_event,
6381 struct task_struct *parent,
6382 struct perf_event_context *parent_ctx,
6383 struct task_struct *child,
6384 struct perf_event *group_leader,
6385 struct perf_event_context *child_ctx)
6387 struct perf_event *child_event;
6388 unsigned long flags;
6391 * Instead of creating recursive hierarchies of events,
6392 * we link inherited events back to the original parent,
6393 * which has a filp for sure, which we use as the reference
6396 if (parent_event->parent)
6397 parent_event = parent_event->parent;
6399 child_event = perf_event_alloc(&parent_event->attr,
6402 group_leader, parent_event,
6404 if (IS_ERR(child_event))
6409 * Make the child state follow the state of the parent event,
6410 * not its attr.disabled bit. We hold the parent's mutex,
6411 * so we won't race with perf_event_{en, dis}able_family.
6413 if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
6414 child_event->state = PERF_EVENT_STATE_INACTIVE;
6416 child_event->state = PERF_EVENT_STATE_OFF;
6418 if (parent_event->attr.freq) {
6419 u64 sample_period = parent_event->hw.sample_period;
6420 struct hw_perf_event *hwc = &child_event->hw;
6422 hwc->sample_period = sample_period;
6423 hwc->last_period = sample_period;
6425 local64_set(&hwc->period_left, sample_period);
6428 child_event->ctx = child_ctx;
6429 child_event->overflow_handler = parent_event->overflow_handler;
6432 * Precalculate sample_data sizes
6434 perf_event__header_size(child_event);
6435 perf_event__id_header_size(child_event);
6438 * Link it up in the child's context:
6440 raw_spin_lock_irqsave(&child_ctx->lock, flags);
6441 add_event_to_ctx(child_event, child_ctx);
6442 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
6445 * Get a reference to the parent filp - we will fput it
6446 * when the child event exits. This is safe to do because
6447 * we are in the parent and we know that the filp still
6448 * exists and has a nonzero count:
6450 atomic_long_inc(&parent_event->filp->f_count);
6453 * Link this into the parent event's child list
6455 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
6456 mutex_lock(&parent_event->child_mutex);
6457 list_add_tail(&child_event->child_list, &parent_event->child_list);
6458 mutex_unlock(&parent_event->child_mutex);
6463 static int inherit_group(struct perf_event *parent_event,
6464 struct task_struct *parent,
6465 struct perf_event_context *parent_ctx,
6466 struct task_struct *child,
6467 struct perf_event_context *child_ctx)
6469 struct perf_event *leader;
6470 struct perf_event *sub;
6471 struct perf_event *child_ctr;
6473 leader = inherit_event(parent_event, parent, parent_ctx,
6474 child, NULL, child_ctx);
6476 return PTR_ERR(leader);
6477 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
6478 child_ctr = inherit_event(sub, parent, parent_ctx,
6479 child, leader, child_ctx);
6480 if (IS_ERR(child_ctr))
6481 return PTR_ERR(child_ctr);
6487 inherit_task_group(struct perf_event *event, struct task_struct *parent,
6488 struct perf_event_context *parent_ctx,
6489 struct task_struct *child, int ctxn,
6493 struct perf_event_context *child_ctx;
6495 if (!event->attr.inherit) {
6500 child_ctx = child->perf_event_ctxp[ctxn];
6503 * This is executed from the parent task context, so
6504 * inherit events that have been marked for cloning.
6505 * First allocate and initialize a context for the
6509 child_ctx = alloc_perf_context(event->pmu, child);
6513 child->perf_event_ctxp[ctxn] = child_ctx;
6516 ret = inherit_group(event, parent, parent_ctx,
6526 * Initialize the perf_event context in task_struct
6528 int perf_event_init_context(struct task_struct *child, int ctxn)
6530 struct perf_event_context *child_ctx, *parent_ctx;
6531 struct perf_event_context *cloned_ctx;
6532 struct perf_event *event;
6533 struct task_struct *parent = current;
6534 int inherited_all = 1;
6535 unsigned long flags;
6538 if (likely(!parent->perf_event_ctxp[ctxn]))
6542 * If the parent's context is a clone, pin it so it won't get
6545 parent_ctx = perf_pin_task_context(parent, ctxn);
6548 * No need to check if parent_ctx != NULL here; since we saw
6549 * it non-NULL earlier, the only reason for it to become NULL
6550 * is if we exit, and since we're currently in the middle of
6551 * a fork we can't be exiting at the same time.
6555 * Lock the parent list. No need to lock the child - not PID
6556 * hashed yet and not running, so nobody can access it.
6558 mutex_lock(&parent_ctx->mutex);
6561 * We dont have to disable NMIs - we are only looking at
6562 * the list, not manipulating it:
6564 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
6565 ret = inherit_task_group(event, parent, parent_ctx,
6566 child, ctxn, &inherited_all);
6572 * We can't hold ctx->lock when iterating the ->flexible_group list due
6573 * to allocations, but we need to prevent rotation because
6574 * rotate_ctx() will change the list from interrupt context.
6576 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
6577 parent_ctx->rotate_disable = 1;
6578 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
6580 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
6581 ret = inherit_task_group(event, parent, parent_ctx,
6582 child, ctxn, &inherited_all);
6587 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
6588 parent_ctx->rotate_disable = 0;
6590 child_ctx = child->perf_event_ctxp[ctxn];
6592 if (child_ctx && inherited_all) {
6594 * Mark the child context as a clone of the parent
6595 * context, or of whatever the parent is a clone of.
6597 * Note that if the parent is a clone, the holding of
6598 * parent_ctx->lock avoids it from being uncloned.
6600 cloned_ctx = parent_ctx->parent_ctx;
6602 child_ctx->parent_ctx = cloned_ctx;
6603 child_ctx->parent_gen = parent_ctx->parent_gen;
6605 child_ctx->parent_ctx = parent_ctx;
6606 child_ctx->parent_gen = parent_ctx->generation;
6608 get_ctx(child_ctx->parent_ctx);
6611 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
6612 mutex_unlock(&parent_ctx->mutex);
6614 perf_unpin_context(parent_ctx);
6615 put_ctx(parent_ctx);
6621 * Initialize the perf_event context in task_struct
6623 int perf_event_init_task(struct task_struct *child)
6627 memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
6628 mutex_init(&child->perf_event_mutex);
6629 INIT_LIST_HEAD(&child->perf_event_list);
6631 for_each_task_context_nr(ctxn) {
6632 ret = perf_event_init_context(child, ctxn);
6640 static void __init perf_event_init_all_cpus(void)
6642 struct swevent_htable *swhash;
6645 for_each_possible_cpu(cpu) {
6646 swhash = &per_cpu(swevent_htable, cpu);
6647 mutex_init(&swhash->hlist_mutex);
6648 INIT_LIST_HEAD(&per_cpu(rotation_list, cpu));
6652 static void __cpuinit perf_event_init_cpu(int cpu)
6654 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
6656 mutex_lock(&swhash->hlist_mutex);
6657 if (swhash->hlist_refcount > 0) {
6658 struct swevent_hlist *hlist;
6660 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
6662 rcu_assign_pointer(swhash->swevent_hlist, hlist);
6664 mutex_unlock(&swhash->hlist_mutex);
6667 #if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC
6668 static void perf_pmu_rotate_stop(struct pmu *pmu)
6670 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
6672 WARN_ON(!irqs_disabled());
6674 list_del_init(&cpuctx->rotation_list);
6677 static void __perf_event_exit_context(void *__info)
6679 struct perf_event_context *ctx = __info;
6680 struct perf_event *event, *tmp;
6682 perf_pmu_rotate_stop(ctx->pmu);
6684 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
6685 __perf_remove_from_context(event);
6686 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, group_entry)
6687 __perf_remove_from_context(event);
6690 static void perf_event_exit_cpu_context(int cpu)
6692 struct perf_event_context *ctx;
6696 idx = srcu_read_lock(&pmus_srcu);
6697 list_for_each_entry_rcu(pmu, &pmus, entry) {
6698 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
6700 mutex_lock(&ctx->mutex);
6701 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
6702 mutex_unlock(&ctx->mutex);
6704 srcu_read_unlock(&pmus_srcu, idx);
6707 static void perf_event_exit_cpu(int cpu)
6709 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
6711 mutex_lock(&swhash->hlist_mutex);
6712 swevent_hlist_release(swhash);
6713 mutex_unlock(&swhash->hlist_mutex);
6715 perf_event_exit_cpu_context(cpu);
6718 static inline void perf_event_exit_cpu(int cpu) { }
6722 perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
6726 for_each_online_cpu(cpu)
6727 perf_event_exit_cpu(cpu);
6733 * Run the perf reboot notifier at the very last possible moment so that
6734 * the generic watchdog code runs as long as possible.
6736 static struct notifier_block perf_reboot_notifier = {
6737 .notifier_call = perf_reboot,
6738 .priority = INT_MIN,
6741 static int __cpuinit
6742 perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
6744 unsigned int cpu = (long)hcpu;
6746 switch (action & ~CPU_TASKS_FROZEN) {
6748 case CPU_UP_PREPARE:
6749 case CPU_DOWN_FAILED:
6750 perf_event_init_cpu(cpu);
6753 case CPU_UP_CANCELED:
6754 case CPU_DOWN_PREPARE:
6755 perf_event_exit_cpu(cpu);
6765 void __init perf_event_init(void)
6771 perf_event_init_all_cpus();
6772 init_srcu_struct(&pmus_srcu);
6773 perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
6774 perf_pmu_register(&perf_cpu_clock, NULL, -1);
6775 perf_pmu_register(&perf_task_clock, NULL, -1);
6777 perf_cpu_notifier(perf_cpu_notify);
6778 register_reboot_notifier(&perf_reboot_notifier);
6780 ret = init_hw_breakpoint();
6781 WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
6784 static int __init perf_event_sysfs_init(void)
6789 mutex_lock(&pmus_lock);
6791 ret = bus_register(&pmu_bus);
6795 list_for_each_entry(pmu, &pmus, entry) {
6796 if (!pmu->name || pmu->type < 0)
6799 ret = pmu_dev_alloc(pmu);
6800 WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
6802 pmu_bus_running = 1;
6806 mutex_unlock(&pmus_lock);
6810 device_initcall(perf_event_sysfs_init);