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>
44 EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
47 atomic_t perf_task_events __read_mostly;
48 static atomic_t nr_mmap_events __read_mostly;
49 static atomic_t nr_comm_events __read_mostly;
50 static atomic_t nr_task_events __read_mostly;
52 static LIST_HEAD(pmus);
53 static DEFINE_MUTEX(pmus_lock);
54 static struct srcu_struct pmus_srcu;
57 * perf event paranoia level:
58 * -1 - not paranoid at all
59 * 0 - disallow raw tracepoint access for unpriv
60 * 1 - disallow cpu events for unpriv
61 * 2 - disallow kernel profiling for unpriv
63 int sysctl_perf_event_paranoid __read_mostly = 1;
65 int sysctl_perf_event_mlock __read_mostly = 512; /* 'free' kb per user */
68 * max perf event sample rate
70 int sysctl_perf_event_sample_rate __read_mostly = 100000;
72 static atomic64_t perf_event_id;
74 static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
75 enum event_type_t event_type);
77 static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
78 enum event_type_t event_type);
80 void __weak perf_event_print_debug(void) { }
82 extern __weak const char *perf_pmu_name(void)
87 static inline u64 perf_clock(void)
92 void perf_pmu_disable(struct pmu *pmu)
94 int *count = this_cpu_ptr(pmu->pmu_disable_count);
96 pmu->pmu_disable(pmu);
99 void perf_pmu_enable(struct pmu *pmu)
101 int *count = this_cpu_ptr(pmu->pmu_disable_count);
103 pmu->pmu_enable(pmu);
106 static DEFINE_PER_CPU(struct list_head, rotation_list);
109 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
110 * because they're strictly cpu affine and rotate_start is called with IRQs
111 * disabled, while rotate_context is called from IRQ context.
113 static void perf_pmu_rotate_start(struct pmu *pmu)
115 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
116 struct list_head *head = &__get_cpu_var(rotation_list);
118 WARN_ON(!irqs_disabled());
120 if (list_empty(&cpuctx->rotation_list))
121 list_add(&cpuctx->rotation_list, head);
124 static void get_ctx(struct perf_event_context *ctx)
126 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
129 static void free_ctx(struct rcu_head *head)
131 struct perf_event_context *ctx;
133 ctx = container_of(head, struct perf_event_context, rcu_head);
137 static void put_ctx(struct perf_event_context *ctx)
139 if (atomic_dec_and_test(&ctx->refcount)) {
141 put_ctx(ctx->parent_ctx);
143 put_task_struct(ctx->task);
144 call_rcu(&ctx->rcu_head, free_ctx);
148 static void unclone_ctx(struct perf_event_context *ctx)
150 if (ctx->parent_ctx) {
151 put_ctx(ctx->parent_ctx);
152 ctx->parent_ctx = NULL;
156 static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
159 * only top level events have the pid namespace they were created in
162 event = event->parent;
164 return task_tgid_nr_ns(p, event->ns);
167 static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
170 * only top level events have the pid namespace they were created in
173 event = event->parent;
175 return task_pid_nr_ns(p, event->ns);
179 * If we inherit events we want to return the parent event id
182 static u64 primary_event_id(struct perf_event *event)
187 id = event->parent->id;
193 * Get the perf_event_context for a task and lock it.
194 * This has to cope with with the fact that until it is locked,
195 * the context could get moved to another task.
197 static struct perf_event_context *
198 perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
200 struct perf_event_context *ctx;
204 ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
207 * If this context is a clone of another, it might
208 * get swapped for another underneath us by
209 * perf_event_task_sched_out, though the
210 * rcu_read_lock() protects us from any context
211 * getting freed. Lock the context and check if it
212 * got swapped before we could get the lock, and retry
213 * if so. If we locked the right context, then it
214 * can't get swapped on us any more.
216 raw_spin_lock_irqsave(&ctx->lock, *flags);
217 if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
218 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
222 if (!atomic_inc_not_zero(&ctx->refcount)) {
223 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
232 * Get the context for a task and increment its pin_count so it
233 * can't get swapped to another task. This also increments its
234 * reference count so that the context can't get freed.
236 static struct perf_event_context *
237 perf_pin_task_context(struct task_struct *task, int ctxn)
239 struct perf_event_context *ctx;
242 ctx = perf_lock_task_context(task, ctxn, &flags);
245 raw_spin_unlock_irqrestore(&ctx->lock, flags);
250 static void perf_unpin_context(struct perf_event_context *ctx)
254 raw_spin_lock_irqsave(&ctx->lock, flags);
256 raw_spin_unlock_irqrestore(&ctx->lock, flags);
261 * Update the record of the current time in a context.
263 static void update_context_time(struct perf_event_context *ctx)
265 u64 now = perf_clock();
267 ctx->time += now - ctx->timestamp;
268 ctx->timestamp = now;
271 static u64 perf_event_time(struct perf_event *event)
273 struct perf_event_context *ctx = event->ctx;
274 return ctx ? ctx->time : 0;
278 * Update the total_time_enabled and total_time_running fields for a event.
280 static void update_event_times(struct perf_event *event)
282 struct perf_event_context *ctx = event->ctx;
285 if (event->state < PERF_EVENT_STATE_INACTIVE ||
286 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
290 run_end = perf_event_time(event);
292 run_end = event->tstamp_stopped;
294 event->total_time_enabled = run_end - event->tstamp_enabled;
296 if (event->state == PERF_EVENT_STATE_INACTIVE)
297 run_end = event->tstamp_stopped;
299 run_end = perf_event_time(event);
301 event->total_time_running = run_end - event->tstamp_running;
305 * Update total_time_enabled and total_time_running for all events in a group.
307 static void update_group_times(struct perf_event *leader)
309 struct perf_event *event;
311 update_event_times(leader);
312 list_for_each_entry(event, &leader->sibling_list, group_entry)
313 update_event_times(event);
316 static struct list_head *
317 ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
319 if (event->attr.pinned)
320 return &ctx->pinned_groups;
322 return &ctx->flexible_groups;
326 * Add a event from the lists for its context.
327 * Must be called with ctx->mutex and ctx->lock held.
330 list_add_event(struct perf_event *event, struct perf_event_context *ctx)
332 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
333 event->attach_state |= PERF_ATTACH_CONTEXT;
336 * If we're a stand alone event or group leader, we go to the context
337 * list, group events are kept attached to the group so that
338 * perf_group_detach can, at all times, locate all siblings.
340 if (event->group_leader == event) {
341 struct list_head *list;
343 if (is_software_event(event))
344 event->group_flags |= PERF_GROUP_SOFTWARE;
346 list = ctx_group_list(event, ctx);
347 list_add_tail(&event->group_entry, list);
350 list_add_rcu(&event->event_entry, &ctx->event_list);
352 perf_pmu_rotate_start(ctx->pmu);
354 if (event->attr.inherit_stat)
359 * Called at perf_event creation and when events are attached/detached from a
362 static void perf_event__read_size(struct perf_event *event)
364 int entry = sizeof(u64); /* value */
368 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
371 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
374 if (event->attr.read_format & PERF_FORMAT_ID)
375 entry += sizeof(u64);
377 if (event->attr.read_format & PERF_FORMAT_GROUP) {
378 nr += event->group_leader->nr_siblings;
383 event->read_size = size;
386 static void perf_event__header_size(struct perf_event *event)
388 struct perf_sample_data *data;
389 u64 sample_type = event->attr.sample_type;
392 perf_event__read_size(event);
394 if (sample_type & PERF_SAMPLE_IP)
395 size += sizeof(data->ip);
397 if (sample_type & PERF_SAMPLE_ADDR)
398 size += sizeof(data->addr);
400 if (sample_type & PERF_SAMPLE_PERIOD)
401 size += sizeof(data->period);
403 if (sample_type & PERF_SAMPLE_READ)
404 size += event->read_size;
406 event->header_size = size;
409 static void perf_event__id_header_size(struct perf_event *event)
411 struct perf_sample_data *data;
412 u64 sample_type = event->attr.sample_type;
415 if (sample_type & PERF_SAMPLE_TID)
416 size += sizeof(data->tid_entry);
418 if (sample_type & PERF_SAMPLE_TIME)
419 size += sizeof(data->time);
421 if (sample_type & PERF_SAMPLE_ID)
422 size += sizeof(data->id);
424 if (sample_type & PERF_SAMPLE_STREAM_ID)
425 size += sizeof(data->stream_id);
427 if (sample_type & PERF_SAMPLE_CPU)
428 size += sizeof(data->cpu_entry);
430 event->id_header_size = size;
433 static void perf_group_attach(struct perf_event *event)
435 struct perf_event *group_leader = event->group_leader, *pos;
438 * We can have double attach due to group movement in perf_event_open.
440 if (event->attach_state & PERF_ATTACH_GROUP)
443 event->attach_state |= PERF_ATTACH_GROUP;
445 if (group_leader == event)
448 if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
449 !is_software_event(event))
450 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
452 list_add_tail(&event->group_entry, &group_leader->sibling_list);
453 group_leader->nr_siblings++;
455 perf_event__header_size(group_leader);
457 list_for_each_entry(pos, &group_leader->sibling_list, group_entry)
458 perf_event__header_size(pos);
462 * Remove a event from the lists for its context.
463 * Must be called with ctx->mutex and ctx->lock held.
466 list_del_event(struct perf_event *event, struct perf_event_context *ctx)
469 * We can have double detach due to exit/hot-unplug + close.
471 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
474 event->attach_state &= ~PERF_ATTACH_CONTEXT;
477 if (event->attr.inherit_stat)
480 list_del_rcu(&event->event_entry);
482 if (event->group_leader == event)
483 list_del_init(&event->group_entry);
485 update_group_times(event);
488 * If event was in error state, then keep it
489 * that way, otherwise bogus counts will be
490 * returned on read(). The only way to get out
491 * of error state is by explicit re-enabling
494 if (event->state > PERF_EVENT_STATE_OFF)
495 event->state = PERF_EVENT_STATE_OFF;
498 static void perf_group_detach(struct perf_event *event)
500 struct perf_event *sibling, *tmp;
501 struct list_head *list = NULL;
504 * We can have double detach due to exit/hot-unplug + close.
506 if (!(event->attach_state & PERF_ATTACH_GROUP))
509 event->attach_state &= ~PERF_ATTACH_GROUP;
512 * If this is a sibling, remove it from its group.
514 if (event->group_leader != event) {
515 list_del_init(&event->group_entry);
516 event->group_leader->nr_siblings--;
520 if (!list_empty(&event->group_entry))
521 list = &event->group_entry;
524 * If this was a group event with sibling events then
525 * upgrade the siblings to singleton events by adding them
526 * to whatever list we are on.
528 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
530 list_move_tail(&sibling->group_entry, list);
531 sibling->group_leader = sibling;
533 /* Inherit group flags from the previous leader */
534 sibling->group_flags = event->group_flags;
538 perf_event__header_size(event->group_leader);
540 list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry)
541 perf_event__header_size(tmp);
545 event_filter_match(struct perf_event *event)
547 return event->cpu == -1 || event->cpu == smp_processor_id();
551 event_sched_out(struct perf_event *event,
552 struct perf_cpu_context *cpuctx,
553 struct perf_event_context *ctx)
555 u64 tstamp = perf_event_time(event);
558 * An event which could not be activated because of
559 * filter mismatch still needs to have its timings
560 * maintained, otherwise bogus information is return
561 * via read() for time_enabled, time_running:
563 if (event->state == PERF_EVENT_STATE_INACTIVE
564 && !event_filter_match(event)) {
565 delta = ctx->time - event->tstamp_stopped;
566 event->tstamp_running += delta;
567 event->tstamp_stopped = tstamp;
570 if (event->state != PERF_EVENT_STATE_ACTIVE)
573 event->state = PERF_EVENT_STATE_INACTIVE;
574 if (event->pending_disable) {
575 event->pending_disable = 0;
576 event->state = PERF_EVENT_STATE_OFF;
578 event->tstamp_stopped = tstamp;
579 event->pmu->del(event, 0);
582 if (!is_software_event(event))
583 cpuctx->active_oncpu--;
585 if (event->attr.exclusive || !cpuctx->active_oncpu)
586 cpuctx->exclusive = 0;
590 group_sched_out(struct perf_event *group_event,
591 struct perf_cpu_context *cpuctx,
592 struct perf_event_context *ctx)
594 struct perf_event *event;
595 int state = group_event->state;
597 event_sched_out(group_event, cpuctx, ctx);
600 * Schedule out siblings (if any):
602 list_for_each_entry(event, &group_event->sibling_list, group_entry)
603 event_sched_out(event, cpuctx, ctx);
605 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
606 cpuctx->exclusive = 0;
609 static inline struct perf_cpu_context *
610 __get_cpu_context(struct perf_event_context *ctx)
612 return this_cpu_ptr(ctx->pmu->pmu_cpu_context);
616 * Cross CPU call to remove a performance event
618 * We disable the event on the hardware level first. After that we
619 * remove it from the context list.
621 static void __perf_event_remove_from_context(void *info)
623 struct perf_event *event = info;
624 struct perf_event_context *ctx = event->ctx;
625 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
628 * If this is a task context, we need to check whether it is
629 * the current task context of this cpu. If not it has been
630 * scheduled out before the smp call arrived.
632 if (ctx->task && cpuctx->task_ctx != ctx)
635 raw_spin_lock(&ctx->lock);
637 event_sched_out(event, cpuctx, ctx);
639 list_del_event(event, ctx);
641 raw_spin_unlock(&ctx->lock);
646 * Remove the event from a task's (or a CPU's) list of events.
648 * Must be called with ctx->mutex held.
650 * CPU events are removed with a smp call. For task events we only
651 * call when the task is on a CPU.
653 * If event->ctx is a cloned context, callers must make sure that
654 * every task struct that event->ctx->task could possibly point to
655 * remains valid. This is OK when called from perf_release since
656 * that only calls us on the top-level context, which can't be a clone.
657 * When called from perf_event_exit_task, it's OK because the
658 * context has been detached from its task.
660 static void perf_event_remove_from_context(struct perf_event *event)
662 struct perf_event_context *ctx = event->ctx;
663 struct task_struct *task = ctx->task;
667 * Per cpu events are removed via an smp call and
668 * the removal is always successful.
670 smp_call_function_single(event->cpu,
671 __perf_event_remove_from_context,
677 task_oncpu_function_call(task, __perf_event_remove_from_context,
680 raw_spin_lock_irq(&ctx->lock);
682 * If the context is active we need to retry the smp call.
684 if (ctx->nr_active && !list_empty(&event->group_entry)) {
685 raw_spin_unlock_irq(&ctx->lock);
690 * The lock prevents that this context is scheduled in so we
691 * can remove the event safely, if the call above did not
694 if (!list_empty(&event->group_entry))
695 list_del_event(event, ctx);
696 raw_spin_unlock_irq(&ctx->lock);
700 * Cross CPU call to disable a performance event
702 static void __perf_event_disable(void *info)
704 struct perf_event *event = info;
705 struct perf_event_context *ctx = event->ctx;
706 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
709 * If this is a per-task event, need to check whether this
710 * event's task is the current task on this cpu.
712 if (ctx->task && cpuctx->task_ctx != ctx)
715 raw_spin_lock(&ctx->lock);
718 * If the event is on, turn it off.
719 * If it is in error state, leave it in error state.
721 if (event->state >= PERF_EVENT_STATE_INACTIVE) {
722 update_context_time(ctx);
723 update_group_times(event);
724 if (event == event->group_leader)
725 group_sched_out(event, cpuctx, ctx);
727 event_sched_out(event, cpuctx, ctx);
728 event->state = PERF_EVENT_STATE_OFF;
731 raw_spin_unlock(&ctx->lock);
737 * If event->ctx is a cloned context, callers must make sure that
738 * every task struct that event->ctx->task could possibly point to
739 * remains valid. This condition is satisifed when called through
740 * perf_event_for_each_child or perf_event_for_each because they
741 * hold the top-level event's child_mutex, so any descendant that
742 * goes to exit will block in sync_child_event.
743 * When called from perf_pending_event it's OK because event->ctx
744 * is the current context on this CPU and preemption is disabled,
745 * hence we can't get into perf_event_task_sched_out for this context.
747 void perf_event_disable(struct perf_event *event)
749 struct perf_event_context *ctx = event->ctx;
750 struct task_struct *task = ctx->task;
754 * Disable the event on the cpu that it's on
756 smp_call_function_single(event->cpu, __perf_event_disable,
762 task_oncpu_function_call(task, __perf_event_disable, event);
764 raw_spin_lock_irq(&ctx->lock);
766 * If the event is still active, we need to retry the cross-call.
768 if (event->state == PERF_EVENT_STATE_ACTIVE) {
769 raw_spin_unlock_irq(&ctx->lock);
774 * Since we have the lock this context can't be scheduled
775 * in, so we can change the state safely.
777 if (event->state == PERF_EVENT_STATE_INACTIVE) {
778 update_group_times(event);
779 event->state = PERF_EVENT_STATE_OFF;
782 raw_spin_unlock_irq(&ctx->lock);
785 #define MAX_INTERRUPTS (~0ULL)
787 static void perf_log_throttle(struct perf_event *event, int enable);
790 event_sched_in(struct perf_event *event,
791 struct perf_cpu_context *cpuctx,
792 struct perf_event_context *ctx)
794 u64 tstamp = perf_event_time(event);
796 if (event->state <= PERF_EVENT_STATE_OFF)
799 event->state = PERF_EVENT_STATE_ACTIVE;
800 event->oncpu = smp_processor_id();
803 * Unthrottle events, since we scheduled we might have missed several
804 * ticks already, also for a heavily scheduling task there is little
805 * guarantee it'll get a tick in a timely manner.
807 if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
808 perf_log_throttle(event, 1);
809 event->hw.interrupts = 0;
813 * The new state must be visible before we turn it on in the hardware:
817 if (event->pmu->add(event, PERF_EF_START)) {
818 event->state = PERF_EVENT_STATE_INACTIVE;
823 event->tstamp_running += tstamp - event->tstamp_stopped;
825 event->shadow_ctx_time = tstamp - ctx->timestamp;
827 if (!is_software_event(event))
828 cpuctx->active_oncpu++;
831 if (event->attr.exclusive)
832 cpuctx->exclusive = 1;
838 group_sched_in(struct perf_event *group_event,
839 struct perf_cpu_context *cpuctx,
840 struct perf_event_context *ctx)
842 struct perf_event *event, *partial_group = NULL;
843 struct pmu *pmu = group_event->pmu;
845 bool simulate = false;
847 if (group_event->state == PERF_EVENT_STATE_OFF)
852 if (event_sched_in(group_event, cpuctx, ctx)) {
853 pmu->cancel_txn(pmu);
858 * Schedule in siblings as one group (if any):
860 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
861 if (event_sched_in(event, cpuctx, ctx)) {
862 partial_group = event;
867 if (!pmu->commit_txn(pmu))
872 * Groups can be scheduled in as one unit only, so undo any
873 * partial group before returning:
874 * The events up to the failed event are scheduled out normally,
875 * tstamp_stopped will be updated.
877 * The failed events and the remaining siblings need to have
878 * their timings updated as if they had gone thru event_sched_in()
879 * and event_sched_out(). This is required to get consistent timings
880 * across the group. This also takes care of the case where the group
881 * could never be scheduled by ensuring tstamp_stopped is set to mark
882 * the time the event was actually stopped, such that time delta
883 * calculation in update_event_times() is correct.
885 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
886 if (event == partial_group)
890 event->tstamp_running += now - event->tstamp_stopped;
891 event->tstamp_stopped = now;
893 event_sched_out(event, cpuctx, ctx);
896 event_sched_out(group_event, cpuctx, ctx);
898 pmu->cancel_txn(pmu);
904 * Work out whether we can put this event group on the CPU now.
906 static int group_can_go_on(struct perf_event *event,
907 struct perf_cpu_context *cpuctx,
911 * Groups consisting entirely of software events can always go on.
913 if (event->group_flags & PERF_GROUP_SOFTWARE)
916 * If an exclusive group is already on, no other hardware
919 if (cpuctx->exclusive)
922 * If this group is exclusive and there are already
923 * events on the CPU, it can't go on.
925 if (event->attr.exclusive && cpuctx->active_oncpu)
928 * Otherwise, try to add it if all previous groups were able
934 static void add_event_to_ctx(struct perf_event *event,
935 struct perf_event_context *ctx)
937 u64 tstamp = perf_event_time(event);
939 list_add_event(event, ctx);
940 perf_group_attach(event);
941 event->tstamp_enabled = tstamp;
942 event->tstamp_running = tstamp;
943 event->tstamp_stopped = tstamp;
947 * Cross CPU call to install and enable a performance event
949 * Must be called with ctx->mutex held
951 static void __perf_install_in_context(void *info)
953 struct perf_event *event = info;
954 struct perf_event_context *ctx = event->ctx;
955 struct perf_event *leader = event->group_leader;
956 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
960 * If this is a task context, we need to check whether it is
961 * the current task context of this cpu. If not it has been
962 * scheduled out before the smp call arrived.
963 * Or possibly this is the right context but it isn't
964 * on this cpu because it had no events.
966 if (ctx->task && cpuctx->task_ctx != ctx) {
967 if (cpuctx->task_ctx || ctx->task != current)
969 cpuctx->task_ctx = ctx;
972 raw_spin_lock(&ctx->lock);
974 update_context_time(ctx);
976 add_event_to_ctx(event, ctx);
978 if (!event_filter_match(event))
982 * Don't put the event on if it is disabled or if
983 * it is in a group and the group isn't on.
985 if (event->state != PERF_EVENT_STATE_INACTIVE ||
986 (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE))
990 * An exclusive event can't go on if there are already active
991 * hardware events, and no hardware event can go on if there
992 * is already an exclusive event on.
994 if (!group_can_go_on(event, cpuctx, 1))
997 err = event_sched_in(event, cpuctx, ctx);
1001 * This event couldn't go on. If it is in a group
1002 * then we have to pull the whole group off.
1003 * If the event group is pinned then put it in error state.
1005 if (leader != event)
1006 group_sched_out(leader, cpuctx, ctx);
1007 if (leader->attr.pinned) {
1008 update_group_times(leader);
1009 leader->state = PERF_EVENT_STATE_ERROR;
1014 raw_spin_unlock(&ctx->lock);
1018 * Attach a performance event to a context
1020 * First we add the event to the list with the hardware enable bit
1021 * in event->hw_config cleared.
1023 * If the event is attached to a task which is on a CPU we use a smp
1024 * call to enable it in the task context. The task might have been
1025 * scheduled away, but we check this in the smp call again.
1027 * Must be called with ctx->mutex held.
1030 perf_install_in_context(struct perf_event_context *ctx,
1031 struct perf_event *event,
1034 struct task_struct *task = ctx->task;
1040 * Per cpu events are installed via an smp call and
1041 * the install is always successful.
1043 smp_call_function_single(cpu, __perf_install_in_context,
1049 task_oncpu_function_call(task, __perf_install_in_context,
1052 raw_spin_lock_irq(&ctx->lock);
1054 * we need to retry the smp call.
1056 if (ctx->is_active && list_empty(&event->group_entry)) {
1057 raw_spin_unlock_irq(&ctx->lock);
1062 * The lock prevents that this context is scheduled in so we
1063 * can add the event safely, if it the call above did not
1066 if (list_empty(&event->group_entry))
1067 add_event_to_ctx(event, ctx);
1068 raw_spin_unlock_irq(&ctx->lock);
1072 * Put a event into inactive state and update time fields.
1073 * Enabling the leader of a group effectively enables all
1074 * the group members that aren't explicitly disabled, so we
1075 * have to update their ->tstamp_enabled also.
1076 * Note: this works for group members as well as group leaders
1077 * since the non-leader members' sibling_lists will be empty.
1079 static void __perf_event_mark_enabled(struct perf_event *event,
1080 struct perf_event_context *ctx)
1082 struct perf_event *sub;
1083 u64 tstamp = perf_event_time(event);
1085 event->state = PERF_EVENT_STATE_INACTIVE;
1086 event->tstamp_enabled = tstamp - event->total_time_enabled;
1087 list_for_each_entry(sub, &event->sibling_list, group_entry) {
1088 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
1089 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
1094 * Cross CPU call to enable a performance event
1096 static void __perf_event_enable(void *info)
1098 struct perf_event *event = info;
1099 struct perf_event_context *ctx = event->ctx;
1100 struct perf_event *leader = event->group_leader;
1101 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1105 * If this is a per-task event, need to check whether this
1106 * event's task is the current task on this cpu.
1108 if (ctx->task && cpuctx->task_ctx != ctx) {
1109 if (cpuctx->task_ctx || ctx->task != current)
1111 cpuctx->task_ctx = ctx;
1114 raw_spin_lock(&ctx->lock);
1116 update_context_time(ctx);
1118 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1120 __perf_event_mark_enabled(event, ctx);
1122 if (!event_filter_match(event))
1126 * If the event is in a group and isn't the group leader,
1127 * then don't put it on unless the group is on.
1129 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
1132 if (!group_can_go_on(event, cpuctx, 1)) {
1135 if (event == leader)
1136 err = group_sched_in(event, cpuctx, ctx);
1138 err = event_sched_in(event, cpuctx, ctx);
1143 * If this event can't go on and it's part of a
1144 * group, then the whole group has to come off.
1146 if (leader != event)
1147 group_sched_out(leader, cpuctx, ctx);
1148 if (leader->attr.pinned) {
1149 update_group_times(leader);
1150 leader->state = PERF_EVENT_STATE_ERROR;
1155 raw_spin_unlock(&ctx->lock);
1161 * If event->ctx is a cloned context, callers must make sure that
1162 * every task struct that event->ctx->task could possibly point to
1163 * remains valid. This condition is satisfied when called through
1164 * perf_event_for_each_child or perf_event_for_each as described
1165 * for perf_event_disable.
1167 void perf_event_enable(struct perf_event *event)
1169 struct perf_event_context *ctx = event->ctx;
1170 struct task_struct *task = ctx->task;
1174 * Enable the event on the cpu that it's on
1176 smp_call_function_single(event->cpu, __perf_event_enable,
1181 raw_spin_lock_irq(&ctx->lock);
1182 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1186 * If the event is in error state, clear that first.
1187 * That way, if we see the event in error state below, we
1188 * know that it has gone back into error state, as distinct
1189 * from the task having been scheduled away before the
1190 * cross-call arrived.
1192 if (event->state == PERF_EVENT_STATE_ERROR)
1193 event->state = PERF_EVENT_STATE_OFF;
1196 raw_spin_unlock_irq(&ctx->lock);
1197 task_oncpu_function_call(task, __perf_event_enable, event);
1199 raw_spin_lock_irq(&ctx->lock);
1202 * If the context is active and the event is still off,
1203 * we need to retry the cross-call.
1205 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF)
1209 * Since we have the lock this context can't be scheduled
1210 * in, so we can change the state safely.
1212 if (event->state == PERF_EVENT_STATE_OFF)
1213 __perf_event_mark_enabled(event, ctx);
1216 raw_spin_unlock_irq(&ctx->lock);
1219 static int perf_event_refresh(struct perf_event *event, int refresh)
1222 * not supported on inherited events
1224 if (event->attr.inherit || !is_sampling_event(event))
1227 atomic_add(refresh, &event->event_limit);
1228 perf_event_enable(event);
1233 static void ctx_sched_out(struct perf_event_context *ctx,
1234 struct perf_cpu_context *cpuctx,
1235 enum event_type_t event_type)
1237 struct perf_event *event;
1239 raw_spin_lock(&ctx->lock);
1240 perf_pmu_disable(ctx->pmu);
1242 if (likely(!ctx->nr_events))
1244 update_context_time(ctx);
1246 if (!ctx->nr_active)
1249 if (event_type & EVENT_PINNED) {
1250 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
1251 group_sched_out(event, cpuctx, ctx);
1254 if (event_type & EVENT_FLEXIBLE) {
1255 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
1256 group_sched_out(event, cpuctx, ctx);
1259 perf_pmu_enable(ctx->pmu);
1260 raw_spin_unlock(&ctx->lock);
1264 * Test whether two contexts are equivalent, i.e. whether they
1265 * have both been cloned from the same version of the same context
1266 * and they both have the same number of enabled events.
1267 * If the number of enabled events is the same, then the set
1268 * of enabled events should be the same, because these are both
1269 * inherited contexts, therefore we can't access individual events
1270 * in them directly with an fd; we can only enable/disable all
1271 * events via prctl, or enable/disable all events in a family
1272 * via ioctl, which will have the same effect on both contexts.
1274 static int context_equiv(struct perf_event_context *ctx1,
1275 struct perf_event_context *ctx2)
1277 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
1278 && ctx1->parent_gen == ctx2->parent_gen
1279 && !ctx1->pin_count && !ctx2->pin_count;
1282 static void __perf_event_sync_stat(struct perf_event *event,
1283 struct perf_event *next_event)
1287 if (!event->attr.inherit_stat)
1291 * Update the event value, we cannot use perf_event_read()
1292 * because we're in the middle of a context switch and have IRQs
1293 * disabled, which upsets smp_call_function_single(), however
1294 * we know the event must be on the current CPU, therefore we
1295 * don't need to use it.
1297 switch (event->state) {
1298 case PERF_EVENT_STATE_ACTIVE:
1299 event->pmu->read(event);
1302 case PERF_EVENT_STATE_INACTIVE:
1303 update_event_times(event);
1311 * In order to keep per-task stats reliable we need to flip the event
1312 * values when we flip the contexts.
1314 value = local64_read(&next_event->count);
1315 value = local64_xchg(&event->count, value);
1316 local64_set(&next_event->count, value);
1318 swap(event->total_time_enabled, next_event->total_time_enabled);
1319 swap(event->total_time_running, next_event->total_time_running);
1322 * Since we swizzled the values, update the user visible data too.
1324 perf_event_update_userpage(event);
1325 perf_event_update_userpage(next_event);
1328 #define list_next_entry(pos, member) \
1329 list_entry(pos->member.next, typeof(*pos), member)
1331 static void perf_event_sync_stat(struct perf_event_context *ctx,
1332 struct perf_event_context *next_ctx)
1334 struct perf_event *event, *next_event;
1339 update_context_time(ctx);
1341 event = list_first_entry(&ctx->event_list,
1342 struct perf_event, event_entry);
1344 next_event = list_first_entry(&next_ctx->event_list,
1345 struct perf_event, event_entry);
1347 while (&event->event_entry != &ctx->event_list &&
1348 &next_event->event_entry != &next_ctx->event_list) {
1350 __perf_event_sync_stat(event, next_event);
1352 event = list_next_entry(event, event_entry);
1353 next_event = list_next_entry(next_event, event_entry);
1357 void perf_event_context_sched_out(struct task_struct *task, int ctxn,
1358 struct task_struct *next)
1360 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
1361 struct perf_event_context *next_ctx;
1362 struct perf_event_context *parent;
1363 struct perf_cpu_context *cpuctx;
1369 cpuctx = __get_cpu_context(ctx);
1370 if (!cpuctx->task_ctx)
1374 parent = rcu_dereference(ctx->parent_ctx);
1375 next_ctx = next->perf_event_ctxp[ctxn];
1376 if (parent && next_ctx &&
1377 rcu_dereference(next_ctx->parent_ctx) == parent) {
1379 * Looks like the two contexts are clones, so we might be
1380 * able to optimize the context switch. We lock both
1381 * contexts and check that they are clones under the
1382 * lock (including re-checking that neither has been
1383 * uncloned in the meantime). It doesn't matter which
1384 * order we take the locks because no other cpu could
1385 * be trying to lock both of these tasks.
1387 raw_spin_lock(&ctx->lock);
1388 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
1389 if (context_equiv(ctx, next_ctx)) {
1391 * XXX do we need a memory barrier of sorts
1392 * wrt to rcu_dereference() of perf_event_ctxp
1394 task->perf_event_ctxp[ctxn] = next_ctx;
1395 next->perf_event_ctxp[ctxn] = ctx;
1397 next_ctx->task = task;
1400 perf_event_sync_stat(ctx, next_ctx);
1402 raw_spin_unlock(&next_ctx->lock);
1403 raw_spin_unlock(&ctx->lock);
1408 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
1409 cpuctx->task_ctx = NULL;
1413 #define for_each_task_context_nr(ctxn) \
1414 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
1417 * Called from scheduler to remove the events of the current task,
1418 * with interrupts disabled.
1420 * We stop each event and update the event value in event->count.
1422 * This does not protect us against NMI, but disable()
1423 * sets the disabled bit in the control field of event _before_
1424 * accessing the event control register. If a NMI hits, then it will
1425 * not restart the event.
1427 void __perf_event_task_sched_out(struct task_struct *task,
1428 struct task_struct *next)
1432 for_each_task_context_nr(ctxn)
1433 perf_event_context_sched_out(task, ctxn, next);
1436 static void task_ctx_sched_out(struct perf_event_context *ctx,
1437 enum event_type_t event_type)
1439 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1441 if (!cpuctx->task_ctx)
1444 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
1447 ctx_sched_out(ctx, cpuctx, event_type);
1448 cpuctx->task_ctx = NULL;
1452 * Called with IRQs disabled
1454 static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
1455 enum event_type_t event_type)
1457 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
1461 ctx_pinned_sched_in(struct perf_event_context *ctx,
1462 struct perf_cpu_context *cpuctx)
1464 struct perf_event *event;
1466 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
1467 if (event->state <= PERF_EVENT_STATE_OFF)
1469 if (!event_filter_match(event))
1472 if (group_can_go_on(event, cpuctx, 1))
1473 group_sched_in(event, cpuctx, ctx);
1476 * If this pinned group hasn't been scheduled,
1477 * put it in error state.
1479 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1480 update_group_times(event);
1481 event->state = PERF_EVENT_STATE_ERROR;
1487 ctx_flexible_sched_in(struct perf_event_context *ctx,
1488 struct perf_cpu_context *cpuctx)
1490 struct perf_event *event;
1493 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
1494 /* Ignore events in OFF or ERROR state */
1495 if (event->state <= PERF_EVENT_STATE_OFF)
1498 * Listen to the 'cpu' scheduling filter constraint
1501 if (!event_filter_match(event))
1504 if (group_can_go_on(event, cpuctx, can_add_hw)) {
1505 if (group_sched_in(event, cpuctx, ctx))
1512 ctx_sched_in(struct perf_event_context *ctx,
1513 struct perf_cpu_context *cpuctx,
1514 enum event_type_t event_type)
1516 raw_spin_lock(&ctx->lock);
1518 if (likely(!ctx->nr_events))
1521 ctx->timestamp = perf_clock();
1524 * First go through the list and put on any pinned groups
1525 * in order to give them the best chance of going on.
1527 if (event_type & EVENT_PINNED)
1528 ctx_pinned_sched_in(ctx, cpuctx);
1530 /* Then walk through the lower prio flexible groups */
1531 if (event_type & EVENT_FLEXIBLE)
1532 ctx_flexible_sched_in(ctx, cpuctx);
1535 raw_spin_unlock(&ctx->lock);
1538 static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
1539 enum event_type_t event_type)
1541 struct perf_event_context *ctx = &cpuctx->ctx;
1543 ctx_sched_in(ctx, cpuctx, event_type);
1546 static void task_ctx_sched_in(struct perf_event_context *ctx,
1547 enum event_type_t event_type)
1549 struct perf_cpu_context *cpuctx;
1551 cpuctx = __get_cpu_context(ctx);
1552 if (cpuctx->task_ctx == ctx)
1555 ctx_sched_in(ctx, cpuctx, event_type);
1556 cpuctx->task_ctx = ctx;
1559 void perf_event_context_sched_in(struct perf_event_context *ctx)
1561 struct perf_cpu_context *cpuctx;
1563 cpuctx = __get_cpu_context(ctx);
1564 if (cpuctx->task_ctx == ctx)
1567 perf_pmu_disable(ctx->pmu);
1569 * We want to keep the following priority order:
1570 * cpu pinned (that don't need to move), task pinned,
1571 * cpu flexible, task flexible.
1573 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
1575 ctx_sched_in(ctx, cpuctx, EVENT_PINNED);
1576 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE);
1577 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE);
1579 cpuctx->task_ctx = ctx;
1582 * Since these rotations are per-cpu, we need to ensure the
1583 * cpu-context we got scheduled on is actually rotating.
1585 perf_pmu_rotate_start(ctx->pmu);
1586 perf_pmu_enable(ctx->pmu);
1590 * Called from scheduler to add the events of the current task
1591 * with interrupts disabled.
1593 * We restore the event value and then enable it.
1595 * This does not protect us against NMI, but enable()
1596 * sets the enabled bit in the control field of event _before_
1597 * accessing the event control register. If a NMI hits, then it will
1598 * keep the event running.
1600 void __perf_event_task_sched_in(struct task_struct *task)
1602 struct perf_event_context *ctx;
1605 for_each_task_context_nr(ctxn) {
1606 ctx = task->perf_event_ctxp[ctxn];
1610 perf_event_context_sched_in(ctx);
1614 static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
1616 u64 frequency = event->attr.sample_freq;
1617 u64 sec = NSEC_PER_SEC;
1618 u64 divisor, dividend;
1620 int count_fls, nsec_fls, frequency_fls, sec_fls;
1622 count_fls = fls64(count);
1623 nsec_fls = fls64(nsec);
1624 frequency_fls = fls64(frequency);
1628 * We got @count in @nsec, with a target of sample_freq HZ
1629 * the target period becomes:
1632 * period = -------------------
1633 * @nsec * sample_freq
1638 * Reduce accuracy by one bit such that @a and @b converge
1639 * to a similar magnitude.
1641 #define REDUCE_FLS(a, b) \
1643 if (a##_fls > b##_fls) { \
1653 * Reduce accuracy until either term fits in a u64, then proceed with
1654 * the other, so that finally we can do a u64/u64 division.
1656 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
1657 REDUCE_FLS(nsec, frequency);
1658 REDUCE_FLS(sec, count);
1661 if (count_fls + sec_fls > 64) {
1662 divisor = nsec * frequency;
1664 while (count_fls + sec_fls > 64) {
1665 REDUCE_FLS(count, sec);
1669 dividend = count * sec;
1671 dividend = count * sec;
1673 while (nsec_fls + frequency_fls > 64) {
1674 REDUCE_FLS(nsec, frequency);
1678 divisor = nsec * frequency;
1684 return div64_u64(dividend, divisor);
1687 static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count)
1689 struct hw_perf_event *hwc = &event->hw;
1690 s64 period, sample_period;
1693 period = perf_calculate_period(event, nsec, count);
1695 delta = (s64)(period - hwc->sample_period);
1696 delta = (delta + 7) / 8; /* low pass filter */
1698 sample_period = hwc->sample_period + delta;
1703 hwc->sample_period = sample_period;
1705 if (local64_read(&hwc->period_left) > 8*sample_period) {
1706 event->pmu->stop(event, PERF_EF_UPDATE);
1707 local64_set(&hwc->period_left, 0);
1708 event->pmu->start(event, PERF_EF_RELOAD);
1712 static void perf_ctx_adjust_freq(struct perf_event_context *ctx, u64 period)
1714 struct perf_event *event;
1715 struct hw_perf_event *hwc;
1716 u64 interrupts, now;
1719 raw_spin_lock(&ctx->lock);
1720 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
1721 if (event->state != PERF_EVENT_STATE_ACTIVE)
1724 if (!event_filter_match(event))
1729 interrupts = hwc->interrupts;
1730 hwc->interrupts = 0;
1733 * unthrottle events on the tick
1735 if (interrupts == MAX_INTERRUPTS) {
1736 perf_log_throttle(event, 1);
1737 event->pmu->start(event, 0);
1740 if (!event->attr.freq || !event->attr.sample_freq)
1743 event->pmu->read(event);
1744 now = local64_read(&event->count);
1745 delta = now - hwc->freq_count_stamp;
1746 hwc->freq_count_stamp = now;
1749 perf_adjust_period(event, period, delta);
1751 raw_spin_unlock(&ctx->lock);
1755 * Round-robin a context's events:
1757 static void rotate_ctx(struct perf_event_context *ctx)
1759 raw_spin_lock(&ctx->lock);
1762 * Rotate the first entry last of non-pinned groups. Rotation might be
1763 * disabled by the inheritance code.
1765 if (!ctx->rotate_disable)
1766 list_rotate_left(&ctx->flexible_groups);
1768 raw_spin_unlock(&ctx->lock);
1772 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
1773 * because they're strictly cpu affine and rotate_start is called with IRQs
1774 * disabled, while rotate_context is called from IRQ context.
1776 static void perf_rotate_context(struct perf_cpu_context *cpuctx)
1778 u64 interval = (u64)cpuctx->jiffies_interval * TICK_NSEC;
1779 struct perf_event_context *ctx = NULL;
1780 int rotate = 0, remove = 1;
1782 if (cpuctx->ctx.nr_events) {
1784 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
1788 ctx = cpuctx->task_ctx;
1789 if (ctx && ctx->nr_events) {
1791 if (ctx->nr_events != ctx->nr_active)
1795 perf_pmu_disable(cpuctx->ctx.pmu);
1796 perf_ctx_adjust_freq(&cpuctx->ctx, interval);
1798 perf_ctx_adjust_freq(ctx, interval);
1803 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
1805 task_ctx_sched_out(ctx, EVENT_FLEXIBLE);
1807 rotate_ctx(&cpuctx->ctx);
1811 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE);
1813 task_ctx_sched_in(ctx, EVENT_FLEXIBLE);
1817 list_del_init(&cpuctx->rotation_list);
1819 perf_pmu_enable(cpuctx->ctx.pmu);
1822 void perf_event_task_tick(void)
1824 struct list_head *head = &__get_cpu_var(rotation_list);
1825 struct perf_cpu_context *cpuctx, *tmp;
1827 WARN_ON(!irqs_disabled());
1829 list_for_each_entry_safe(cpuctx, tmp, head, rotation_list) {
1830 if (cpuctx->jiffies_interval == 1 ||
1831 !(jiffies % cpuctx->jiffies_interval))
1832 perf_rotate_context(cpuctx);
1836 static int event_enable_on_exec(struct perf_event *event,
1837 struct perf_event_context *ctx)
1839 if (!event->attr.enable_on_exec)
1842 event->attr.enable_on_exec = 0;
1843 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1846 __perf_event_mark_enabled(event, ctx);
1852 * Enable all of a task's events that have been marked enable-on-exec.
1853 * This expects task == current.
1855 static void perf_event_enable_on_exec(struct perf_event_context *ctx)
1857 struct perf_event *event;
1858 unsigned long flags;
1862 local_irq_save(flags);
1863 if (!ctx || !ctx->nr_events)
1866 task_ctx_sched_out(ctx, EVENT_ALL);
1868 raw_spin_lock(&ctx->lock);
1870 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
1871 ret = event_enable_on_exec(event, ctx);
1876 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
1877 ret = event_enable_on_exec(event, ctx);
1883 * Unclone this context if we enabled any event.
1888 raw_spin_unlock(&ctx->lock);
1890 perf_event_context_sched_in(ctx);
1892 local_irq_restore(flags);
1896 * Cross CPU call to read the hardware event
1898 static void __perf_event_read(void *info)
1900 struct perf_event *event = info;
1901 struct perf_event_context *ctx = event->ctx;
1902 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1905 * If this is a task context, we need to check whether it is
1906 * the current task context of this cpu. If not it has been
1907 * scheduled out before the smp call arrived. In that case
1908 * event->count would have been updated to a recent sample
1909 * when the event was scheduled out.
1911 if (ctx->task && cpuctx->task_ctx != ctx)
1914 raw_spin_lock(&ctx->lock);
1916 update_context_time(ctx);
1917 update_event_times(event);
1918 if (event->state == PERF_EVENT_STATE_ACTIVE)
1919 event->pmu->read(event);
1920 raw_spin_unlock(&ctx->lock);
1923 static inline u64 perf_event_count(struct perf_event *event)
1925 return local64_read(&event->count) + atomic64_read(&event->child_count);
1928 static u64 perf_event_read(struct perf_event *event)
1931 * If event is enabled and currently active on a CPU, update the
1932 * value in the event structure:
1934 if (event->state == PERF_EVENT_STATE_ACTIVE) {
1935 smp_call_function_single(event->oncpu,
1936 __perf_event_read, event, 1);
1937 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
1938 struct perf_event_context *ctx = event->ctx;
1939 unsigned long flags;
1941 raw_spin_lock_irqsave(&ctx->lock, flags);
1943 * may read while context is not active
1944 * (e.g., thread is blocked), in that case
1945 * we cannot update context time
1948 update_context_time(ctx);
1949 update_event_times(event);
1950 raw_spin_unlock_irqrestore(&ctx->lock, flags);
1953 return perf_event_count(event);
1960 struct callchain_cpus_entries {
1961 struct rcu_head rcu_head;
1962 struct perf_callchain_entry *cpu_entries[0];
1965 static DEFINE_PER_CPU(int, callchain_recursion[PERF_NR_CONTEXTS]);
1966 static atomic_t nr_callchain_events;
1967 static DEFINE_MUTEX(callchain_mutex);
1968 struct callchain_cpus_entries *callchain_cpus_entries;
1971 __weak void perf_callchain_kernel(struct perf_callchain_entry *entry,
1972 struct pt_regs *regs)
1976 __weak void perf_callchain_user(struct perf_callchain_entry *entry,
1977 struct pt_regs *regs)
1981 static void release_callchain_buffers_rcu(struct rcu_head *head)
1983 struct callchain_cpus_entries *entries;
1986 entries = container_of(head, struct callchain_cpus_entries, rcu_head);
1988 for_each_possible_cpu(cpu)
1989 kfree(entries->cpu_entries[cpu]);
1994 static void release_callchain_buffers(void)
1996 struct callchain_cpus_entries *entries;
1998 entries = callchain_cpus_entries;
1999 rcu_assign_pointer(callchain_cpus_entries, NULL);
2000 call_rcu(&entries->rcu_head, release_callchain_buffers_rcu);
2003 static int alloc_callchain_buffers(void)
2007 struct callchain_cpus_entries *entries;
2010 * We can't use the percpu allocation API for data that can be
2011 * accessed from NMI. Use a temporary manual per cpu allocation
2012 * until that gets sorted out.
2014 size = offsetof(struct callchain_cpus_entries, cpu_entries[nr_cpu_ids]);
2016 entries = kzalloc(size, GFP_KERNEL);
2020 size = sizeof(struct perf_callchain_entry) * PERF_NR_CONTEXTS;
2022 for_each_possible_cpu(cpu) {
2023 entries->cpu_entries[cpu] = kmalloc_node(size, GFP_KERNEL,
2025 if (!entries->cpu_entries[cpu])
2029 rcu_assign_pointer(callchain_cpus_entries, entries);
2034 for_each_possible_cpu(cpu)
2035 kfree(entries->cpu_entries[cpu]);
2041 static int get_callchain_buffers(void)
2046 mutex_lock(&callchain_mutex);
2048 count = atomic_inc_return(&nr_callchain_events);
2049 if (WARN_ON_ONCE(count < 1)) {
2055 /* If the allocation failed, give up */
2056 if (!callchain_cpus_entries)
2061 err = alloc_callchain_buffers();
2063 release_callchain_buffers();
2065 mutex_unlock(&callchain_mutex);
2070 static void put_callchain_buffers(void)
2072 if (atomic_dec_and_mutex_lock(&nr_callchain_events, &callchain_mutex)) {
2073 release_callchain_buffers();
2074 mutex_unlock(&callchain_mutex);
2078 static int get_recursion_context(int *recursion)
2086 else if (in_softirq())
2091 if (recursion[rctx])
2100 static inline void put_recursion_context(int *recursion, int rctx)
2106 static struct perf_callchain_entry *get_callchain_entry(int *rctx)
2109 struct callchain_cpus_entries *entries;
2111 *rctx = get_recursion_context(__get_cpu_var(callchain_recursion));
2115 entries = rcu_dereference(callchain_cpus_entries);
2119 cpu = smp_processor_id();
2121 return &entries->cpu_entries[cpu][*rctx];
2125 put_callchain_entry(int rctx)
2127 put_recursion_context(__get_cpu_var(callchain_recursion), rctx);
2130 static struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
2133 struct perf_callchain_entry *entry;
2136 entry = get_callchain_entry(&rctx);
2145 if (!user_mode(regs)) {
2146 perf_callchain_store(entry, PERF_CONTEXT_KERNEL);
2147 perf_callchain_kernel(entry, regs);
2149 regs = task_pt_regs(current);
2155 perf_callchain_store(entry, PERF_CONTEXT_USER);
2156 perf_callchain_user(entry, regs);
2160 put_callchain_entry(rctx);
2166 * Initialize the perf_event context in a task_struct:
2168 static void __perf_event_init_context(struct perf_event_context *ctx)
2170 raw_spin_lock_init(&ctx->lock);
2171 mutex_init(&ctx->mutex);
2172 INIT_LIST_HEAD(&ctx->pinned_groups);
2173 INIT_LIST_HEAD(&ctx->flexible_groups);
2174 INIT_LIST_HEAD(&ctx->event_list);
2175 atomic_set(&ctx->refcount, 1);
2178 static struct perf_event_context *
2179 alloc_perf_context(struct pmu *pmu, struct task_struct *task)
2181 struct perf_event_context *ctx;
2183 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
2187 __perf_event_init_context(ctx);
2190 get_task_struct(task);
2197 static struct task_struct *
2198 find_lively_task_by_vpid(pid_t vpid)
2200 struct task_struct *task;
2207 task = find_task_by_vpid(vpid);
2209 get_task_struct(task);
2213 return ERR_PTR(-ESRCH);
2215 /* Reuse ptrace permission checks for now. */
2217 if (!ptrace_may_access(task, PTRACE_MODE_READ))
2222 put_task_struct(task);
2223 return ERR_PTR(err);
2227 static struct perf_event_context *
2228 find_get_context(struct pmu *pmu, struct task_struct *task, int cpu)
2230 struct perf_event_context *ctx;
2231 struct perf_cpu_context *cpuctx;
2232 unsigned long flags;
2236 /* Must be root to operate on a CPU event: */
2237 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
2238 return ERR_PTR(-EACCES);
2241 * We could be clever and allow to attach a event to an
2242 * offline CPU and activate it when the CPU comes up, but
2245 if (!cpu_online(cpu))
2246 return ERR_PTR(-ENODEV);
2248 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
2256 ctxn = pmu->task_ctx_nr;
2261 ctx = perf_lock_task_context(task, ctxn, &flags);
2264 raw_spin_unlock_irqrestore(&ctx->lock, flags);
2268 ctx = alloc_perf_context(pmu, task);
2276 mutex_lock(&task->perf_event_mutex);
2278 * If it has already passed perf_event_exit_task().
2279 * we must see PF_EXITING, it takes this mutex too.
2281 if (task->flags & PF_EXITING)
2283 else if (task->perf_event_ctxp[ctxn])
2286 rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
2287 mutex_unlock(&task->perf_event_mutex);
2289 if (unlikely(err)) {
2290 put_task_struct(task);
2302 return ERR_PTR(err);
2305 static void perf_event_free_filter(struct perf_event *event);
2307 static void free_event_rcu(struct rcu_head *head)
2309 struct perf_event *event;
2311 event = container_of(head, struct perf_event, rcu_head);
2313 put_pid_ns(event->ns);
2314 perf_event_free_filter(event);
2318 static void perf_buffer_put(struct perf_buffer *buffer);
2320 static void free_event(struct perf_event *event)
2322 irq_work_sync(&event->pending);
2324 if (!event->parent) {
2325 if (event->attach_state & PERF_ATTACH_TASK)
2326 jump_label_dec(&perf_task_events);
2327 if (event->attr.mmap || event->attr.mmap_data)
2328 atomic_dec(&nr_mmap_events);
2329 if (event->attr.comm)
2330 atomic_dec(&nr_comm_events);
2331 if (event->attr.task)
2332 atomic_dec(&nr_task_events);
2333 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
2334 put_callchain_buffers();
2337 if (event->buffer) {
2338 perf_buffer_put(event->buffer);
2339 event->buffer = NULL;
2343 event->destroy(event);
2346 put_ctx(event->ctx);
2348 call_rcu(&event->rcu_head, free_event_rcu);
2351 int perf_event_release_kernel(struct perf_event *event)
2353 struct perf_event_context *ctx = event->ctx;
2356 * Remove from the PMU, can't get re-enabled since we got
2357 * here because the last ref went.
2359 perf_event_disable(event);
2361 WARN_ON_ONCE(ctx->parent_ctx);
2363 * There are two ways this annotation is useful:
2365 * 1) there is a lock recursion from perf_event_exit_task
2366 * see the comment there.
2368 * 2) there is a lock-inversion with mmap_sem through
2369 * perf_event_read_group(), which takes faults while
2370 * holding ctx->mutex, however this is called after
2371 * the last filedesc died, so there is no possibility
2372 * to trigger the AB-BA case.
2374 mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
2375 raw_spin_lock_irq(&ctx->lock);
2376 perf_group_detach(event);
2377 list_del_event(event, ctx);
2378 raw_spin_unlock_irq(&ctx->lock);
2379 mutex_unlock(&ctx->mutex);
2385 EXPORT_SYMBOL_GPL(perf_event_release_kernel);
2388 * Called when the last reference to the file is gone.
2390 static int perf_release(struct inode *inode, struct file *file)
2392 struct perf_event *event = file->private_data;
2393 struct task_struct *owner;
2395 file->private_data = NULL;
2398 owner = ACCESS_ONCE(event->owner);
2400 * Matches the smp_wmb() in perf_event_exit_task(). If we observe
2401 * !owner it means the list deletion is complete and we can indeed
2402 * free this event, otherwise we need to serialize on
2403 * owner->perf_event_mutex.
2405 smp_read_barrier_depends();
2408 * Since delayed_put_task_struct() also drops the last
2409 * task reference we can safely take a new reference
2410 * while holding the rcu_read_lock().
2412 get_task_struct(owner);
2417 mutex_lock(&owner->perf_event_mutex);
2419 * We have to re-check the event->owner field, if it is cleared
2420 * we raced with perf_event_exit_task(), acquiring the mutex
2421 * ensured they're done, and we can proceed with freeing the
2425 list_del_init(&event->owner_entry);
2426 mutex_unlock(&owner->perf_event_mutex);
2427 put_task_struct(owner);
2430 return perf_event_release_kernel(event);
2433 u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
2435 struct perf_event *child;
2441 mutex_lock(&event->child_mutex);
2442 total += perf_event_read(event);
2443 *enabled += event->total_time_enabled +
2444 atomic64_read(&event->child_total_time_enabled);
2445 *running += event->total_time_running +
2446 atomic64_read(&event->child_total_time_running);
2448 list_for_each_entry(child, &event->child_list, child_list) {
2449 total += perf_event_read(child);
2450 *enabled += child->total_time_enabled;
2451 *running += child->total_time_running;
2453 mutex_unlock(&event->child_mutex);
2457 EXPORT_SYMBOL_GPL(perf_event_read_value);
2459 static int perf_event_read_group(struct perf_event *event,
2460 u64 read_format, char __user *buf)
2462 struct perf_event *leader = event->group_leader, *sub;
2463 int n = 0, size = 0, ret = -EFAULT;
2464 struct perf_event_context *ctx = leader->ctx;
2466 u64 count, enabled, running;
2468 mutex_lock(&ctx->mutex);
2469 count = perf_event_read_value(leader, &enabled, &running);
2471 values[n++] = 1 + leader->nr_siblings;
2472 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2473 values[n++] = enabled;
2474 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2475 values[n++] = running;
2476 values[n++] = count;
2477 if (read_format & PERF_FORMAT_ID)
2478 values[n++] = primary_event_id(leader);
2480 size = n * sizeof(u64);
2482 if (copy_to_user(buf, values, size))
2487 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
2490 values[n++] = perf_event_read_value(sub, &enabled, &running);
2491 if (read_format & PERF_FORMAT_ID)
2492 values[n++] = primary_event_id(sub);
2494 size = n * sizeof(u64);
2496 if (copy_to_user(buf + ret, values, size)) {
2504 mutex_unlock(&ctx->mutex);
2509 static int perf_event_read_one(struct perf_event *event,
2510 u64 read_format, char __user *buf)
2512 u64 enabled, running;
2516 values[n++] = perf_event_read_value(event, &enabled, &running);
2517 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2518 values[n++] = enabled;
2519 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2520 values[n++] = running;
2521 if (read_format & PERF_FORMAT_ID)
2522 values[n++] = primary_event_id(event);
2524 if (copy_to_user(buf, values, n * sizeof(u64)))
2527 return n * sizeof(u64);
2531 * Read the performance event - simple non blocking version for now
2534 perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
2536 u64 read_format = event->attr.read_format;
2540 * Return end-of-file for a read on a event that is in
2541 * error state (i.e. because it was pinned but it couldn't be
2542 * scheduled on to the CPU at some point).
2544 if (event->state == PERF_EVENT_STATE_ERROR)
2547 if (count < event->read_size)
2550 WARN_ON_ONCE(event->ctx->parent_ctx);
2551 if (read_format & PERF_FORMAT_GROUP)
2552 ret = perf_event_read_group(event, read_format, buf);
2554 ret = perf_event_read_one(event, read_format, buf);
2560 perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
2562 struct perf_event *event = file->private_data;
2564 return perf_read_hw(event, buf, count);
2567 static unsigned int perf_poll(struct file *file, poll_table *wait)
2569 struct perf_event *event = file->private_data;
2570 struct perf_buffer *buffer;
2571 unsigned int events = POLL_HUP;
2574 buffer = rcu_dereference(event->buffer);
2576 events = atomic_xchg(&buffer->poll, 0);
2579 poll_wait(file, &event->waitq, wait);
2584 static void perf_event_reset(struct perf_event *event)
2586 (void)perf_event_read(event);
2587 local64_set(&event->count, 0);
2588 perf_event_update_userpage(event);
2592 * Holding the top-level event's child_mutex means that any
2593 * descendant process that has inherited this event will block
2594 * in sync_child_event if it goes to exit, thus satisfying the
2595 * task existence requirements of perf_event_enable/disable.
2597 static void perf_event_for_each_child(struct perf_event *event,
2598 void (*func)(struct perf_event *))
2600 struct perf_event *child;
2602 WARN_ON_ONCE(event->ctx->parent_ctx);
2603 mutex_lock(&event->child_mutex);
2605 list_for_each_entry(child, &event->child_list, child_list)
2607 mutex_unlock(&event->child_mutex);
2610 static void perf_event_for_each(struct perf_event *event,
2611 void (*func)(struct perf_event *))
2613 struct perf_event_context *ctx = event->ctx;
2614 struct perf_event *sibling;
2616 WARN_ON_ONCE(ctx->parent_ctx);
2617 mutex_lock(&ctx->mutex);
2618 event = event->group_leader;
2620 perf_event_for_each_child(event, func);
2622 list_for_each_entry(sibling, &event->sibling_list, group_entry)
2623 perf_event_for_each_child(event, func);
2624 mutex_unlock(&ctx->mutex);
2627 static int perf_event_period(struct perf_event *event, u64 __user *arg)
2629 struct perf_event_context *ctx = event->ctx;
2633 if (!is_sampling_event(event))
2636 if (copy_from_user(&value, arg, sizeof(value)))
2642 raw_spin_lock_irq(&ctx->lock);
2643 if (event->attr.freq) {
2644 if (value > sysctl_perf_event_sample_rate) {
2649 event->attr.sample_freq = value;
2651 event->attr.sample_period = value;
2652 event->hw.sample_period = value;
2655 raw_spin_unlock_irq(&ctx->lock);
2660 static const struct file_operations perf_fops;
2662 static struct perf_event *perf_fget_light(int fd, int *fput_needed)
2666 file = fget_light(fd, fput_needed);
2668 return ERR_PTR(-EBADF);
2670 if (file->f_op != &perf_fops) {
2671 fput_light(file, *fput_needed);
2673 return ERR_PTR(-EBADF);
2676 return file->private_data;
2679 static int perf_event_set_output(struct perf_event *event,
2680 struct perf_event *output_event);
2681 static int perf_event_set_filter(struct perf_event *event, void __user *arg);
2683 static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2685 struct perf_event *event = file->private_data;
2686 void (*func)(struct perf_event *);
2690 case PERF_EVENT_IOC_ENABLE:
2691 func = perf_event_enable;
2693 case PERF_EVENT_IOC_DISABLE:
2694 func = perf_event_disable;
2696 case PERF_EVENT_IOC_RESET:
2697 func = perf_event_reset;
2700 case PERF_EVENT_IOC_REFRESH:
2701 return perf_event_refresh(event, arg);
2703 case PERF_EVENT_IOC_PERIOD:
2704 return perf_event_period(event, (u64 __user *)arg);
2706 case PERF_EVENT_IOC_SET_OUTPUT:
2708 struct perf_event *output_event = NULL;
2709 int fput_needed = 0;
2713 output_event = perf_fget_light(arg, &fput_needed);
2714 if (IS_ERR(output_event))
2715 return PTR_ERR(output_event);
2718 ret = perf_event_set_output(event, output_event);
2720 fput_light(output_event->filp, fput_needed);
2725 case PERF_EVENT_IOC_SET_FILTER:
2726 return perf_event_set_filter(event, (void __user *)arg);
2732 if (flags & PERF_IOC_FLAG_GROUP)
2733 perf_event_for_each(event, func);
2735 perf_event_for_each_child(event, func);
2740 int perf_event_task_enable(void)
2742 struct perf_event *event;
2744 mutex_lock(¤t->perf_event_mutex);
2745 list_for_each_entry(event, ¤t->perf_event_list, owner_entry)
2746 perf_event_for_each_child(event, perf_event_enable);
2747 mutex_unlock(¤t->perf_event_mutex);
2752 int perf_event_task_disable(void)
2754 struct perf_event *event;
2756 mutex_lock(¤t->perf_event_mutex);
2757 list_for_each_entry(event, ¤t->perf_event_list, owner_entry)
2758 perf_event_for_each_child(event, perf_event_disable);
2759 mutex_unlock(¤t->perf_event_mutex);
2764 #ifndef PERF_EVENT_INDEX_OFFSET
2765 # define PERF_EVENT_INDEX_OFFSET 0
2768 static int perf_event_index(struct perf_event *event)
2770 if (event->hw.state & PERF_HES_STOPPED)
2773 if (event->state != PERF_EVENT_STATE_ACTIVE)
2776 return event->hw.idx + 1 - PERF_EVENT_INDEX_OFFSET;
2780 * Callers need to ensure there can be no nesting of this function, otherwise
2781 * the seqlock logic goes bad. We can not serialize this because the arch
2782 * code calls this from NMI context.
2784 void perf_event_update_userpage(struct perf_event *event)
2786 struct perf_event_mmap_page *userpg;
2787 struct perf_buffer *buffer;
2790 buffer = rcu_dereference(event->buffer);
2794 userpg = buffer->user_page;
2797 * Disable preemption so as to not let the corresponding user-space
2798 * spin too long if we get preempted.
2803 userpg->index = perf_event_index(event);
2804 userpg->offset = perf_event_count(event);
2805 if (event->state == PERF_EVENT_STATE_ACTIVE)
2806 userpg->offset -= local64_read(&event->hw.prev_count);
2808 userpg->time_enabled = event->total_time_enabled +
2809 atomic64_read(&event->child_total_time_enabled);
2811 userpg->time_running = event->total_time_running +
2812 atomic64_read(&event->child_total_time_running);
2821 static unsigned long perf_data_size(struct perf_buffer *buffer);
2824 perf_buffer_init(struct perf_buffer *buffer, long watermark, int flags)
2826 long max_size = perf_data_size(buffer);
2829 buffer->watermark = min(max_size, watermark);
2831 if (!buffer->watermark)
2832 buffer->watermark = max_size / 2;
2834 if (flags & PERF_BUFFER_WRITABLE)
2835 buffer->writable = 1;
2837 atomic_set(&buffer->refcount, 1);
2840 #ifndef CONFIG_PERF_USE_VMALLOC
2843 * Back perf_mmap() with regular GFP_KERNEL-0 pages.
2846 static struct page *
2847 perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
2849 if (pgoff > buffer->nr_pages)
2853 return virt_to_page(buffer->user_page);
2855 return virt_to_page(buffer->data_pages[pgoff - 1]);
2858 static void *perf_mmap_alloc_page(int cpu)
2863 node = (cpu == -1) ? cpu : cpu_to_node(cpu);
2864 page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
2868 return page_address(page);
2871 static struct perf_buffer *
2872 perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
2874 struct perf_buffer *buffer;
2878 size = sizeof(struct perf_buffer);
2879 size += nr_pages * sizeof(void *);
2881 buffer = kzalloc(size, GFP_KERNEL);
2885 buffer->user_page = perf_mmap_alloc_page(cpu);
2886 if (!buffer->user_page)
2887 goto fail_user_page;
2889 for (i = 0; i < nr_pages; i++) {
2890 buffer->data_pages[i] = perf_mmap_alloc_page(cpu);
2891 if (!buffer->data_pages[i])
2892 goto fail_data_pages;
2895 buffer->nr_pages = nr_pages;
2897 perf_buffer_init(buffer, watermark, flags);
2902 for (i--; i >= 0; i--)
2903 free_page((unsigned long)buffer->data_pages[i]);
2905 free_page((unsigned long)buffer->user_page);
2914 static void perf_mmap_free_page(unsigned long addr)
2916 struct page *page = virt_to_page((void *)addr);
2918 page->mapping = NULL;
2922 static void perf_buffer_free(struct perf_buffer *buffer)
2926 perf_mmap_free_page((unsigned long)buffer->user_page);
2927 for (i = 0; i < buffer->nr_pages; i++)
2928 perf_mmap_free_page((unsigned long)buffer->data_pages[i]);
2932 static inline int page_order(struct perf_buffer *buffer)
2940 * Back perf_mmap() with vmalloc memory.
2942 * Required for architectures that have d-cache aliasing issues.
2945 static inline int page_order(struct perf_buffer *buffer)
2947 return buffer->page_order;
2950 static struct page *
2951 perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
2953 if (pgoff > (1UL << page_order(buffer)))
2956 return vmalloc_to_page((void *)buffer->user_page + pgoff * PAGE_SIZE);
2959 static void perf_mmap_unmark_page(void *addr)
2961 struct page *page = vmalloc_to_page(addr);
2963 page->mapping = NULL;
2966 static void perf_buffer_free_work(struct work_struct *work)
2968 struct perf_buffer *buffer;
2972 buffer = container_of(work, struct perf_buffer, work);
2973 nr = 1 << page_order(buffer);
2975 base = buffer->user_page;
2976 for (i = 0; i < nr + 1; i++)
2977 perf_mmap_unmark_page(base + (i * PAGE_SIZE));
2983 static void perf_buffer_free(struct perf_buffer *buffer)
2985 schedule_work(&buffer->work);
2988 static struct perf_buffer *
2989 perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
2991 struct perf_buffer *buffer;
2995 size = sizeof(struct perf_buffer);
2996 size += sizeof(void *);
2998 buffer = kzalloc(size, GFP_KERNEL);
3002 INIT_WORK(&buffer->work, perf_buffer_free_work);
3004 all_buf = vmalloc_user((nr_pages + 1) * PAGE_SIZE);
3008 buffer->user_page = all_buf;
3009 buffer->data_pages[0] = all_buf + PAGE_SIZE;
3010 buffer->page_order = ilog2(nr_pages);
3011 buffer->nr_pages = 1;
3013 perf_buffer_init(buffer, watermark, flags);
3026 static unsigned long perf_data_size(struct perf_buffer *buffer)
3028 return buffer->nr_pages << (PAGE_SHIFT + page_order(buffer));
3031 static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
3033 struct perf_event *event = vma->vm_file->private_data;
3034 struct perf_buffer *buffer;
3035 int ret = VM_FAULT_SIGBUS;
3037 if (vmf->flags & FAULT_FLAG_MKWRITE) {
3038 if (vmf->pgoff == 0)
3044 buffer = rcu_dereference(event->buffer);
3048 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
3051 vmf->page = perf_mmap_to_page(buffer, vmf->pgoff);
3055 get_page(vmf->page);
3056 vmf->page->mapping = vma->vm_file->f_mapping;
3057 vmf->page->index = vmf->pgoff;
3066 static void perf_buffer_free_rcu(struct rcu_head *rcu_head)
3068 struct perf_buffer *buffer;
3070 buffer = container_of(rcu_head, struct perf_buffer, rcu_head);
3071 perf_buffer_free(buffer);
3074 static struct perf_buffer *perf_buffer_get(struct perf_event *event)
3076 struct perf_buffer *buffer;
3079 buffer = rcu_dereference(event->buffer);
3081 if (!atomic_inc_not_zero(&buffer->refcount))
3089 static void perf_buffer_put(struct perf_buffer *buffer)
3091 if (!atomic_dec_and_test(&buffer->refcount))
3094 call_rcu(&buffer->rcu_head, perf_buffer_free_rcu);
3097 static void perf_mmap_open(struct vm_area_struct *vma)
3099 struct perf_event *event = vma->vm_file->private_data;
3101 atomic_inc(&event->mmap_count);
3104 static void perf_mmap_close(struct vm_area_struct *vma)
3106 struct perf_event *event = vma->vm_file->private_data;
3108 if (atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) {
3109 unsigned long size = perf_data_size(event->buffer);
3110 struct user_struct *user = event->mmap_user;
3111 struct perf_buffer *buffer = event->buffer;
3113 atomic_long_sub((size >> PAGE_SHIFT) + 1, &user->locked_vm);
3114 vma->vm_mm->locked_vm -= event->mmap_locked;
3115 rcu_assign_pointer(event->buffer, NULL);
3116 mutex_unlock(&event->mmap_mutex);
3118 perf_buffer_put(buffer);
3123 static const struct vm_operations_struct perf_mmap_vmops = {
3124 .open = perf_mmap_open,
3125 .close = perf_mmap_close,
3126 .fault = perf_mmap_fault,
3127 .page_mkwrite = perf_mmap_fault,
3130 static int perf_mmap(struct file *file, struct vm_area_struct *vma)
3132 struct perf_event *event = file->private_data;
3133 unsigned long user_locked, user_lock_limit;
3134 struct user_struct *user = current_user();
3135 unsigned long locked, lock_limit;
3136 struct perf_buffer *buffer;
3137 unsigned long vma_size;
3138 unsigned long nr_pages;
3139 long user_extra, extra;
3140 int ret = 0, flags = 0;
3143 * Don't allow mmap() of inherited per-task counters. This would
3144 * create a performance issue due to all children writing to the
3147 if (event->cpu == -1 && event->attr.inherit)
3150 if (!(vma->vm_flags & VM_SHARED))
3153 vma_size = vma->vm_end - vma->vm_start;
3154 nr_pages = (vma_size / PAGE_SIZE) - 1;
3157 * If we have buffer pages ensure they're a power-of-two number, so we
3158 * can do bitmasks instead of modulo.
3160 if (nr_pages != 0 && !is_power_of_2(nr_pages))
3163 if (vma_size != PAGE_SIZE * (1 + nr_pages))
3166 if (vma->vm_pgoff != 0)
3169 WARN_ON_ONCE(event->ctx->parent_ctx);
3170 mutex_lock(&event->mmap_mutex);
3171 if (event->buffer) {
3172 if (event->buffer->nr_pages == nr_pages)
3173 atomic_inc(&event->buffer->refcount);
3179 user_extra = nr_pages + 1;
3180 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
3183 * Increase the limit linearly with more CPUs:
3185 user_lock_limit *= num_online_cpus();
3187 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
3190 if (user_locked > user_lock_limit)
3191 extra = user_locked - user_lock_limit;
3193 lock_limit = rlimit(RLIMIT_MEMLOCK);
3194 lock_limit >>= PAGE_SHIFT;
3195 locked = vma->vm_mm->locked_vm + extra;
3197 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
3198 !capable(CAP_IPC_LOCK)) {
3203 WARN_ON(event->buffer);
3205 if (vma->vm_flags & VM_WRITE)
3206 flags |= PERF_BUFFER_WRITABLE;
3208 buffer = perf_buffer_alloc(nr_pages, event->attr.wakeup_watermark,
3214 rcu_assign_pointer(event->buffer, buffer);
3216 atomic_long_add(user_extra, &user->locked_vm);
3217 event->mmap_locked = extra;
3218 event->mmap_user = get_current_user();
3219 vma->vm_mm->locked_vm += event->mmap_locked;
3223 atomic_inc(&event->mmap_count);
3224 mutex_unlock(&event->mmap_mutex);
3226 vma->vm_flags |= VM_RESERVED;
3227 vma->vm_ops = &perf_mmap_vmops;
3232 static int perf_fasync(int fd, struct file *filp, int on)
3234 struct inode *inode = filp->f_path.dentry->d_inode;
3235 struct perf_event *event = filp->private_data;
3238 mutex_lock(&inode->i_mutex);
3239 retval = fasync_helper(fd, filp, on, &event->fasync);
3240 mutex_unlock(&inode->i_mutex);
3248 static const struct file_operations perf_fops = {
3249 .llseek = no_llseek,
3250 .release = perf_release,
3253 .unlocked_ioctl = perf_ioctl,
3254 .compat_ioctl = perf_ioctl,
3256 .fasync = perf_fasync,
3262 * If there's data, ensure we set the poll() state and publish everything
3263 * to user-space before waking everybody up.
3266 void perf_event_wakeup(struct perf_event *event)
3268 wake_up_all(&event->waitq);
3270 if (event->pending_kill) {
3271 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
3272 event->pending_kill = 0;
3276 static void perf_pending_event(struct irq_work *entry)
3278 struct perf_event *event = container_of(entry,
3279 struct perf_event, pending);
3281 if (event->pending_disable) {
3282 event->pending_disable = 0;
3283 __perf_event_disable(event);
3286 if (event->pending_wakeup) {
3287 event->pending_wakeup = 0;
3288 perf_event_wakeup(event);
3293 * We assume there is only KVM supporting the callbacks.
3294 * Later on, we might change it to a list if there is
3295 * another virtualization implementation supporting the callbacks.
3297 struct perf_guest_info_callbacks *perf_guest_cbs;
3299 int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3301 perf_guest_cbs = cbs;
3304 EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
3306 int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3308 perf_guest_cbs = NULL;
3311 EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
3316 static bool perf_output_space(struct perf_buffer *buffer, unsigned long tail,
3317 unsigned long offset, unsigned long head)
3321 if (!buffer->writable)
3324 mask = perf_data_size(buffer) - 1;
3326 offset = (offset - tail) & mask;
3327 head = (head - tail) & mask;
3329 if ((int)(head - offset) < 0)
3335 static void perf_output_wakeup(struct perf_output_handle *handle)
3337 atomic_set(&handle->buffer->poll, POLL_IN);
3340 handle->event->pending_wakeup = 1;
3341 irq_work_queue(&handle->event->pending);
3343 perf_event_wakeup(handle->event);
3347 * We need to ensure a later event_id doesn't publish a head when a former
3348 * event isn't done writing. However since we need to deal with NMIs we
3349 * cannot fully serialize things.
3351 * We only publish the head (and generate a wakeup) when the outer-most
3354 static void perf_output_get_handle(struct perf_output_handle *handle)
3356 struct perf_buffer *buffer = handle->buffer;
3359 local_inc(&buffer->nest);
3360 handle->wakeup = local_read(&buffer->wakeup);
3363 static void perf_output_put_handle(struct perf_output_handle *handle)
3365 struct perf_buffer *buffer = handle->buffer;
3369 head = local_read(&buffer->head);
3372 * IRQ/NMI can happen here, which means we can miss a head update.
3375 if (!local_dec_and_test(&buffer->nest))
3379 * Publish the known good head. Rely on the full barrier implied
3380 * by atomic_dec_and_test() order the buffer->head read and this
3383 buffer->user_page->data_head = head;
3386 * Now check if we missed an update, rely on the (compiler)
3387 * barrier in atomic_dec_and_test() to re-read buffer->head.
3389 if (unlikely(head != local_read(&buffer->head))) {
3390 local_inc(&buffer->nest);
3394 if (handle->wakeup != local_read(&buffer->wakeup))
3395 perf_output_wakeup(handle);
3401 __always_inline void perf_output_copy(struct perf_output_handle *handle,
3402 const void *buf, unsigned int len)
3405 unsigned long size = min_t(unsigned long, handle->size, len);
3407 memcpy(handle->addr, buf, size);
3410 handle->addr += size;
3412 handle->size -= size;
3413 if (!handle->size) {
3414 struct perf_buffer *buffer = handle->buffer;
3417 handle->page &= buffer->nr_pages - 1;
3418 handle->addr = buffer->data_pages[handle->page];
3419 handle->size = PAGE_SIZE << page_order(buffer);
3424 static void __perf_event_header__init_id(struct perf_event_header *header,
3425 struct perf_sample_data *data,
3426 struct perf_event *event)
3428 u64 sample_type = event->attr.sample_type;
3430 data->type = sample_type;
3431 header->size += event->id_header_size;
3433 if (sample_type & PERF_SAMPLE_TID) {
3434 /* namespace issues */
3435 data->tid_entry.pid = perf_event_pid(event, current);
3436 data->tid_entry.tid = perf_event_tid(event, current);
3439 if (sample_type & PERF_SAMPLE_TIME)
3440 data->time = perf_clock();
3442 if (sample_type & PERF_SAMPLE_ID)
3443 data->id = primary_event_id(event);
3445 if (sample_type & PERF_SAMPLE_STREAM_ID)
3446 data->stream_id = event->id;
3448 if (sample_type & PERF_SAMPLE_CPU) {
3449 data->cpu_entry.cpu = raw_smp_processor_id();
3450 data->cpu_entry.reserved = 0;
3454 static void perf_event_header__init_id(struct perf_event_header *header,
3455 struct perf_sample_data *data,
3456 struct perf_event *event)
3458 if (event->attr.sample_id_all)
3459 __perf_event_header__init_id(header, data, event);
3462 static void __perf_event__output_id_sample(struct perf_output_handle *handle,
3463 struct perf_sample_data *data)
3465 u64 sample_type = data->type;
3467 if (sample_type & PERF_SAMPLE_TID)
3468 perf_output_put(handle, data->tid_entry);
3470 if (sample_type & PERF_SAMPLE_TIME)
3471 perf_output_put(handle, data->time);
3473 if (sample_type & PERF_SAMPLE_ID)
3474 perf_output_put(handle, data->id);
3476 if (sample_type & PERF_SAMPLE_STREAM_ID)
3477 perf_output_put(handle, data->stream_id);
3479 if (sample_type & PERF_SAMPLE_CPU)
3480 perf_output_put(handle, data->cpu_entry);
3483 static void perf_event__output_id_sample(struct perf_event *event,
3484 struct perf_output_handle *handle,
3485 struct perf_sample_data *sample)
3487 if (event->attr.sample_id_all)
3488 __perf_event__output_id_sample(handle, sample);
3491 int perf_output_begin(struct perf_output_handle *handle,
3492 struct perf_event *event, unsigned int size,
3493 int nmi, int sample)
3495 struct perf_buffer *buffer;
3496 unsigned long tail, offset, head;
3498 struct perf_sample_data sample_data;
3500 struct perf_event_header header;
3507 * For inherited events we send all the output towards the parent.
3510 event = event->parent;
3512 buffer = rcu_dereference(event->buffer);
3516 handle->buffer = buffer;
3517 handle->event = event;
3519 handle->sample = sample;
3521 if (!buffer->nr_pages)
3524 have_lost = local_read(&buffer->lost);
3526 lost_event.header.size = sizeof(lost_event);
3527 perf_event_header__init_id(&lost_event.header, &sample_data,
3529 size += lost_event.header.size;
3532 perf_output_get_handle(handle);
3536 * Userspace could choose to issue a mb() before updating the
3537 * tail pointer. So that all reads will be completed before the
3540 tail = ACCESS_ONCE(buffer->user_page->data_tail);
3542 offset = head = local_read(&buffer->head);
3544 if (unlikely(!perf_output_space(buffer, tail, offset, head)))
3546 } while (local_cmpxchg(&buffer->head, offset, head) != offset);
3548 if (head - local_read(&buffer->wakeup) > buffer->watermark)
3549 local_add(buffer->watermark, &buffer->wakeup);
3551 handle->page = offset >> (PAGE_SHIFT + page_order(buffer));
3552 handle->page &= buffer->nr_pages - 1;
3553 handle->size = offset & ((PAGE_SIZE << page_order(buffer)) - 1);
3554 handle->addr = buffer->data_pages[handle->page];
3555 handle->addr += handle->size;
3556 handle->size = (PAGE_SIZE << page_order(buffer)) - handle->size;
3559 lost_event.header.type = PERF_RECORD_LOST;
3560 lost_event.header.misc = 0;
3561 lost_event.id = event->id;
3562 lost_event.lost = local_xchg(&buffer->lost, 0);
3564 perf_output_put(handle, lost_event);
3565 perf_event__output_id_sample(event, handle, &sample_data);
3571 local_inc(&buffer->lost);
3572 perf_output_put_handle(handle);
3579 void perf_output_end(struct perf_output_handle *handle)
3581 struct perf_event *event = handle->event;
3582 struct perf_buffer *buffer = handle->buffer;
3584 int wakeup_events = event->attr.wakeup_events;
3586 if (handle->sample && wakeup_events) {
3587 int events = local_inc_return(&buffer->events);
3588 if (events >= wakeup_events) {
3589 local_sub(wakeup_events, &buffer->events);
3590 local_inc(&buffer->wakeup);
3594 perf_output_put_handle(handle);
3598 static void perf_output_read_one(struct perf_output_handle *handle,
3599 struct perf_event *event,
3600 u64 enabled, u64 running)
3602 u64 read_format = event->attr.read_format;
3606 values[n++] = perf_event_count(event);
3607 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
3608 values[n++] = enabled +
3609 atomic64_read(&event->child_total_time_enabled);
3611 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
3612 values[n++] = running +
3613 atomic64_read(&event->child_total_time_running);
3615 if (read_format & PERF_FORMAT_ID)
3616 values[n++] = primary_event_id(event);
3618 perf_output_copy(handle, values, n * sizeof(u64));
3622 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
3624 static void perf_output_read_group(struct perf_output_handle *handle,
3625 struct perf_event *event,
3626 u64 enabled, u64 running)
3628 struct perf_event *leader = event->group_leader, *sub;
3629 u64 read_format = event->attr.read_format;
3633 values[n++] = 1 + leader->nr_siblings;
3635 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3636 values[n++] = enabled;
3638 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3639 values[n++] = running;
3641 if (leader != event)
3642 leader->pmu->read(leader);
3644 values[n++] = perf_event_count(leader);
3645 if (read_format & PERF_FORMAT_ID)
3646 values[n++] = primary_event_id(leader);
3648 perf_output_copy(handle, values, n * sizeof(u64));
3650 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
3654 sub->pmu->read(sub);
3656 values[n++] = perf_event_count(sub);
3657 if (read_format & PERF_FORMAT_ID)
3658 values[n++] = primary_event_id(sub);
3660 perf_output_copy(handle, values, n * sizeof(u64));
3664 #define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
3665 PERF_FORMAT_TOTAL_TIME_RUNNING)
3667 static void perf_output_read(struct perf_output_handle *handle,
3668 struct perf_event *event)
3670 u64 enabled = 0, running = 0, now, ctx_time;
3671 u64 read_format = event->attr.read_format;
3674 * compute total_time_enabled, total_time_running
3675 * based on snapshot values taken when the event
3676 * was last scheduled in.
3678 * we cannot simply called update_context_time()
3679 * because of locking issue as we are called in
3682 if (read_format & PERF_FORMAT_TOTAL_TIMES) {
3684 ctx_time = event->shadow_ctx_time + now;
3685 enabled = ctx_time - event->tstamp_enabled;
3686 running = ctx_time - event->tstamp_running;
3689 if (event->attr.read_format & PERF_FORMAT_GROUP)
3690 perf_output_read_group(handle, event, enabled, running);
3692 perf_output_read_one(handle, event, enabled, running);
3695 void perf_output_sample(struct perf_output_handle *handle,
3696 struct perf_event_header *header,
3697 struct perf_sample_data *data,
3698 struct perf_event *event)
3700 u64 sample_type = data->type;
3702 perf_output_put(handle, *header);
3704 if (sample_type & PERF_SAMPLE_IP)
3705 perf_output_put(handle, data->ip);
3707 if (sample_type & PERF_SAMPLE_TID)
3708 perf_output_put(handle, data->tid_entry);
3710 if (sample_type & PERF_SAMPLE_TIME)
3711 perf_output_put(handle, data->time);
3713 if (sample_type & PERF_SAMPLE_ADDR)
3714 perf_output_put(handle, data->addr);
3716 if (sample_type & PERF_SAMPLE_ID)
3717 perf_output_put(handle, data->id);
3719 if (sample_type & PERF_SAMPLE_STREAM_ID)
3720 perf_output_put(handle, data->stream_id);
3722 if (sample_type & PERF_SAMPLE_CPU)
3723 perf_output_put(handle, data->cpu_entry);
3725 if (sample_type & PERF_SAMPLE_PERIOD)
3726 perf_output_put(handle, data->period);
3728 if (sample_type & PERF_SAMPLE_READ)
3729 perf_output_read(handle, event);
3731 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3732 if (data->callchain) {
3735 if (data->callchain)
3736 size += data->callchain->nr;
3738 size *= sizeof(u64);
3740 perf_output_copy(handle, data->callchain, size);
3743 perf_output_put(handle, nr);
3747 if (sample_type & PERF_SAMPLE_RAW) {
3749 perf_output_put(handle, data->raw->size);
3750 perf_output_copy(handle, data->raw->data,
3757 .size = sizeof(u32),
3760 perf_output_put(handle, raw);
3765 void perf_prepare_sample(struct perf_event_header *header,
3766 struct perf_sample_data *data,
3767 struct perf_event *event,
3768 struct pt_regs *regs)
3770 u64 sample_type = event->attr.sample_type;
3772 header->type = PERF_RECORD_SAMPLE;
3773 header->size = sizeof(*header) + event->header_size;
3776 header->misc |= perf_misc_flags(regs);
3778 __perf_event_header__init_id(header, data, event);
3780 if (sample_type & PERF_SAMPLE_IP)
3781 data->ip = perf_instruction_pointer(regs);
3783 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3786 data->callchain = perf_callchain(regs);
3788 if (data->callchain)
3789 size += data->callchain->nr;
3791 header->size += size * sizeof(u64);
3794 if (sample_type & PERF_SAMPLE_RAW) {
3795 int size = sizeof(u32);
3798 size += data->raw->size;
3800 size += sizeof(u32);
3802 WARN_ON_ONCE(size & (sizeof(u64)-1));
3803 header->size += size;
3807 static void perf_event_output(struct perf_event *event, int nmi,
3808 struct perf_sample_data *data,
3809 struct pt_regs *regs)
3811 struct perf_output_handle handle;
3812 struct perf_event_header header;
3814 /* protect the callchain buffers */
3817 perf_prepare_sample(&header, data, event, regs);
3819 if (perf_output_begin(&handle, event, header.size, nmi, 1))
3822 perf_output_sample(&handle, &header, data, event);
3824 perf_output_end(&handle);
3834 struct perf_read_event {
3835 struct perf_event_header header;
3842 perf_event_read_event(struct perf_event *event,
3843 struct task_struct *task)
3845 struct perf_output_handle handle;
3846 struct perf_sample_data sample;
3847 struct perf_read_event read_event = {
3849 .type = PERF_RECORD_READ,
3851 .size = sizeof(read_event) + event->read_size,
3853 .pid = perf_event_pid(event, task),
3854 .tid = perf_event_tid(event, task),
3858 perf_event_header__init_id(&read_event.header, &sample, event);
3859 ret = perf_output_begin(&handle, event, read_event.header.size, 0, 0);
3863 perf_output_put(&handle, read_event);
3864 perf_output_read(&handle, event);
3865 perf_event__output_id_sample(event, &handle, &sample);
3867 perf_output_end(&handle);
3871 * task tracking -- fork/exit
3873 * enabled by: attr.comm | attr.mmap | attr.mmap_data | attr.task
3876 struct perf_task_event {
3877 struct task_struct *task;
3878 struct perf_event_context *task_ctx;
3881 struct perf_event_header header;
3891 static void perf_event_task_output(struct perf_event *event,
3892 struct perf_task_event *task_event)
3894 struct perf_output_handle handle;
3895 struct perf_sample_data sample;
3896 struct task_struct *task = task_event->task;
3897 int ret, size = task_event->event_id.header.size;
3899 perf_event_header__init_id(&task_event->event_id.header, &sample, event);
3901 ret = perf_output_begin(&handle, event,
3902 task_event->event_id.header.size, 0, 0);
3906 task_event->event_id.pid = perf_event_pid(event, task);
3907 task_event->event_id.ppid = perf_event_pid(event, current);
3909 task_event->event_id.tid = perf_event_tid(event, task);
3910 task_event->event_id.ptid = perf_event_tid(event, current);
3912 perf_output_put(&handle, task_event->event_id);
3914 perf_event__output_id_sample(event, &handle, &sample);
3916 perf_output_end(&handle);
3918 task_event->event_id.header.size = size;
3921 static int perf_event_task_match(struct perf_event *event)
3923 if (event->state < PERF_EVENT_STATE_INACTIVE)
3926 if (!event_filter_match(event))
3929 if (event->attr.comm || event->attr.mmap ||
3930 event->attr.mmap_data || event->attr.task)
3936 static void perf_event_task_ctx(struct perf_event_context *ctx,
3937 struct perf_task_event *task_event)
3939 struct perf_event *event;
3941 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3942 if (perf_event_task_match(event))
3943 perf_event_task_output(event, task_event);
3947 static void perf_event_task_event(struct perf_task_event *task_event)
3949 struct perf_cpu_context *cpuctx;
3950 struct perf_event_context *ctx;
3955 list_for_each_entry_rcu(pmu, &pmus, entry) {
3956 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
3957 if (cpuctx->active_pmu != pmu)
3959 perf_event_task_ctx(&cpuctx->ctx, task_event);
3961 ctx = task_event->task_ctx;
3963 ctxn = pmu->task_ctx_nr;
3966 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
3969 perf_event_task_ctx(ctx, task_event);
3971 put_cpu_ptr(pmu->pmu_cpu_context);
3976 static void perf_event_task(struct task_struct *task,
3977 struct perf_event_context *task_ctx,
3980 struct perf_task_event task_event;
3982 if (!atomic_read(&nr_comm_events) &&
3983 !atomic_read(&nr_mmap_events) &&
3984 !atomic_read(&nr_task_events))
3987 task_event = (struct perf_task_event){
3989 .task_ctx = task_ctx,
3992 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
3994 .size = sizeof(task_event.event_id),
4000 .time = perf_clock(),
4004 perf_event_task_event(&task_event);
4007 void perf_event_fork(struct task_struct *task)
4009 perf_event_task(task, NULL, 1);
4016 struct perf_comm_event {
4017 struct task_struct *task;
4022 struct perf_event_header header;
4029 static void perf_event_comm_output(struct perf_event *event,
4030 struct perf_comm_event *comm_event)
4032 struct perf_output_handle handle;
4033 struct perf_sample_data sample;
4034 int size = comm_event->event_id.header.size;
4037 perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
4038 ret = perf_output_begin(&handle, event,
4039 comm_event->event_id.header.size, 0, 0);
4044 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
4045 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
4047 perf_output_put(&handle, comm_event->event_id);
4048 perf_output_copy(&handle, comm_event->comm,
4049 comm_event->comm_size);
4051 perf_event__output_id_sample(event, &handle, &sample);
4053 perf_output_end(&handle);
4055 comm_event->event_id.header.size = size;
4058 static int perf_event_comm_match(struct perf_event *event)
4060 if (event->state < PERF_EVENT_STATE_INACTIVE)
4063 if (!event_filter_match(event))
4066 if (event->attr.comm)
4072 static void perf_event_comm_ctx(struct perf_event_context *ctx,
4073 struct perf_comm_event *comm_event)
4075 struct perf_event *event;
4077 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4078 if (perf_event_comm_match(event))
4079 perf_event_comm_output(event, comm_event);
4083 static void perf_event_comm_event(struct perf_comm_event *comm_event)
4085 struct perf_cpu_context *cpuctx;
4086 struct perf_event_context *ctx;
4087 char comm[TASK_COMM_LEN];
4092 memset(comm, 0, sizeof(comm));
4093 strlcpy(comm, comm_event->task->comm, sizeof(comm));
4094 size = ALIGN(strlen(comm)+1, sizeof(u64));
4096 comm_event->comm = comm;
4097 comm_event->comm_size = size;
4099 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
4101 list_for_each_entry_rcu(pmu, &pmus, entry) {
4102 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
4103 if (cpuctx->active_pmu != pmu)
4105 perf_event_comm_ctx(&cpuctx->ctx, comm_event);
4107 ctxn = pmu->task_ctx_nr;
4111 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4113 perf_event_comm_ctx(ctx, comm_event);
4115 put_cpu_ptr(pmu->pmu_cpu_context);
4120 void perf_event_comm(struct task_struct *task)
4122 struct perf_comm_event comm_event;
4123 struct perf_event_context *ctx;
4126 for_each_task_context_nr(ctxn) {
4127 ctx = task->perf_event_ctxp[ctxn];
4131 perf_event_enable_on_exec(ctx);
4134 if (!atomic_read(&nr_comm_events))
4137 comm_event = (struct perf_comm_event){
4143 .type = PERF_RECORD_COMM,
4152 perf_event_comm_event(&comm_event);
4159 struct perf_mmap_event {
4160 struct vm_area_struct *vma;
4162 const char *file_name;
4166 struct perf_event_header header;
4176 static void perf_event_mmap_output(struct perf_event *event,
4177 struct perf_mmap_event *mmap_event)
4179 struct perf_output_handle handle;
4180 struct perf_sample_data sample;
4181 int size = mmap_event->event_id.header.size;
4184 perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
4185 ret = perf_output_begin(&handle, event,
4186 mmap_event->event_id.header.size, 0, 0);
4190 mmap_event->event_id.pid = perf_event_pid(event, current);
4191 mmap_event->event_id.tid = perf_event_tid(event, current);
4193 perf_output_put(&handle, mmap_event->event_id);
4194 perf_output_copy(&handle, mmap_event->file_name,
4195 mmap_event->file_size);
4197 perf_event__output_id_sample(event, &handle, &sample);
4199 perf_output_end(&handle);
4201 mmap_event->event_id.header.size = size;
4204 static int perf_event_mmap_match(struct perf_event *event,
4205 struct perf_mmap_event *mmap_event,
4208 if (event->state < PERF_EVENT_STATE_INACTIVE)
4211 if (!event_filter_match(event))
4214 if ((!executable && event->attr.mmap_data) ||
4215 (executable && event->attr.mmap))
4221 static void perf_event_mmap_ctx(struct perf_event_context *ctx,
4222 struct perf_mmap_event *mmap_event,
4225 struct perf_event *event;
4227 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4228 if (perf_event_mmap_match(event, mmap_event, executable))
4229 perf_event_mmap_output(event, mmap_event);
4233 static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
4235 struct perf_cpu_context *cpuctx;
4236 struct perf_event_context *ctx;
4237 struct vm_area_struct *vma = mmap_event->vma;
4238 struct file *file = vma->vm_file;
4246 memset(tmp, 0, sizeof(tmp));
4250 * d_path works from the end of the buffer backwards, so we
4251 * need to add enough zero bytes after the string to handle
4252 * the 64bit alignment we do later.
4254 buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
4256 name = strncpy(tmp, "//enomem", sizeof(tmp));
4259 name = d_path(&file->f_path, buf, PATH_MAX);
4261 name = strncpy(tmp, "//toolong", sizeof(tmp));
4265 if (arch_vma_name(mmap_event->vma)) {
4266 name = strncpy(tmp, arch_vma_name(mmap_event->vma),
4272 name = strncpy(tmp, "[vdso]", sizeof(tmp));
4274 } else if (vma->vm_start <= vma->vm_mm->start_brk &&
4275 vma->vm_end >= vma->vm_mm->brk) {
4276 name = strncpy(tmp, "[heap]", sizeof(tmp));
4278 } else if (vma->vm_start <= vma->vm_mm->start_stack &&
4279 vma->vm_end >= vma->vm_mm->start_stack) {
4280 name = strncpy(tmp, "[stack]", sizeof(tmp));
4284 name = strncpy(tmp, "//anon", sizeof(tmp));
4289 size = ALIGN(strlen(name)+1, sizeof(u64));
4291 mmap_event->file_name = name;
4292 mmap_event->file_size = size;
4294 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
4297 list_for_each_entry_rcu(pmu, &pmus, entry) {
4298 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
4299 if (cpuctx->active_pmu != pmu)
4301 perf_event_mmap_ctx(&cpuctx->ctx, mmap_event,
4302 vma->vm_flags & VM_EXEC);
4304 ctxn = pmu->task_ctx_nr;
4308 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4310 perf_event_mmap_ctx(ctx, mmap_event,
4311 vma->vm_flags & VM_EXEC);
4314 put_cpu_ptr(pmu->pmu_cpu_context);
4321 void perf_event_mmap(struct vm_area_struct *vma)
4323 struct perf_mmap_event mmap_event;
4325 if (!atomic_read(&nr_mmap_events))
4328 mmap_event = (struct perf_mmap_event){
4334 .type = PERF_RECORD_MMAP,
4335 .misc = PERF_RECORD_MISC_USER,
4340 .start = vma->vm_start,
4341 .len = vma->vm_end - vma->vm_start,
4342 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
4346 perf_event_mmap_event(&mmap_event);
4350 * IRQ throttle logging
4353 static void perf_log_throttle(struct perf_event *event, int enable)
4355 struct perf_output_handle handle;
4356 struct perf_sample_data sample;
4360 struct perf_event_header header;
4364 } throttle_event = {
4366 .type = PERF_RECORD_THROTTLE,
4368 .size = sizeof(throttle_event),
4370 .time = perf_clock(),
4371 .id = primary_event_id(event),
4372 .stream_id = event->id,
4376 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
4378 perf_event_header__init_id(&throttle_event.header, &sample, event);
4380 ret = perf_output_begin(&handle, event,
4381 throttle_event.header.size, 1, 0);
4385 perf_output_put(&handle, throttle_event);
4386 perf_event__output_id_sample(event, &handle, &sample);
4387 perf_output_end(&handle);
4391 * Generic event overflow handling, sampling.
4394 static int __perf_event_overflow(struct perf_event *event, int nmi,
4395 int throttle, struct perf_sample_data *data,
4396 struct pt_regs *regs)
4398 int events = atomic_read(&event->event_limit);
4399 struct hw_perf_event *hwc = &event->hw;
4403 * Non-sampling counters might still use the PMI to fold short
4404 * hardware counters, ignore those.
4406 if (unlikely(!is_sampling_event(event)))
4412 if (hwc->interrupts != MAX_INTERRUPTS) {
4414 if (HZ * hwc->interrupts >
4415 (u64)sysctl_perf_event_sample_rate) {
4416 hwc->interrupts = MAX_INTERRUPTS;
4417 perf_log_throttle(event, 0);
4422 * Keep re-disabling events even though on the previous
4423 * pass we disabled it - just in case we raced with a
4424 * sched-in and the event got enabled again:
4430 if (event->attr.freq) {
4431 u64 now = perf_clock();
4432 s64 delta = now - hwc->freq_time_stamp;
4434 hwc->freq_time_stamp = now;
4436 if (delta > 0 && delta < 2*TICK_NSEC)
4437 perf_adjust_period(event, delta, hwc->last_period);
4441 * XXX event_limit might not quite work as expected on inherited
4445 event->pending_kill = POLL_IN;
4446 if (events && atomic_dec_and_test(&event->event_limit)) {
4448 event->pending_kill = POLL_HUP;
4450 event->pending_disable = 1;
4451 irq_work_queue(&event->pending);
4453 perf_event_disable(event);
4456 if (event->overflow_handler)
4457 event->overflow_handler(event, nmi, data, regs);
4459 perf_event_output(event, nmi, data, regs);
4464 int perf_event_overflow(struct perf_event *event, int nmi,
4465 struct perf_sample_data *data,
4466 struct pt_regs *regs)
4468 return __perf_event_overflow(event, nmi, 1, data, regs);
4472 * Generic software event infrastructure
4475 struct swevent_htable {
4476 struct swevent_hlist *swevent_hlist;
4477 struct mutex hlist_mutex;
4480 /* Recursion avoidance in each contexts */
4481 int recursion[PERF_NR_CONTEXTS];
4484 static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
4487 * We directly increment event->count and keep a second value in
4488 * event->hw.period_left to count intervals. This period event
4489 * is kept in the range [-sample_period, 0] so that we can use the
4493 static u64 perf_swevent_set_period(struct perf_event *event)
4495 struct hw_perf_event *hwc = &event->hw;
4496 u64 period = hwc->last_period;
4500 hwc->last_period = hwc->sample_period;
4503 old = val = local64_read(&hwc->period_left);
4507 nr = div64_u64(period + val, period);
4508 offset = nr * period;
4510 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
4516 static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
4517 int nmi, struct perf_sample_data *data,
4518 struct pt_regs *regs)
4520 struct hw_perf_event *hwc = &event->hw;
4523 data->period = event->hw.last_period;
4525 overflow = perf_swevent_set_period(event);
4527 if (hwc->interrupts == MAX_INTERRUPTS)
4530 for (; overflow; overflow--) {
4531 if (__perf_event_overflow(event, nmi, throttle,
4534 * We inhibit the overflow from happening when
4535 * hwc->interrupts == MAX_INTERRUPTS.
4543 static void perf_swevent_event(struct perf_event *event, u64 nr,
4544 int nmi, struct perf_sample_data *data,
4545 struct pt_regs *regs)
4547 struct hw_perf_event *hwc = &event->hw;
4549 local64_add(nr, &event->count);
4554 if (!is_sampling_event(event))
4557 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
4558 return perf_swevent_overflow(event, 1, nmi, data, regs);
4560 if (local64_add_negative(nr, &hwc->period_left))
4563 perf_swevent_overflow(event, 0, nmi, data, regs);
4566 static int perf_exclude_event(struct perf_event *event,
4567 struct pt_regs *regs)
4569 if (event->hw.state & PERF_HES_STOPPED)
4573 if (event->attr.exclude_user && user_mode(regs))
4576 if (event->attr.exclude_kernel && !user_mode(regs))
4583 static int perf_swevent_match(struct perf_event *event,
4584 enum perf_type_id type,
4586 struct perf_sample_data *data,
4587 struct pt_regs *regs)
4589 if (event->attr.type != type)
4592 if (event->attr.config != event_id)
4595 if (perf_exclude_event(event, regs))
4601 static inline u64 swevent_hash(u64 type, u32 event_id)
4603 u64 val = event_id | (type << 32);
4605 return hash_64(val, SWEVENT_HLIST_BITS);
4608 static inline struct hlist_head *
4609 __find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
4611 u64 hash = swevent_hash(type, event_id);
4613 return &hlist->heads[hash];
4616 /* For the read side: events when they trigger */
4617 static inline struct hlist_head *
4618 find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
4620 struct swevent_hlist *hlist;
4622 hlist = rcu_dereference(swhash->swevent_hlist);
4626 return __find_swevent_head(hlist, type, event_id);
4629 /* For the event head insertion and removal in the hlist */
4630 static inline struct hlist_head *
4631 find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
4633 struct swevent_hlist *hlist;
4634 u32 event_id = event->attr.config;
4635 u64 type = event->attr.type;
4638 * Event scheduling is always serialized against hlist allocation
4639 * and release. Which makes the protected version suitable here.
4640 * The context lock guarantees that.
4642 hlist = rcu_dereference_protected(swhash->swevent_hlist,
4643 lockdep_is_held(&event->ctx->lock));
4647 return __find_swevent_head(hlist, type, event_id);
4650 static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
4652 struct perf_sample_data *data,
4653 struct pt_regs *regs)
4655 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
4656 struct perf_event *event;
4657 struct hlist_node *node;
4658 struct hlist_head *head;
4661 head = find_swevent_head_rcu(swhash, type, event_id);
4665 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
4666 if (perf_swevent_match(event, type, event_id, data, regs))
4667 perf_swevent_event(event, nr, nmi, data, regs);
4673 int perf_swevent_get_recursion_context(void)
4675 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
4677 return get_recursion_context(swhash->recursion);
4679 EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
4681 inline void perf_swevent_put_recursion_context(int rctx)
4683 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
4685 put_recursion_context(swhash->recursion, rctx);
4688 void __perf_sw_event(u32 event_id, u64 nr, int nmi,
4689 struct pt_regs *regs, u64 addr)
4691 struct perf_sample_data data;
4694 preempt_disable_notrace();
4695 rctx = perf_swevent_get_recursion_context();
4699 perf_sample_data_init(&data, addr);
4701 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, nmi, &data, regs);
4703 perf_swevent_put_recursion_context(rctx);
4704 preempt_enable_notrace();
4707 static void perf_swevent_read(struct perf_event *event)
4711 static int perf_swevent_add(struct perf_event *event, int flags)
4713 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
4714 struct hw_perf_event *hwc = &event->hw;
4715 struct hlist_head *head;
4717 if (is_sampling_event(event)) {
4718 hwc->last_period = hwc->sample_period;
4719 perf_swevent_set_period(event);
4722 hwc->state = !(flags & PERF_EF_START);
4724 head = find_swevent_head(swhash, event);
4725 if (WARN_ON_ONCE(!head))
4728 hlist_add_head_rcu(&event->hlist_entry, head);
4733 static void perf_swevent_del(struct perf_event *event, int flags)
4735 hlist_del_rcu(&event->hlist_entry);
4738 static void perf_swevent_start(struct perf_event *event, int flags)
4740 event->hw.state = 0;
4743 static void perf_swevent_stop(struct perf_event *event, int flags)
4745 event->hw.state = PERF_HES_STOPPED;
4748 /* Deref the hlist from the update side */
4749 static inline struct swevent_hlist *
4750 swevent_hlist_deref(struct swevent_htable *swhash)
4752 return rcu_dereference_protected(swhash->swevent_hlist,
4753 lockdep_is_held(&swhash->hlist_mutex));
4756 static void swevent_hlist_release_rcu(struct rcu_head *rcu_head)
4758 struct swevent_hlist *hlist;
4760 hlist = container_of(rcu_head, struct swevent_hlist, rcu_head);
4764 static void swevent_hlist_release(struct swevent_htable *swhash)
4766 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
4771 rcu_assign_pointer(swhash->swevent_hlist, NULL);
4772 call_rcu(&hlist->rcu_head, swevent_hlist_release_rcu);
4775 static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
4777 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
4779 mutex_lock(&swhash->hlist_mutex);
4781 if (!--swhash->hlist_refcount)
4782 swevent_hlist_release(swhash);
4784 mutex_unlock(&swhash->hlist_mutex);
4787 static void swevent_hlist_put(struct perf_event *event)
4791 if (event->cpu != -1) {
4792 swevent_hlist_put_cpu(event, event->cpu);
4796 for_each_possible_cpu(cpu)
4797 swevent_hlist_put_cpu(event, cpu);
4800 static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
4802 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
4805 mutex_lock(&swhash->hlist_mutex);
4807 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
4808 struct swevent_hlist *hlist;
4810 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
4815 rcu_assign_pointer(swhash->swevent_hlist, hlist);
4817 swhash->hlist_refcount++;
4819 mutex_unlock(&swhash->hlist_mutex);
4824 static int swevent_hlist_get(struct perf_event *event)
4827 int cpu, failed_cpu;
4829 if (event->cpu != -1)
4830 return swevent_hlist_get_cpu(event, event->cpu);
4833 for_each_possible_cpu(cpu) {
4834 err = swevent_hlist_get_cpu(event, cpu);
4844 for_each_possible_cpu(cpu) {
4845 if (cpu == failed_cpu)
4847 swevent_hlist_put_cpu(event, cpu);
4854 atomic_t perf_swevent_enabled[PERF_COUNT_SW_MAX];
4856 static void sw_perf_event_destroy(struct perf_event *event)
4858 u64 event_id = event->attr.config;
4860 WARN_ON(event->parent);
4862 jump_label_dec(&perf_swevent_enabled[event_id]);
4863 swevent_hlist_put(event);
4866 static int perf_swevent_init(struct perf_event *event)
4868 int event_id = event->attr.config;
4870 if (event->attr.type != PERF_TYPE_SOFTWARE)
4874 case PERF_COUNT_SW_CPU_CLOCK:
4875 case PERF_COUNT_SW_TASK_CLOCK:
4882 if (event_id >= PERF_COUNT_SW_MAX)
4885 if (!event->parent) {
4888 err = swevent_hlist_get(event);
4892 jump_label_inc(&perf_swevent_enabled[event_id]);
4893 event->destroy = sw_perf_event_destroy;
4899 static struct pmu perf_swevent = {
4900 .task_ctx_nr = perf_sw_context,
4902 .event_init = perf_swevent_init,
4903 .add = perf_swevent_add,
4904 .del = perf_swevent_del,
4905 .start = perf_swevent_start,
4906 .stop = perf_swevent_stop,
4907 .read = perf_swevent_read,
4910 #ifdef CONFIG_EVENT_TRACING
4912 static int perf_tp_filter_match(struct perf_event *event,
4913 struct perf_sample_data *data)
4915 void *record = data->raw->data;
4917 if (likely(!event->filter) || filter_match_preds(event->filter, record))
4922 static int perf_tp_event_match(struct perf_event *event,
4923 struct perf_sample_data *data,
4924 struct pt_regs *regs)
4927 * All tracepoints are from kernel-space.
4929 if (event->attr.exclude_kernel)
4932 if (!perf_tp_filter_match(event, data))
4938 void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
4939 struct pt_regs *regs, struct hlist_head *head, int rctx)
4941 struct perf_sample_data data;
4942 struct perf_event *event;
4943 struct hlist_node *node;
4945 struct perf_raw_record raw = {
4950 perf_sample_data_init(&data, addr);
4953 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
4954 if (perf_tp_event_match(event, &data, regs))
4955 perf_swevent_event(event, count, 1, &data, regs);
4958 perf_swevent_put_recursion_context(rctx);
4960 EXPORT_SYMBOL_GPL(perf_tp_event);
4962 static void tp_perf_event_destroy(struct perf_event *event)
4964 perf_trace_destroy(event);
4967 static int perf_tp_event_init(struct perf_event *event)
4971 if (event->attr.type != PERF_TYPE_TRACEPOINT)
4974 err = perf_trace_init(event);
4978 event->destroy = tp_perf_event_destroy;
4983 static struct pmu perf_tracepoint = {
4984 .task_ctx_nr = perf_sw_context,
4986 .event_init = perf_tp_event_init,
4987 .add = perf_trace_add,
4988 .del = perf_trace_del,
4989 .start = perf_swevent_start,
4990 .stop = perf_swevent_stop,
4991 .read = perf_swevent_read,
4994 static inline void perf_tp_register(void)
4996 perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
4999 static int perf_event_set_filter(struct perf_event *event, void __user *arg)
5004 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5007 filter_str = strndup_user(arg, PAGE_SIZE);
5008 if (IS_ERR(filter_str))
5009 return PTR_ERR(filter_str);
5011 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
5017 static void perf_event_free_filter(struct perf_event *event)
5019 ftrace_profile_free_filter(event);
5024 static inline void perf_tp_register(void)
5028 static int perf_event_set_filter(struct perf_event *event, void __user *arg)
5033 static void perf_event_free_filter(struct perf_event *event)
5037 #endif /* CONFIG_EVENT_TRACING */
5039 #ifdef CONFIG_HAVE_HW_BREAKPOINT
5040 void perf_bp_event(struct perf_event *bp, void *data)
5042 struct perf_sample_data sample;
5043 struct pt_regs *regs = data;
5045 perf_sample_data_init(&sample, bp->attr.bp_addr);
5047 if (!bp->hw.state && !perf_exclude_event(bp, regs))
5048 perf_swevent_event(bp, 1, 1, &sample, regs);
5053 * hrtimer based swevent callback
5056 static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
5058 enum hrtimer_restart ret = HRTIMER_RESTART;
5059 struct perf_sample_data data;
5060 struct pt_regs *regs;
5061 struct perf_event *event;
5064 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
5065 event->pmu->read(event);
5067 perf_sample_data_init(&data, 0);
5068 data.period = event->hw.last_period;
5069 regs = get_irq_regs();
5071 if (regs && !perf_exclude_event(event, regs)) {
5072 if (!(event->attr.exclude_idle && current->pid == 0))
5073 if (perf_event_overflow(event, 0, &data, regs))
5074 ret = HRTIMER_NORESTART;
5077 period = max_t(u64, 10000, event->hw.sample_period);
5078 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
5083 static void perf_swevent_start_hrtimer(struct perf_event *event)
5085 struct hw_perf_event *hwc = &event->hw;
5088 if (!is_sampling_event(event))
5091 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
5092 hwc->hrtimer.function = perf_swevent_hrtimer;
5094 period = local64_read(&hwc->period_left);
5099 local64_set(&hwc->period_left, 0);
5101 period = max_t(u64, 10000, hwc->sample_period);
5103 __hrtimer_start_range_ns(&hwc->hrtimer,
5104 ns_to_ktime(period), 0,
5105 HRTIMER_MODE_REL_PINNED, 0);
5108 static void perf_swevent_cancel_hrtimer(struct perf_event *event)
5110 struct hw_perf_event *hwc = &event->hw;
5112 if (is_sampling_event(event)) {
5113 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
5114 local64_set(&hwc->period_left, ktime_to_ns(remaining));
5116 hrtimer_cancel(&hwc->hrtimer);
5121 * Software event: cpu wall time clock
5124 static void cpu_clock_event_update(struct perf_event *event)
5129 now = local_clock();
5130 prev = local64_xchg(&event->hw.prev_count, now);
5131 local64_add(now - prev, &event->count);
5134 static void cpu_clock_event_start(struct perf_event *event, int flags)
5136 local64_set(&event->hw.prev_count, local_clock());
5137 perf_swevent_start_hrtimer(event);
5140 static void cpu_clock_event_stop(struct perf_event *event, int flags)
5142 perf_swevent_cancel_hrtimer(event);
5143 cpu_clock_event_update(event);
5146 static int cpu_clock_event_add(struct perf_event *event, int flags)
5148 if (flags & PERF_EF_START)
5149 cpu_clock_event_start(event, flags);
5154 static void cpu_clock_event_del(struct perf_event *event, int flags)
5156 cpu_clock_event_stop(event, flags);
5159 static void cpu_clock_event_read(struct perf_event *event)
5161 cpu_clock_event_update(event);
5164 static int cpu_clock_event_init(struct perf_event *event)
5166 if (event->attr.type != PERF_TYPE_SOFTWARE)
5169 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
5175 static struct pmu perf_cpu_clock = {
5176 .task_ctx_nr = perf_sw_context,
5178 .event_init = cpu_clock_event_init,
5179 .add = cpu_clock_event_add,
5180 .del = cpu_clock_event_del,
5181 .start = cpu_clock_event_start,
5182 .stop = cpu_clock_event_stop,
5183 .read = cpu_clock_event_read,
5187 * Software event: task time clock
5190 static void task_clock_event_update(struct perf_event *event, u64 now)
5195 prev = local64_xchg(&event->hw.prev_count, now);
5197 local64_add(delta, &event->count);
5200 static void task_clock_event_start(struct perf_event *event, int flags)
5202 local64_set(&event->hw.prev_count, event->ctx->time);
5203 perf_swevent_start_hrtimer(event);
5206 static void task_clock_event_stop(struct perf_event *event, int flags)
5208 perf_swevent_cancel_hrtimer(event);
5209 task_clock_event_update(event, event->ctx->time);
5212 static int task_clock_event_add(struct perf_event *event, int flags)
5214 if (flags & PERF_EF_START)
5215 task_clock_event_start(event, flags);
5220 static void task_clock_event_del(struct perf_event *event, int flags)
5222 task_clock_event_stop(event, PERF_EF_UPDATE);
5225 static void task_clock_event_read(struct perf_event *event)
5230 update_context_time(event->ctx);
5231 time = event->ctx->time;
5233 u64 now = perf_clock();
5234 u64 delta = now - event->ctx->timestamp;
5235 time = event->ctx->time + delta;
5238 task_clock_event_update(event, time);
5241 static int task_clock_event_init(struct perf_event *event)
5243 if (event->attr.type != PERF_TYPE_SOFTWARE)
5246 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
5252 static struct pmu perf_task_clock = {
5253 .task_ctx_nr = perf_sw_context,
5255 .event_init = task_clock_event_init,
5256 .add = task_clock_event_add,
5257 .del = task_clock_event_del,
5258 .start = task_clock_event_start,
5259 .stop = task_clock_event_stop,
5260 .read = task_clock_event_read,
5263 static void perf_pmu_nop_void(struct pmu *pmu)
5267 static int perf_pmu_nop_int(struct pmu *pmu)
5272 static void perf_pmu_start_txn(struct pmu *pmu)
5274 perf_pmu_disable(pmu);
5277 static int perf_pmu_commit_txn(struct pmu *pmu)
5279 perf_pmu_enable(pmu);
5283 static void perf_pmu_cancel_txn(struct pmu *pmu)
5285 perf_pmu_enable(pmu);
5289 * Ensures all contexts with the same task_ctx_nr have the same
5290 * pmu_cpu_context too.
5292 static void *find_pmu_context(int ctxn)
5299 list_for_each_entry(pmu, &pmus, entry) {
5300 if (pmu->task_ctx_nr == ctxn)
5301 return pmu->pmu_cpu_context;
5307 static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
5311 for_each_possible_cpu(cpu) {
5312 struct perf_cpu_context *cpuctx;
5314 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
5316 if (cpuctx->active_pmu == old_pmu)
5317 cpuctx->active_pmu = pmu;
5321 static void free_pmu_context(struct pmu *pmu)
5325 mutex_lock(&pmus_lock);
5327 * Like a real lame refcount.
5329 list_for_each_entry(i, &pmus, entry) {
5330 if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
5331 update_pmu_context(i, pmu);
5336 free_percpu(pmu->pmu_cpu_context);
5338 mutex_unlock(&pmus_lock);
5340 static struct idr pmu_idr;
5343 type_show(struct device *dev, struct device_attribute *attr, char *page)
5345 struct pmu *pmu = dev_get_drvdata(dev);
5347 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
5350 static struct device_attribute pmu_dev_attrs[] = {
5355 static int pmu_bus_running;
5356 static struct bus_type pmu_bus = {
5357 .name = "event_source",
5358 .dev_attrs = pmu_dev_attrs,
5361 static void pmu_dev_release(struct device *dev)
5366 static int pmu_dev_alloc(struct pmu *pmu)
5370 pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
5374 device_initialize(pmu->dev);
5375 ret = dev_set_name(pmu->dev, "%s", pmu->name);
5379 dev_set_drvdata(pmu->dev, pmu);
5380 pmu->dev->bus = &pmu_bus;
5381 pmu->dev->release = pmu_dev_release;
5382 ret = device_add(pmu->dev);
5390 put_device(pmu->dev);
5394 static struct lock_class_key cpuctx_mutex;
5396 int perf_pmu_register(struct pmu *pmu, char *name, int type)
5400 mutex_lock(&pmus_lock);
5402 pmu->pmu_disable_count = alloc_percpu(int);
5403 if (!pmu->pmu_disable_count)
5412 int err = idr_pre_get(&pmu_idr, GFP_KERNEL);
5416 err = idr_get_new_above(&pmu_idr, pmu, PERF_TYPE_MAX, &type);
5424 if (pmu_bus_running) {
5425 ret = pmu_dev_alloc(pmu);
5431 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
5432 if (pmu->pmu_cpu_context)
5433 goto got_cpu_context;
5435 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
5436 if (!pmu->pmu_cpu_context)
5439 for_each_possible_cpu(cpu) {
5440 struct perf_cpu_context *cpuctx;
5442 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
5443 __perf_event_init_context(&cpuctx->ctx);
5444 lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
5445 cpuctx->ctx.type = cpu_context;
5446 cpuctx->ctx.pmu = pmu;
5447 cpuctx->jiffies_interval = 1;
5448 INIT_LIST_HEAD(&cpuctx->rotation_list);
5449 cpuctx->active_pmu = pmu;
5453 if (!pmu->start_txn) {
5454 if (pmu->pmu_enable) {
5456 * If we have pmu_enable/pmu_disable calls, install
5457 * transaction stubs that use that to try and batch
5458 * hardware accesses.
5460 pmu->start_txn = perf_pmu_start_txn;
5461 pmu->commit_txn = perf_pmu_commit_txn;
5462 pmu->cancel_txn = perf_pmu_cancel_txn;
5464 pmu->start_txn = perf_pmu_nop_void;
5465 pmu->commit_txn = perf_pmu_nop_int;
5466 pmu->cancel_txn = perf_pmu_nop_void;
5470 if (!pmu->pmu_enable) {
5471 pmu->pmu_enable = perf_pmu_nop_void;
5472 pmu->pmu_disable = perf_pmu_nop_void;
5475 list_add_rcu(&pmu->entry, &pmus);
5478 mutex_unlock(&pmus_lock);
5483 device_del(pmu->dev);
5484 put_device(pmu->dev);
5487 if (pmu->type >= PERF_TYPE_MAX)
5488 idr_remove(&pmu_idr, pmu->type);
5491 free_percpu(pmu->pmu_disable_count);
5495 void perf_pmu_unregister(struct pmu *pmu)
5497 mutex_lock(&pmus_lock);
5498 list_del_rcu(&pmu->entry);
5499 mutex_unlock(&pmus_lock);
5502 * We dereference the pmu list under both SRCU and regular RCU, so
5503 * synchronize against both of those.
5505 synchronize_srcu(&pmus_srcu);
5508 free_percpu(pmu->pmu_disable_count);
5509 if (pmu->type >= PERF_TYPE_MAX)
5510 idr_remove(&pmu_idr, pmu->type);
5511 device_del(pmu->dev);
5512 put_device(pmu->dev);
5513 free_pmu_context(pmu);
5516 struct pmu *perf_init_event(struct perf_event *event)
5518 struct pmu *pmu = NULL;
5521 idx = srcu_read_lock(&pmus_srcu);
5524 pmu = idr_find(&pmu_idr, event->attr.type);
5529 list_for_each_entry_rcu(pmu, &pmus, entry) {
5530 int ret = pmu->event_init(event);
5534 if (ret != -ENOENT) {
5539 pmu = ERR_PTR(-ENOENT);
5541 srcu_read_unlock(&pmus_srcu, idx);
5547 * Allocate and initialize a event structure
5549 static struct perf_event *
5550 perf_event_alloc(struct perf_event_attr *attr, int cpu,
5551 struct task_struct *task,
5552 struct perf_event *group_leader,
5553 struct perf_event *parent_event,
5554 perf_overflow_handler_t overflow_handler)
5557 struct perf_event *event;
5558 struct hw_perf_event *hwc;
5561 if ((unsigned)cpu >= nr_cpu_ids) {
5562 if (!task || cpu != -1)
5563 return ERR_PTR(-EINVAL);
5566 event = kzalloc(sizeof(*event), GFP_KERNEL);
5568 return ERR_PTR(-ENOMEM);
5571 * Single events are their own group leaders, with an
5572 * empty sibling list:
5575 group_leader = event;
5577 mutex_init(&event->child_mutex);
5578 INIT_LIST_HEAD(&event->child_list);
5580 INIT_LIST_HEAD(&event->group_entry);
5581 INIT_LIST_HEAD(&event->event_entry);
5582 INIT_LIST_HEAD(&event->sibling_list);
5583 init_waitqueue_head(&event->waitq);
5584 init_irq_work(&event->pending, perf_pending_event);
5586 mutex_init(&event->mmap_mutex);
5589 event->attr = *attr;
5590 event->group_leader = group_leader;
5594 event->parent = parent_event;
5596 event->ns = get_pid_ns(current->nsproxy->pid_ns);
5597 event->id = atomic64_inc_return(&perf_event_id);
5599 event->state = PERF_EVENT_STATE_INACTIVE;
5602 event->attach_state = PERF_ATTACH_TASK;
5603 #ifdef CONFIG_HAVE_HW_BREAKPOINT
5605 * hw_breakpoint is a bit difficult here..
5607 if (attr->type == PERF_TYPE_BREAKPOINT)
5608 event->hw.bp_target = task;
5612 if (!overflow_handler && parent_event)
5613 overflow_handler = parent_event->overflow_handler;
5615 event->overflow_handler = overflow_handler;
5618 event->state = PERF_EVENT_STATE_OFF;
5623 hwc->sample_period = attr->sample_period;
5624 if (attr->freq && attr->sample_freq)
5625 hwc->sample_period = 1;
5626 hwc->last_period = hwc->sample_period;
5628 local64_set(&hwc->period_left, hwc->sample_period);
5631 * we currently do not support PERF_FORMAT_GROUP on inherited events
5633 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
5636 pmu = perf_init_event(event);
5642 else if (IS_ERR(pmu))
5647 put_pid_ns(event->ns);
5649 return ERR_PTR(err);
5654 if (!event->parent) {
5655 if (event->attach_state & PERF_ATTACH_TASK)
5656 jump_label_inc(&perf_task_events);
5657 if (event->attr.mmap || event->attr.mmap_data)
5658 atomic_inc(&nr_mmap_events);
5659 if (event->attr.comm)
5660 atomic_inc(&nr_comm_events);
5661 if (event->attr.task)
5662 atomic_inc(&nr_task_events);
5663 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
5664 err = get_callchain_buffers();
5667 return ERR_PTR(err);
5675 static int perf_copy_attr(struct perf_event_attr __user *uattr,
5676 struct perf_event_attr *attr)
5681 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
5685 * zero the full structure, so that a short copy will be nice.
5687 memset(attr, 0, sizeof(*attr));
5689 ret = get_user(size, &uattr->size);
5693 if (size > PAGE_SIZE) /* silly large */
5696 if (!size) /* abi compat */
5697 size = PERF_ATTR_SIZE_VER0;
5699 if (size < PERF_ATTR_SIZE_VER0)
5703 * If we're handed a bigger struct than we know of,
5704 * ensure all the unknown bits are 0 - i.e. new
5705 * user-space does not rely on any kernel feature
5706 * extensions we dont know about yet.
5708 if (size > sizeof(*attr)) {
5709 unsigned char __user *addr;
5710 unsigned char __user *end;
5713 addr = (void __user *)uattr + sizeof(*attr);
5714 end = (void __user *)uattr + size;
5716 for (; addr < end; addr++) {
5717 ret = get_user(val, addr);
5723 size = sizeof(*attr);
5726 ret = copy_from_user(attr, uattr, size);
5731 * If the type exists, the corresponding creation will verify
5734 if (attr->type >= PERF_TYPE_MAX)
5737 if (attr->__reserved_1)
5740 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
5743 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
5750 put_user(sizeof(*attr), &uattr->size);
5756 perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
5758 struct perf_buffer *buffer = NULL, *old_buffer = NULL;
5764 /* don't allow circular references */
5765 if (event == output_event)
5769 * Don't allow cross-cpu buffers
5771 if (output_event->cpu != event->cpu)
5775 * If its not a per-cpu buffer, it must be the same task.
5777 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
5781 mutex_lock(&event->mmap_mutex);
5782 /* Can't redirect output if we've got an active mmap() */
5783 if (atomic_read(&event->mmap_count))
5787 /* get the buffer we want to redirect to */
5788 buffer = perf_buffer_get(output_event);
5793 old_buffer = event->buffer;
5794 rcu_assign_pointer(event->buffer, buffer);
5797 mutex_unlock(&event->mmap_mutex);
5800 perf_buffer_put(old_buffer);
5806 * sys_perf_event_open - open a performance event, associate it to a task/cpu
5808 * @attr_uptr: event_id type attributes for monitoring/sampling
5811 * @group_fd: group leader event fd
5813 SYSCALL_DEFINE5(perf_event_open,
5814 struct perf_event_attr __user *, attr_uptr,
5815 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
5817 struct perf_event *group_leader = NULL, *output_event = NULL;
5818 struct perf_event *event, *sibling;
5819 struct perf_event_attr attr;
5820 struct perf_event_context *ctx;
5821 struct file *event_file = NULL;
5822 struct file *group_file = NULL;
5823 struct task_struct *task = NULL;
5827 int fput_needed = 0;
5830 /* for future expandability... */
5831 if (flags & ~(PERF_FLAG_FD_NO_GROUP | PERF_FLAG_FD_OUTPUT))
5834 err = perf_copy_attr(attr_uptr, &attr);
5838 if (!attr.exclude_kernel) {
5839 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
5844 if (attr.sample_freq > sysctl_perf_event_sample_rate)
5848 event_fd = get_unused_fd_flags(O_RDWR);
5852 if (group_fd != -1) {
5853 group_leader = perf_fget_light(group_fd, &fput_needed);
5854 if (IS_ERR(group_leader)) {
5855 err = PTR_ERR(group_leader);
5858 group_file = group_leader->filp;
5859 if (flags & PERF_FLAG_FD_OUTPUT)
5860 output_event = group_leader;
5861 if (flags & PERF_FLAG_FD_NO_GROUP)
5862 group_leader = NULL;
5866 task = find_lively_task_by_vpid(pid);
5868 err = PTR_ERR(task);
5873 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL, NULL);
5874 if (IS_ERR(event)) {
5875 err = PTR_ERR(event);
5880 * Special case software events and allow them to be part of
5881 * any hardware group.
5886 (is_software_event(event) != is_software_event(group_leader))) {
5887 if (is_software_event(event)) {
5889 * If event and group_leader are not both a software
5890 * event, and event is, then group leader is not.
5892 * Allow the addition of software events to !software
5893 * groups, this is safe because software events never
5896 pmu = group_leader->pmu;
5897 } else if (is_software_event(group_leader) &&
5898 (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
5900 * In case the group is a pure software group, and we
5901 * try to add a hardware event, move the whole group to
5902 * the hardware context.
5909 * Get the target context (task or percpu):
5911 ctx = find_get_context(pmu, task, cpu);
5918 * Look up the group leader (we will attach this event to it):
5924 * Do not allow a recursive hierarchy (this new sibling
5925 * becoming part of another group-sibling):
5927 if (group_leader->group_leader != group_leader)
5930 * Do not allow to attach to a group in a different
5931 * task or CPU context:
5934 if (group_leader->ctx->type != ctx->type)
5937 if (group_leader->ctx != ctx)
5942 * Only a group leader can be exclusive or pinned
5944 if (attr.exclusive || attr.pinned)
5949 err = perf_event_set_output(event, output_event);
5954 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, O_RDWR);
5955 if (IS_ERR(event_file)) {
5956 err = PTR_ERR(event_file);
5961 struct perf_event_context *gctx = group_leader->ctx;
5963 mutex_lock(&gctx->mutex);
5964 perf_event_remove_from_context(group_leader);
5965 list_for_each_entry(sibling, &group_leader->sibling_list,
5967 perf_event_remove_from_context(sibling);
5970 mutex_unlock(&gctx->mutex);
5974 event->filp = event_file;
5975 WARN_ON_ONCE(ctx->parent_ctx);
5976 mutex_lock(&ctx->mutex);
5979 perf_install_in_context(ctx, group_leader, cpu);
5981 list_for_each_entry(sibling, &group_leader->sibling_list,
5983 perf_install_in_context(ctx, sibling, cpu);
5988 perf_install_in_context(ctx, event, cpu);
5990 mutex_unlock(&ctx->mutex);
5992 event->owner = current;
5994 mutex_lock(¤t->perf_event_mutex);
5995 list_add_tail(&event->owner_entry, ¤t->perf_event_list);
5996 mutex_unlock(¤t->perf_event_mutex);
5999 * Precalculate sample_data sizes
6001 perf_event__header_size(event);
6002 perf_event__id_header_size(event);
6005 * Drop the reference on the group_event after placing the
6006 * new event on the sibling_list. This ensures destruction
6007 * of the group leader will find the pointer to itself in
6008 * perf_group_detach().
6010 fput_light(group_file, fput_needed);
6011 fd_install(event_fd, event_file);
6020 put_task_struct(task);
6022 fput_light(group_file, fput_needed);
6024 put_unused_fd(event_fd);
6029 * perf_event_create_kernel_counter
6031 * @attr: attributes of the counter to create
6032 * @cpu: cpu in which the counter is bound
6033 * @task: task to profile (NULL for percpu)
6036 perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
6037 struct task_struct *task,
6038 perf_overflow_handler_t overflow_handler)
6040 struct perf_event_context *ctx;
6041 struct perf_event *event;
6045 * Get the target context (task or percpu):
6048 event = perf_event_alloc(attr, cpu, task, NULL, NULL, overflow_handler);
6049 if (IS_ERR(event)) {
6050 err = PTR_ERR(event);
6054 ctx = find_get_context(event->pmu, task, cpu);
6061 WARN_ON_ONCE(ctx->parent_ctx);
6062 mutex_lock(&ctx->mutex);
6063 perf_install_in_context(ctx, event, cpu);
6065 mutex_unlock(&ctx->mutex);
6072 return ERR_PTR(err);
6074 EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
6076 static void sync_child_event(struct perf_event *child_event,
6077 struct task_struct *child)
6079 struct perf_event *parent_event = child_event->parent;
6082 if (child_event->attr.inherit_stat)
6083 perf_event_read_event(child_event, child);
6085 child_val = perf_event_count(child_event);
6088 * Add back the child's count to the parent's count:
6090 atomic64_add(child_val, &parent_event->child_count);
6091 atomic64_add(child_event->total_time_enabled,
6092 &parent_event->child_total_time_enabled);
6093 atomic64_add(child_event->total_time_running,
6094 &parent_event->child_total_time_running);
6097 * Remove this event from the parent's list
6099 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
6100 mutex_lock(&parent_event->child_mutex);
6101 list_del_init(&child_event->child_list);
6102 mutex_unlock(&parent_event->child_mutex);
6105 * Release the parent event, if this was the last
6108 fput(parent_event->filp);
6112 __perf_event_exit_task(struct perf_event *child_event,
6113 struct perf_event_context *child_ctx,
6114 struct task_struct *child)
6116 struct perf_event *parent_event;
6118 perf_event_remove_from_context(child_event);
6120 parent_event = child_event->parent;
6122 * It can happen that parent exits first, and has events
6123 * that are still around due to the child reference. These
6124 * events need to be zapped - but otherwise linger.
6127 sync_child_event(child_event, child);
6128 free_event(child_event);
6132 static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
6134 struct perf_event *child_event, *tmp;
6135 struct perf_event_context *child_ctx;
6136 unsigned long flags;
6138 if (likely(!child->perf_event_ctxp[ctxn])) {
6139 perf_event_task(child, NULL, 0);
6143 local_irq_save(flags);
6145 * We can't reschedule here because interrupts are disabled,
6146 * and either child is current or it is a task that can't be
6147 * scheduled, so we are now safe from rescheduling changing
6150 child_ctx = rcu_dereference_raw(child->perf_event_ctxp[ctxn]);
6151 task_ctx_sched_out(child_ctx, EVENT_ALL);
6154 * Take the context lock here so that if find_get_context is
6155 * reading child->perf_event_ctxp, we wait until it has
6156 * incremented the context's refcount before we do put_ctx below.
6158 raw_spin_lock(&child_ctx->lock);
6159 child->perf_event_ctxp[ctxn] = NULL;
6161 * If this context is a clone; unclone it so it can't get
6162 * swapped to another process while we're removing all
6163 * the events from it.
6165 unclone_ctx(child_ctx);
6166 update_context_time(child_ctx);
6167 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
6170 * Report the task dead after unscheduling the events so that we
6171 * won't get any samples after PERF_RECORD_EXIT. We can however still
6172 * get a few PERF_RECORD_READ events.
6174 perf_event_task(child, child_ctx, 0);
6177 * We can recurse on the same lock type through:
6179 * __perf_event_exit_task()
6180 * sync_child_event()
6181 * fput(parent_event->filp)
6183 * mutex_lock(&ctx->mutex)
6185 * But since its the parent context it won't be the same instance.
6187 mutex_lock(&child_ctx->mutex);
6190 list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
6192 __perf_event_exit_task(child_event, child_ctx, child);
6194 list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
6196 __perf_event_exit_task(child_event, child_ctx, child);
6199 * If the last event was a group event, it will have appended all
6200 * its siblings to the list, but we obtained 'tmp' before that which
6201 * will still point to the list head terminating the iteration.
6203 if (!list_empty(&child_ctx->pinned_groups) ||
6204 !list_empty(&child_ctx->flexible_groups))
6207 mutex_unlock(&child_ctx->mutex);
6213 * When a child task exits, feed back event values to parent events.
6215 void perf_event_exit_task(struct task_struct *child)
6217 struct perf_event *event, *tmp;
6220 mutex_lock(&child->perf_event_mutex);
6221 list_for_each_entry_safe(event, tmp, &child->perf_event_list,
6223 list_del_init(&event->owner_entry);
6226 * Ensure the list deletion is visible before we clear
6227 * the owner, closes a race against perf_release() where
6228 * we need to serialize on the owner->perf_event_mutex.
6231 event->owner = NULL;
6233 mutex_unlock(&child->perf_event_mutex);
6235 for_each_task_context_nr(ctxn)
6236 perf_event_exit_task_context(child, ctxn);
6239 static void perf_free_event(struct perf_event *event,
6240 struct perf_event_context *ctx)
6242 struct perf_event *parent = event->parent;
6244 if (WARN_ON_ONCE(!parent))
6247 mutex_lock(&parent->child_mutex);
6248 list_del_init(&event->child_list);
6249 mutex_unlock(&parent->child_mutex);
6253 perf_group_detach(event);
6254 list_del_event(event, ctx);
6259 * free an unexposed, unused context as created by inheritance by
6260 * perf_event_init_task below, used by fork() in case of fail.
6262 void perf_event_free_task(struct task_struct *task)
6264 struct perf_event_context *ctx;
6265 struct perf_event *event, *tmp;
6268 for_each_task_context_nr(ctxn) {
6269 ctx = task->perf_event_ctxp[ctxn];
6273 mutex_lock(&ctx->mutex);
6275 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
6277 perf_free_event(event, ctx);
6279 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
6281 perf_free_event(event, ctx);
6283 if (!list_empty(&ctx->pinned_groups) ||
6284 !list_empty(&ctx->flexible_groups))
6287 mutex_unlock(&ctx->mutex);
6293 void perf_event_delayed_put(struct task_struct *task)
6297 for_each_task_context_nr(ctxn)
6298 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
6302 * inherit a event from parent task to child task:
6304 static struct perf_event *
6305 inherit_event(struct perf_event *parent_event,
6306 struct task_struct *parent,
6307 struct perf_event_context *parent_ctx,
6308 struct task_struct *child,
6309 struct perf_event *group_leader,
6310 struct perf_event_context *child_ctx)
6312 struct perf_event *child_event;
6313 unsigned long flags;
6316 * Instead of creating recursive hierarchies of events,
6317 * we link inherited events back to the original parent,
6318 * which has a filp for sure, which we use as the reference
6321 if (parent_event->parent)
6322 parent_event = parent_event->parent;
6324 child_event = perf_event_alloc(&parent_event->attr,
6327 group_leader, parent_event,
6329 if (IS_ERR(child_event))
6334 * Make the child state follow the state of the parent event,
6335 * not its attr.disabled bit. We hold the parent's mutex,
6336 * so we won't race with perf_event_{en, dis}able_family.
6338 if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
6339 child_event->state = PERF_EVENT_STATE_INACTIVE;
6341 child_event->state = PERF_EVENT_STATE_OFF;
6343 if (parent_event->attr.freq) {
6344 u64 sample_period = parent_event->hw.sample_period;
6345 struct hw_perf_event *hwc = &child_event->hw;
6347 hwc->sample_period = sample_period;
6348 hwc->last_period = sample_period;
6350 local64_set(&hwc->period_left, sample_period);
6353 child_event->ctx = child_ctx;
6354 child_event->overflow_handler = parent_event->overflow_handler;
6357 * Precalculate sample_data sizes
6359 perf_event__header_size(child_event);
6360 perf_event__id_header_size(child_event);
6363 * Link it up in the child's context:
6365 raw_spin_lock_irqsave(&child_ctx->lock, flags);
6366 add_event_to_ctx(child_event, child_ctx);
6367 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
6370 * Get a reference to the parent filp - we will fput it
6371 * when the child event exits. This is safe to do because
6372 * we are in the parent and we know that the filp still
6373 * exists and has a nonzero count:
6375 atomic_long_inc(&parent_event->filp->f_count);
6378 * Link this into the parent event's child list
6380 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
6381 mutex_lock(&parent_event->child_mutex);
6382 list_add_tail(&child_event->child_list, &parent_event->child_list);
6383 mutex_unlock(&parent_event->child_mutex);
6388 static int inherit_group(struct perf_event *parent_event,
6389 struct task_struct *parent,
6390 struct perf_event_context *parent_ctx,
6391 struct task_struct *child,
6392 struct perf_event_context *child_ctx)
6394 struct perf_event *leader;
6395 struct perf_event *sub;
6396 struct perf_event *child_ctr;
6398 leader = inherit_event(parent_event, parent, parent_ctx,
6399 child, NULL, child_ctx);
6401 return PTR_ERR(leader);
6402 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
6403 child_ctr = inherit_event(sub, parent, parent_ctx,
6404 child, leader, child_ctx);
6405 if (IS_ERR(child_ctr))
6406 return PTR_ERR(child_ctr);
6412 inherit_task_group(struct perf_event *event, struct task_struct *parent,
6413 struct perf_event_context *parent_ctx,
6414 struct task_struct *child, int ctxn,
6418 struct perf_event_context *child_ctx;
6420 if (!event->attr.inherit) {
6425 child_ctx = child->perf_event_ctxp[ctxn];
6428 * This is executed from the parent task context, so
6429 * inherit events that have been marked for cloning.
6430 * First allocate and initialize a context for the
6434 child_ctx = alloc_perf_context(event->pmu, child);
6438 child->perf_event_ctxp[ctxn] = child_ctx;
6441 ret = inherit_group(event, parent, parent_ctx,
6451 * Initialize the perf_event context in task_struct
6453 int perf_event_init_context(struct task_struct *child, int ctxn)
6455 struct perf_event_context *child_ctx, *parent_ctx;
6456 struct perf_event_context *cloned_ctx;
6457 struct perf_event *event;
6458 struct task_struct *parent = current;
6459 int inherited_all = 1;
6460 unsigned long flags;
6463 if (likely(!parent->perf_event_ctxp[ctxn]))
6467 * If the parent's context is a clone, pin it so it won't get
6470 parent_ctx = perf_pin_task_context(parent, ctxn);
6473 * No need to check if parent_ctx != NULL here; since we saw
6474 * it non-NULL earlier, the only reason for it to become NULL
6475 * is if we exit, and since we're currently in the middle of
6476 * a fork we can't be exiting at the same time.
6480 * Lock the parent list. No need to lock the child - not PID
6481 * hashed yet and not running, so nobody can access it.
6483 mutex_lock(&parent_ctx->mutex);
6486 * We dont have to disable NMIs - we are only looking at
6487 * the list, not manipulating it:
6489 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
6490 ret = inherit_task_group(event, parent, parent_ctx,
6491 child, ctxn, &inherited_all);
6497 * We can't hold ctx->lock when iterating the ->flexible_group list due
6498 * to allocations, but we need to prevent rotation because
6499 * rotate_ctx() will change the list from interrupt context.
6501 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
6502 parent_ctx->rotate_disable = 1;
6503 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
6505 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
6506 ret = inherit_task_group(event, parent, parent_ctx,
6507 child, ctxn, &inherited_all);
6512 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
6513 parent_ctx->rotate_disable = 0;
6515 child_ctx = child->perf_event_ctxp[ctxn];
6517 if (child_ctx && inherited_all) {
6519 * Mark the child context as a clone of the parent
6520 * context, or of whatever the parent is a clone of.
6522 * Note that if the parent is a clone, the holding of
6523 * parent_ctx->lock avoids it from being uncloned.
6525 cloned_ctx = parent_ctx->parent_ctx;
6527 child_ctx->parent_ctx = cloned_ctx;
6528 child_ctx->parent_gen = parent_ctx->parent_gen;
6530 child_ctx->parent_ctx = parent_ctx;
6531 child_ctx->parent_gen = parent_ctx->generation;
6533 get_ctx(child_ctx->parent_ctx);
6536 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
6537 mutex_unlock(&parent_ctx->mutex);
6539 perf_unpin_context(parent_ctx);
6545 * Initialize the perf_event context in task_struct
6547 int perf_event_init_task(struct task_struct *child)
6551 memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
6552 mutex_init(&child->perf_event_mutex);
6553 INIT_LIST_HEAD(&child->perf_event_list);
6555 for_each_task_context_nr(ctxn) {
6556 ret = perf_event_init_context(child, ctxn);
6564 static void __init perf_event_init_all_cpus(void)
6566 struct swevent_htable *swhash;
6569 for_each_possible_cpu(cpu) {
6570 swhash = &per_cpu(swevent_htable, cpu);
6571 mutex_init(&swhash->hlist_mutex);
6572 INIT_LIST_HEAD(&per_cpu(rotation_list, cpu));
6576 static void __cpuinit perf_event_init_cpu(int cpu)
6578 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
6580 mutex_lock(&swhash->hlist_mutex);
6581 if (swhash->hlist_refcount > 0) {
6582 struct swevent_hlist *hlist;
6584 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
6586 rcu_assign_pointer(swhash->swevent_hlist, hlist);
6588 mutex_unlock(&swhash->hlist_mutex);
6591 #if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC
6592 static void perf_pmu_rotate_stop(struct pmu *pmu)
6594 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
6596 WARN_ON(!irqs_disabled());
6598 list_del_init(&cpuctx->rotation_list);
6601 static void __perf_event_exit_context(void *__info)
6603 struct perf_event_context *ctx = __info;
6604 struct perf_event *event, *tmp;
6606 perf_pmu_rotate_stop(ctx->pmu);
6608 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
6609 __perf_event_remove_from_context(event);
6610 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, group_entry)
6611 __perf_event_remove_from_context(event);
6614 static void perf_event_exit_cpu_context(int cpu)
6616 struct perf_event_context *ctx;
6620 idx = srcu_read_lock(&pmus_srcu);
6621 list_for_each_entry_rcu(pmu, &pmus, entry) {
6622 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
6624 mutex_lock(&ctx->mutex);
6625 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
6626 mutex_unlock(&ctx->mutex);
6628 srcu_read_unlock(&pmus_srcu, idx);
6631 static void perf_event_exit_cpu(int cpu)
6633 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
6635 mutex_lock(&swhash->hlist_mutex);
6636 swevent_hlist_release(swhash);
6637 mutex_unlock(&swhash->hlist_mutex);
6639 perf_event_exit_cpu_context(cpu);
6642 static inline void perf_event_exit_cpu(int cpu) { }
6646 perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
6650 for_each_online_cpu(cpu)
6651 perf_event_exit_cpu(cpu);
6657 * Run the perf reboot notifier at the very last possible moment so that
6658 * the generic watchdog code runs as long as possible.
6660 static struct notifier_block perf_reboot_notifier = {
6661 .notifier_call = perf_reboot,
6662 .priority = INT_MIN,
6665 static int __cpuinit
6666 perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
6668 unsigned int cpu = (long)hcpu;
6670 switch (action & ~CPU_TASKS_FROZEN) {
6672 case CPU_UP_PREPARE:
6673 case CPU_DOWN_FAILED:
6674 perf_event_init_cpu(cpu);
6677 case CPU_UP_CANCELED:
6678 case CPU_DOWN_PREPARE:
6679 perf_event_exit_cpu(cpu);
6689 void __init perf_event_init(void)
6695 perf_event_init_all_cpus();
6696 init_srcu_struct(&pmus_srcu);
6697 perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
6698 perf_pmu_register(&perf_cpu_clock, NULL, -1);
6699 perf_pmu_register(&perf_task_clock, NULL, -1);
6701 perf_cpu_notifier(perf_cpu_notify);
6702 register_reboot_notifier(&perf_reboot_notifier);
6704 ret = init_hw_breakpoint();
6705 WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
6708 static int __init perf_event_sysfs_init(void)
6713 mutex_lock(&pmus_lock);
6715 ret = bus_register(&pmu_bus);
6719 list_for_each_entry(pmu, &pmus, entry) {
6720 if (!pmu->name || pmu->type < 0)
6723 ret = pmu_dev_alloc(pmu);
6724 WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
6726 pmu_bus_running = 1;
6730 mutex_unlock(&pmus_lock);
6734 device_initcall(perf_event_sysfs_init);