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/file.h>
17 #include <linux/poll.h>
18 #include <linux/slab.h>
19 #include <linux/hash.h>
20 #include <linux/sysfs.h>
21 #include <linux/dcache.h>
22 #include <linux/percpu.h>
23 #include <linux/ptrace.h>
24 #include <linux/vmstat.h>
25 #include <linux/vmalloc.h>
26 #include <linux/hardirq.h>
27 #include <linux/rculist.h>
28 #include <linux/uaccess.h>
29 #include <linux/syscalls.h>
30 #include <linux/anon_inodes.h>
31 #include <linux/kernel_stat.h>
32 #include <linux/perf_event.h>
33 #include <linux/ftrace_event.h>
34 #include <linux/hw_breakpoint.h>
36 #include <asm/irq_regs.h>
39 * Each CPU has a list of per CPU events:
41 static DEFINE_PER_CPU(struct perf_cpu_context, perf_cpu_context);
43 int perf_max_events __read_mostly = 1;
44 static int perf_reserved_percpu __read_mostly;
45 static int perf_overcommit __read_mostly = 1;
47 static atomic_t nr_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;
53 * perf event paranoia level:
54 * -1 - not paranoid at all
55 * 0 - disallow raw tracepoint access for unpriv
56 * 1 - disallow cpu events for unpriv
57 * 2 - disallow kernel profiling for unpriv
59 int sysctl_perf_event_paranoid __read_mostly = 1;
61 int sysctl_perf_event_mlock __read_mostly = 512; /* 'free' kb per user */
64 * max perf event sample rate
66 int sysctl_perf_event_sample_rate __read_mostly = 100000;
68 static atomic64_t perf_event_id;
71 * Lock for (sysadmin-configurable) event reservations:
73 static DEFINE_SPINLOCK(perf_resource_lock);
76 * Architecture provided APIs - weak aliases:
78 extern __weak const struct pmu *hw_perf_event_init(struct perf_event *event)
83 void __weak hw_perf_disable(void) { barrier(); }
84 void __weak hw_perf_enable(void) { barrier(); }
86 void __weak perf_event_print_debug(void) { }
88 static DEFINE_PER_CPU(int, perf_disable_count);
90 void perf_disable(void)
92 if (!__get_cpu_var(perf_disable_count)++)
96 void perf_enable(void)
98 if (!--__get_cpu_var(perf_disable_count))
102 static void get_ctx(struct perf_event_context *ctx)
104 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
107 static void free_ctx(struct rcu_head *head)
109 struct perf_event_context *ctx;
111 ctx = container_of(head, struct perf_event_context, rcu_head);
115 static void put_ctx(struct perf_event_context *ctx)
117 if (atomic_dec_and_test(&ctx->refcount)) {
119 put_ctx(ctx->parent_ctx);
121 put_task_struct(ctx->task);
122 call_rcu(&ctx->rcu_head, free_ctx);
126 static void unclone_ctx(struct perf_event_context *ctx)
128 if (ctx->parent_ctx) {
129 put_ctx(ctx->parent_ctx);
130 ctx->parent_ctx = NULL;
135 * If we inherit events we want to return the parent event id
138 static u64 primary_event_id(struct perf_event *event)
143 id = event->parent->id;
149 * Get the perf_event_context for a task and lock it.
150 * This has to cope with with the fact that until it is locked,
151 * the context could get moved to another task.
153 static struct perf_event_context *
154 perf_lock_task_context(struct task_struct *task, unsigned long *flags)
156 struct perf_event_context *ctx;
160 ctx = rcu_dereference(task->perf_event_ctxp);
163 * If this context is a clone of another, it might
164 * get swapped for another underneath us by
165 * perf_event_task_sched_out, though the
166 * rcu_read_lock() protects us from any context
167 * getting freed. Lock the context and check if it
168 * got swapped before we could get the lock, and retry
169 * if so. If we locked the right context, then it
170 * can't get swapped on us any more.
172 raw_spin_lock_irqsave(&ctx->lock, *flags);
173 if (ctx != rcu_dereference(task->perf_event_ctxp)) {
174 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
178 if (!atomic_inc_not_zero(&ctx->refcount)) {
179 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
188 * Get the context for a task and increment its pin_count so it
189 * can't get swapped to another task. This also increments its
190 * reference count so that the context can't get freed.
192 static struct perf_event_context *perf_pin_task_context(struct task_struct *task)
194 struct perf_event_context *ctx;
197 ctx = perf_lock_task_context(task, &flags);
200 raw_spin_unlock_irqrestore(&ctx->lock, flags);
205 static void perf_unpin_context(struct perf_event_context *ctx)
209 raw_spin_lock_irqsave(&ctx->lock, flags);
211 raw_spin_unlock_irqrestore(&ctx->lock, flags);
215 static inline u64 perf_clock(void)
217 return cpu_clock(raw_smp_processor_id());
221 * Update the record of the current time in a context.
223 static void update_context_time(struct perf_event_context *ctx)
225 u64 now = perf_clock();
227 ctx->time += now - ctx->timestamp;
228 ctx->timestamp = now;
232 * Update the total_time_enabled and total_time_running fields for a event.
234 static void update_event_times(struct perf_event *event)
236 struct perf_event_context *ctx = event->ctx;
239 if (event->state < PERF_EVENT_STATE_INACTIVE ||
240 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
246 run_end = event->tstamp_stopped;
248 event->total_time_enabled = run_end - event->tstamp_enabled;
250 if (event->state == PERF_EVENT_STATE_INACTIVE)
251 run_end = event->tstamp_stopped;
255 event->total_time_running = run_end - event->tstamp_running;
258 static struct list_head *
259 ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
261 if (event->attr.pinned)
262 return &ctx->pinned_groups;
264 return &ctx->flexible_groups;
268 * Add a event from the lists for its context.
269 * Must be called with ctx->mutex and ctx->lock held.
272 list_add_event(struct perf_event *event, struct perf_event_context *ctx)
274 struct perf_event *group_leader = event->group_leader;
277 * Depending on whether it is a standalone or sibling event,
278 * add it straight to the context's event list, or to the group
279 * leader's sibling list:
281 if (group_leader == event) {
282 struct list_head *list;
284 if (is_software_event(event))
285 event->group_flags |= PERF_GROUP_SOFTWARE;
287 list = ctx_group_list(event, ctx);
288 list_add_tail(&event->group_entry, list);
290 if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
291 !is_software_event(event))
292 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
294 list_add_tail(&event->group_entry, &group_leader->sibling_list);
295 group_leader->nr_siblings++;
298 list_add_rcu(&event->event_entry, &ctx->event_list);
300 if (event->attr.inherit_stat)
305 * Remove a event from the lists for its context.
306 * Must be called with ctx->mutex and ctx->lock held.
309 list_del_event(struct perf_event *event, struct perf_event_context *ctx)
311 struct perf_event *sibling, *tmp;
313 if (list_empty(&event->group_entry))
316 if (event->attr.inherit_stat)
319 list_del_init(&event->group_entry);
320 list_del_rcu(&event->event_entry);
322 if (event->group_leader != event)
323 event->group_leader->nr_siblings--;
325 update_event_times(event);
328 * If event was in error state, then keep it
329 * that way, otherwise bogus counts will be
330 * returned on read(). The only way to get out
331 * of error state is by explicit re-enabling
334 if (event->state > PERF_EVENT_STATE_OFF)
335 event->state = PERF_EVENT_STATE_OFF;
338 * If this was a group event with sibling events then
339 * upgrade the siblings to singleton events by adding them
340 * to the context list directly:
342 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
343 struct list_head *list;
345 list = ctx_group_list(event, ctx);
346 list_move_tail(&sibling->group_entry, list);
347 sibling->group_leader = sibling;
349 /* Inherit group flags from the previous leader */
350 sibling->group_flags = event->group_flags;
355 event_sched_out(struct perf_event *event,
356 struct perf_cpu_context *cpuctx,
357 struct perf_event_context *ctx)
359 if (event->state != PERF_EVENT_STATE_ACTIVE)
362 event->state = PERF_EVENT_STATE_INACTIVE;
363 if (event->pending_disable) {
364 event->pending_disable = 0;
365 event->state = PERF_EVENT_STATE_OFF;
367 event->tstamp_stopped = ctx->time;
368 event->pmu->disable(event);
371 if (!is_software_event(event))
372 cpuctx->active_oncpu--;
374 if (event->attr.exclusive || !cpuctx->active_oncpu)
375 cpuctx->exclusive = 0;
379 group_sched_out(struct perf_event *group_event,
380 struct perf_cpu_context *cpuctx,
381 struct perf_event_context *ctx)
383 struct perf_event *event;
385 if (group_event->state != PERF_EVENT_STATE_ACTIVE)
388 event_sched_out(group_event, cpuctx, ctx);
391 * Schedule out siblings (if any):
393 list_for_each_entry(event, &group_event->sibling_list, group_entry)
394 event_sched_out(event, cpuctx, ctx);
396 if (group_event->attr.exclusive)
397 cpuctx->exclusive = 0;
401 * Cross CPU call to remove a performance event
403 * We disable the event on the hardware level first. After that we
404 * remove it from the context list.
406 static void __perf_event_remove_from_context(void *info)
408 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
409 struct perf_event *event = info;
410 struct perf_event_context *ctx = event->ctx;
413 * If this is a task context, we need to check whether it is
414 * the current task context of this cpu. If not it has been
415 * scheduled out before the smp call arrived.
417 if (ctx->task && cpuctx->task_ctx != ctx)
420 raw_spin_lock(&ctx->lock);
422 * Protect the list operation against NMI by disabling the
423 * events on a global level.
427 event_sched_out(event, cpuctx, ctx);
429 list_del_event(event, ctx);
433 * Allow more per task events with respect to the
436 cpuctx->max_pertask =
437 min(perf_max_events - ctx->nr_events,
438 perf_max_events - perf_reserved_percpu);
442 raw_spin_unlock(&ctx->lock);
447 * Remove the event from a task's (or a CPU's) list of events.
449 * Must be called with ctx->mutex held.
451 * CPU events are removed with a smp call. For task events we only
452 * call when the task is on a CPU.
454 * If event->ctx is a cloned context, callers must make sure that
455 * every task struct that event->ctx->task could possibly point to
456 * remains valid. This is OK when called from perf_release since
457 * that only calls us on the top-level context, which can't be a clone.
458 * When called from perf_event_exit_task, it's OK because the
459 * context has been detached from its task.
461 static void perf_event_remove_from_context(struct perf_event *event)
463 struct perf_event_context *ctx = event->ctx;
464 struct task_struct *task = ctx->task;
468 * Per cpu events are removed via an smp call and
469 * the removal is always successful.
471 smp_call_function_single(event->cpu,
472 __perf_event_remove_from_context,
478 task_oncpu_function_call(task, __perf_event_remove_from_context,
481 raw_spin_lock_irq(&ctx->lock);
483 * If the context is active we need to retry the smp call.
485 if (ctx->nr_active && !list_empty(&event->group_entry)) {
486 raw_spin_unlock_irq(&ctx->lock);
491 * The lock prevents that this context is scheduled in so we
492 * can remove the event safely, if the call above did not
495 if (!list_empty(&event->group_entry))
496 list_del_event(event, ctx);
497 raw_spin_unlock_irq(&ctx->lock);
501 * Update total_time_enabled and total_time_running for all events in a group.
503 static void update_group_times(struct perf_event *leader)
505 struct perf_event *event;
507 update_event_times(leader);
508 list_for_each_entry(event, &leader->sibling_list, group_entry)
509 update_event_times(event);
513 * Cross CPU call to disable a performance event
515 static void __perf_event_disable(void *info)
517 struct perf_event *event = info;
518 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
519 struct perf_event_context *ctx = event->ctx;
522 * If this is a per-task event, need to check whether this
523 * event's task is the current task on this cpu.
525 if (ctx->task && cpuctx->task_ctx != ctx)
528 raw_spin_lock(&ctx->lock);
531 * If the event is on, turn it off.
532 * If it is in error state, leave it in error state.
534 if (event->state >= PERF_EVENT_STATE_INACTIVE) {
535 update_context_time(ctx);
536 update_group_times(event);
537 if (event == event->group_leader)
538 group_sched_out(event, cpuctx, ctx);
540 event_sched_out(event, cpuctx, ctx);
541 event->state = PERF_EVENT_STATE_OFF;
544 raw_spin_unlock(&ctx->lock);
550 * If event->ctx is a cloned context, callers must make sure that
551 * every task struct that event->ctx->task could possibly point to
552 * remains valid. This condition is satisifed when called through
553 * perf_event_for_each_child or perf_event_for_each because they
554 * hold the top-level event's child_mutex, so any descendant that
555 * goes to exit will block in sync_child_event.
556 * When called from perf_pending_event it's OK because event->ctx
557 * is the current context on this CPU and preemption is disabled,
558 * hence we can't get into perf_event_task_sched_out for this context.
560 void perf_event_disable(struct perf_event *event)
562 struct perf_event_context *ctx = event->ctx;
563 struct task_struct *task = ctx->task;
567 * Disable the event on the cpu that it's on
569 smp_call_function_single(event->cpu, __perf_event_disable,
575 task_oncpu_function_call(task, __perf_event_disable, event);
577 raw_spin_lock_irq(&ctx->lock);
579 * If the event is still active, we need to retry the cross-call.
581 if (event->state == PERF_EVENT_STATE_ACTIVE) {
582 raw_spin_unlock_irq(&ctx->lock);
587 * Since we have the lock this context can't be scheduled
588 * in, so we can change the state safely.
590 if (event->state == PERF_EVENT_STATE_INACTIVE) {
591 update_group_times(event);
592 event->state = PERF_EVENT_STATE_OFF;
595 raw_spin_unlock_irq(&ctx->lock);
599 event_sched_in(struct perf_event *event,
600 struct perf_cpu_context *cpuctx,
601 struct perf_event_context *ctx)
603 if (event->state <= PERF_EVENT_STATE_OFF)
606 event->state = PERF_EVENT_STATE_ACTIVE;
607 event->oncpu = smp_processor_id();
609 * The new state must be visible before we turn it on in the hardware:
613 if (event->pmu->enable(event)) {
614 event->state = PERF_EVENT_STATE_INACTIVE;
619 event->tstamp_running += ctx->time - event->tstamp_stopped;
621 if (!is_software_event(event))
622 cpuctx->active_oncpu++;
625 if (event->attr.exclusive)
626 cpuctx->exclusive = 1;
632 group_sched_in(struct perf_event *group_event,
633 struct perf_cpu_context *cpuctx,
634 struct perf_event_context *ctx)
636 struct perf_event *event, *partial_group = NULL;
637 const struct pmu *pmu = group_event->pmu;
641 if (group_event->state == PERF_EVENT_STATE_OFF)
644 /* Check if group transaction availabe */
651 if (event_sched_in(group_event, cpuctx, ctx))
655 * Schedule in siblings as one group (if any):
657 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
658 if (event_sched_in(event, cpuctx, ctx)) {
659 partial_group = event;
667 ret = pmu->commit_txn(pmu);
669 pmu->cancel_txn(pmu);
675 pmu->cancel_txn(pmu);
678 * Groups can be scheduled in as one unit only, so undo any
679 * partial group before returning:
681 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
682 if (event == partial_group)
684 event_sched_out(event, cpuctx, ctx);
686 event_sched_out(group_event, cpuctx, ctx);
692 * Work out whether we can put this event group on the CPU now.
694 static int group_can_go_on(struct perf_event *event,
695 struct perf_cpu_context *cpuctx,
699 * Groups consisting entirely of software events can always go on.
701 if (event->group_flags & PERF_GROUP_SOFTWARE)
704 * If an exclusive group is already on, no other hardware
707 if (cpuctx->exclusive)
710 * If this group is exclusive and there are already
711 * events on the CPU, it can't go on.
713 if (event->attr.exclusive && cpuctx->active_oncpu)
716 * Otherwise, try to add it if all previous groups were able
722 static void add_event_to_ctx(struct perf_event *event,
723 struct perf_event_context *ctx)
725 list_add_event(event, ctx);
726 event->tstamp_enabled = ctx->time;
727 event->tstamp_running = ctx->time;
728 event->tstamp_stopped = ctx->time;
732 * Cross CPU call to install and enable a performance event
734 * Must be called with ctx->mutex held
736 static void __perf_install_in_context(void *info)
738 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
739 struct perf_event *event = info;
740 struct perf_event_context *ctx = event->ctx;
741 struct perf_event *leader = event->group_leader;
745 * If this is a task context, we need to check whether it is
746 * the current task context of this cpu. If not it has been
747 * scheduled out before the smp call arrived.
748 * Or possibly this is the right context but it isn't
749 * on this cpu because it had no events.
751 if (ctx->task && cpuctx->task_ctx != ctx) {
752 if (cpuctx->task_ctx || ctx->task != current)
754 cpuctx->task_ctx = ctx;
757 raw_spin_lock(&ctx->lock);
759 update_context_time(ctx);
762 * Protect the list operation against NMI by disabling the
763 * events on a global level. NOP for non NMI based events.
767 add_event_to_ctx(event, ctx);
769 if (event->cpu != -1 && event->cpu != smp_processor_id())
773 * Don't put the event on if it is disabled or if
774 * it is in a group and the group isn't on.
776 if (event->state != PERF_EVENT_STATE_INACTIVE ||
777 (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE))
781 * An exclusive event can't go on if there are already active
782 * hardware events, and no hardware event can go on if there
783 * is already an exclusive event on.
785 if (!group_can_go_on(event, cpuctx, 1))
788 err = event_sched_in(event, cpuctx, ctx);
792 * This event couldn't go on. If it is in a group
793 * then we have to pull the whole group off.
794 * If the event group is pinned then put it in error state.
797 group_sched_out(leader, cpuctx, ctx);
798 if (leader->attr.pinned) {
799 update_group_times(leader);
800 leader->state = PERF_EVENT_STATE_ERROR;
804 if (!err && !ctx->task && cpuctx->max_pertask)
805 cpuctx->max_pertask--;
810 raw_spin_unlock(&ctx->lock);
814 * Attach a performance event to a context
816 * First we add the event to the list with the hardware enable bit
817 * in event->hw_config cleared.
819 * If the event is attached to a task which is on a CPU we use a smp
820 * call to enable it in the task context. The task might have been
821 * scheduled away, but we check this in the smp call again.
823 * Must be called with ctx->mutex held.
826 perf_install_in_context(struct perf_event_context *ctx,
827 struct perf_event *event,
830 struct task_struct *task = ctx->task;
834 * Per cpu events are installed via an smp call and
835 * the install is always successful.
837 smp_call_function_single(cpu, __perf_install_in_context,
843 task_oncpu_function_call(task, __perf_install_in_context,
846 raw_spin_lock_irq(&ctx->lock);
848 * we need to retry the smp call.
850 if (ctx->is_active && list_empty(&event->group_entry)) {
851 raw_spin_unlock_irq(&ctx->lock);
856 * The lock prevents that this context is scheduled in so we
857 * can add the event safely, if it the call above did not
860 if (list_empty(&event->group_entry))
861 add_event_to_ctx(event, ctx);
862 raw_spin_unlock_irq(&ctx->lock);
866 * Put a event into inactive state and update time fields.
867 * Enabling the leader of a group effectively enables all
868 * the group members that aren't explicitly disabled, so we
869 * have to update their ->tstamp_enabled also.
870 * Note: this works for group members as well as group leaders
871 * since the non-leader members' sibling_lists will be empty.
873 static void __perf_event_mark_enabled(struct perf_event *event,
874 struct perf_event_context *ctx)
876 struct perf_event *sub;
878 event->state = PERF_EVENT_STATE_INACTIVE;
879 event->tstamp_enabled = ctx->time - event->total_time_enabled;
880 list_for_each_entry(sub, &event->sibling_list, group_entry)
881 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
882 sub->tstamp_enabled =
883 ctx->time - sub->total_time_enabled;
887 * Cross CPU call to enable a performance event
889 static void __perf_event_enable(void *info)
891 struct perf_event *event = info;
892 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
893 struct perf_event_context *ctx = event->ctx;
894 struct perf_event *leader = event->group_leader;
898 * If this is a per-task event, need to check whether this
899 * event's task is the current task on this cpu.
901 if (ctx->task && cpuctx->task_ctx != ctx) {
902 if (cpuctx->task_ctx || ctx->task != current)
904 cpuctx->task_ctx = ctx;
907 raw_spin_lock(&ctx->lock);
909 update_context_time(ctx);
911 if (event->state >= PERF_EVENT_STATE_INACTIVE)
913 __perf_event_mark_enabled(event, ctx);
915 if (event->cpu != -1 && event->cpu != smp_processor_id())
919 * If the event is in a group and isn't the group leader,
920 * then don't put it on unless the group is on.
922 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
925 if (!group_can_go_on(event, cpuctx, 1)) {
930 err = group_sched_in(event, cpuctx, ctx);
932 err = event_sched_in(event, cpuctx, ctx);
938 * If this event can't go on and it's part of a
939 * group, then the whole group has to come off.
942 group_sched_out(leader, cpuctx, ctx);
943 if (leader->attr.pinned) {
944 update_group_times(leader);
945 leader->state = PERF_EVENT_STATE_ERROR;
950 raw_spin_unlock(&ctx->lock);
956 * If event->ctx is a cloned context, callers must make sure that
957 * every task struct that event->ctx->task could possibly point to
958 * remains valid. This condition is satisfied when called through
959 * perf_event_for_each_child or perf_event_for_each as described
960 * for perf_event_disable.
962 void perf_event_enable(struct perf_event *event)
964 struct perf_event_context *ctx = event->ctx;
965 struct task_struct *task = ctx->task;
969 * Enable the event on the cpu that it's on
971 smp_call_function_single(event->cpu, __perf_event_enable,
976 raw_spin_lock_irq(&ctx->lock);
977 if (event->state >= PERF_EVENT_STATE_INACTIVE)
981 * If the event is in error state, clear that first.
982 * That way, if we see the event in error state below, we
983 * know that it has gone back into error state, as distinct
984 * from the task having been scheduled away before the
985 * cross-call arrived.
987 if (event->state == PERF_EVENT_STATE_ERROR)
988 event->state = PERF_EVENT_STATE_OFF;
991 raw_spin_unlock_irq(&ctx->lock);
992 task_oncpu_function_call(task, __perf_event_enable, event);
994 raw_spin_lock_irq(&ctx->lock);
997 * If the context is active and the event is still off,
998 * we need to retry the cross-call.
1000 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF)
1004 * Since we have the lock this context can't be scheduled
1005 * in, so we can change the state safely.
1007 if (event->state == PERF_EVENT_STATE_OFF)
1008 __perf_event_mark_enabled(event, ctx);
1011 raw_spin_unlock_irq(&ctx->lock);
1014 static int perf_event_refresh(struct perf_event *event, int refresh)
1017 * not supported on inherited events
1019 if (event->attr.inherit)
1022 atomic_add(refresh, &event->event_limit);
1023 perf_event_enable(event);
1029 EVENT_FLEXIBLE = 0x1,
1031 EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
1034 static void ctx_sched_out(struct perf_event_context *ctx,
1035 struct perf_cpu_context *cpuctx,
1036 enum event_type_t event_type)
1038 struct perf_event *event;
1040 raw_spin_lock(&ctx->lock);
1042 if (likely(!ctx->nr_events))
1044 update_context_time(ctx);
1047 if (!ctx->nr_active)
1050 if (event_type & EVENT_PINNED)
1051 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
1052 group_sched_out(event, cpuctx, ctx);
1054 if (event_type & EVENT_FLEXIBLE)
1055 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
1056 group_sched_out(event, cpuctx, ctx);
1061 raw_spin_unlock(&ctx->lock);
1065 * Test whether two contexts are equivalent, i.e. whether they
1066 * have both been cloned from the same version of the same context
1067 * and they both have the same number of enabled events.
1068 * If the number of enabled events is the same, then the set
1069 * of enabled events should be the same, because these are both
1070 * inherited contexts, therefore we can't access individual events
1071 * in them directly with an fd; we can only enable/disable all
1072 * events via prctl, or enable/disable all events in a family
1073 * via ioctl, which will have the same effect on both contexts.
1075 static int context_equiv(struct perf_event_context *ctx1,
1076 struct perf_event_context *ctx2)
1078 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
1079 && ctx1->parent_gen == ctx2->parent_gen
1080 && !ctx1->pin_count && !ctx2->pin_count;
1083 static void __perf_event_sync_stat(struct perf_event *event,
1084 struct perf_event *next_event)
1088 if (!event->attr.inherit_stat)
1092 * Update the event value, we cannot use perf_event_read()
1093 * because we're in the middle of a context switch and have IRQs
1094 * disabled, which upsets smp_call_function_single(), however
1095 * we know the event must be on the current CPU, therefore we
1096 * don't need to use it.
1098 switch (event->state) {
1099 case PERF_EVENT_STATE_ACTIVE:
1100 event->pmu->read(event);
1103 case PERF_EVENT_STATE_INACTIVE:
1104 update_event_times(event);
1112 * In order to keep per-task stats reliable we need to flip the event
1113 * values when we flip the contexts.
1115 value = atomic64_read(&next_event->count);
1116 value = atomic64_xchg(&event->count, value);
1117 atomic64_set(&next_event->count, value);
1119 swap(event->total_time_enabled, next_event->total_time_enabled);
1120 swap(event->total_time_running, next_event->total_time_running);
1123 * Since we swizzled the values, update the user visible data too.
1125 perf_event_update_userpage(event);
1126 perf_event_update_userpage(next_event);
1129 #define list_next_entry(pos, member) \
1130 list_entry(pos->member.next, typeof(*pos), member)
1132 static void perf_event_sync_stat(struct perf_event_context *ctx,
1133 struct perf_event_context *next_ctx)
1135 struct perf_event *event, *next_event;
1140 update_context_time(ctx);
1142 event = list_first_entry(&ctx->event_list,
1143 struct perf_event, event_entry);
1145 next_event = list_first_entry(&next_ctx->event_list,
1146 struct perf_event, event_entry);
1148 while (&event->event_entry != &ctx->event_list &&
1149 &next_event->event_entry != &next_ctx->event_list) {
1151 __perf_event_sync_stat(event, next_event);
1153 event = list_next_entry(event, event_entry);
1154 next_event = list_next_entry(next_event, event_entry);
1159 * Called from scheduler to remove the events of the current task,
1160 * with interrupts disabled.
1162 * We stop each event and update the event value in event->count.
1164 * This does not protect us against NMI, but disable()
1165 * sets the disabled bit in the control field of event _before_
1166 * accessing the event control register. If a NMI hits, then it will
1167 * not restart the event.
1169 void perf_event_task_sched_out(struct task_struct *task,
1170 struct task_struct *next)
1172 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1173 struct perf_event_context *ctx = task->perf_event_ctxp;
1174 struct perf_event_context *next_ctx;
1175 struct perf_event_context *parent;
1178 perf_sw_event(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 1, NULL, 0);
1180 if (likely(!ctx || !cpuctx->task_ctx))
1184 parent = rcu_dereference(ctx->parent_ctx);
1185 next_ctx = next->perf_event_ctxp;
1186 if (parent && next_ctx &&
1187 rcu_dereference(next_ctx->parent_ctx) == parent) {
1189 * Looks like the two contexts are clones, so we might be
1190 * able to optimize the context switch. We lock both
1191 * contexts and check that they are clones under the
1192 * lock (including re-checking that neither has been
1193 * uncloned in the meantime). It doesn't matter which
1194 * order we take the locks because no other cpu could
1195 * be trying to lock both of these tasks.
1197 raw_spin_lock(&ctx->lock);
1198 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
1199 if (context_equiv(ctx, next_ctx)) {
1201 * XXX do we need a memory barrier of sorts
1202 * wrt to rcu_dereference() of perf_event_ctxp
1204 task->perf_event_ctxp = next_ctx;
1205 next->perf_event_ctxp = ctx;
1207 next_ctx->task = task;
1210 perf_event_sync_stat(ctx, next_ctx);
1212 raw_spin_unlock(&next_ctx->lock);
1213 raw_spin_unlock(&ctx->lock);
1218 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
1219 cpuctx->task_ctx = NULL;
1223 static void task_ctx_sched_out(struct perf_event_context *ctx,
1224 enum event_type_t event_type)
1226 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1228 if (!cpuctx->task_ctx)
1231 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
1234 ctx_sched_out(ctx, cpuctx, event_type);
1235 cpuctx->task_ctx = NULL;
1239 * Called with IRQs disabled
1241 static void __perf_event_task_sched_out(struct perf_event_context *ctx)
1243 task_ctx_sched_out(ctx, EVENT_ALL);
1247 * Called with IRQs disabled
1249 static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
1250 enum event_type_t event_type)
1252 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
1256 ctx_pinned_sched_in(struct perf_event_context *ctx,
1257 struct perf_cpu_context *cpuctx)
1259 struct perf_event *event;
1261 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
1262 if (event->state <= PERF_EVENT_STATE_OFF)
1264 if (event->cpu != -1 && event->cpu != smp_processor_id())
1267 if (group_can_go_on(event, cpuctx, 1))
1268 group_sched_in(event, cpuctx, ctx);
1271 * If this pinned group hasn't been scheduled,
1272 * put it in error state.
1274 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1275 update_group_times(event);
1276 event->state = PERF_EVENT_STATE_ERROR;
1282 ctx_flexible_sched_in(struct perf_event_context *ctx,
1283 struct perf_cpu_context *cpuctx)
1285 struct perf_event *event;
1288 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
1289 /* Ignore events in OFF or ERROR state */
1290 if (event->state <= PERF_EVENT_STATE_OFF)
1293 * Listen to the 'cpu' scheduling filter constraint
1296 if (event->cpu != -1 && event->cpu != smp_processor_id())
1299 if (group_can_go_on(event, cpuctx, can_add_hw))
1300 if (group_sched_in(event, cpuctx, ctx))
1306 ctx_sched_in(struct perf_event_context *ctx,
1307 struct perf_cpu_context *cpuctx,
1308 enum event_type_t event_type)
1310 raw_spin_lock(&ctx->lock);
1312 if (likely(!ctx->nr_events))
1315 ctx->timestamp = perf_clock();
1320 * First go through the list and put on any pinned groups
1321 * in order to give them the best chance of going on.
1323 if (event_type & EVENT_PINNED)
1324 ctx_pinned_sched_in(ctx, cpuctx);
1326 /* Then walk through the lower prio flexible groups */
1327 if (event_type & EVENT_FLEXIBLE)
1328 ctx_flexible_sched_in(ctx, cpuctx);
1332 raw_spin_unlock(&ctx->lock);
1335 static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
1336 enum event_type_t event_type)
1338 struct perf_event_context *ctx = &cpuctx->ctx;
1340 ctx_sched_in(ctx, cpuctx, event_type);
1343 static void task_ctx_sched_in(struct task_struct *task,
1344 enum event_type_t event_type)
1346 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1347 struct perf_event_context *ctx = task->perf_event_ctxp;
1351 if (cpuctx->task_ctx == ctx)
1353 ctx_sched_in(ctx, cpuctx, event_type);
1354 cpuctx->task_ctx = ctx;
1357 * Called from scheduler to add the events of the current task
1358 * with interrupts disabled.
1360 * We restore the event value and then enable it.
1362 * This does not protect us against NMI, but enable()
1363 * sets the enabled bit in the control field of event _before_
1364 * accessing the event control register. If a NMI hits, then it will
1365 * keep the event running.
1367 void perf_event_task_sched_in(struct task_struct *task)
1369 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1370 struct perf_event_context *ctx = task->perf_event_ctxp;
1375 if (cpuctx->task_ctx == ctx)
1381 * We want to keep the following priority order:
1382 * cpu pinned (that don't need to move), task pinned,
1383 * cpu flexible, task flexible.
1385 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
1387 ctx_sched_in(ctx, cpuctx, EVENT_PINNED);
1388 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE);
1389 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE);
1391 cpuctx->task_ctx = ctx;
1396 #define MAX_INTERRUPTS (~0ULL)
1398 static void perf_log_throttle(struct perf_event *event, int enable);
1400 static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
1402 u64 frequency = event->attr.sample_freq;
1403 u64 sec = NSEC_PER_SEC;
1404 u64 divisor, dividend;
1406 int count_fls, nsec_fls, frequency_fls, sec_fls;
1408 count_fls = fls64(count);
1409 nsec_fls = fls64(nsec);
1410 frequency_fls = fls64(frequency);
1414 * We got @count in @nsec, with a target of sample_freq HZ
1415 * the target period becomes:
1418 * period = -------------------
1419 * @nsec * sample_freq
1424 * Reduce accuracy by one bit such that @a and @b converge
1425 * to a similar magnitude.
1427 #define REDUCE_FLS(a, b) \
1429 if (a##_fls > b##_fls) { \
1439 * Reduce accuracy until either term fits in a u64, then proceed with
1440 * the other, so that finally we can do a u64/u64 division.
1442 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
1443 REDUCE_FLS(nsec, frequency);
1444 REDUCE_FLS(sec, count);
1447 if (count_fls + sec_fls > 64) {
1448 divisor = nsec * frequency;
1450 while (count_fls + sec_fls > 64) {
1451 REDUCE_FLS(count, sec);
1455 dividend = count * sec;
1457 dividend = count * sec;
1459 while (nsec_fls + frequency_fls > 64) {
1460 REDUCE_FLS(nsec, frequency);
1464 divisor = nsec * frequency;
1467 return div64_u64(dividend, divisor);
1470 static void perf_event_stop(struct perf_event *event)
1472 if (!event->pmu->stop)
1473 return event->pmu->disable(event);
1475 return event->pmu->stop(event);
1478 static int perf_event_start(struct perf_event *event)
1480 if (!event->pmu->start)
1481 return event->pmu->enable(event);
1483 return event->pmu->start(event);
1486 static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count)
1488 struct hw_perf_event *hwc = &event->hw;
1489 u64 period, sample_period;
1492 period = perf_calculate_period(event, nsec, count);
1494 delta = (s64)(period - hwc->sample_period);
1495 delta = (delta + 7) / 8; /* low pass filter */
1497 sample_period = hwc->sample_period + delta;
1502 hwc->sample_period = sample_period;
1504 if (atomic64_read(&hwc->period_left) > 8*sample_period) {
1506 perf_event_stop(event);
1507 atomic64_set(&hwc->period_left, 0);
1508 perf_event_start(event);
1513 static void perf_ctx_adjust_freq(struct perf_event_context *ctx)
1515 struct perf_event *event;
1516 struct hw_perf_event *hwc;
1517 u64 interrupts, now;
1520 raw_spin_lock(&ctx->lock);
1521 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
1522 if (event->state != PERF_EVENT_STATE_ACTIVE)
1525 if (event->cpu != -1 && event->cpu != smp_processor_id())
1530 interrupts = hwc->interrupts;
1531 hwc->interrupts = 0;
1534 * unthrottle events on the tick
1536 if (interrupts == MAX_INTERRUPTS) {
1537 perf_log_throttle(event, 1);
1539 event->pmu->unthrottle(event);
1543 if (!event->attr.freq || !event->attr.sample_freq)
1547 event->pmu->read(event);
1548 now = atomic64_read(&event->count);
1549 delta = now - hwc->freq_count_stamp;
1550 hwc->freq_count_stamp = now;
1553 perf_adjust_period(event, TICK_NSEC, delta);
1556 raw_spin_unlock(&ctx->lock);
1560 * Round-robin a context's events:
1562 static void rotate_ctx(struct perf_event_context *ctx)
1564 raw_spin_lock(&ctx->lock);
1566 /* Rotate the first entry last of non-pinned groups */
1567 list_rotate_left(&ctx->flexible_groups);
1569 raw_spin_unlock(&ctx->lock);
1572 void perf_event_task_tick(struct task_struct *curr)
1574 struct perf_cpu_context *cpuctx;
1575 struct perf_event_context *ctx;
1578 if (!atomic_read(&nr_events))
1581 cpuctx = &__get_cpu_var(perf_cpu_context);
1582 if (cpuctx->ctx.nr_events &&
1583 cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
1586 ctx = curr->perf_event_ctxp;
1587 if (ctx && ctx->nr_events && ctx->nr_events != ctx->nr_active)
1590 perf_ctx_adjust_freq(&cpuctx->ctx);
1592 perf_ctx_adjust_freq(ctx);
1598 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
1600 task_ctx_sched_out(ctx, EVENT_FLEXIBLE);
1602 rotate_ctx(&cpuctx->ctx);
1606 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE);
1608 task_ctx_sched_in(curr, EVENT_FLEXIBLE);
1612 static int event_enable_on_exec(struct perf_event *event,
1613 struct perf_event_context *ctx)
1615 if (!event->attr.enable_on_exec)
1618 event->attr.enable_on_exec = 0;
1619 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1622 __perf_event_mark_enabled(event, ctx);
1628 * Enable all of a task's events that have been marked enable-on-exec.
1629 * This expects task == current.
1631 static void perf_event_enable_on_exec(struct task_struct *task)
1633 struct perf_event_context *ctx;
1634 struct perf_event *event;
1635 unsigned long flags;
1639 local_irq_save(flags);
1640 ctx = task->perf_event_ctxp;
1641 if (!ctx || !ctx->nr_events)
1644 __perf_event_task_sched_out(ctx);
1646 raw_spin_lock(&ctx->lock);
1648 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
1649 ret = event_enable_on_exec(event, ctx);
1654 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
1655 ret = event_enable_on_exec(event, ctx);
1661 * Unclone this context if we enabled any event.
1666 raw_spin_unlock(&ctx->lock);
1668 perf_event_task_sched_in(task);
1670 local_irq_restore(flags);
1674 * Cross CPU call to read the hardware event
1676 static void __perf_event_read(void *info)
1678 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1679 struct perf_event *event = info;
1680 struct perf_event_context *ctx = event->ctx;
1683 * If this is a task context, we need to check whether it is
1684 * the current task context of this cpu. If not it has been
1685 * scheduled out before the smp call arrived. In that case
1686 * event->count would have been updated to a recent sample
1687 * when the event was scheduled out.
1689 if (ctx->task && cpuctx->task_ctx != ctx)
1692 raw_spin_lock(&ctx->lock);
1693 update_context_time(ctx);
1694 update_event_times(event);
1695 raw_spin_unlock(&ctx->lock);
1697 event->pmu->read(event);
1700 static u64 perf_event_read(struct perf_event *event)
1703 * If event is enabled and currently active on a CPU, update the
1704 * value in the event structure:
1706 if (event->state == PERF_EVENT_STATE_ACTIVE) {
1707 smp_call_function_single(event->oncpu,
1708 __perf_event_read, event, 1);
1709 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
1710 struct perf_event_context *ctx = event->ctx;
1711 unsigned long flags;
1713 raw_spin_lock_irqsave(&ctx->lock, flags);
1714 update_context_time(ctx);
1715 update_event_times(event);
1716 raw_spin_unlock_irqrestore(&ctx->lock, flags);
1719 return atomic64_read(&event->count);
1723 * Initialize the perf_event context in a task_struct:
1726 __perf_event_init_context(struct perf_event_context *ctx,
1727 struct task_struct *task)
1729 raw_spin_lock_init(&ctx->lock);
1730 mutex_init(&ctx->mutex);
1731 INIT_LIST_HEAD(&ctx->pinned_groups);
1732 INIT_LIST_HEAD(&ctx->flexible_groups);
1733 INIT_LIST_HEAD(&ctx->event_list);
1734 atomic_set(&ctx->refcount, 1);
1738 static struct perf_event_context *find_get_context(pid_t pid, int cpu)
1740 struct perf_event_context *ctx;
1741 struct perf_cpu_context *cpuctx;
1742 struct task_struct *task;
1743 unsigned long flags;
1746 if (pid == -1 && cpu != -1) {
1747 /* Must be root to operate on a CPU event: */
1748 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
1749 return ERR_PTR(-EACCES);
1751 if (cpu < 0 || cpu >= nr_cpumask_bits)
1752 return ERR_PTR(-EINVAL);
1755 * We could be clever and allow to attach a event to an
1756 * offline CPU and activate it when the CPU comes up, but
1759 if (!cpu_online(cpu))
1760 return ERR_PTR(-ENODEV);
1762 cpuctx = &per_cpu(perf_cpu_context, cpu);
1773 task = find_task_by_vpid(pid);
1775 get_task_struct(task);
1779 return ERR_PTR(-ESRCH);
1782 * Can't attach events to a dying task.
1785 if (task->flags & PF_EXITING)
1788 /* Reuse ptrace permission checks for now. */
1790 if (!ptrace_may_access(task, PTRACE_MODE_READ))
1794 ctx = perf_lock_task_context(task, &flags);
1797 raw_spin_unlock_irqrestore(&ctx->lock, flags);
1801 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
1805 __perf_event_init_context(ctx, task);
1807 if (cmpxchg(&task->perf_event_ctxp, NULL, ctx)) {
1809 * We raced with some other task; use
1810 * the context they set.
1815 get_task_struct(task);
1818 put_task_struct(task);
1822 put_task_struct(task);
1823 return ERR_PTR(err);
1826 static void perf_event_free_filter(struct perf_event *event);
1828 static void free_event_rcu(struct rcu_head *head)
1830 struct perf_event *event;
1832 event = container_of(head, struct perf_event, rcu_head);
1834 put_pid_ns(event->ns);
1835 perf_event_free_filter(event);
1839 static void perf_pending_sync(struct perf_event *event);
1841 static void free_event(struct perf_event *event)
1843 perf_pending_sync(event);
1845 if (!event->parent) {
1846 atomic_dec(&nr_events);
1847 if (event->attr.mmap)
1848 atomic_dec(&nr_mmap_events);
1849 if (event->attr.comm)
1850 atomic_dec(&nr_comm_events);
1851 if (event->attr.task)
1852 atomic_dec(&nr_task_events);
1855 if (event->output) {
1856 fput(event->output->filp);
1857 event->output = NULL;
1861 event->destroy(event);
1863 put_ctx(event->ctx);
1864 call_rcu(&event->rcu_head, free_event_rcu);
1867 int perf_event_release_kernel(struct perf_event *event)
1869 struct perf_event_context *ctx = event->ctx;
1871 WARN_ON_ONCE(ctx->parent_ctx);
1873 * There are two ways this annotation is useful:
1875 * 1) there is a lock recursion from perf_event_exit_task
1876 * see the comment there.
1878 * 2) there is a lock-inversion with mmap_sem through
1879 * perf_event_read_group(), which takes faults while
1880 * holding ctx->mutex, however this is called after
1881 * the last filedesc died, so there is no possibility
1882 * to trigger the AB-BA case.
1884 mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
1885 perf_event_remove_from_context(event);
1886 mutex_unlock(&ctx->mutex);
1888 mutex_lock(&event->owner->perf_event_mutex);
1889 list_del_init(&event->owner_entry);
1890 mutex_unlock(&event->owner->perf_event_mutex);
1891 put_task_struct(event->owner);
1897 EXPORT_SYMBOL_GPL(perf_event_release_kernel);
1900 * Called when the last reference to the file is gone.
1902 static int perf_release(struct inode *inode, struct file *file)
1904 struct perf_event *event = file->private_data;
1906 file->private_data = NULL;
1908 return perf_event_release_kernel(event);
1911 static int perf_event_read_size(struct perf_event *event)
1913 int entry = sizeof(u64); /* value */
1917 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1918 size += sizeof(u64);
1920 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1921 size += sizeof(u64);
1923 if (event->attr.read_format & PERF_FORMAT_ID)
1924 entry += sizeof(u64);
1926 if (event->attr.read_format & PERF_FORMAT_GROUP) {
1927 nr += event->group_leader->nr_siblings;
1928 size += sizeof(u64);
1936 u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
1938 struct perf_event *child;
1944 mutex_lock(&event->child_mutex);
1945 total += perf_event_read(event);
1946 *enabled += event->total_time_enabled +
1947 atomic64_read(&event->child_total_time_enabled);
1948 *running += event->total_time_running +
1949 atomic64_read(&event->child_total_time_running);
1951 list_for_each_entry(child, &event->child_list, child_list) {
1952 total += perf_event_read(child);
1953 *enabled += child->total_time_enabled;
1954 *running += child->total_time_running;
1956 mutex_unlock(&event->child_mutex);
1960 EXPORT_SYMBOL_GPL(perf_event_read_value);
1962 static int perf_event_read_group(struct perf_event *event,
1963 u64 read_format, char __user *buf)
1965 struct perf_event *leader = event->group_leader, *sub;
1966 int n = 0, size = 0, ret = -EFAULT;
1967 struct perf_event_context *ctx = leader->ctx;
1969 u64 count, enabled, running;
1971 mutex_lock(&ctx->mutex);
1972 count = perf_event_read_value(leader, &enabled, &running);
1974 values[n++] = 1 + leader->nr_siblings;
1975 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1976 values[n++] = enabled;
1977 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1978 values[n++] = running;
1979 values[n++] = count;
1980 if (read_format & PERF_FORMAT_ID)
1981 values[n++] = primary_event_id(leader);
1983 size = n * sizeof(u64);
1985 if (copy_to_user(buf, values, size))
1990 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
1993 values[n++] = perf_event_read_value(sub, &enabled, &running);
1994 if (read_format & PERF_FORMAT_ID)
1995 values[n++] = primary_event_id(sub);
1997 size = n * sizeof(u64);
1999 if (copy_to_user(buf + ret, values, size)) {
2007 mutex_unlock(&ctx->mutex);
2012 static int perf_event_read_one(struct perf_event *event,
2013 u64 read_format, char __user *buf)
2015 u64 enabled, running;
2019 values[n++] = perf_event_read_value(event, &enabled, &running);
2020 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2021 values[n++] = enabled;
2022 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2023 values[n++] = running;
2024 if (read_format & PERF_FORMAT_ID)
2025 values[n++] = primary_event_id(event);
2027 if (copy_to_user(buf, values, n * sizeof(u64)))
2030 return n * sizeof(u64);
2034 * Read the performance event - simple non blocking version for now
2037 perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
2039 u64 read_format = event->attr.read_format;
2043 * Return end-of-file for a read on a event that is in
2044 * error state (i.e. because it was pinned but it couldn't be
2045 * scheduled on to the CPU at some point).
2047 if (event->state == PERF_EVENT_STATE_ERROR)
2050 if (count < perf_event_read_size(event))
2053 WARN_ON_ONCE(event->ctx->parent_ctx);
2054 if (read_format & PERF_FORMAT_GROUP)
2055 ret = perf_event_read_group(event, read_format, buf);
2057 ret = perf_event_read_one(event, read_format, buf);
2063 perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
2065 struct perf_event *event = file->private_data;
2067 return perf_read_hw(event, buf, count);
2070 static unsigned int perf_poll(struct file *file, poll_table *wait)
2072 struct perf_event *event = file->private_data;
2073 struct perf_mmap_data *data;
2074 unsigned int events = POLL_HUP;
2077 data = rcu_dereference(event->data);
2079 events = atomic_xchg(&data->poll, 0);
2082 poll_wait(file, &event->waitq, wait);
2087 static void perf_event_reset(struct perf_event *event)
2089 (void)perf_event_read(event);
2090 atomic64_set(&event->count, 0);
2091 perf_event_update_userpage(event);
2095 * Holding the top-level event's child_mutex means that any
2096 * descendant process that has inherited this event will block
2097 * in sync_child_event if it goes to exit, thus satisfying the
2098 * task existence requirements of perf_event_enable/disable.
2100 static void perf_event_for_each_child(struct perf_event *event,
2101 void (*func)(struct perf_event *))
2103 struct perf_event *child;
2105 WARN_ON_ONCE(event->ctx->parent_ctx);
2106 mutex_lock(&event->child_mutex);
2108 list_for_each_entry(child, &event->child_list, child_list)
2110 mutex_unlock(&event->child_mutex);
2113 static void perf_event_for_each(struct perf_event *event,
2114 void (*func)(struct perf_event *))
2116 struct perf_event_context *ctx = event->ctx;
2117 struct perf_event *sibling;
2119 WARN_ON_ONCE(ctx->parent_ctx);
2120 mutex_lock(&ctx->mutex);
2121 event = event->group_leader;
2123 perf_event_for_each_child(event, func);
2125 list_for_each_entry(sibling, &event->sibling_list, group_entry)
2126 perf_event_for_each_child(event, func);
2127 mutex_unlock(&ctx->mutex);
2130 static int perf_event_period(struct perf_event *event, u64 __user *arg)
2132 struct perf_event_context *ctx = event->ctx;
2137 if (!event->attr.sample_period)
2140 size = copy_from_user(&value, arg, sizeof(value));
2141 if (size != sizeof(value))
2147 raw_spin_lock_irq(&ctx->lock);
2148 if (event->attr.freq) {
2149 if (value > sysctl_perf_event_sample_rate) {
2154 event->attr.sample_freq = value;
2156 event->attr.sample_period = value;
2157 event->hw.sample_period = value;
2160 raw_spin_unlock_irq(&ctx->lock);
2165 static int perf_event_set_output(struct perf_event *event, int output_fd);
2166 static int perf_event_set_filter(struct perf_event *event, void __user *arg);
2168 static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2170 struct perf_event *event = file->private_data;
2171 void (*func)(struct perf_event *);
2175 case PERF_EVENT_IOC_ENABLE:
2176 func = perf_event_enable;
2178 case PERF_EVENT_IOC_DISABLE:
2179 func = perf_event_disable;
2181 case PERF_EVENT_IOC_RESET:
2182 func = perf_event_reset;
2185 case PERF_EVENT_IOC_REFRESH:
2186 return perf_event_refresh(event, arg);
2188 case PERF_EVENT_IOC_PERIOD:
2189 return perf_event_period(event, (u64 __user *)arg);
2191 case PERF_EVENT_IOC_SET_OUTPUT:
2192 return perf_event_set_output(event, arg);
2194 case PERF_EVENT_IOC_SET_FILTER:
2195 return perf_event_set_filter(event, (void __user *)arg);
2201 if (flags & PERF_IOC_FLAG_GROUP)
2202 perf_event_for_each(event, func);
2204 perf_event_for_each_child(event, func);
2209 int perf_event_task_enable(void)
2211 struct perf_event *event;
2213 mutex_lock(¤t->perf_event_mutex);
2214 list_for_each_entry(event, ¤t->perf_event_list, owner_entry)
2215 perf_event_for_each_child(event, perf_event_enable);
2216 mutex_unlock(¤t->perf_event_mutex);
2221 int perf_event_task_disable(void)
2223 struct perf_event *event;
2225 mutex_lock(¤t->perf_event_mutex);
2226 list_for_each_entry(event, ¤t->perf_event_list, owner_entry)
2227 perf_event_for_each_child(event, perf_event_disable);
2228 mutex_unlock(¤t->perf_event_mutex);
2233 #ifndef PERF_EVENT_INDEX_OFFSET
2234 # define PERF_EVENT_INDEX_OFFSET 0
2237 static int perf_event_index(struct perf_event *event)
2239 if (event->state != PERF_EVENT_STATE_ACTIVE)
2242 return event->hw.idx + 1 - PERF_EVENT_INDEX_OFFSET;
2246 * Callers need to ensure there can be no nesting of this function, otherwise
2247 * the seqlock logic goes bad. We can not serialize this because the arch
2248 * code calls this from NMI context.
2250 void perf_event_update_userpage(struct perf_event *event)
2252 struct perf_event_mmap_page *userpg;
2253 struct perf_mmap_data *data;
2256 data = rcu_dereference(event->data);
2260 userpg = data->user_page;
2263 * Disable preemption so as to not let the corresponding user-space
2264 * spin too long if we get preempted.
2269 userpg->index = perf_event_index(event);
2270 userpg->offset = atomic64_read(&event->count);
2271 if (event->state == PERF_EVENT_STATE_ACTIVE)
2272 userpg->offset -= atomic64_read(&event->hw.prev_count);
2274 userpg->time_enabled = event->total_time_enabled +
2275 atomic64_read(&event->child_total_time_enabled);
2277 userpg->time_running = event->total_time_running +
2278 atomic64_read(&event->child_total_time_running);
2287 static unsigned long perf_data_size(struct perf_mmap_data *data)
2289 return data->nr_pages << (PAGE_SHIFT + data->data_order);
2292 #ifndef CONFIG_PERF_USE_VMALLOC
2295 * Back perf_mmap() with regular GFP_KERNEL-0 pages.
2298 static struct page *
2299 perf_mmap_to_page(struct perf_mmap_data *data, unsigned long pgoff)
2301 if (pgoff > data->nr_pages)
2305 return virt_to_page(data->user_page);
2307 return virt_to_page(data->data_pages[pgoff - 1]);
2310 static struct perf_mmap_data *
2311 perf_mmap_data_alloc(struct perf_event *event, int nr_pages)
2313 struct perf_mmap_data *data;
2317 WARN_ON(atomic_read(&event->mmap_count));
2319 size = sizeof(struct perf_mmap_data);
2320 size += nr_pages * sizeof(void *);
2322 data = kzalloc(size, GFP_KERNEL);
2326 data->user_page = (void *)get_zeroed_page(GFP_KERNEL);
2327 if (!data->user_page)
2328 goto fail_user_page;
2330 for (i = 0; i < nr_pages; i++) {
2331 data->data_pages[i] = (void *)get_zeroed_page(GFP_KERNEL);
2332 if (!data->data_pages[i])
2333 goto fail_data_pages;
2336 data->data_order = 0;
2337 data->nr_pages = nr_pages;
2342 for (i--; i >= 0; i--)
2343 free_page((unsigned long)data->data_pages[i]);
2345 free_page((unsigned long)data->user_page);
2354 static void perf_mmap_free_page(unsigned long addr)
2356 struct page *page = virt_to_page((void *)addr);
2358 page->mapping = NULL;
2362 static void perf_mmap_data_free(struct perf_mmap_data *data)
2366 perf_mmap_free_page((unsigned long)data->user_page);
2367 for (i = 0; i < data->nr_pages; i++)
2368 perf_mmap_free_page((unsigned long)data->data_pages[i]);
2375 * Back perf_mmap() with vmalloc memory.
2377 * Required for architectures that have d-cache aliasing issues.
2380 static struct page *
2381 perf_mmap_to_page(struct perf_mmap_data *data, unsigned long pgoff)
2383 if (pgoff > (1UL << data->data_order))
2386 return vmalloc_to_page((void *)data->user_page + pgoff * PAGE_SIZE);
2389 static void perf_mmap_unmark_page(void *addr)
2391 struct page *page = vmalloc_to_page(addr);
2393 page->mapping = NULL;
2396 static void perf_mmap_data_free_work(struct work_struct *work)
2398 struct perf_mmap_data *data;
2402 data = container_of(work, struct perf_mmap_data, work);
2403 nr = 1 << data->data_order;
2405 base = data->user_page;
2406 for (i = 0; i < nr + 1; i++)
2407 perf_mmap_unmark_page(base + (i * PAGE_SIZE));
2413 static void perf_mmap_data_free(struct perf_mmap_data *data)
2415 schedule_work(&data->work);
2418 static struct perf_mmap_data *
2419 perf_mmap_data_alloc(struct perf_event *event, int nr_pages)
2421 struct perf_mmap_data *data;
2425 WARN_ON(atomic_read(&event->mmap_count));
2427 size = sizeof(struct perf_mmap_data);
2428 size += sizeof(void *);
2430 data = kzalloc(size, GFP_KERNEL);
2434 INIT_WORK(&data->work, perf_mmap_data_free_work);
2436 all_buf = vmalloc_user((nr_pages + 1) * PAGE_SIZE);
2440 data->user_page = all_buf;
2441 data->data_pages[0] = all_buf + PAGE_SIZE;
2442 data->data_order = ilog2(nr_pages);
2456 static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
2458 struct perf_event *event = vma->vm_file->private_data;
2459 struct perf_mmap_data *data;
2460 int ret = VM_FAULT_SIGBUS;
2462 if (vmf->flags & FAULT_FLAG_MKWRITE) {
2463 if (vmf->pgoff == 0)
2469 data = rcu_dereference(event->data);
2473 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
2476 vmf->page = perf_mmap_to_page(data, vmf->pgoff);
2480 get_page(vmf->page);
2481 vmf->page->mapping = vma->vm_file->f_mapping;
2482 vmf->page->index = vmf->pgoff;
2492 perf_mmap_data_init(struct perf_event *event, struct perf_mmap_data *data)
2494 long max_size = perf_data_size(data);
2496 atomic_set(&data->lock, -1);
2498 if (event->attr.watermark) {
2499 data->watermark = min_t(long, max_size,
2500 event->attr.wakeup_watermark);
2503 if (!data->watermark)
2504 data->watermark = max_size / 2;
2507 rcu_assign_pointer(event->data, data);
2510 static void perf_mmap_data_free_rcu(struct rcu_head *rcu_head)
2512 struct perf_mmap_data *data;
2514 data = container_of(rcu_head, struct perf_mmap_data, rcu_head);
2515 perf_mmap_data_free(data);
2518 static void perf_mmap_data_release(struct perf_event *event)
2520 struct perf_mmap_data *data = event->data;
2522 WARN_ON(atomic_read(&event->mmap_count));
2524 rcu_assign_pointer(event->data, NULL);
2525 call_rcu(&data->rcu_head, perf_mmap_data_free_rcu);
2528 static void perf_mmap_open(struct vm_area_struct *vma)
2530 struct perf_event *event = vma->vm_file->private_data;
2532 atomic_inc(&event->mmap_count);
2535 static void perf_mmap_close(struct vm_area_struct *vma)
2537 struct perf_event *event = vma->vm_file->private_data;
2539 WARN_ON_ONCE(event->ctx->parent_ctx);
2540 if (atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) {
2541 unsigned long size = perf_data_size(event->data);
2542 struct user_struct *user = current_user();
2544 atomic_long_sub((size >> PAGE_SHIFT) + 1, &user->locked_vm);
2545 vma->vm_mm->locked_vm -= event->data->nr_locked;
2546 perf_mmap_data_release(event);
2547 mutex_unlock(&event->mmap_mutex);
2551 static const struct vm_operations_struct perf_mmap_vmops = {
2552 .open = perf_mmap_open,
2553 .close = perf_mmap_close,
2554 .fault = perf_mmap_fault,
2555 .page_mkwrite = perf_mmap_fault,
2558 static int perf_mmap(struct file *file, struct vm_area_struct *vma)
2560 struct perf_event *event = file->private_data;
2561 unsigned long user_locked, user_lock_limit;
2562 struct user_struct *user = current_user();
2563 unsigned long locked, lock_limit;
2564 struct perf_mmap_data *data;
2565 unsigned long vma_size;
2566 unsigned long nr_pages;
2567 long user_extra, extra;
2570 if (!(vma->vm_flags & VM_SHARED))
2573 vma_size = vma->vm_end - vma->vm_start;
2574 nr_pages = (vma_size / PAGE_SIZE) - 1;
2577 * If we have data pages ensure they're a power-of-two number, so we
2578 * can do bitmasks instead of modulo.
2580 if (nr_pages != 0 && !is_power_of_2(nr_pages))
2583 if (vma_size != PAGE_SIZE * (1 + nr_pages))
2586 if (vma->vm_pgoff != 0)
2589 WARN_ON_ONCE(event->ctx->parent_ctx);
2590 mutex_lock(&event->mmap_mutex);
2591 if (event->output) {
2596 if (atomic_inc_not_zero(&event->mmap_count)) {
2597 if (nr_pages != event->data->nr_pages)
2602 user_extra = nr_pages + 1;
2603 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
2606 * Increase the limit linearly with more CPUs:
2608 user_lock_limit *= num_online_cpus();
2610 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
2613 if (user_locked > user_lock_limit)
2614 extra = user_locked - user_lock_limit;
2616 lock_limit = rlimit(RLIMIT_MEMLOCK);
2617 lock_limit >>= PAGE_SHIFT;
2618 locked = vma->vm_mm->locked_vm + extra;
2620 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
2621 !capable(CAP_IPC_LOCK)) {
2626 WARN_ON(event->data);
2628 data = perf_mmap_data_alloc(event, nr_pages);
2634 perf_mmap_data_init(event, data);
2636 atomic_set(&event->mmap_count, 1);
2637 atomic_long_add(user_extra, &user->locked_vm);
2638 vma->vm_mm->locked_vm += extra;
2639 event->data->nr_locked = extra;
2640 if (vma->vm_flags & VM_WRITE)
2641 event->data->writable = 1;
2644 mutex_unlock(&event->mmap_mutex);
2646 vma->vm_flags |= VM_RESERVED;
2647 vma->vm_ops = &perf_mmap_vmops;
2652 static int perf_fasync(int fd, struct file *filp, int on)
2654 struct inode *inode = filp->f_path.dentry->d_inode;
2655 struct perf_event *event = filp->private_data;
2658 mutex_lock(&inode->i_mutex);
2659 retval = fasync_helper(fd, filp, on, &event->fasync);
2660 mutex_unlock(&inode->i_mutex);
2668 static const struct file_operations perf_fops = {
2669 .llseek = no_llseek,
2670 .release = perf_release,
2673 .unlocked_ioctl = perf_ioctl,
2674 .compat_ioctl = perf_ioctl,
2676 .fasync = perf_fasync,
2682 * If there's data, ensure we set the poll() state and publish everything
2683 * to user-space before waking everybody up.
2686 void perf_event_wakeup(struct perf_event *event)
2688 wake_up_all(&event->waitq);
2690 if (event->pending_kill) {
2691 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
2692 event->pending_kill = 0;
2699 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
2701 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
2702 * single linked list and use cmpxchg() to add entries lockless.
2705 static void perf_pending_event(struct perf_pending_entry *entry)
2707 struct perf_event *event = container_of(entry,
2708 struct perf_event, pending);
2710 if (event->pending_disable) {
2711 event->pending_disable = 0;
2712 __perf_event_disable(event);
2715 if (event->pending_wakeup) {
2716 event->pending_wakeup = 0;
2717 perf_event_wakeup(event);
2721 #define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
2723 static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
2727 static void perf_pending_queue(struct perf_pending_entry *entry,
2728 void (*func)(struct perf_pending_entry *))
2730 struct perf_pending_entry **head;
2732 if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
2737 head = &get_cpu_var(perf_pending_head);
2740 entry->next = *head;
2741 } while (cmpxchg(head, entry->next, entry) != entry->next);
2743 set_perf_event_pending();
2745 put_cpu_var(perf_pending_head);
2748 static int __perf_pending_run(void)
2750 struct perf_pending_entry *list;
2753 list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
2754 while (list != PENDING_TAIL) {
2755 void (*func)(struct perf_pending_entry *);
2756 struct perf_pending_entry *entry = list;
2763 * Ensure we observe the unqueue before we issue the wakeup,
2764 * so that we won't be waiting forever.
2765 * -- see perf_not_pending().
2776 static inline int perf_not_pending(struct perf_event *event)
2779 * If we flush on whatever cpu we run, there is a chance we don't
2783 __perf_pending_run();
2787 * Ensure we see the proper queue state before going to sleep
2788 * so that we do not miss the wakeup. -- see perf_pending_handle()
2791 return event->pending.next == NULL;
2794 static void perf_pending_sync(struct perf_event *event)
2796 wait_event(event->waitq, perf_not_pending(event));
2799 void perf_event_do_pending(void)
2801 __perf_pending_run();
2805 * Callchain support -- arch specific
2808 __weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
2814 void perf_arch_fetch_caller_regs(struct pt_regs *regs, unsigned long ip, int skip)
2820 * We assume there is only KVM supporting the callbacks.
2821 * Later on, we might change it to a list if there is
2822 * another virtualization implementation supporting the callbacks.
2824 struct perf_guest_info_callbacks *perf_guest_cbs;
2826 int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
2828 perf_guest_cbs = cbs;
2831 EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
2833 int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
2835 perf_guest_cbs = NULL;
2838 EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
2843 static bool perf_output_space(struct perf_mmap_data *data, unsigned long tail,
2844 unsigned long offset, unsigned long head)
2848 if (!data->writable)
2851 mask = perf_data_size(data) - 1;
2853 offset = (offset - tail) & mask;
2854 head = (head - tail) & mask;
2856 if ((int)(head - offset) < 0)
2862 static void perf_output_wakeup(struct perf_output_handle *handle)
2864 atomic_set(&handle->data->poll, POLL_IN);
2867 handle->event->pending_wakeup = 1;
2868 perf_pending_queue(&handle->event->pending,
2869 perf_pending_event);
2871 perf_event_wakeup(handle->event);
2875 * Curious locking construct.
2877 * We need to ensure a later event_id doesn't publish a head when a former
2878 * event_id isn't done writing. However since we need to deal with NMIs we
2879 * cannot fully serialize things.
2881 * What we do is serialize between CPUs so we only have to deal with NMI
2882 * nesting on a single CPU.
2884 * We only publish the head (and generate a wakeup) when the outer-most
2885 * event_id completes.
2887 static void perf_output_lock(struct perf_output_handle *handle)
2889 struct perf_mmap_data *data = handle->data;
2890 int cur, cpu = get_cpu();
2895 cur = atomic_cmpxchg(&data->lock, -1, cpu);
2907 static void perf_output_unlock(struct perf_output_handle *handle)
2909 struct perf_mmap_data *data = handle->data;
2913 data->done_head = data->head;
2915 if (!handle->locked)
2920 * The xchg implies a full barrier that ensures all writes are done
2921 * before we publish the new head, matched by a rmb() in userspace when
2922 * reading this position.
2924 while ((head = atomic_long_xchg(&data->done_head, 0)))
2925 data->user_page->data_head = head;
2928 * NMI can happen here, which means we can miss a done_head update.
2931 cpu = atomic_xchg(&data->lock, -1);
2932 WARN_ON_ONCE(cpu != smp_processor_id());
2935 * Therefore we have to validate we did not indeed do so.
2937 if (unlikely(atomic_long_read(&data->done_head))) {
2939 * Since we had it locked, we can lock it again.
2941 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
2947 if (atomic_xchg(&data->wakeup, 0))
2948 perf_output_wakeup(handle);
2953 void perf_output_copy(struct perf_output_handle *handle,
2954 const void *buf, unsigned int len)
2956 unsigned int pages_mask;
2957 unsigned long offset;
2961 offset = handle->offset;
2962 pages_mask = handle->data->nr_pages - 1;
2963 pages = handle->data->data_pages;
2966 unsigned long page_offset;
2967 unsigned long page_size;
2970 nr = (offset >> PAGE_SHIFT) & pages_mask;
2971 page_size = 1UL << (handle->data->data_order + PAGE_SHIFT);
2972 page_offset = offset & (page_size - 1);
2973 size = min_t(unsigned int, page_size - page_offset, len);
2975 memcpy(pages[nr] + page_offset, buf, size);
2982 handle->offset = offset;
2985 * Check we didn't copy past our reservation window, taking the
2986 * possible unsigned int wrap into account.
2988 WARN_ON_ONCE(((long)(handle->head - handle->offset)) < 0);
2991 int perf_output_begin(struct perf_output_handle *handle,
2992 struct perf_event *event, unsigned int size,
2993 int nmi, int sample)
2995 struct perf_event *output_event;
2996 struct perf_mmap_data *data;
2997 unsigned long tail, offset, head;
3000 struct perf_event_header header;
3007 * For inherited events we send all the output towards the parent.
3010 event = event->parent;
3012 output_event = rcu_dereference(event->output);
3014 event = output_event;
3016 data = rcu_dereference(event->data);
3020 handle->data = data;
3021 handle->event = event;
3023 handle->sample = sample;
3025 if (!data->nr_pages)
3028 have_lost = atomic_read(&data->lost);
3030 size += sizeof(lost_event);
3032 perf_output_lock(handle);
3036 * Userspace could choose to issue a mb() before updating the
3037 * tail pointer. So that all reads will be completed before the
3040 tail = ACCESS_ONCE(data->user_page->data_tail);
3042 offset = head = atomic_long_read(&data->head);
3044 if (unlikely(!perf_output_space(data, tail, offset, head)))
3046 } while (atomic_long_cmpxchg(&data->head, offset, head) != offset);
3048 handle->offset = offset;
3049 handle->head = head;
3051 if (head - tail > data->watermark)
3052 atomic_set(&data->wakeup, 1);
3055 lost_event.header.type = PERF_RECORD_LOST;
3056 lost_event.header.misc = 0;
3057 lost_event.header.size = sizeof(lost_event);
3058 lost_event.id = event->id;
3059 lost_event.lost = atomic_xchg(&data->lost, 0);
3061 perf_output_put(handle, lost_event);
3067 atomic_inc(&data->lost);
3068 perf_output_unlock(handle);
3075 void perf_output_end(struct perf_output_handle *handle)
3077 struct perf_event *event = handle->event;
3078 struct perf_mmap_data *data = handle->data;
3080 int wakeup_events = event->attr.wakeup_events;
3082 if (handle->sample && wakeup_events) {
3083 int events = atomic_inc_return(&data->events);
3084 if (events >= wakeup_events) {
3085 atomic_sub(wakeup_events, &data->events);
3086 atomic_set(&data->wakeup, 1);
3090 perf_output_unlock(handle);
3094 static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
3097 * only top level events have the pid namespace they were created in
3100 event = event->parent;
3102 return task_tgid_nr_ns(p, event->ns);
3105 static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
3108 * only top level events have the pid namespace they were created in
3111 event = event->parent;
3113 return task_pid_nr_ns(p, event->ns);
3116 static void perf_output_read_one(struct perf_output_handle *handle,
3117 struct perf_event *event)
3119 u64 read_format = event->attr.read_format;
3123 values[n++] = atomic64_read(&event->count);
3124 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
3125 values[n++] = event->total_time_enabled +
3126 atomic64_read(&event->child_total_time_enabled);
3128 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
3129 values[n++] = event->total_time_running +
3130 atomic64_read(&event->child_total_time_running);
3132 if (read_format & PERF_FORMAT_ID)
3133 values[n++] = primary_event_id(event);
3135 perf_output_copy(handle, values, n * sizeof(u64));
3139 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
3141 static void perf_output_read_group(struct perf_output_handle *handle,
3142 struct perf_event *event)
3144 struct perf_event *leader = event->group_leader, *sub;
3145 u64 read_format = event->attr.read_format;
3149 values[n++] = 1 + leader->nr_siblings;
3151 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3152 values[n++] = leader->total_time_enabled;
3154 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3155 values[n++] = leader->total_time_running;
3157 if (leader != event)
3158 leader->pmu->read(leader);
3160 values[n++] = atomic64_read(&leader->count);
3161 if (read_format & PERF_FORMAT_ID)
3162 values[n++] = primary_event_id(leader);
3164 perf_output_copy(handle, values, n * sizeof(u64));
3166 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
3170 sub->pmu->read(sub);
3172 values[n++] = atomic64_read(&sub->count);
3173 if (read_format & PERF_FORMAT_ID)
3174 values[n++] = primary_event_id(sub);
3176 perf_output_copy(handle, values, n * sizeof(u64));
3180 static void perf_output_read(struct perf_output_handle *handle,
3181 struct perf_event *event)
3183 if (event->attr.read_format & PERF_FORMAT_GROUP)
3184 perf_output_read_group(handle, event);
3186 perf_output_read_one(handle, event);
3189 void perf_output_sample(struct perf_output_handle *handle,
3190 struct perf_event_header *header,
3191 struct perf_sample_data *data,
3192 struct perf_event *event)
3194 u64 sample_type = data->type;
3196 perf_output_put(handle, *header);
3198 if (sample_type & PERF_SAMPLE_IP)
3199 perf_output_put(handle, data->ip);
3201 if (sample_type & PERF_SAMPLE_TID)
3202 perf_output_put(handle, data->tid_entry);
3204 if (sample_type & PERF_SAMPLE_TIME)
3205 perf_output_put(handle, data->time);
3207 if (sample_type & PERF_SAMPLE_ADDR)
3208 perf_output_put(handle, data->addr);
3210 if (sample_type & PERF_SAMPLE_ID)
3211 perf_output_put(handle, data->id);
3213 if (sample_type & PERF_SAMPLE_STREAM_ID)
3214 perf_output_put(handle, data->stream_id);
3216 if (sample_type & PERF_SAMPLE_CPU)
3217 perf_output_put(handle, data->cpu_entry);
3219 if (sample_type & PERF_SAMPLE_PERIOD)
3220 perf_output_put(handle, data->period);
3222 if (sample_type & PERF_SAMPLE_READ)
3223 perf_output_read(handle, event);
3225 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3226 if (data->callchain) {
3229 if (data->callchain)
3230 size += data->callchain->nr;
3232 size *= sizeof(u64);
3234 perf_output_copy(handle, data->callchain, size);
3237 perf_output_put(handle, nr);
3241 if (sample_type & PERF_SAMPLE_RAW) {
3243 perf_output_put(handle, data->raw->size);
3244 perf_output_copy(handle, data->raw->data,
3251 .size = sizeof(u32),
3254 perf_output_put(handle, raw);
3259 void perf_prepare_sample(struct perf_event_header *header,
3260 struct perf_sample_data *data,
3261 struct perf_event *event,
3262 struct pt_regs *regs)
3264 u64 sample_type = event->attr.sample_type;
3266 data->type = sample_type;
3268 header->type = PERF_RECORD_SAMPLE;
3269 header->size = sizeof(*header);
3272 header->misc |= perf_misc_flags(regs);
3274 if (sample_type & PERF_SAMPLE_IP) {
3275 data->ip = perf_instruction_pointer(regs);
3277 header->size += sizeof(data->ip);
3280 if (sample_type & PERF_SAMPLE_TID) {
3281 /* namespace issues */
3282 data->tid_entry.pid = perf_event_pid(event, current);
3283 data->tid_entry.tid = perf_event_tid(event, current);
3285 header->size += sizeof(data->tid_entry);
3288 if (sample_type & PERF_SAMPLE_TIME) {
3289 data->time = perf_clock();
3291 header->size += sizeof(data->time);
3294 if (sample_type & PERF_SAMPLE_ADDR)
3295 header->size += sizeof(data->addr);
3297 if (sample_type & PERF_SAMPLE_ID) {
3298 data->id = primary_event_id(event);
3300 header->size += sizeof(data->id);
3303 if (sample_type & PERF_SAMPLE_STREAM_ID) {
3304 data->stream_id = event->id;
3306 header->size += sizeof(data->stream_id);
3309 if (sample_type & PERF_SAMPLE_CPU) {
3310 data->cpu_entry.cpu = raw_smp_processor_id();
3311 data->cpu_entry.reserved = 0;
3313 header->size += sizeof(data->cpu_entry);
3316 if (sample_type & PERF_SAMPLE_PERIOD)
3317 header->size += sizeof(data->period);
3319 if (sample_type & PERF_SAMPLE_READ)
3320 header->size += perf_event_read_size(event);
3322 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3325 data->callchain = perf_callchain(regs);
3327 if (data->callchain)
3328 size += data->callchain->nr;
3330 header->size += size * sizeof(u64);
3333 if (sample_type & PERF_SAMPLE_RAW) {
3334 int size = sizeof(u32);
3337 size += data->raw->size;
3339 size += sizeof(u32);
3341 WARN_ON_ONCE(size & (sizeof(u64)-1));
3342 header->size += size;
3346 static void perf_event_output(struct perf_event *event, int nmi,
3347 struct perf_sample_data *data,
3348 struct pt_regs *regs)
3350 struct perf_output_handle handle;
3351 struct perf_event_header header;
3353 perf_prepare_sample(&header, data, event, regs);
3355 if (perf_output_begin(&handle, event, header.size, nmi, 1))
3358 perf_output_sample(&handle, &header, data, event);
3360 perf_output_end(&handle);
3367 struct perf_read_event {
3368 struct perf_event_header header;
3375 perf_event_read_event(struct perf_event *event,
3376 struct task_struct *task)
3378 struct perf_output_handle handle;
3379 struct perf_read_event read_event = {
3381 .type = PERF_RECORD_READ,
3383 .size = sizeof(read_event) + perf_event_read_size(event),
3385 .pid = perf_event_pid(event, task),
3386 .tid = perf_event_tid(event, task),
3390 ret = perf_output_begin(&handle, event, read_event.header.size, 0, 0);
3394 perf_output_put(&handle, read_event);
3395 perf_output_read(&handle, event);
3397 perf_output_end(&handle);
3401 * task tracking -- fork/exit
3403 * enabled by: attr.comm | attr.mmap | attr.task
3406 struct perf_task_event {
3407 struct task_struct *task;
3408 struct perf_event_context *task_ctx;
3411 struct perf_event_header header;
3421 static void perf_event_task_output(struct perf_event *event,
3422 struct perf_task_event *task_event)
3424 struct perf_output_handle handle;
3425 struct task_struct *task = task_event->task;
3426 unsigned long flags;
3430 * If this CPU attempts to acquire an rq lock held by a CPU spinning
3431 * in perf_output_lock() from interrupt context, it's game over.
3433 local_irq_save(flags);
3435 size = task_event->event_id.header.size;
3436 ret = perf_output_begin(&handle, event, size, 0, 0);
3439 local_irq_restore(flags);
3443 task_event->event_id.pid = perf_event_pid(event, task);
3444 task_event->event_id.ppid = perf_event_pid(event, current);
3446 task_event->event_id.tid = perf_event_tid(event, task);
3447 task_event->event_id.ptid = perf_event_tid(event, current);
3449 perf_output_put(&handle, task_event->event_id);
3451 perf_output_end(&handle);
3452 local_irq_restore(flags);
3455 static int perf_event_task_match(struct perf_event *event)
3457 if (event->state < PERF_EVENT_STATE_INACTIVE)
3460 if (event->cpu != -1 && event->cpu != smp_processor_id())
3463 if (event->attr.comm || event->attr.mmap || event->attr.task)
3469 static void perf_event_task_ctx(struct perf_event_context *ctx,
3470 struct perf_task_event *task_event)
3472 struct perf_event *event;
3474 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3475 if (perf_event_task_match(event))
3476 perf_event_task_output(event, task_event);
3480 static void perf_event_task_event(struct perf_task_event *task_event)
3482 struct perf_cpu_context *cpuctx;
3483 struct perf_event_context *ctx = task_event->task_ctx;
3486 cpuctx = &get_cpu_var(perf_cpu_context);
3487 perf_event_task_ctx(&cpuctx->ctx, task_event);
3489 ctx = rcu_dereference(current->perf_event_ctxp);
3491 perf_event_task_ctx(ctx, task_event);
3492 put_cpu_var(perf_cpu_context);
3496 static void perf_event_task(struct task_struct *task,
3497 struct perf_event_context *task_ctx,
3500 struct perf_task_event task_event;
3502 if (!atomic_read(&nr_comm_events) &&
3503 !atomic_read(&nr_mmap_events) &&
3504 !atomic_read(&nr_task_events))
3507 task_event = (struct perf_task_event){
3509 .task_ctx = task_ctx,
3512 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
3514 .size = sizeof(task_event.event_id),
3520 .time = perf_clock(),
3524 perf_event_task_event(&task_event);
3527 void perf_event_fork(struct task_struct *task)
3529 perf_event_task(task, NULL, 1);
3536 struct perf_comm_event {
3537 struct task_struct *task;
3542 struct perf_event_header header;
3549 static void perf_event_comm_output(struct perf_event *event,
3550 struct perf_comm_event *comm_event)
3552 struct perf_output_handle handle;
3553 int size = comm_event->event_id.header.size;
3554 int ret = perf_output_begin(&handle, event, size, 0, 0);
3559 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
3560 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
3562 perf_output_put(&handle, comm_event->event_id);
3563 perf_output_copy(&handle, comm_event->comm,
3564 comm_event->comm_size);
3565 perf_output_end(&handle);
3568 static int perf_event_comm_match(struct perf_event *event)
3570 if (event->state < PERF_EVENT_STATE_INACTIVE)
3573 if (event->cpu != -1 && event->cpu != smp_processor_id())
3576 if (event->attr.comm)
3582 static void perf_event_comm_ctx(struct perf_event_context *ctx,
3583 struct perf_comm_event *comm_event)
3585 struct perf_event *event;
3587 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3588 if (perf_event_comm_match(event))
3589 perf_event_comm_output(event, comm_event);
3593 static void perf_event_comm_event(struct perf_comm_event *comm_event)
3595 struct perf_cpu_context *cpuctx;
3596 struct perf_event_context *ctx;
3598 char comm[TASK_COMM_LEN];
3600 memset(comm, 0, sizeof(comm));
3601 strlcpy(comm, comm_event->task->comm, sizeof(comm));
3602 size = ALIGN(strlen(comm)+1, sizeof(u64));
3604 comm_event->comm = comm;
3605 comm_event->comm_size = size;
3607 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
3610 cpuctx = &get_cpu_var(perf_cpu_context);
3611 perf_event_comm_ctx(&cpuctx->ctx, comm_event);
3612 ctx = rcu_dereference(current->perf_event_ctxp);
3614 perf_event_comm_ctx(ctx, comm_event);
3615 put_cpu_var(perf_cpu_context);
3619 void perf_event_comm(struct task_struct *task)
3621 struct perf_comm_event comm_event;
3623 if (task->perf_event_ctxp)
3624 perf_event_enable_on_exec(task);
3626 if (!atomic_read(&nr_comm_events))
3629 comm_event = (struct perf_comm_event){
3635 .type = PERF_RECORD_COMM,
3644 perf_event_comm_event(&comm_event);
3651 struct perf_mmap_event {
3652 struct vm_area_struct *vma;
3654 const char *file_name;
3658 struct perf_event_header header;
3668 static void perf_event_mmap_output(struct perf_event *event,
3669 struct perf_mmap_event *mmap_event)
3671 struct perf_output_handle handle;
3672 int size = mmap_event->event_id.header.size;
3673 int ret = perf_output_begin(&handle, event, size, 0, 0);
3678 mmap_event->event_id.pid = perf_event_pid(event, current);
3679 mmap_event->event_id.tid = perf_event_tid(event, current);
3681 perf_output_put(&handle, mmap_event->event_id);
3682 perf_output_copy(&handle, mmap_event->file_name,
3683 mmap_event->file_size);
3684 perf_output_end(&handle);
3687 static int perf_event_mmap_match(struct perf_event *event,
3688 struct perf_mmap_event *mmap_event)
3690 if (event->state < PERF_EVENT_STATE_INACTIVE)
3693 if (event->cpu != -1 && event->cpu != smp_processor_id())
3696 if (event->attr.mmap)
3702 static void perf_event_mmap_ctx(struct perf_event_context *ctx,
3703 struct perf_mmap_event *mmap_event)
3705 struct perf_event *event;
3707 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3708 if (perf_event_mmap_match(event, mmap_event))
3709 perf_event_mmap_output(event, mmap_event);
3713 static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
3715 struct perf_cpu_context *cpuctx;
3716 struct perf_event_context *ctx;
3717 struct vm_area_struct *vma = mmap_event->vma;
3718 struct file *file = vma->vm_file;
3724 memset(tmp, 0, sizeof(tmp));
3728 * d_path works from the end of the buffer backwards, so we
3729 * need to add enough zero bytes after the string to handle
3730 * the 64bit alignment we do later.
3732 buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
3734 name = strncpy(tmp, "//enomem", sizeof(tmp));
3737 name = d_path(&file->f_path, buf, PATH_MAX);
3739 name = strncpy(tmp, "//toolong", sizeof(tmp));
3743 if (arch_vma_name(mmap_event->vma)) {
3744 name = strncpy(tmp, arch_vma_name(mmap_event->vma),
3750 name = strncpy(tmp, "[vdso]", sizeof(tmp));
3754 name = strncpy(tmp, "//anon", sizeof(tmp));
3759 size = ALIGN(strlen(name)+1, sizeof(u64));
3761 mmap_event->file_name = name;
3762 mmap_event->file_size = size;
3764 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
3767 cpuctx = &get_cpu_var(perf_cpu_context);
3768 perf_event_mmap_ctx(&cpuctx->ctx, mmap_event);
3769 ctx = rcu_dereference(current->perf_event_ctxp);
3771 perf_event_mmap_ctx(ctx, mmap_event);
3772 put_cpu_var(perf_cpu_context);
3778 void __perf_event_mmap(struct vm_area_struct *vma)
3780 struct perf_mmap_event mmap_event;
3782 if (!atomic_read(&nr_mmap_events))
3785 mmap_event = (struct perf_mmap_event){
3791 .type = PERF_RECORD_MMAP,
3792 .misc = PERF_RECORD_MISC_USER,
3797 .start = vma->vm_start,
3798 .len = vma->vm_end - vma->vm_start,
3799 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
3803 perf_event_mmap_event(&mmap_event);
3807 * IRQ throttle logging
3810 static void perf_log_throttle(struct perf_event *event, int enable)
3812 struct perf_output_handle handle;
3816 struct perf_event_header header;
3820 } throttle_event = {
3822 .type = PERF_RECORD_THROTTLE,
3824 .size = sizeof(throttle_event),
3826 .time = perf_clock(),
3827 .id = primary_event_id(event),
3828 .stream_id = event->id,
3832 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
3834 ret = perf_output_begin(&handle, event, sizeof(throttle_event), 1, 0);
3838 perf_output_put(&handle, throttle_event);
3839 perf_output_end(&handle);
3843 * Generic event overflow handling, sampling.
3846 static int __perf_event_overflow(struct perf_event *event, int nmi,
3847 int throttle, struct perf_sample_data *data,
3848 struct pt_regs *regs)
3850 int events = atomic_read(&event->event_limit);
3851 struct hw_perf_event *hwc = &event->hw;
3854 throttle = (throttle && event->pmu->unthrottle != NULL);
3859 if (hwc->interrupts != MAX_INTERRUPTS) {
3861 if (HZ * hwc->interrupts >
3862 (u64)sysctl_perf_event_sample_rate) {
3863 hwc->interrupts = MAX_INTERRUPTS;
3864 perf_log_throttle(event, 0);
3869 * Keep re-disabling events even though on the previous
3870 * pass we disabled it - just in case we raced with a
3871 * sched-in and the event got enabled again:
3877 if (event->attr.freq) {
3878 u64 now = perf_clock();
3879 s64 delta = now - hwc->freq_time_stamp;
3881 hwc->freq_time_stamp = now;
3883 if (delta > 0 && delta < 2*TICK_NSEC)
3884 perf_adjust_period(event, delta, hwc->last_period);
3888 * XXX event_limit might not quite work as expected on inherited
3892 event->pending_kill = POLL_IN;
3893 if (events && atomic_dec_and_test(&event->event_limit)) {
3895 event->pending_kill = POLL_HUP;
3897 event->pending_disable = 1;
3898 perf_pending_queue(&event->pending,
3899 perf_pending_event);
3901 perf_event_disable(event);
3904 if (event->overflow_handler)
3905 event->overflow_handler(event, nmi, data, regs);
3907 perf_event_output(event, nmi, data, regs);
3912 int perf_event_overflow(struct perf_event *event, int nmi,
3913 struct perf_sample_data *data,
3914 struct pt_regs *regs)
3916 return __perf_event_overflow(event, nmi, 1, data, regs);
3920 * Generic software event infrastructure
3924 * We directly increment event->count and keep a second value in
3925 * event->hw.period_left to count intervals. This period event
3926 * is kept in the range [-sample_period, 0] so that we can use the
3930 static u64 perf_swevent_set_period(struct perf_event *event)
3932 struct hw_perf_event *hwc = &event->hw;
3933 u64 period = hwc->last_period;
3937 hwc->last_period = hwc->sample_period;
3940 old = val = atomic64_read(&hwc->period_left);
3944 nr = div64_u64(period + val, period);
3945 offset = nr * period;
3947 if (atomic64_cmpxchg(&hwc->period_left, old, val) != old)
3953 static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
3954 int nmi, struct perf_sample_data *data,
3955 struct pt_regs *regs)
3957 struct hw_perf_event *hwc = &event->hw;
3960 data->period = event->hw.last_period;
3962 overflow = perf_swevent_set_period(event);
3964 if (hwc->interrupts == MAX_INTERRUPTS)
3967 for (; overflow; overflow--) {
3968 if (__perf_event_overflow(event, nmi, throttle,
3971 * We inhibit the overflow from happening when
3972 * hwc->interrupts == MAX_INTERRUPTS.
3980 static void perf_swevent_unthrottle(struct perf_event *event)
3983 * Nothing to do, we already reset hwc->interrupts.
3987 static void perf_swevent_add(struct perf_event *event, u64 nr,
3988 int nmi, struct perf_sample_data *data,
3989 struct pt_regs *regs)
3991 struct hw_perf_event *hwc = &event->hw;
3993 atomic64_add(nr, &event->count);
3998 if (!hwc->sample_period)
4001 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
4002 return perf_swevent_overflow(event, 1, nmi, data, regs);
4004 if (atomic64_add_negative(nr, &hwc->period_left))
4007 perf_swevent_overflow(event, 0, nmi, data, regs);
4010 static int perf_tp_event_match(struct perf_event *event,
4011 struct perf_sample_data *data);
4013 static int perf_exclude_event(struct perf_event *event,
4014 struct pt_regs *regs)
4017 if (event->attr.exclude_user && user_mode(regs))
4020 if (event->attr.exclude_kernel && !user_mode(regs))
4027 static int perf_swevent_match(struct perf_event *event,
4028 enum perf_type_id type,
4030 struct perf_sample_data *data,
4031 struct pt_regs *regs)
4033 if (event->attr.type != type)
4036 if (event->attr.config != event_id)
4039 if (perf_exclude_event(event, regs))
4042 if (event->attr.type == PERF_TYPE_TRACEPOINT &&
4043 !perf_tp_event_match(event, data))
4049 static inline u64 swevent_hash(u64 type, u32 event_id)
4051 u64 val = event_id | (type << 32);
4053 return hash_64(val, SWEVENT_HLIST_BITS);
4056 static struct hlist_head *
4057 find_swevent_head(struct perf_cpu_context *ctx, u64 type, u32 event_id)
4060 struct swevent_hlist *hlist;
4062 hash = swevent_hash(type, event_id);
4064 hlist = rcu_dereference(ctx->swevent_hlist);
4068 return &hlist->heads[hash];
4071 static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
4073 struct perf_sample_data *data,
4074 struct pt_regs *regs)
4076 struct perf_cpu_context *cpuctx;
4077 struct perf_event *event;
4078 struct hlist_node *node;
4079 struct hlist_head *head;
4081 cpuctx = &__get_cpu_var(perf_cpu_context);
4085 head = find_swevent_head(cpuctx, type, event_id);
4090 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
4091 if (perf_swevent_match(event, type, event_id, data, regs))
4092 perf_swevent_add(event, nr, nmi, data, regs);
4098 int perf_swevent_get_recursion_context(void)
4100 struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
4107 else if (in_softirq())
4112 if (cpuctx->recursion[rctx]) {
4113 put_cpu_var(perf_cpu_context);
4117 cpuctx->recursion[rctx]++;
4122 EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
4124 void perf_swevent_put_recursion_context(int rctx)
4126 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
4128 cpuctx->recursion[rctx]--;
4129 put_cpu_var(perf_cpu_context);
4131 EXPORT_SYMBOL_GPL(perf_swevent_put_recursion_context);
4134 void __perf_sw_event(u32 event_id, u64 nr, int nmi,
4135 struct pt_regs *regs, u64 addr)
4137 struct perf_sample_data data;
4140 rctx = perf_swevent_get_recursion_context();
4144 perf_sample_data_init(&data, addr);
4146 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, nmi, &data, regs);
4148 perf_swevent_put_recursion_context(rctx);
4151 static void perf_swevent_read(struct perf_event *event)
4155 static int perf_swevent_enable(struct perf_event *event)
4157 struct hw_perf_event *hwc = &event->hw;
4158 struct perf_cpu_context *cpuctx;
4159 struct hlist_head *head;
4161 cpuctx = &__get_cpu_var(perf_cpu_context);
4163 if (hwc->sample_period) {
4164 hwc->last_period = hwc->sample_period;
4165 perf_swevent_set_period(event);
4168 head = find_swevent_head(cpuctx, event->attr.type, event->attr.config);
4169 if (WARN_ON_ONCE(!head))
4172 hlist_add_head_rcu(&event->hlist_entry, head);
4177 static void perf_swevent_disable(struct perf_event *event)
4179 hlist_del_rcu(&event->hlist_entry);
4182 static const struct pmu perf_ops_generic = {
4183 .enable = perf_swevent_enable,
4184 .disable = perf_swevent_disable,
4185 .read = perf_swevent_read,
4186 .unthrottle = perf_swevent_unthrottle,
4190 * hrtimer based swevent callback
4193 static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
4195 enum hrtimer_restart ret = HRTIMER_RESTART;
4196 struct perf_sample_data data;
4197 struct pt_regs *regs;
4198 struct perf_event *event;
4201 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
4202 event->pmu->read(event);
4204 perf_sample_data_init(&data, 0);
4205 data.period = event->hw.last_period;
4206 regs = get_irq_regs();
4208 if (regs && !perf_exclude_event(event, regs)) {
4209 if (!(event->attr.exclude_idle && current->pid == 0))
4210 if (perf_event_overflow(event, 0, &data, regs))
4211 ret = HRTIMER_NORESTART;
4214 period = max_t(u64, 10000, event->hw.sample_period);
4215 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
4220 static void perf_swevent_start_hrtimer(struct perf_event *event)
4222 struct hw_perf_event *hwc = &event->hw;
4224 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
4225 hwc->hrtimer.function = perf_swevent_hrtimer;
4226 if (hwc->sample_period) {
4229 if (hwc->remaining) {
4230 if (hwc->remaining < 0)
4233 period = hwc->remaining;
4236 period = max_t(u64, 10000, hwc->sample_period);
4238 __hrtimer_start_range_ns(&hwc->hrtimer,
4239 ns_to_ktime(period), 0,
4240 HRTIMER_MODE_REL, 0);
4244 static void perf_swevent_cancel_hrtimer(struct perf_event *event)
4246 struct hw_perf_event *hwc = &event->hw;
4248 if (hwc->sample_period) {
4249 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
4250 hwc->remaining = ktime_to_ns(remaining);
4252 hrtimer_cancel(&hwc->hrtimer);
4257 * Software event: cpu wall time clock
4260 static void cpu_clock_perf_event_update(struct perf_event *event)
4262 int cpu = raw_smp_processor_id();
4266 now = cpu_clock(cpu);
4267 prev = atomic64_xchg(&event->hw.prev_count, now);
4268 atomic64_add(now - prev, &event->count);
4271 static int cpu_clock_perf_event_enable(struct perf_event *event)
4273 struct hw_perf_event *hwc = &event->hw;
4274 int cpu = raw_smp_processor_id();
4276 atomic64_set(&hwc->prev_count, cpu_clock(cpu));
4277 perf_swevent_start_hrtimer(event);
4282 static void cpu_clock_perf_event_disable(struct perf_event *event)
4284 perf_swevent_cancel_hrtimer(event);
4285 cpu_clock_perf_event_update(event);
4288 static void cpu_clock_perf_event_read(struct perf_event *event)
4290 cpu_clock_perf_event_update(event);
4293 static const struct pmu perf_ops_cpu_clock = {
4294 .enable = cpu_clock_perf_event_enable,
4295 .disable = cpu_clock_perf_event_disable,
4296 .read = cpu_clock_perf_event_read,
4300 * Software event: task time clock
4303 static void task_clock_perf_event_update(struct perf_event *event, u64 now)
4308 prev = atomic64_xchg(&event->hw.prev_count, now);
4310 atomic64_add(delta, &event->count);
4313 static int task_clock_perf_event_enable(struct perf_event *event)
4315 struct hw_perf_event *hwc = &event->hw;
4318 now = event->ctx->time;
4320 atomic64_set(&hwc->prev_count, now);
4322 perf_swevent_start_hrtimer(event);
4327 static void task_clock_perf_event_disable(struct perf_event *event)
4329 perf_swevent_cancel_hrtimer(event);
4330 task_clock_perf_event_update(event, event->ctx->time);
4334 static void task_clock_perf_event_read(struct perf_event *event)
4339 update_context_time(event->ctx);
4340 time = event->ctx->time;
4342 u64 now = perf_clock();
4343 u64 delta = now - event->ctx->timestamp;
4344 time = event->ctx->time + delta;
4347 task_clock_perf_event_update(event, time);
4350 static const struct pmu perf_ops_task_clock = {
4351 .enable = task_clock_perf_event_enable,
4352 .disable = task_clock_perf_event_disable,
4353 .read = task_clock_perf_event_read,
4356 static void swevent_hlist_release_rcu(struct rcu_head *rcu_head)
4358 struct swevent_hlist *hlist;
4360 hlist = container_of(rcu_head, struct swevent_hlist, rcu_head);
4364 static void swevent_hlist_release(struct perf_cpu_context *cpuctx)
4366 struct swevent_hlist *hlist;
4368 if (!cpuctx->swevent_hlist)
4371 hlist = cpuctx->swevent_hlist;
4372 rcu_assign_pointer(cpuctx->swevent_hlist, NULL);
4373 call_rcu(&hlist->rcu_head, swevent_hlist_release_rcu);
4376 static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
4378 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
4380 mutex_lock(&cpuctx->hlist_mutex);
4382 if (!--cpuctx->hlist_refcount)
4383 swevent_hlist_release(cpuctx);
4385 mutex_unlock(&cpuctx->hlist_mutex);
4388 static void swevent_hlist_put(struct perf_event *event)
4392 if (event->cpu != -1) {
4393 swevent_hlist_put_cpu(event, event->cpu);
4397 for_each_possible_cpu(cpu)
4398 swevent_hlist_put_cpu(event, cpu);
4401 static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
4403 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
4406 mutex_lock(&cpuctx->hlist_mutex);
4408 if (!cpuctx->swevent_hlist && cpu_online(cpu)) {
4409 struct swevent_hlist *hlist;
4411 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
4416 rcu_assign_pointer(cpuctx->swevent_hlist, hlist);
4418 cpuctx->hlist_refcount++;
4420 mutex_unlock(&cpuctx->hlist_mutex);
4425 static int swevent_hlist_get(struct perf_event *event)
4428 int cpu, failed_cpu;
4430 if (event->cpu != -1)
4431 return swevent_hlist_get_cpu(event, event->cpu);
4434 for_each_possible_cpu(cpu) {
4435 err = swevent_hlist_get_cpu(event, cpu);
4445 for_each_possible_cpu(cpu) {
4446 if (cpu == failed_cpu)
4448 swevent_hlist_put_cpu(event, cpu);
4455 #ifdef CONFIG_EVENT_TRACING
4457 void perf_tp_event(int event_id, u64 addr, u64 count, void *record,
4458 int entry_size, struct pt_regs *regs)
4460 struct perf_sample_data data;
4461 struct perf_raw_record raw = {
4466 perf_sample_data_init(&data, addr);
4469 /* Trace events already protected against recursion */
4470 do_perf_sw_event(PERF_TYPE_TRACEPOINT, event_id, count, 1,
4473 EXPORT_SYMBOL_GPL(perf_tp_event);
4475 static int perf_tp_event_match(struct perf_event *event,
4476 struct perf_sample_data *data)
4478 void *record = data->raw->data;
4480 if (likely(!event->filter) || filter_match_preds(event->filter, record))
4485 static void tp_perf_event_destroy(struct perf_event *event)
4487 perf_trace_disable(event->attr.config);
4488 swevent_hlist_put(event);
4491 static const struct pmu *tp_perf_event_init(struct perf_event *event)
4496 * Raw tracepoint data is a severe data leak, only allow root to
4499 if ((event->attr.sample_type & PERF_SAMPLE_RAW) &&
4500 perf_paranoid_tracepoint_raw() &&
4501 !capable(CAP_SYS_ADMIN))
4502 return ERR_PTR(-EPERM);
4504 if (perf_trace_enable(event->attr.config))
4507 event->destroy = tp_perf_event_destroy;
4508 err = swevent_hlist_get(event);
4510 perf_trace_disable(event->attr.config);
4511 return ERR_PTR(err);
4514 return &perf_ops_generic;
4517 static int perf_event_set_filter(struct perf_event *event, void __user *arg)
4522 if (event->attr.type != PERF_TYPE_TRACEPOINT)
4525 filter_str = strndup_user(arg, PAGE_SIZE);
4526 if (IS_ERR(filter_str))
4527 return PTR_ERR(filter_str);
4529 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
4535 static void perf_event_free_filter(struct perf_event *event)
4537 ftrace_profile_free_filter(event);
4542 static int perf_tp_event_match(struct perf_event *event,
4543 struct perf_sample_data *data)
4548 static const struct pmu *tp_perf_event_init(struct perf_event *event)
4553 static int perf_event_set_filter(struct perf_event *event, void __user *arg)
4558 static void perf_event_free_filter(struct perf_event *event)
4562 #endif /* CONFIG_EVENT_TRACING */
4564 #ifdef CONFIG_HAVE_HW_BREAKPOINT
4565 static void bp_perf_event_destroy(struct perf_event *event)
4567 release_bp_slot(event);
4570 static const struct pmu *bp_perf_event_init(struct perf_event *bp)
4574 err = register_perf_hw_breakpoint(bp);
4576 return ERR_PTR(err);
4578 bp->destroy = bp_perf_event_destroy;
4580 return &perf_ops_bp;
4583 void perf_bp_event(struct perf_event *bp, void *data)
4585 struct perf_sample_data sample;
4586 struct pt_regs *regs = data;
4588 perf_sample_data_init(&sample, bp->attr.bp_addr);
4590 if (!perf_exclude_event(bp, regs))
4591 perf_swevent_add(bp, 1, 1, &sample, regs);
4594 static const struct pmu *bp_perf_event_init(struct perf_event *bp)
4599 void perf_bp_event(struct perf_event *bp, void *regs)
4604 atomic_t perf_swevent_enabled[PERF_COUNT_SW_MAX];
4606 static void sw_perf_event_destroy(struct perf_event *event)
4608 u64 event_id = event->attr.config;
4610 WARN_ON(event->parent);
4612 atomic_dec(&perf_swevent_enabled[event_id]);
4613 swevent_hlist_put(event);
4616 static const struct pmu *sw_perf_event_init(struct perf_event *event)
4618 const struct pmu *pmu = NULL;
4619 u64 event_id = event->attr.config;
4622 * Software events (currently) can't in general distinguish
4623 * between user, kernel and hypervisor events.
4624 * However, context switches and cpu migrations are considered
4625 * to be kernel events, and page faults are never hypervisor
4629 case PERF_COUNT_SW_CPU_CLOCK:
4630 pmu = &perf_ops_cpu_clock;
4633 case PERF_COUNT_SW_TASK_CLOCK:
4635 * If the user instantiates this as a per-cpu event,
4636 * use the cpu_clock event instead.
4638 if (event->ctx->task)
4639 pmu = &perf_ops_task_clock;
4641 pmu = &perf_ops_cpu_clock;
4644 case PERF_COUNT_SW_PAGE_FAULTS:
4645 case PERF_COUNT_SW_PAGE_FAULTS_MIN:
4646 case PERF_COUNT_SW_PAGE_FAULTS_MAJ:
4647 case PERF_COUNT_SW_CONTEXT_SWITCHES:
4648 case PERF_COUNT_SW_CPU_MIGRATIONS:
4649 case PERF_COUNT_SW_ALIGNMENT_FAULTS:
4650 case PERF_COUNT_SW_EMULATION_FAULTS:
4651 if (!event->parent) {
4654 err = swevent_hlist_get(event);
4656 return ERR_PTR(err);
4658 atomic_inc(&perf_swevent_enabled[event_id]);
4659 event->destroy = sw_perf_event_destroy;
4661 pmu = &perf_ops_generic;
4669 * Allocate and initialize a event structure
4671 static struct perf_event *
4672 perf_event_alloc(struct perf_event_attr *attr,
4674 struct perf_event_context *ctx,
4675 struct perf_event *group_leader,
4676 struct perf_event *parent_event,
4677 perf_overflow_handler_t overflow_handler,
4680 const struct pmu *pmu;
4681 struct perf_event *event;
4682 struct hw_perf_event *hwc;
4685 event = kzalloc(sizeof(*event), gfpflags);
4687 return ERR_PTR(-ENOMEM);
4690 * Single events are their own group leaders, with an
4691 * empty sibling list:
4694 group_leader = event;
4696 mutex_init(&event->child_mutex);
4697 INIT_LIST_HEAD(&event->child_list);
4699 INIT_LIST_HEAD(&event->group_entry);
4700 INIT_LIST_HEAD(&event->event_entry);
4701 INIT_LIST_HEAD(&event->sibling_list);
4702 init_waitqueue_head(&event->waitq);
4704 mutex_init(&event->mmap_mutex);
4707 event->attr = *attr;
4708 event->group_leader = group_leader;
4713 event->parent = parent_event;
4715 event->ns = get_pid_ns(current->nsproxy->pid_ns);
4716 event->id = atomic64_inc_return(&perf_event_id);
4718 event->state = PERF_EVENT_STATE_INACTIVE;
4720 if (!overflow_handler && parent_event)
4721 overflow_handler = parent_event->overflow_handler;
4723 event->overflow_handler = overflow_handler;
4726 event->state = PERF_EVENT_STATE_OFF;
4731 hwc->sample_period = attr->sample_period;
4732 if (attr->freq && attr->sample_freq)
4733 hwc->sample_period = 1;
4734 hwc->last_period = hwc->sample_period;
4736 atomic64_set(&hwc->period_left, hwc->sample_period);
4739 * we currently do not support PERF_FORMAT_GROUP on inherited events
4741 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
4744 switch (attr->type) {
4746 case PERF_TYPE_HARDWARE:
4747 case PERF_TYPE_HW_CACHE:
4748 pmu = hw_perf_event_init(event);
4751 case PERF_TYPE_SOFTWARE:
4752 pmu = sw_perf_event_init(event);
4755 case PERF_TYPE_TRACEPOINT:
4756 pmu = tp_perf_event_init(event);
4759 case PERF_TYPE_BREAKPOINT:
4760 pmu = bp_perf_event_init(event);
4771 else if (IS_ERR(pmu))
4776 put_pid_ns(event->ns);
4778 return ERR_PTR(err);
4783 if (!event->parent) {
4784 atomic_inc(&nr_events);
4785 if (event->attr.mmap)
4786 atomic_inc(&nr_mmap_events);
4787 if (event->attr.comm)
4788 atomic_inc(&nr_comm_events);
4789 if (event->attr.task)
4790 atomic_inc(&nr_task_events);
4796 static int perf_copy_attr(struct perf_event_attr __user *uattr,
4797 struct perf_event_attr *attr)
4802 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
4806 * zero the full structure, so that a short copy will be nice.
4808 memset(attr, 0, sizeof(*attr));
4810 ret = get_user(size, &uattr->size);
4814 if (size > PAGE_SIZE) /* silly large */
4817 if (!size) /* abi compat */
4818 size = PERF_ATTR_SIZE_VER0;
4820 if (size < PERF_ATTR_SIZE_VER0)
4824 * If we're handed a bigger struct than we know of,
4825 * ensure all the unknown bits are 0 - i.e. new
4826 * user-space does not rely on any kernel feature
4827 * extensions we dont know about yet.
4829 if (size > sizeof(*attr)) {
4830 unsigned char __user *addr;
4831 unsigned char __user *end;
4834 addr = (void __user *)uattr + sizeof(*attr);
4835 end = (void __user *)uattr + size;
4837 for (; addr < end; addr++) {
4838 ret = get_user(val, addr);
4844 size = sizeof(*attr);
4847 ret = copy_from_user(attr, uattr, size);
4852 * If the type exists, the corresponding creation will verify
4855 if (attr->type >= PERF_TYPE_MAX)
4858 if (attr->__reserved_1)
4861 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
4864 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
4871 put_user(sizeof(*attr), &uattr->size);
4876 static int perf_event_set_output(struct perf_event *event, int output_fd)
4878 struct perf_event *output_event = NULL;
4879 struct file *output_file = NULL;
4880 struct perf_event *old_output;
4881 int fput_needed = 0;
4887 output_file = fget_light(output_fd, &fput_needed);
4891 if (output_file->f_op != &perf_fops)
4894 output_event = output_file->private_data;
4896 /* Don't chain output fds */
4897 if (output_event->output)
4900 /* Don't set an output fd when we already have an output channel */
4904 atomic_long_inc(&output_file->f_count);
4907 mutex_lock(&event->mmap_mutex);
4908 old_output = event->output;
4909 rcu_assign_pointer(event->output, output_event);
4910 mutex_unlock(&event->mmap_mutex);
4914 * we need to make sure no existing perf_output_*()
4915 * is still referencing this event.
4918 fput(old_output->filp);
4923 fput_light(output_file, fput_needed);
4928 * sys_perf_event_open - open a performance event, associate it to a task/cpu
4930 * @attr_uptr: event_id type attributes for monitoring/sampling
4933 * @group_fd: group leader event fd
4935 SYSCALL_DEFINE5(perf_event_open,
4936 struct perf_event_attr __user *, attr_uptr,
4937 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
4939 struct perf_event *event, *group_leader;
4940 struct perf_event_attr attr;
4941 struct perf_event_context *ctx;
4942 struct file *event_file = NULL;
4943 struct file *group_file = NULL;
4944 int fput_needed = 0;
4945 int fput_needed2 = 0;
4948 /* for future expandability... */
4949 if (flags & ~(PERF_FLAG_FD_NO_GROUP | PERF_FLAG_FD_OUTPUT))
4952 err = perf_copy_attr(attr_uptr, &attr);
4956 if (!attr.exclude_kernel) {
4957 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
4962 if (attr.sample_freq > sysctl_perf_event_sample_rate)
4967 * Get the target context (task or percpu):
4969 ctx = find_get_context(pid, cpu);
4971 return PTR_ERR(ctx);
4974 * Look up the group leader (we will attach this event to it):
4976 group_leader = NULL;
4977 if (group_fd != -1 && !(flags & PERF_FLAG_FD_NO_GROUP)) {
4979 group_file = fget_light(group_fd, &fput_needed);
4981 goto err_put_context;
4982 if (group_file->f_op != &perf_fops)
4983 goto err_put_context;
4985 group_leader = group_file->private_data;
4987 * Do not allow a recursive hierarchy (this new sibling
4988 * becoming part of another group-sibling):
4990 if (group_leader->group_leader != group_leader)
4991 goto err_put_context;
4993 * Do not allow to attach to a group in a different
4994 * task or CPU context:
4996 if (group_leader->ctx != ctx)
4997 goto err_put_context;
4999 * Only a group leader can be exclusive or pinned
5001 if (attr.exclusive || attr.pinned)
5002 goto err_put_context;
5005 event = perf_event_alloc(&attr, cpu, ctx, group_leader,
5006 NULL, NULL, GFP_KERNEL);
5007 err = PTR_ERR(event);
5009 goto err_put_context;
5011 err = anon_inode_getfd("[perf_event]", &perf_fops, event, O_RDWR);
5013 goto err_free_put_context;
5015 event_file = fget_light(err, &fput_needed2);
5017 goto err_free_put_context;
5019 if (flags & PERF_FLAG_FD_OUTPUT) {
5020 err = perf_event_set_output(event, group_fd);
5022 goto err_fput_free_put_context;
5025 event->filp = event_file;
5026 WARN_ON_ONCE(ctx->parent_ctx);
5027 mutex_lock(&ctx->mutex);
5028 perf_install_in_context(ctx, event, cpu);
5030 mutex_unlock(&ctx->mutex);
5032 event->owner = current;
5033 get_task_struct(current);
5034 mutex_lock(¤t->perf_event_mutex);
5035 list_add_tail(&event->owner_entry, ¤t->perf_event_list);
5036 mutex_unlock(¤t->perf_event_mutex);
5038 err_fput_free_put_context:
5039 fput_light(event_file, fput_needed2);
5041 err_free_put_context:
5049 fput_light(group_file, fput_needed);
5055 * perf_event_create_kernel_counter
5057 * @attr: attributes of the counter to create
5058 * @cpu: cpu in which the counter is bound
5059 * @pid: task to profile
5062 perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
5064 perf_overflow_handler_t overflow_handler)
5066 struct perf_event *event;
5067 struct perf_event_context *ctx;
5071 * Get the target context (task or percpu):
5074 ctx = find_get_context(pid, cpu);
5080 event = perf_event_alloc(attr, cpu, ctx, NULL,
5081 NULL, overflow_handler, GFP_KERNEL);
5082 if (IS_ERR(event)) {
5083 err = PTR_ERR(event);
5084 goto err_put_context;
5088 WARN_ON_ONCE(ctx->parent_ctx);
5089 mutex_lock(&ctx->mutex);
5090 perf_install_in_context(ctx, event, cpu);
5092 mutex_unlock(&ctx->mutex);
5094 event->owner = current;
5095 get_task_struct(current);
5096 mutex_lock(¤t->perf_event_mutex);
5097 list_add_tail(&event->owner_entry, ¤t->perf_event_list);
5098 mutex_unlock(¤t->perf_event_mutex);
5105 return ERR_PTR(err);
5107 EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
5110 * inherit a event from parent task to child task:
5112 static struct perf_event *
5113 inherit_event(struct perf_event *parent_event,
5114 struct task_struct *parent,
5115 struct perf_event_context *parent_ctx,
5116 struct task_struct *child,
5117 struct perf_event *group_leader,
5118 struct perf_event_context *child_ctx)
5120 struct perf_event *child_event;
5123 * Instead of creating recursive hierarchies of events,
5124 * we link inherited events back to the original parent,
5125 * which has a filp for sure, which we use as the reference
5128 if (parent_event->parent)
5129 parent_event = parent_event->parent;
5131 child_event = perf_event_alloc(&parent_event->attr,
5132 parent_event->cpu, child_ctx,
5133 group_leader, parent_event,
5135 if (IS_ERR(child_event))
5140 * Make the child state follow the state of the parent event,
5141 * not its attr.disabled bit. We hold the parent's mutex,
5142 * so we won't race with perf_event_{en, dis}able_family.
5144 if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
5145 child_event->state = PERF_EVENT_STATE_INACTIVE;
5147 child_event->state = PERF_EVENT_STATE_OFF;
5149 if (parent_event->attr.freq) {
5150 u64 sample_period = parent_event->hw.sample_period;
5151 struct hw_perf_event *hwc = &child_event->hw;
5153 hwc->sample_period = sample_period;
5154 hwc->last_period = sample_period;
5156 atomic64_set(&hwc->period_left, sample_period);
5159 child_event->overflow_handler = parent_event->overflow_handler;
5162 * Link it up in the child's context:
5164 add_event_to_ctx(child_event, child_ctx);
5167 * Get a reference to the parent filp - we will fput it
5168 * when the child event exits. This is safe to do because
5169 * we are in the parent and we know that the filp still
5170 * exists and has a nonzero count:
5172 atomic_long_inc(&parent_event->filp->f_count);
5175 * Link this into the parent event's child list
5177 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
5178 mutex_lock(&parent_event->child_mutex);
5179 list_add_tail(&child_event->child_list, &parent_event->child_list);
5180 mutex_unlock(&parent_event->child_mutex);
5185 static int inherit_group(struct perf_event *parent_event,
5186 struct task_struct *parent,
5187 struct perf_event_context *parent_ctx,
5188 struct task_struct *child,
5189 struct perf_event_context *child_ctx)
5191 struct perf_event *leader;
5192 struct perf_event *sub;
5193 struct perf_event *child_ctr;
5195 leader = inherit_event(parent_event, parent, parent_ctx,
5196 child, NULL, child_ctx);
5198 return PTR_ERR(leader);
5199 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
5200 child_ctr = inherit_event(sub, parent, parent_ctx,
5201 child, leader, child_ctx);
5202 if (IS_ERR(child_ctr))
5203 return PTR_ERR(child_ctr);
5208 static void sync_child_event(struct perf_event *child_event,
5209 struct task_struct *child)
5211 struct perf_event *parent_event = child_event->parent;
5214 if (child_event->attr.inherit_stat)
5215 perf_event_read_event(child_event, child);
5217 child_val = atomic64_read(&child_event->count);
5220 * Add back the child's count to the parent's count:
5222 atomic64_add(child_val, &parent_event->count);
5223 atomic64_add(child_event->total_time_enabled,
5224 &parent_event->child_total_time_enabled);
5225 atomic64_add(child_event->total_time_running,
5226 &parent_event->child_total_time_running);
5229 * Remove this event from the parent's list
5231 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
5232 mutex_lock(&parent_event->child_mutex);
5233 list_del_init(&child_event->child_list);
5234 mutex_unlock(&parent_event->child_mutex);
5237 * Release the parent event, if this was the last
5240 fput(parent_event->filp);
5244 __perf_event_exit_task(struct perf_event *child_event,
5245 struct perf_event_context *child_ctx,
5246 struct task_struct *child)
5248 struct perf_event *parent_event;
5250 perf_event_remove_from_context(child_event);
5252 parent_event = child_event->parent;
5254 * It can happen that parent exits first, and has events
5255 * that are still around due to the child reference. These
5256 * events need to be zapped - but otherwise linger.
5259 sync_child_event(child_event, child);
5260 free_event(child_event);
5265 * When a child task exits, feed back event values to parent events.
5267 void perf_event_exit_task(struct task_struct *child)
5269 struct perf_event *child_event, *tmp;
5270 struct perf_event_context *child_ctx;
5271 unsigned long flags;
5273 if (likely(!child->perf_event_ctxp)) {
5274 perf_event_task(child, NULL, 0);
5278 local_irq_save(flags);
5280 * We can't reschedule here because interrupts are disabled,
5281 * and either child is current or it is a task that can't be
5282 * scheduled, so we are now safe from rescheduling changing
5285 child_ctx = child->perf_event_ctxp;
5286 __perf_event_task_sched_out(child_ctx);
5289 * Take the context lock here so that if find_get_context is
5290 * reading child->perf_event_ctxp, we wait until it has
5291 * incremented the context's refcount before we do put_ctx below.
5293 raw_spin_lock(&child_ctx->lock);
5294 child->perf_event_ctxp = NULL;
5296 * If this context is a clone; unclone it so it can't get
5297 * swapped to another process while we're removing all
5298 * the events from it.
5300 unclone_ctx(child_ctx);
5301 update_context_time(child_ctx);
5302 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
5305 * Report the task dead after unscheduling the events so that we
5306 * won't get any samples after PERF_RECORD_EXIT. We can however still
5307 * get a few PERF_RECORD_READ events.
5309 perf_event_task(child, child_ctx, 0);
5312 * We can recurse on the same lock type through:
5314 * __perf_event_exit_task()
5315 * sync_child_event()
5316 * fput(parent_event->filp)
5318 * mutex_lock(&ctx->mutex)
5320 * But since its the parent context it won't be the same instance.
5322 mutex_lock(&child_ctx->mutex);
5325 list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
5327 __perf_event_exit_task(child_event, child_ctx, child);
5329 list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
5331 __perf_event_exit_task(child_event, child_ctx, child);
5334 * If the last event was a group event, it will have appended all
5335 * its siblings to the list, but we obtained 'tmp' before that which
5336 * will still point to the list head terminating the iteration.
5338 if (!list_empty(&child_ctx->pinned_groups) ||
5339 !list_empty(&child_ctx->flexible_groups))
5342 mutex_unlock(&child_ctx->mutex);
5347 static void perf_free_event(struct perf_event *event,
5348 struct perf_event_context *ctx)
5350 struct perf_event *parent = event->parent;
5352 if (WARN_ON_ONCE(!parent))
5355 mutex_lock(&parent->child_mutex);
5356 list_del_init(&event->child_list);
5357 mutex_unlock(&parent->child_mutex);
5361 list_del_event(event, ctx);
5366 * free an unexposed, unused context as created by inheritance by
5367 * init_task below, used by fork() in case of fail.
5369 void perf_event_free_task(struct task_struct *task)
5371 struct perf_event_context *ctx = task->perf_event_ctxp;
5372 struct perf_event *event, *tmp;
5377 mutex_lock(&ctx->mutex);
5379 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
5380 perf_free_event(event, ctx);
5382 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
5384 perf_free_event(event, ctx);
5386 if (!list_empty(&ctx->pinned_groups) ||
5387 !list_empty(&ctx->flexible_groups))
5390 mutex_unlock(&ctx->mutex);
5396 inherit_task_group(struct perf_event *event, struct task_struct *parent,
5397 struct perf_event_context *parent_ctx,
5398 struct task_struct *child,
5402 struct perf_event_context *child_ctx = child->perf_event_ctxp;
5404 if (!event->attr.inherit) {
5411 * This is executed from the parent task context, so
5412 * inherit events that have been marked for cloning.
5413 * First allocate and initialize a context for the
5417 child_ctx = kzalloc(sizeof(struct perf_event_context),
5422 __perf_event_init_context(child_ctx, child);
5423 child->perf_event_ctxp = child_ctx;
5424 get_task_struct(child);
5427 ret = inherit_group(event, parent, parent_ctx,
5438 * Initialize the perf_event context in task_struct
5440 int perf_event_init_task(struct task_struct *child)
5442 struct perf_event_context *child_ctx, *parent_ctx;
5443 struct perf_event_context *cloned_ctx;
5444 struct perf_event *event;
5445 struct task_struct *parent = current;
5446 int inherited_all = 1;
5449 child->perf_event_ctxp = NULL;
5451 mutex_init(&child->perf_event_mutex);
5452 INIT_LIST_HEAD(&child->perf_event_list);
5454 if (likely(!parent->perf_event_ctxp))
5458 * If the parent's context is a clone, pin it so it won't get
5461 parent_ctx = perf_pin_task_context(parent);
5464 * No need to check if parent_ctx != NULL here; since we saw
5465 * it non-NULL earlier, the only reason for it to become NULL
5466 * is if we exit, and since we're currently in the middle of
5467 * a fork we can't be exiting at the same time.
5471 * Lock the parent list. No need to lock the child - not PID
5472 * hashed yet and not running, so nobody can access it.
5474 mutex_lock(&parent_ctx->mutex);
5477 * We dont have to disable NMIs - we are only looking at
5478 * the list, not manipulating it:
5480 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
5481 ret = inherit_task_group(event, parent, parent_ctx, child,
5487 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
5488 ret = inherit_task_group(event, parent, parent_ctx, child,
5494 child_ctx = child->perf_event_ctxp;
5496 if (child_ctx && inherited_all) {
5498 * Mark the child context as a clone of the parent
5499 * context, or of whatever the parent is a clone of.
5500 * Note that if the parent is a clone, it could get
5501 * uncloned at any point, but that doesn't matter
5502 * because the list of events and the generation
5503 * count can't have changed since we took the mutex.
5505 cloned_ctx = rcu_dereference(parent_ctx->parent_ctx);
5507 child_ctx->parent_ctx = cloned_ctx;
5508 child_ctx->parent_gen = parent_ctx->parent_gen;
5510 child_ctx->parent_ctx = parent_ctx;
5511 child_ctx->parent_gen = parent_ctx->generation;
5513 get_ctx(child_ctx->parent_ctx);
5516 mutex_unlock(&parent_ctx->mutex);
5518 perf_unpin_context(parent_ctx);
5523 static void __init perf_event_init_all_cpus(void)
5526 struct perf_cpu_context *cpuctx;
5528 for_each_possible_cpu(cpu) {
5529 cpuctx = &per_cpu(perf_cpu_context, cpu);
5530 mutex_init(&cpuctx->hlist_mutex);
5531 __perf_event_init_context(&cpuctx->ctx, NULL);
5535 static void __cpuinit perf_event_init_cpu(int cpu)
5537 struct perf_cpu_context *cpuctx;
5539 cpuctx = &per_cpu(perf_cpu_context, cpu);
5541 spin_lock(&perf_resource_lock);
5542 cpuctx->max_pertask = perf_max_events - perf_reserved_percpu;
5543 spin_unlock(&perf_resource_lock);
5545 mutex_lock(&cpuctx->hlist_mutex);
5546 if (cpuctx->hlist_refcount > 0) {
5547 struct swevent_hlist *hlist;
5549 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
5550 WARN_ON_ONCE(!hlist);
5551 rcu_assign_pointer(cpuctx->swevent_hlist, hlist);
5553 mutex_unlock(&cpuctx->hlist_mutex);
5556 #ifdef CONFIG_HOTPLUG_CPU
5557 static void __perf_event_exit_cpu(void *info)
5559 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
5560 struct perf_event_context *ctx = &cpuctx->ctx;
5561 struct perf_event *event, *tmp;
5563 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
5564 __perf_event_remove_from_context(event);
5565 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, group_entry)
5566 __perf_event_remove_from_context(event);
5568 static void perf_event_exit_cpu(int cpu)
5570 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
5571 struct perf_event_context *ctx = &cpuctx->ctx;
5573 mutex_lock(&cpuctx->hlist_mutex);
5574 swevent_hlist_release(cpuctx);
5575 mutex_unlock(&cpuctx->hlist_mutex);
5577 mutex_lock(&ctx->mutex);
5578 smp_call_function_single(cpu, __perf_event_exit_cpu, NULL, 1);
5579 mutex_unlock(&ctx->mutex);
5582 static inline void perf_event_exit_cpu(int cpu) { }
5585 static int __cpuinit
5586 perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
5588 unsigned int cpu = (long)hcpu;
5592 case CPU_UP_PREPARE:
5593 case CPU_UP_PREPARE_FROZEN:
5594 perf_event_init_cpu(cpu);
5597 case CPU_DOWN_PREPARE:
5598 case CPU_DOWN_PREPARE_FROZEN:
5599 perf_event_exit_cpu(cpu);
5610 * This has to have a higher priority than migration_notifier in sched.c.
5612 static struct notifier_block __cpuinitdata perf_cpu_nb = {
5613 .notifier_call = perf_cpu_notify,
5617 void __init perf_event_init(void)
5619 perf_event_init_all_cpus();
5620 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
5621 (void *)(long)smp_processor_id());
5622 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_ONLINE,
5623 (void *)(long)smp_processor_id());
5624 register_cpu_notifier(&perf_cpu_nb);
5627 static ssize_t perf_show_reserve_percpu(struct sysdev_class *class,
5628 struct sysdev_class_attribute *attr,
5631 return sprintf(buf, "%d\n", perf_reserved_percpu);
5635 perf_set_reserve_percpu(struct sysdev_class *class,
5636 struct sysdev_class_attribute *attr,
5640 struct perf_cpu_context *cpuctx;
5644 err = strict_strtoul(buf, 10, &val);
5647 if (val > perf_max_events)
5650 spin_lock(&perf_resource_lock);
5651 perf_reserved_percpu = val;
5652 for_each_online_cpu(cpu) {
5653 cpuctx = &per_cpu(perf_cpu_context, cpu);
5654 raw_spin_lock_irq(&cpuctx->ctx.lock);
5655 mpt = min(perf_max_events - cpuctx->ctx.nr_events,
5656 perf_max_events - perf_reserved_percpu);
5657 cpuctx->max_pertask = mpt;
5658 raw_spin_unlock_irq(&cpuctx->ctx.lock);
5660 spin_unlock(&perf_resource_lock);
5665 static ssize_t perf_show_overcommit(struct sysdev_class *class,
5666 struct sysdev_class_attribute *attr,
5669 return sprintf(buf, "%d\n", perf_overcommit);
5673 perf_set_overcommit(struct sysdev_class *class,
5674 struct sysdev_class_attribute *attr,
5675 const char *buf, size_t count)
5680 err = strict_strtoul(buf, 10, &val);
5686 spin_lock(&perf_resource_lock);
5687 perf_overcommit = val;
5688 spin_unlock(&perf_resource_lock);
5693 static SYSDEV_CLASS_ATTR(
5696 perf_show_reserve_percpu,
5697 perf_set_reserve_percpu
5700 static SYSDEV_CLASS_ATTR(
5703 perf_show_overcommit,
5707 static struct attribute *perfclass_attrs[] = {
5708 &attr_reserve_percpu.attr,
5709 &attr_overcommit.attr,
5713 static struct attribute_group perfclass_attr_group = {
5714 .attrs = perfclass_attrs,
5715 .name = "perf_events",
5718 static int __init perf_event_sysfs_init(void)
5720 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
5721 &perfclass_attr_group);
5723 device_initcall(perf_event_sysfs_init);