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);
786 event_sched_in(struct perf_event *event,
787 struct perf_cpu_context *cpuctx,
788 struct perf_event_context *ctx)
790 u64 tstamp = perf_event_time(event);
792 if (event->state <= PERF_EVENT_STATE_OFF)
795 event->state = PERF_EVENT_STATE_ACTIVE;
796 event->oncpu = smp_processor_id();
798 * The new state must be visible before we turn it on in the hardware:
802 if (event->pmu->add(event, PERF_EF_START)) {
803 event->state = PERF_EVENT_STATE_INACTIVE;
808 event->tstamp_running += tstamp - event->tstamp_stopped;
810 event->shadow_ctx_time = tstamp - ctx->timestamp;
812 if (!is_software_event(event))
813 cpuctx->active_oncpu++;
816 if (event->attr.exclusive)
817 cpuctx->exclusive = 1;
823 group_sched_in(struct perf_event *group_event,
824 struct perf_cpu_context *cpuctx,
825 struct perf_event_context *ctx)
827 struct perf_event *event, *partial_group = NULL;
828 struct pmu *pmu = group_event->pmu;
830 bool simulate = false;
832 if (group_event->state == PERF_EVENT_STATE_OFF)
837 if (event_sched_in(group_event, cpuctx, ctx)) {
838 pmu->cancel_txn(pmu);
843 * Schedule in siblings as one group (if any):
845 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
846 if (event_sched_in(event, cpuctx, ctx)) {
847 partial_group = event;
852 if (!pmu->commit_txn(pmu))
857 * Groups can be scheduled in as one unit only, so undo any
858 * partial group before returning:
859 * The events up to the failed event are scheduled out normally,
860 * tstamp_stopped will be updated.
862 * The failed events and the remaining siblings need to have
863 * their timings updated as if they had gone thru event_sched_in()
864 * and event_sched_out(). This is required to get consistent timings
865 * across the group. This also takes care of the case where the group
866 * could never be scheduled by ensuring tstamp_stopped is set to mark
867 * the time the event was actually stopped, such that time delta
868 * calculation in update_event_times() is correct.
870 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
871 if (event == partial_group)
875 event->tstamp_running += now - event->tstamp_stopped;
876 event->tstamp_stopped = now;
878 event_sched_out(event, cpuctx, ctx);
881 event_sched_out(group_event, cpuctx, ctx);
883 pmu->cancel_txn(pmu);
889 * Work out whether we can put this event group on the CPU now.
891 static int group_can_go_on(struct perf_event *event,
892 struct perf_cpu_context *cpuctx,
896 * Groups consisting entirely of software events can always go on.
898 if (event->group_flags & PERF_GROUP_SOFTWARE)
901 * If an exclusive group is already on, no other hardware
904 if (cpuctx->exclusive)
907 * If this group is exclusive and there are already
908 * events on the CPU, it can't go on.
910 if (event->attr.exclusive && cpuctx->active_oncpu)
913 * Otherwise, try to add it if all previous groups were able
919 static void add_event_to_ctx(struct perf_event *event,
920 struct perf_event_context *ctx)
922 u64 tstamp = perf_event_time(event);
924 list_add_event(event, ctx);
925 perf_group_attach(event);
926 event->tstamp_enabled = tstamp;
927 event->tstamp_running = tstamp;
928 event->tstamp_stopped = tstamp;
932 * Cross CPU call to install and enable a performance event
934 * Must be called with ctx->mutex held
936 static void __perf_install_in_context(void *info)
938 struct perf_event *event = info;
939 struct perf_event_context *ctx = event->ctx;
940 struct perf_event *leader = event->group_leader;
941 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
945 * If this is a task context, we need to check whether it is
946 * the current task context of this cpu. If not it has been
947 * scheduled out before the smp call arrived.
948 * Or possibly this is the right context but it isn't
949 * on this cpu because it had no events.
951 if (ctx->task && cpuctx->task_ctx != ctx) {
952 if (cpuctx->task_ctx || ctx->task != current)
954 cpuctx->task_ctx = ctx;
957 raw_spin_lock(&ctx->lock);
959 update_context_time(ctx);
961 add_event_to_ctx(event, ctx);
963 if (!event_filter_match(event))
967 * Don't put the event on if it is disabled or if
968 * it is in a group and the group isn't on.
970 if (event->state != PERF_EVENT_STATE_INACTIVE ||
971 (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE))
975 * An exclusive event can't go on if there are already active
976 * hardware events, and no hardware event can go on if there
977 * is already an exclusive event on.
979 if (!group_can_go_on(event, cpuctx, 1))
982 err = event_sched_in(event, cpuctx, ctx);
986 * This event couldn't go on. If it is in a group
987 * then we have to pull the whole group off.
988 * If the event group is pinned then put it in error state.
991 group_sched_out(leader, cpuctx, ctx);
992 if (leader->attr.pinned) {
993 update_group_times(leader);
994 leader->state = PERF_EVENT_STATE_ERROR;
999 raw_spin_unlock(&ctx->lock);
1003 * Attach a performance event to a context
1005 * First we add the event to the list with the hardware enable bit
1006 * in event->hw_config cleared.
1008 * If the event is attached to a task which is on a CPU we use a smp
1009 * call to enable it in the task context. The task might have been
1010 * scheduled away, but we check this in the smp call again.
1012 * Must be called with ctx->mutex held.
1015 perf_install_in_context(struct perf_event_context *ctx,
1016 struct perf_event *event,
1019 struct task_struct *task = ctx->task;
1025 * Per cpu events are installed via an smp call and
1026 * the install is always successful.
1028 smp_call_function_single(cpu, __perf_install_in_context,
1034 task_oncpu_function_call(task, __perf_install_in_context,
1037 raw_spin_lock_irq(&ctx->lock);
1039 * we need to retry the smp call.
1041 if (ctx->is_active && list_empty(&event->group_entry)) {
1042 raw_spin_unlock_irq(&ctx->lock);
1047 * The lock prevents that this context is scheduled in so we
1048 * can add the event safely, if it the call above did not
1051 if (list_empty(&event->group_entry))
1052 add_event_to_ctx(event, ctx);
1053 raw_spin_unlock_irq(&ctx->lock);
1057 * Put a event into inactive state and update time fields.
1058 * Enabling the leader of a group effectively enables all
1059 * the group members that aren't explicitly disabled, so we
1060 * have to update their ->tstamp_enabled also.
1061 * Note: this works for group members as well as group leaders
1062 * since the non-leader members' sibling_lists will be empty.
1064 static void __perf_event_mark_enabled(struct perf_event *event,
1065 struct perf_event_context *ctx)
1067 struct perf_event *sub;
1068 u64 tstamp = perf_event_time(event);
1070 event->state = PERF_EVENT_STATE_INACTIVE;
1071 event->tstamp_enabled = tstamp - event->total_time_enabled;
1072 list_for_each_entry(sub, &event->sibling_list, group_entry) {
1073 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
1074 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
1079 * Cross CPU call to enable a performance event
1081 static void __perf_event_enable(void *info)
1083 struct perf_event *event = info;
1084 struct perf_event_context *ctx = event->ctx;
1085 struct perf_event *leader = event->group_leader;
1086 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1090 * If this is a per-task event, need to check whether this
1091 * event's task is the current task on this cpu.
1093 if (ctx->task && cpuctx->task_ctx != ctx) {
1094 if (cpuctx->task_ctx || ctx->task != current)
1096 cpuctx->task_ctx = ctx;
1099 raw_spin_lock(&ctx->lock);
1101 update_context_time(ctx);
1103 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1105 __perf_event_mark_enabled(event, ctx);
1107 if (!event_filter_match(event))
1111 * If the event is in a group and isn't the group leader,
1112 * then don't put it on unless the group is on.
1114 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
1117 if (!group_can_go_on(event, cpuctx, 1)) {
1120 if (event == leader)
1121 err = group_sched_in(event, cpuctx, ctx);
1123 err = event_sched_in(event, cpuctx, ctx);
1128 * If this event can't go on and it's part of a
1129 * group, then the whole group has to come off.
1131 if (leader != event)
1132 group_sched_out(leader, cpuctx, ctx);
1133 if (leader->attr.pinned) {
1134 update_group_times(leader);
1135 leader->state = PERF_EVENT_STATE_ERROR;
1140 raw_spin_unlock(&ctx->lock);
1146 * If event->ctx is a cloned context, callers must make sure that
1147 * every task struct that event->ctx->task could possibly point to
1148 * remains valid. This condition is satisfied when called through
1149 * perf_event_for_each_child or perf_event_for_each as described
1150 * for perf_event_disable.
1152 void perf_event_enable(struct perf_event *event)
1154 struct perf_event_context *ctx = event->ctx;
1155 struct task_struct *task = ctx->task;
1159 * Enable the event on the cpu that it's on
1161 smp_call_function_single(event->cpu, __perf_event_enable,
1166 raw_spin_lock_irq(&ctx->lock);
1167 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1171 * If the event is in error state, clear that first.
1172 * That way, if we see the event in error state below, we
1173 * know that it has gone back into error state, as distinct
1174 * from the task having been scheduled away before the
1175 * cross-call arrived.
1177 if (event->state == PERF_EVENT_STATE_ERROR)
1178 event->state = PERF_EVENT_STATE_OFF;
1181 raw_spin_unlock_irq(&ctx->lock);
1182 task_oncpu_function_call(task, __perf_event_enable, event);
1184 raw_spin_lock_irq(&ctx->lock);
1187 * If the context is active and the event is still off,
1188 * we need to retry the cross-call.
1190 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF)
1194 * Since we have the lock this context can't be scheduled
1195 * in, so we can change the state safely.
1197 if (event->state == PERF_EVENT_STATE_OFF)
1198 __perf_event_mark_enabled(event, ctx);
1201 raw_spin_unlock_irq(&ctx->lock);
1204 static int perf_event_refresh(struct perf_event *event, int refresh)
1207 * not supported on inherited events
1209 if (event->attr.inherit || !is_sampling_event(event))
1212 atomic_add(refresh, &event->event_limit);
1213 perf_event_enable(event);
1218 static void ctx_sched_out(struct perf_event_context *ctx,
1219 struct perf_cpu_context *cpuctx,
1220 enum event_type_t event_type)
1222 struct perf_event *event;
1224 raw_spin_lock(&ctx->lock);
1225 perf_pmu_disable(ctx->pmu);
1227 if (likely(!ctx->nr_events))
1229 update_context_time(ctx);
1231 if (!ctx->nr_active)
1234 if (event_type & EVENT_PINNED) {
1235 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
1236 group_sched_out(event, cpuctx, ctx);
1239 if (event_type & EVENT_FLEXIBLE) {
1240 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
1241 group_sched_out(event, cpuctx, ctx);
1244 perf_pmu_enable(ctx->pmu);
1245 raw_spin_unlock(&ctx->lock);
1249 * Test whether two contexts are equivalent, i.e. whether they
1250 * have both been cloned from the same version of the same context
1251 * and they both have the same number of enabled events.
1252 * If the number of enabled events is the same, then the set
1253 * of enabled events should be the same, because these are both
1254 * inherited contexts, therefore we can't access individual events
1255 * in them directly with an fd; we can only enable/disable all
1256 * events via prctl, or enable/disable all events in a family
1257 * via ioctl, which will have the same effect on both contexts.
1259 static int context_equiv(struct perf_event_context *ctx1,
1260 struct perf_event_context *ctx2)
1262 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
1263 && ctx1->parent_gen == ctx2->parent_gen
1264 && !ctx1->pin_count && !ctx2->pin_count;
1267 static void __perf_event_sync_stat(struct perf_event *event,
1268 struct perf_event *next_event)
1272 if (!event->attr.inherit_stat)
1276 * Update the event value, we cannot use perf_event_read()
1277 * because we're in the middle of a context switch and have IRQs
1278 * disabled, which upsets smp_call_function_single(), however
1279 * we know the event must be on the current CPU, therefore we
1280 * don't need to use it.
1282 switch (event->state) {
1283 case PERF_EVENT_STATE_ACTIVE:
1284 event->pmu->read(event);
1287 case PERF_EVENT_STATE_INACTIVE:
1288 update_event_times(event);
1296 * In order to keep per-task stats reliable we need to flip the event
1297 * values when we flip the contexts.
1299 value = local64_read(&next_event->count);
1300 value = local64_xchg(&event->count, value);
1301 local64_set(&next_event->count, value);
1303 swap(event->total_time_enabled, next_event->total_time_enabled);
1304 swap(event->total_time_running, next_event->total_time_running);
1307 * Since we swizzled the values, update the user visible data too.
1309 perf_event_update_userpage(event);
1310 perf_event_update_userpage(next_event);
1313 #define list_next_entry(pos, member) \
1314 list_entry(pos->member.next, typeof(*pos), member)
1316 static void perf_event_sync_stat(struct perf_event_context *ctx,
1317 struct perf_event_context *next_ctx)
1319 struct perf_event *event, *next_event;
1324 update_context_time(ctx);
1326 event = list_first_entry(&ctx->event_list,
1327 struct perf_event, event_entry);
1329 next_event = list_first_entry(&next_ctx->event_list,
1330 struct perf_event, event_entry);
1332 while (&event->event_entry != &ctx->event_list &&
1333 &next_event->event_entry != &next_ctx->event_list) {
1335 __perf_event_sync_stat(event, next_event);
1337 event = list_next_entry(event, event_entry);
1338 next_event = list_next_entry(next_event, event_entry);
1342 void perf_event_context_sched_out(struct task_struct *task, int ctxn,
1343 struct task_struct *next)
1345 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
1346 struct perf_event_context *next_ctx;
1347 struct perf_event_context *parent;
1348 struct perf_cpu_context *cpuctx;
1354 cpuctx = __get_cpu_context(ctx);
1355 if (!cpuctx->task_ctx)
1359 parent = rcu_dereference(ctx->parent_ctx);
1360 next_ctx = next->perf_event_ctxp[ctxn];
1361 if (parent && next_ctx &&
1362 rcu_dereference(next_ctx->parent_ctx) == parent) {
1364 * Looks like the two contexts are clones, so we might be
1365 * able to optimize the context switch. We lock both
1366 * contexts and check that they are clones under the
1367 * lock (including re-checking that neither has been
1368 * uncloned in the meantime). It doesn't matter which
1369 * order we take the locks because no other cpu could
1370 * be trying to lock both of these tasks.
1372 raw_spin_lock(&ctx->lock);
1373 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
1374 if (context_equiv(ctx, next_ctx)) {
1376 * XXX do we need a memory barrier of sorts
1377 * wrt to rcu_dereference() of perf_event_ctxp
1379 task->perf_event_ctxp[ctxn] = next_ctx;
1380 next->perf_event_ctxp[ctxn] = ctx;
1382 next_ctx->task = task;
1385 perf_event_sync_stat(ctx, next_ctx);
1387 raw_spin_unlock(&next_ctx->lock);
1388 raw_spin_unlock(&ctx->lock);
1393 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
1394 cpuctx->task_ctx = NULL;
1398 #define for_each_task_context_nr(ctxn) \
1399 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
1402 * Called from scheduler to remove the events of the current task,
1403 * with interrupts disabled.
1405 * We stop each event and update the event value in event->count.
1407 * This does not protect us against NMI, but disable()
1408 * sets the disabled bit in the control field of event _before_
1409 * accessing the event control register. If a NMI hits, then it will
1410 * not restart the event.
1412 void __perf_event_task_sched_out(struct task_struct *task,
1413 struct task_struct *next)
1417 for_each_task_context_nr(ctxn)
1418 perf_event_context_sched_out(task, ctxn, next);
1421 static void task_ctx_sched_out(struct perf_event_context *ctx,
1422 enum event_type_t event_type)
1424 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1426 if (!cpuctx->task_ctx)
1429 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
1432 ctx_sched_out(ctx, cpuctx, event_type);
1433 cpuctx->task_ctx = NULL;
1437 * Called with IRQs disabled
1439 static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
1440 enum event_type_t event_type)
1442 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
1446 ctx_pinned_sched_in(struct perf_event_context *ctx,
1447 struct perf_cpu_context *cpuctx)
1449 struct perf_event *event;
1451 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
1452 if (event->state <= PERF_EVENT_STATE_OFF)
1454 if (!event_filter_match(event))
1457 if (group_can_go_on(event, cpuctx, 1))
1458 group_sched_in(event, cpuctx, ctx);
1461 * If this pinned group hasn't been scheduled,
1462 * put it in error state.
1464 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1465 update_group_times(event);
1466 event->state = PERF_EVENT_STATE_ERROR;
1472 ctx_flexible_sched_in(struct perf_event_context *ctx,
1473 struct perf_cpu_context *cpuctx)
1475 struct perf_event *event;
1478 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
1479 /* Ignore events in OFF or ERROR state */
1480 if (event->state <= PERF_EVENT_STATE_OFF)
1483 * Listen to the 'cpu' scheduling filter constraint
1486 if (!event_filter_match(event))
1489 if (group_can_go_on(event, cpuctx, can_add_hw)) {
1490 if (group_sched_in(event, cpuctx, ctx))
1497 ctx_sched_in(struct perf_event_context *ctx,
1498 struct perf_cpu_context *cpuctx,
1499 enum event_type_t event_type)
1501 raw_spin_lock(&ctx->lock);
1503 if (likely(!ctx->nr_events))
1506 ctx->timestamp = perf_clock();
1509 * First go through the list and put on any pinned groups
1510 * in order to give them the best chance of going on.
1512 if (event_type & EVENT_PINNED)
1513 ctx_pinned_sched_in(ctx, cpuctx);
1515 /* Then walk through the lower prio flexible groups */
1516 if (event_type & EVENT_FLEXIBLE)
1517 ctx_flexible_sched_in(ctx, cpuctx);
1520 raw_spin_unlock(&ctx->lock);
1523 static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
1524 enum event_type_t event_type)
1526 struct perf_event_context *ctx = &cpuctx->ctx;
1528 ctx_sched_in(ctx, cpuctx, event_type);
1531 static void task_ctx_sched_in(struct perf_event_context *ctx,
1532 enum event_type_t event_type)
1534 struct perf_cpu_context *cpuctx;
1536 cpuctx = __get_cpu_context(ctx);
1537 if (cpuctx->task_ctx == ctx)
1540 ctx_sched_in(ctx, cpuctx, event_type);
1541 cpuctx->task_ctx = ctx;
1544 void perf_event_context_sched_in(struct perf_event_context *ctx)
1546 struct perf_cpu_context *cpuctx;
1548 cpuctx = __get_cpu_context(ctx);
1549 if (cpuctx->task_ctx == ctx)
1552 perf_pmu_disable(ctx->pmu);
1554 * We want to keep the following priority order:
1555 * cpu pinned (that don't need to move), task pinned,
1556 * cpu flexible, task flexible.
1558 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
1560 ctx_sched_in(ctx, cpuctx, EVENT_PINNED);
1561 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE);
1562 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE);
1564 cpuctx->task_ctx = ctx;
1567 * Since these rotations are per-cpu, we need to ensure the
1568 * cpu-context we got scheduled on is actually rotating.
1570 perf_pmu_rotate_start(ctx->pmu);
1571 perf_pmu_enable(ctx->pmu);
1575 * Called from scheduler to add the events of the current task
1576 * with interrupts disabled.
1578 * We restore the event value and then enable it.
1580 * This does not protect us against NMI, but enable()
1581 * sets the enabled bit in the control field of event _before_
1582 * accessing the event control register. If a NMI hits, then it will
1583 * keep the event running.
1585 void __perf_event_task_sched_in(struct task_struct *task)
1587 struct perf_event_context *ctx;
1590 for_each_task_context_nr(ctxn) {
1591 ctx = task->perf_event_ctxp[ctxn];
1595 perf_event_context_sched_in(ctx);
1599 #define MAX_INTERRUPTS (~0ULL)
1601 static void perf_log_throttle(struct perf_event *event, int enable);
1603 static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
1605 u64 frequency = event->attr.sample_freq;
1606 u64 sec = NSEC_PER_SEC;
1607 u64 divisor, dividend;
1609 int count_fls, nsec_fls, frequency_fls, sec_fls;
1611 count_fls = fls64(count);
1612 nsec_fls = fls64(nsec);
1613 frequency_fls = fls64(frequency);
1617 * We got @count in @nsec, with a target of sample_freq HZ
1618 * the target period becomes:
1621 * period = -------------------
1622 * @nsec * sample_freq
1627 * Reduce accuracy by one bit such that @a and @b converge
1628 * to a similar magnitude.
1630 #define REDUCE_FLS(a, b) \
1632 if (a##_fls > b##_fls) { \
1642 * Reduce accuracy until either term fits in a u64, then proceed with
1643 * the other, so that finally we can do a u64/u64 division.
1645 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
1646 REDUCE_FLS(nsec, frequency);
1647 REDUCE_FLS(sec, count);
1650 if (count_fls + sec_fls > 64) {
1651 divisor = nsec * frequency;
1653 while (count_fls + sec_fls > 64) {
1654 REDUCE_FLS(count, sec);
1658 dividend = count * sec;
1660 dividend = count * sec;
1662 while (nsec_fls + frequency_fls > 64) {
1663 REDUCE_FLS(nsec, frequency);
1667 divisor = nsec * frequency;
1673 return div64_u64(dividend, divisor);
1676 static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count)
1678 struct hw_perf_event *hwc = &event->hw;
1679 s64 period, sample_period;
1682 period = perf_calculate_period(event, nsec, count);
1684 delta = (s64)(period - hwc->sample_period);
1685 delta = (delta + 7) / 8; /* low pass filter */
1687 sample_period = hwc->sample_period + delta;
1692 hwc->sample_period = sample_period;
1694 if (local64_read(&hwc->period_left) > 8*sample_period) {
1695 event->pmu->stop(event, PERF_EF_UPDATE);
1696 local64_set(&hwc->period_left, 0);
1697 event->pmu->start(event, PERF_EF_RELOAD);
1701 static void perf_ctx_adjust_freq(struct perf_event_context *ctx, u64 period)
1703 struct perf_event *event;
1704 struct hw_perf_event *hwc;
1705 u64 interrupts, now;
1708 raw_spin_lock(&ctx->lock);
1709 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
1710 if (event->state != PERF_EVENT_STATE_ACTIVE)
1713 if (!event_filter_match(event))
1718 interrupts = hwc->interrupts;
1719 hwc->interrupts = 0;
1722 * unthrottle events on the tick
1724 if (interrupts == MAX_INTERRUPTS) {
1725 perf_log_throttle(event, 1);
1726 event->pmu->start(event, 0);
1729 if (!event->attr.freq || !event->attr.sample_freq)
1732 event->pmu->read(event);
1733 now = local64_read(&event->count);
1734 delta = now - hwc->freq_count_stamp;
1735 hwc->freq_count_stamp = now;
1738 perf_adjust_period(event, period, delta);
1740 raw_spin_unlock(&ctx->lock);
1744 * Round-robin a context's events:
1746 static void rotate_ctx(struct perf_event_context *ctx)
1748 raw_spin_lock(&ctx->lock);
1751 * Rotate the first entry last of non-pinned groups. Rotation might be
1752 * disabled by the inheritance code.
1754 if (!ctx->rotate_disable)
1755 list_rotate_left(&ctx->flexible_groups);
1757 raw_spin_unlock(&ctx->lock);
1761 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
1762 * because they're strictly cpu affine and rotate_start is called with IRQs
1763 * disabled, while rotate_context is called from IRQ context.
1765 static void perf_rotate_context(struct perf_cpu_context *cpuctx)
1767 u64 interval = (u64)cpuctx->jiffies_interval * TICK_NSEC;
1768 struct perf_event_context *ctx = NULL;
1769 int rotate = 0, remove = 1;
1771 if (cpuctx->ctx.nr_events) {
1773 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
1777 ctx = cpuctx->task_ctx;
1778 if (ctx && ctx->nr_events) {
1780 if (ctx->nr_events != ctx->nr_active)
1784 perf_pmu_disable(cpuctx->ctx.pmu);
1785 perf_ctx_adjust_freq(&cpuctx->ctx, interval);
1787 perf_ctx_adjust_freq(ctx, interval);
1792 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
1794 task_ctx_sched_out(ctx, EVENT_FLEXIBLE);
1796 rotate_ctx(&cpuctx->ctx);
1800 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE);
1802 task_ctx_sched_in(ctx, EVENT_FLEXIBLE);
1806 list_del_init(&cpuctx->rotation_list);
1808 perf_pmu_enable(cpuctx->ctx.pmu);
1811 void perf_event_task_tick(void)
1813 struct list_head *head = &__get_cpu_var(rotation_list);
1814 struct perf_cpu_context *cpuctx, *tmp;
1816 WARN_ON(!irqs_disabled());
1818 list_for_each_entry_safe(cpuctx, tmp, head, rotation_list) {
1819 if (cpuctx->jiffies_interval == 1 ||
1820 !(jiffies % cpuctx->jiffies_interval))
1821 perf_rotate_context(cpuctx);
1825 static int event_enable_on_exec(struct perf_event *event,
1826 struct perf_event_context *ctx)
1828 if (!event->attr.enable_on_exec)
1831 event->attr.enable_on_exec = 0;
1832 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1835 __perf_event_mark_enabled(event, ctx);
1841 * Enable all of a task's events that have been marked enable-on-exec.
1842 * This expects task == current.
1844 static void perf_event_enable_on_exec(struct perf_event_context *ctx)
1846 struct perf_event *event;
1847 unsigned long flags;
1851 local_irq_save(flags);
1852 if (!ctx || !ctx->nr_events)
1855 task_ctx_sched_out(ctx, EVENT_ALL);
1857 raw_spin_lock(&ctx->lock);
1859 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
1860 ret = event_enable_on_exec(event, ctx);
1865 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
1866 ret = event_enable_on_exec(event, ctx);
1872 * Unclone this context if we enabled any event.
1877 raw_spin_unlock(&ctx->lock);
1879 perf_event_context_sched_in(ctx);
1881 local_irq_restore(flags);
1885 * Cross CPU call to read the hardware event
1887 static void __perf_event_read(void *info)
1889 struct perf_event *event = info;
1890 struct perf_event_context *ctx = event->ctx;
1891 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1894 * If this is a task context, we need to check whether it is
1895 * the current task context of this cpu. If not it has been
1896 * scheduled out before the smp call arrived. In that case
1897 * event->count would have been updated to a recent sample
1898 * when the event was scheduled out.
1900 if (ctx->task && cpuctx->task_ctx != ctx)
1903 raw_spin_lock(&ctx->lock);
1904 update_context_time(ctx);
1905 update_event_times(event);
1906 raw_spin_unlock(&ctx->lock);
1908 event->pmu->read(event);
1911 static inline u64 perf_event_count(struct perf_event *event)
1913 return local64_read(&event->count) + atomic64_read(&event->child_count);
1916 static u64 perf_event_read(struct perf_event *event)
1919 * If event is enabled and currently active on a CPU, update the
1920 * value in the event structure:
1922 if (event->state == PERF_EVENT_STATE_ACTIVE) {
1923 smp_call_function_single(event->oncpu,
1924 __perf_event_read, event, 1);
1925 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
1926 struct perf_event_context *ctx = event->ctx;
1927 unsigned long flags;
1929 raw_spin_lock_irqsave(&ctx->lock, flags);
1931 * may read while context is not active
1932 * (e.g., thread is blocked), in that case
1933 * we cannot update context time
1936 update_context_time(ctx);
1937 update_event_times(event);
1938 raw_spin_unlock_irqrestore(&ctx->lock, flags);
1941 return perf_event_count(event);
1948 struct callchain_cpus_entries {
1949 struct rcu_head rcu_head;
1950 struct perf_callchain_entry *cpu_entries[0];
1953 static DEFINE_PER_CPU(int, callchain_recursion[PERF_NR_CONTEXTS]);
1954 static atomic_t nr_callchain_events;
1955 static DEFINE_MUTEX(callchain_mutex);
1956 struct callchain_cpus_entries *callchain_cpus_entries;
1959 __weak void perf_callchain_kernel(struct perf_callchain_entry *entry,
1960 struct pt_regs *regs)
1964 __weak void perf_callchain_user(struct perf_callchain_entry *entry,
1965 struct pt_regs *regs)
1969 static void release_callchain_buffers_rcu(struct rcu_head *head)
1971 struct callchain_cpus_entries *entries;
1974 entries = container_of(head, struct callchain_cpus_entries, rcu_head);
1976 for_each_possible_cpu(cpu)
1977 kfree(entries->cpu_entries[cpu]);
1982 static void release_callchain_buffers(void)
1984 struct callchain_cpus_entries *entries;
1986 entries = callchain_cpus_entries;
1987 rcu_assign_pointer(callchain_cpus_entries, NULL);
1988 call_rcu(&entries->rcu_head, release_callchain_buffers_rcu);
1991 static int alloc_callchain_buffers(void)
1995 struct callchain_cpus_entries *entries;
1998 * We can't use the percpu allocation API for data that can be
1999 * accessed from NMI. Use a temporary manual per cpu allocation
2000 * until that gets sorted out.
2002 size = sizeof(*entries) + sizeof(struct perf_callchain_entry *) *
2003 num_possible_cpus();
2005 entries = kzalloc(size, GFP_KERNEL);
2009 size = sizeof(struct perf_callchain_entry) * PERF_NR_CONTEXTS;
2011 for_each_possible_cpu(cpu) {
2012 entries->cpu_entries[cpu] = kmalloc_node(size, GFP_KERNEL,
2014 if (!entries->cpu_entries[cpu])
2018 rcu_assign_pointer(callchain_cpus_entries, entries);
2023 for_each_possible_cpu(cpu)
2024 kfree(entries->cpu_entries[cpu]);
2030 static int get_callchain_buffers(void)
2035 mutex_lock(&callchain_mutex);
2037 count = atomic_inc_return(&nr_callchain_events);
2038 if (WARN_ON_ONCE(count < 1)) {
2044 /* If the allocation failed, give up */
2045 if (!callchain_cpus_entries)
2050 err = alloc_callchain_buffers();
2052 release_callchain_buffers();
2054 mutex_unlock(&callchain_mutex);
2059 static void put_callchain_buffers(void)
2061 if (atomic_dec_and_mutex_lock(&nr_callchain_events, &callchain_mutex)) {
2062 release_callchain_buffers();
2063 mutex_unlock(&callchain_mutex);
2067 static int get_recursion_context(int *recursion)
2075 else if (in_softirq())
2080 if (recursion[rctx])
2089 static inline void put_recursion_context(int *recursion, int rctx)
2095 static struct perf_callchain_entry *get_callchain_entry(int *rctx)
2098 struct callchain_cpus_entries *entries;
2100 *rctx = get_recursion_context(__get_cpu_var(callchain_recursion));
2104 entries = rcu_dereference(callchain_cpus_entries);
2108 cpu = smp_processor_id();
2110 return &entries->cpu_entries[cpu][*rctx];
2114 put_callchain_entry(int rctx)
2116 put_recursion_context(__get_cpu_var(callchain_recursion), rctx);
2119 static struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
2122 struct perf_callchain_entry *entry;
2125 entry = get_callchain_entry(&rctx);
2134 if (!user_mode(regs)) {
2135 perf_callchain_store(entry, PERF_CONTEXT_KERNEL);
2136 perf_callchain_kernel(entry, regs);
2138 regs = task_pt_regs(current);
2144 perf_callchain_store(entry, PERF_CONTEXT_USER);
2145 perf_callchain_user(entry, regs);
2149 put_callchain_entry(rctx);
2155 * Initialize the perf_event context in a task_struct:
2157 static void __perf_event_init_context(struct perf_event_context *ctx)
2159 raw_spin_lock_init(&ctx->lock);
2160 mutex_init(&ctx->mutex);
2161 INIT_LIST_HEAD(&ctx->pinned_groups);
2162 INIT_LIST_HEAD(&ctx->flexible_groups);
2163 INIT_LIST_HEAD(&ctx->event_list);
2164 atomic_set(&ctx->refcount, 1);
2167 static struct perf_event_context *
2168 alloc_perf_context(struct pmu *pmu, struct task_struct *task)
2170 struct perf_event_context *ctx;
2172 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
2176 __perf_event_init_context(ctx);
2179 get_task_struct(task);
2186 static struct task_struct *
2187 find_lively_task_by_vpid(pid_t vpid)
2189 struct task_struct *task;
2196 task = find_task_by_vpid(vpid);
2198 get_task_struct(task);
2202 return ERR_PTR(-ESRCH);
2205 * Can't attach events to a dying task.
2208 if (task->flags & PF_EXITING)
2211 /* Reuse ptrace permission checks for now. */
2213 if (!ptrace_may_access(task, PTRACE_MODE_READ))
2218 put_task_struct(task);
2219 return ERR_PTR(err);
2223 static struct perf_event_context *
2224 find_get_context(struct pmu *pmu, struct task_struct *task, int cpu)
2226 struct perf_event_context *ctx;
2227 struct perf_cpu_context *cpuctx;
2228 unsigned long flags;
2231 if (!task && cpu != -1) {
2232 /* Must be root to operate on a CPU event: */
2233 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
2234 return ERR_PTR(-EACCES);
2236 if (cpu < 0 || cpu >= nr_cpumask_bits)
2237 return ERR_PTR(-EINVAL);
2240 * We could be clever and allow to attach a event to an
2241 * offline CPU and activate it when the CPU comes up, but
2244 if (!cpu_online(cpu))
2245 return ERR_PTR(-ENODEV);
2247 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
2255 ctxn = pmu->task_ctx_nr;
2260 ctx = perf_lock_task_context(task, ctxn, &flags);
2263 raw_spin_unlock_irqrestore(&ctx->lock, flags);
2267 ctx = alloc_perf_context(pmu, task);
2274 if (cmpxchg(&task->perf_event_ctxp[ctxn], NULL, ctx)) {
2276 * We raced with some other task; use
2277 * the context they set.
2279 put_task_struct(task);
2288 return ERR_PTR(err);
2291 static void perf_event_free_filter(struct perf_event *event);
2293 static void free_event_rcu(struct rcu_head *head)
2295 struct perf_event *event;
2297 event = container_of(head, struct perf_event, rcu_head);
2299 put_pid_ns(event->ns);
2300 perf_event_free_filter(event);
2304 static void perf_buffer_put(struct perf_buffer *buffer);
2306 static void free_event(struct perf_event *event)
2308 irq_work_sync(&event->pending);
2310 if (!event->parent) {
2311 if (event->attach_state & PERF_ATTACH_TASK)
2312 jump_label_dec(&perf_task_events);
2313 if (event->attr.mmap || event->attr.mmap_data)
2314 atomic_dec(&nr_mmap_events);
2315 if (event->attr.comm)
2316 atomic_dec(&nr_comm_events);
2317 if (event->attr.task)
2318 atomic_dec(&nr_task_events);
2319 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
2320 put_callchain_buffers();
2323 if (event->buffer) {
2324 perf_buffer_put(event->buffer);
2325 event->buffer = NULL;
2329 event->destroy(event);
2332 put_ctx(event->ctx);
2334 call_rcu(&event->rcu_head, free_event_rcu);
2337 int perf_event_release_kernel(struct perf_event *event)
2339 struct perf_event_context *ctx = event->ctx;
2342 * Remove from the PMU, can't get re-enabled since we got
2343 * here because the last ref went.
2345 perf_event_disable(event);
2347 WARN_ON_ONCE(ctx->parent_ctx);
2349 * There are two ways this annotation is useful:
2351 * 1) there is a lock recursion from perf_event_exit_task
2352 * see the comment there.
2354 * 2) there is a lock-inversion with mmap_sem through
2355 * perf_event_read_group(), which takes faults while
2356 * holding ctx->mutex, however this is called after
2357 * the last filedesc died, so there is no possibility
2358 * to trigger the AB-BA case.
2360 mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
2361 raw_spin_lock_irq(&ctx->lock);
2362 perf_group_detach(event);
2363 list_del_event(event, ctx);
2364 raw_spin_unlock_irq(&ctx->lock);
2365 mutex_unlock(&ctx->mutex);
2371 EXPORT_SYMBOL_GPL(perf_event_release_kernel);
2374 * Called when the last reference to the file is gone.
2376 static int perf_release(struct inode *inode, struct file *file)
2378 struct perf_event *event = file->private_data;
2379 struct task_struct *owner;
2381 file->private_data = NULL;
2384 owner = ACCESS_ONCE(event->owner);
2386 * Matches the smp_wmb() in perf_event_exit_task(). If we observe
2387 * !owner it means the list deletion is complete and we can indeed
2388 * free this event, otherwise we need to serialize on
2389 * owner->perf_event_mutex.
2391 smp_read_barrier_depends();
2394 * Since delayed_put_task_struct() also drops the last
2395 * task reference we can safely take a new reference
2396 * while holding the rcu_read_lock().
2398 get_task_struct(owner);
2403 mutex_lock(&owner->perf_event_mutex);
2405 * We have to re-check the event->owner field, if it is cleared
2406 * we raced with perf_event_exit_task(), acquiring the mutex
2407 * ensured they're done, and we can proceed with freeing the
2411 list_del_init(&event->owner_entry);
2412 mutex_unlock(&owner->perf_event_mutex);
2413 put_task_struct(owner);
2416 return perf_event_release_kernel(event);
2419 u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
2421 struct perf_event *child;
2427 mutex_lock(&event->child_mutex);
2428 total += perf_event_read(event);
2429 *enabled += event->total_time_enabled +
2430 atomic64_read(&event->child_total_time_enabled);
2431 *running += event->total_time_running +
2432 atomic64_read(&event->child_total_time_running);
2434 list_for_each_entry(child, &event->child_list, child_list) {
2435 total += perf_event_read(child);
2436 *enabled += child->total_time_enabled;
2437 *running += child->total_time_running;
2439 mutex_unlock(&event->child_mutex);
2443 EXPORT_SYMBOL_GPL(perf_event_read_value);
2445 static int perf_event_read_group(struct perf_event *event,
2446 u64 read_format, char __user *buf)
2448 struct perf_event *leader = event->group_leader, *sub;
2449 int n = 0, size = 0, ret = -EFAULT;
2450 struct perf_event_context *ctx = leader->ctx;
2452 u64 count, enabled, running;
2454 mutex_lock(&ctx->mutex);
2455 count = perf_event_read_value(leader, &enabled, &running);
2457 values[n++] = 1 + leader->nr_siblings;
2458 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2459 values[n++] = enabled;
2460 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2461 values[n++] = running;
2462 values[n++] = count;
2463 if (read_format & PERF_FORMAT_ID)
2464 values[n++] = primary_event_id(leader);
2466 size = n * sizeof(u64);
2468 if (copy_to_user(buf, values, size))
2473 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
2476 values[n++] = perf_event_read_value(sub, &enabled, &running);
2477 if (read_format & PERF_FORMAT_ID)
2478 values[n++] = primary_event_id(sub);
2480 size = n * sizeof(u64);
2482 if (copy_to_user(buf + ret, values, size)) {
2490 mutex_unlock(&ctx->mutex);
2495 static int perf_event_read_one(struct perf_event *event,
2496 u64 read_format, char __user *buf)
2498 u64 enabled, running;
2502 values[n++] = perf_event_read_value(event, &enabled, &running);
2503 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2504 values[n++] = enabled;
2505 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2506 values[n++] = running;
2507 if (read_format & PERF_FORMAT_ID)
2508 values[n++] = primary_event_id(event);
2510 if (copy_to_user(buf, values, n * sizeof(u64)))
2513 return n * sizeof(u64);
2517 * Read the performance event - simple non blocking version for now
2520 perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
2522 u64 read_format = event->attr.read_format;
2526 * Return end-of-file for a read on a event that is in
2527 * error state (i.e. because it was pinned but it couldn't be
2528 * scheduled on to the CPU at some point).
2530 if (event->state == PERF_EVENT_STATE_ERROR)
2533 if (count < event->read_size)
2536 WARN_ON_ONCE(event->ctx->parent_ctx);
2537 if (read_format & PERF_FORMAT_GROUP)
2538 ret = perf_event_read_group(event, read_format, buf);
2540 ret = perf_event_read_one(event, read_format, buf);
2546 perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
2548 struct perf_event *event = file->private_data;
2550 return perf_read_hw(event, buf, count);
2553 static unsigned int perf_poll(struct file *file, poll_table *wait)
2555 struct perf_event *event = file->private_data;
2556 struct perf_buffer *buffer;
2557 unsigned int events = POLL_HUP;
2560 buffer = rcu_dereference(event->buffer);
2562 events = atomic_xchg(&buffer->poll, 0);
2565 poll_wait(file, &event->waitq, wait);
2570 static void perf_event_reset(struct perf_event *event)
2572 (void)perf_event_read(event);
2573 local64_set(&event->count, 0);
2574 perf_event_update_userpage(event);
2578 * Holding the top-level event's child_mutex means that any
2579 * descendant process that has inherited this event will block
2580 * in sync_child_event if it goes to exit, thus satisfying the
2581 * task existence requirements of perf_event_enable/disable.
2583 static void perf_event_for_each_child(struct perf_event *event,
2584 void (*func)(struct perf_event *))
2586 struct perf_event *child;
2588 WARN_ON_ONCE(event->ctx->parent_ctx);
2589 mutex_lock(&event->child_mutex);
2591 list_for_each_entry(child, &event->child_list, child_list)
2593 mutex_unlock(&event->child_mutex);
2596 static void perf_event_for_each(struct perf_event *event,
2597 void (*func)(struct perf_event *))
2599 struct perf_event_context *ctx = event->ctx;
2600 struct perf_event *sibling;
2602 WARN_ON_ONCE(ctx->parent_ctx);
2603 mutex_lock(&ctx->mutex);
2604 event = event->group_leader;
2606 perf_event_for_each_child(event, func);
2608 list_for_each_entry(sibling, &event->sibling_list, group_entry)
2609 perf_event_for_each_child(event, func);
2610 mutex_unlock(&ctx->mutex);
2613 static int perf_event_period(struct perf_event *event, u64 __user *arg)
2615 struct perf_event_context *ctx = event->ctx;
2619 if (!is_sampling_event(event))
2622 if (copy_from_user(&value, arg, sizeof(value)))
2628 raw_spin_lock_irq(&ctx->lock);
2629 if (event->attr.freq) {
2630 if (value > sysctl_perf_event_sample_rate) {
2635 event->attr.sample_freq = value;
2637 event->attr.sample_period = value;
2638 event->hw.sample_period = value;
2641 raw_spin_unlock_irq(&ctx->lock);
2646 static const struct file_operations perf_fops;
2648 static struct perf_event *perf_fget_light(int fd, int *fput_needed)
2652 file = fget_light(fd, fput_needed);
2654 return ERR_PTR(-EBADF);
2656 if (file->f_op != &perf_fops) {
2657 fput_light(file, *fput_needed);
2659 return ERR_PTR(-EBADF);
2662 return file->private_data;
2665 static int perf_event_set_output(struct perf_event *event,
2666 struct perf_event *output_event);
2667 static int perf_event_set_filter(struct perf_event *event, void __user *arg);
2669 static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2671 struct perf_event *event = file->private_data;
2672 void (*func)(struct perf_event *);
2676 case PERF_EVENT_IOC_ENABLE:
2677 func = perf_event_enable;
2679 case PERF_EVENT_IOC_DISABLE:
2680 func = perf_event_disable;
2682 case PERF_EVENT_IOC_RESET:
2683 func = perf_event_reset;
2686 case PERF_EVENT_IOC_REFRESH:
2687 return perf_event_refresh(event, arg);
2689 case PERF_EVENT_IOC_PERIOD:
2690 return perf_event_period(event, (u64 __user *)arg);
2692 case PERF_EVENT_IOC_SET_OUTPUT:
2694 struct perf_event *output_event = NULL;
2695 int fput_needed = 0;
2699 output_event = perf_fget_light(arg, &fput_needed);
2700 if (IS_ERR(output_event))
2701 return PTR_ERR(output_event);
2704 ret = perf_event_set_output(event, output_event);
2706 fput_light(output_event->filp, fput_needed);
2711 case PERF_EVENT_IOC_SET_FILTER:
2712 return perf_event_set_filter(event, (void __user *)arg);
2718 if (flags & PERF_IOC_FLAG_GROUP)
2719 perf_event_for_each(event, func);
2721 perf_event_for_each_child(event, func);
2726 int perf_event_task_enable(void)
2728 struct perf_event *event;
2730 mutex_lock(¤t->perf_event_mutex);
2731 list_for_each_entry(event, ¤t->perf_event_list, owner_entry)
2732 perf_event_for_each_child(event, perf_event_enable);
2733 mutex_unlock(¤t->perf_event_mutex);
2738 int perf_event_task_disable(void)
2740 struct perf_event *event;
2742 mutex_lock(¤t->perf_event_mutex);
2743 list_for_each_entry(event, ¤t->perf_event_list, owner_entry)
2744 perf_event_for_each_child(event, perf_event_disable);
2745 mutex_unlock(¤t->perf_event_mutex);
2750 #ifndef PERF_EVENT_INDEX_OFFSET
2751 # define PERF_EVENT_INDEX_OFFSET 0
2754 static int perf_event_index(struct perf_event *event)
2756 if (event->hw.state & PERF_HES_STOPPED)
2759 if (event->state != PERF_EVENT_STATE_ACTIVE)
2762 return event->hw.idx + 1 - PERF_EVENT_INDEX_OFFSET;
2766 * Callers need to ensure there can be no nesting of this function, otherwise
2767 * the seqlock logic goes bad. We can not serialize this because the arch
2768 * code calls this from NMI context.
2770 void perf_event_update_userpage(struct perf_event *event)
2772 struct perf_event_mmap_page *userpg;
2773 struct perf_buffer *buffer;
2776 buffer = rcu_dereference(event->buffer);
2780 userpg = buffer->user_page;
2783 * Disable preemption so as to not let the corresponding user-space
2784 * spin too long if we get preempted.
2789 userpg->index = perf_event_index(event);
2790 userpg->offset = perf_event_count(event);
2791 if (event->state == PERF_EVENT_STATE_ACTIVE)
2792 userpg->offset -= local64_read(&event->hw.prev_count);
2794 userpg->time_enabled = event->total_time_enabled +
2795 atomic64_read(&event->child_total_time_enabled);
2797 userpg->time_running = event->total_time_running +
2798 atomic64_read(&event->child_total_time_running);
2807 static unsigned long perf_data_size(struct perf_buffer *buffer);
2810 perf_buffer_init(struct perf_buffer *buffer, long watermark, int flags)
2812 long max_size = perf_data_size(buffer);
2815 buffer->watermark = min(max_size, watermark);
2817 if (!buffer->watermark)
2818 buffer->watermark = max_size / 2;
2820 if (flags & PERF_BUFFER_WRITABLE)
2821 buffer->writable = 1;
2823 atomic_set(&buffer->refcount, 1);
2826 #ifndef CONFIG_PERF_USE_VMALLOC
2829 * Back perf_mmap() with regular GFP_KERNEL-0 pages.
2832 static struct page *
2833 perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
2835 if (pgoff > buffer->nr_pages)
2839 return virt_to_page(buffer->user_page);
2841 return virt_to_page(buffer->data_pages[pgoff - 1]);
2844 static void *perf_mmap_alloc_page(int cpu)
2849 node = (cpu == -1) ? cpu : cpu_to_node(cpu);
2850 page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
2854 return page_address(page);
2857 static struct perf_buffer *
2858 perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
2860 struct perf_buffer *buffer;
2864 size = sizeof(struct perf_buffer);
2865 size += nr_pages * sizeof(void *);
2867 buffer = kzalloc(size, GFP_KERNEL);
2871 buffer->user_page = perf_mmap_alloc_page(cpu);
2872 if (!buffer->user_page)
2873 goto fail_user_page;
2875 for (i = 0; i < nr_pages; i++) {
2876 buffer->data_pages[i] = perf_mmap_alloc_page(cpu);
2877 if (!buffer->data_pages[i])
2878 goto fail_data_pages;
2881 buffer->nr_pages = nr_pages;
2883 perf_buffer_init(buffer, watermark, flags);
2888 for (i--; i >= 0; i--)
2889 free_page((unsigned long)buffer->data_pages[i]);
2891 free_page((unsigned long)buffer->user_page);
2900 static void perf_mmap_free_page(unsigned long addr)
2902 struct page *page = virt_to_page((void *)addr);
2904 page->mapping = NULL;
2908 static void perf_buffer_free(struct perf_buffer *buffer)
2912 perf_mmap_free_page((unsigned long)buffer->user_page);
2913 for (i = 0; i < buffer->nr_pages; i++)
2914 perf_mmap_free_page((unsigned long)buffer->data_pages[i]);
2918 static inline int page_order(struct perf_buffer *buffer)
2926 * Back perf_mmap() with vmalloc memory.
2928 * Required for architectures that have d-cache aliasing issues.
2931 static inline int page_order(struct perf_buffer *buffer)
2933 return buffer->page_order;
2936 static struct page *
2937 perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
2939 if (pgoff > (1UL << page_order(buffer)))
2942 return vmalloc_to_page((void *)buffer->user_page + pgoff * PAGE_SIZE);
2945 static void perf_mmap_unmark_page(void *addr)
2947 struct page *page = vmalloc_to_page(addr);
2949 page->mapping = NULL;
2952 static void perf_buffer_free_work(struct work_struct *work)
2954 struct perf_buffer *buffer;
2958 buffer = container_of(work, struct perf_buffer, work);
2959 nr = 1 << page_order(buffer);
2961 base = buffer->user_page;
2962 for (i = 0; i < nr + 1; i++)
2963 perf_mmap_unmark_page(base + (i * PAGE_SIZE));
2969 static void perf_buffer_free(struct perf_buffer *buffer)
2971 schedule_work(&buffer->work);
2974 static struct perf_buffer *
2975 perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
2977 struct perf_buffer *buffer;
2981 size = sizeof(struct perf_buffer);
2982 size += sizeof(void *);
2984 buffer = kzalloc(size, GFP_KERNEL);
2988 INIT_WORK(&buffer->work, perf_buffer_free_work);
2990 all_buf = vmalloc_user((nr_pages + 1) * PAGE_SIZE);
2994 buffer->user_page = all_buf;
2995 buffer->data_pages[0] = all_buf + PAGE_SIZE;
2996 buffer->page_order = ilog2(nr_pages);
2997 buffer->nr_pages = 1;
2999 perf_buffer_init(buffer, watermark, flags);
3012 static unsigned long perf_data_size(struct perf_buffer *buffer)
3014 return buffer->nr_pages << (PAGE_SHIFT + page_order(buffer));
3017 static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
3019 struct perf_event *event = vma->vm_file->private_data;
3020 struct perf_buffer *buffer;
3021 int ret = VM_FAULT_SIGBUS;
3023 if (vmf->flags & FAULT_FLAG_MKWRITE) {
3024 if (vmf->pgoff == 0)
3030 buffer = rcu_dereference(event->buffer);
3034 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
3037 vmf->page = perf_mmap_to_page(buffer, vmf->pgoff);
3041 get_page(vmf->page);
3042 vmf->page->mapping = vma->vm_file->f_mapping;
3043 vmf->page->index = vmf->pgoff;
3052 static void perf_buffer_free_rcu(struct rcu_head *rcu_head)
3054 struct perf_buffer *buffer;
3056 buffer = container_of(rcu_head, struct perf_buffer, rcu_head);
3057 perf_buffer_free(buffer);
3060 static struct perf_buffer *perf_buffer_get(struct perf_event *event)
3062 struct perf_buffer *buffer;
3065 buffer = rcu_dereference(event->buffer);
3067 if (!atomic_inc_not_zero(&buffer->refcount))
3075 static void perf_buffer_put(struct perf_buffer *buffer)
3077 if (!atomic_dec_and_test(&buffer->refcount))
3080 call_rcu(&buffer->rcu_head, perf_buffer_free_rcu);
3083 static void perf_mmap_open(struct vm_area_struct *vma)
3085 struct perf_event *event = vma->vm_file->private_data;
3087 atomic_inc(&event->mmap_count);
3090 static void perf_mmap_close(struct vm_area_struct *vma)
3092 struct perf_event *event = vma->vm_file->private_data;
3094 if (atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) {
3095 unsigned long size = perf_data_size(event->buffer);
3096 struct user_struct *user = event->mmap_user;
3097 struct perf_buffer *buffer = event->buffer;
3099 atomic_long_sub((size >> PAGE_SHIFT) + 1, &user->locked_vm);
3100 vma->vm_mm->locked_vm -= event->mmap_locked;
3101 rcu_assign_pointer(event->buffer, NULL);
3102 mutex_unlock(&event->mmap_mutex);
3104 perf_buffer_put(buffer);
3109 static const struct vm_operations_struct perf_mmap_vmops = {
3110 .open = perf_mmap_open,
3111 .close = perf_mmap_close,
3112 .fault = perf_mmap_fault,
3113 .page_mkwrite = perf_mmap_fault,
3116 static int perf_mmap(struct file *file, struct vm_area_struct *vma)
3118 struct perf_event *event = file->private_data;
3119 unsigned long user_locked, user_lock_limit;
3120 struct user_struct *user = current_user();
3121 unsigned long locked, lock_limit;
3122 struct perf_buffer *buffer;
3123 unsigned long vma_size;
3124 unsigned long nr_pages;
3125 long user_extra, extra;
3126 int ret = 0, flags = 0;
3129 * Don't allow mmap() of inherited per-task counters. This would
3130 * create a performance issue due to all children writing to the
3133 if (event->cpu == -1 && event->attr.inherit)
3136 if (!(vma->vm_flags & VM_SHARED))
3139 vma_size = vma->vm_end - vma->vm_start;
3140 nr_pages = (vma_size / PAGE_SIZE) - 1;
3143 * If we have buffer pages ensure they're a power-of-two number, so we
3144 * can do bitmasks instead of modulo.
3146 if (nr_pages != 0 && !is_power_of_2(nr_pages))
3149 if (vma_size != PAGE_SIZE * (1 + nr_pages))
3152 if (vma->vm_pgoff != 0)
3155 WARN_ON_ONCE(event->ctx->parent_ctx);
3156 mutex_lock(&event->mmap_mutex);
3157 if (event->buffer) {
3158 if (event->buffer->nr_pages == nr_pages)
3159 atomic_inc(&event->buffer->refcount);
3165 user_extra = nr_pages + 1;
3166 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
3169 * Increase the limit linearly with more CPUs:
3171 user_lock_limit *= num_online_cpus();
3173 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
3176 if (user_locked > user_lock_limit)
3177 extra = user_locked - user_lock_limit;
3179 lock_limit = rlimit(RLIMIT_MEMLOCK);
3180 lock_limit >>= PAGE_SHIFT;
3181 locked = vma->vm_mm->locked_vm + extra;
3183 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
3184 !capable(CAP_IPC_LOCK)) {
3189 WARN_ON(event->buffer);
3191 if (vma->vm_flags & VM_WRITE)
3192 flags |= PERF_BUFFER_WRITABLE;
3194 buffer = perf_buffer_alloc(nr_pages, event->attr.wakeup_watermark,
3200 rcu_assign_pointer(event->buffer, buffer);
3202 atomic_long_add(user_extra, &user->locked_vm);
3203 event->mmap_locked = extra;
3204 event->mmap_user = get_current_user();
3205 vma->vm_mm->locked_vm += event->mmap_locked;
3209 atomic_inc(&event->mmap_count);
3210 mutex_unlock(&event->mmap_mutex);
3212 vma->vm_flags |= VM_RESERVED;
3213 vma->vm_ops = &perf_mmap_vmops;
3218 static int perf_fasync(int fd, struct file *filp, int on)
3220 struct inode *inode = filp->f_path.dentry->d_inode;
3221 struct perf_event *event = filp->private_data;
3224 mutex_lock(&inode->i_mutex);
3225 retval = fasync_helper(fd, filp, on, &event->fasync);
3226 mutex_unlock(&inode->i_mutex);
3234 static const struct file_operations perf_fops = {
3235 .llseek = no_llseek,
3236 .release = perf_release,
3239 .unlocked_ioctl = perf_ioctl,
3240 .compat_ioctl = perf_ioctl,
3242 .fasync = perf_fasync,
3248 * If there's data, ensure we set the poll() state and publish everything
3249 * to user-space before waking everybody up.
3252 void perf_event_wakeup(struct perf_event *event)
3254 wake_up_all(&event->waitq);
3256 if (event->pending_kill) {
3257 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
3258 event->pending_kill = 0;
3262 static void perf_pending_event(struct irq_work *entry)
3264 struct perf_event *event = container_of(entry,
3265 struct perf_event, pending);
3267 if (event->pending_disable) {
3268 event->pending_disable = 0;
3269 __perf_event_disable(event);
3272 if (event->pending_wakeup) {
3273 event->pending_wakeup = 0;
3274 perf_event_wakeup(event);
3279 * We assume there is only KVM supporting the callbacks.
3280 * Later on, we might change it to a list if there is
3281 * another virtualization implementation supporting the callbacks.
3283 struct perf_guest_info_callbacks *perf_guest_cbs;
3285 int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3287 perf_guest_cbs = cbs;
3290 EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
3292 int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3294 perf_guest_cbs = NULL;
3297 EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
3302 static bool perf_output_space(struct perf_buffer *buffer, unsigned long tail,
3303 unsigned long offset, unsigned long head)
3307 if (!buffer->writable)
3310 mask = perf_data_size(buffer) - 1;
3312 offset = (offset - tail) & mask;
3313 head = (head - tail) & mask;
3315 if ((int)(head - offset) < 0)
3321 static void perf_output_wakeup(struct perf_output_handle *handle)
3323 atomic_set(&handle->buffer->poll, POLL_IN);
3326 handle->event->pending_wakeup = 1;
3327 irq_work_queue(&handle->event->pending);
3329 perf_event_wakeup(handle->event);
3333 * We need to ensure a later event_id doesn't publish a head when a former
3334 * event isn't done writing. However since we need to deal with NMIs we
3335 * cannot fully serialize things.
3337 * We only publish the head (and generate a wakeup) when the outer-most
3340 static void perf_output_get_handle(struct perf_output_handle *handle)
3342 struct perf_buffer *buffer = handle->buffer;
3345 local_inc(&buffer->nest);
3346 handle->wakeup = local_read(&buffer->wakeup);
3349 static void perf_output_put_handle(struct perf_output_handle *handle)
3351 struct perf_buffer *buffer = handle->buffer;
3355 head = local_read(&buffer->head);
3358 * IRQ/NMI can happen here, which means we can miss a head update.
3361 if (!local_dec_and_test(&buffer->nest))
3365 * Publish the known good head. Rely on the full barrier implied
3366 * by atomic_dec_and_test() order the buffer->head read and this
3369 buffer->user_page->data_head = head;
3372 * Now check if we missed an update, rely on the (compiler)
3373 * barrier in atomic_dec_and_test() to re-read buffer->head.
3375 if (unlikely(head != local_read(&buffer->head))) {
3376 local_inc(&buffer->nest);
3380 if (handle->wakeup != local_read(&buffer->wakeup))
3381 perf_output_wakeup(handle);
3387 __always_inline void perf_output_copy(struct perf_output_handle *handle,
3388 const void *buf, unsigned int len)
3391 unsigned long size = min_t(unsigned long, handle->size, len);
3393 memcpy(handle->addr, buf, size);
3396 handle->addr += size;
3398 handle->size -= size;
3399 if (!handle->size) {
3400 struct perf_buffer *buffer = handle->buffer;
3403 handle->page &= buffer->nr_pages - 1;
3404 handle->addr = buffer->data_pages[handle->page];
3405 handle->size = PAGE_SIZE << page_order(buffer);
3410 static void __perf_event_header__init_id(struct perf_event_header *header,
3411 struct perf_sample_data *data,