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/sysfs.h>
19 #include <linux/dcache.h>
20 #include <linux/percpu.h>
21 #include <linux/ptrace.h>
22 #include <linux/vmstat.h>
23 #include <linux/vmalloc.h>
24 #include <linux/hardirq.h>
25 #include <linux/rculist.h>
26 #include <linux/uaccess.h>
27 #include <linux/syscalls.h>
28 #include <linux/anon_inodes.h>
29 #include <linux/kernel_stat.h>
30 #include <linux/perf_event.h>
31 #include <linux/ftrace_event.h>
32 #include <linux/hw_breakpoint.h>
34 #include <asm/irq_regs.h>
37 * Each CPU has a list of per CPU events:
39 static DEFINE_PER_CPU(struct perf_cpu_context, perf_cpu_context);
41 int perf_max_events __read_mostly = 1;
42 static int perf_reserved_percpu __read_mostly;
43 static int perf_overcommit __read_mostly = 1;
45 static atomic_t nr_events __read_mostly;
46 static atomic_t nr_mmap_events __read_mostly;
47 static atomic_t nr_comm_events __read_mostly;
48 static atomic_t nr_task_events __read_mostly;
51 * perf event paranoia level:
52 * -1 - not paranoid at all
53 * 0 - disallow raw tracepoint access for unpriv
54 * 1 - disallow cpu events for unpriv
55 * 2 - disallow kernel profiling for unpriv
57 int sysctl_perf_event_paranoid __read_mostly = 1;
59 static inline bool perf_paranoid_tracepoint_raw(void)
61 return sysctl_perf_event_paranoid > -1;
64 static inline bool perf_paranoid_cpu(void)
66 return sysctl_perf_event_paranoid > 0;
69 static inline bool perf_paranoid_kernel(void)
71 return sysctl_perf_event_paranoid > 1;
74 int sysctl_perf_event_mlock __read_mostly = 512; /* 'free' kb per user */
77 * max perf event sample rate
79 int sysctl_perf_event_sample_rate __read_mostly = 100000;
81 static atomic64_t perf_event_id;
84 * Lock for (sysadmin-configurable) event reservations:
86 static DEFINE_SPINLOCK(perf_resource_lock);
89 * Architecture provided APIs - weak aliases:
91 extern __weak const struct pmu *hw_perf_event_init(struct perf_event *event)
96 void __weak hw_perf_disable(void) { barrier(); }
97 void __weak hw_perf_enable(void) { barrier(); }
99 void __weak hw_perf_event_setup(int cpu) { barrier(); }
100 void __weak hw_perf_event_setup_online(int cpu) { barrier(); }
103 hw_perf_group_sched_in(struct perf_event *group_leader,
104 struct perf_cpu_context *cpuctx,
105 struct perf_event_context *ctx, int cpu)
110 void __weak perf_event_print_debug(void) { }
112 static DEFINE_PER_CPU(int, perf_disable_count);
114 void __perf_disable(void)
116 __get_cpu_var(perf_disable_count)++;
119 bool __perf_enable(void)
121 return !--__get_cpu_var(perf_disable_count);
124 void perf_disable(void)
130 void perf_enable(void)
136 static void get_ctx(struct perf_event_context *ctx)
138 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
141 static void free_ctx(struct rcu_head *head)
143 struct perf_event_context *ctx;
145 ctx = container_of(head, struct perf_event_context, rcu_head);
149 static void put_ctx(struct perf_event_context *ctx)
151 if (atomic_dec_and_test(&ctx->refcount)) {
153 put_ctx(ctx->parent_ctx);
155 put_task_struct(ctx->task);
156 call_rcu(&ctx->rcu_head, free_ctx);
160 static void unclone_ctx(struct perf_event_context *ctx)
162 if (ctx->parent_ctx) {
163 put_ctx(ctx->parent_ctx);
164 ctx->parent_ctx = NULL;
169 * If we inherit events we want to return the parent event id
172 static u64 primary_event_id(struct perf_event *event)
177 id = event->parent->id;
183 * Get the perf_event_context for a task and lock it.
184 * This has to cope with with the fact that until it is locked,
185 * the context could get moved to another task.
187 static struct perf_event_context *
188 perf_lock_task_context(struct task_struct *task, unsigned long *flags)
190 struct perf_event_context *ctx;
194 ctx = rcu_dereference(task->perf_event_ctxp);
197 * If this context is a clone of another, it might
198 * get swapped for another underneath us by
199 * perf_event_task_sched_out, though the
200 * rcu_read_lock() protects us from any context
201 * getting freed. Lock the context and check if it
202 * got swapped before we could get the lock, and retry
203 * if so. If we locked the right context, then it
204 * can't get swapped on us any more.
206 raw_spin_lock_irqsave(&ctx->lock, *flags);
207 if (ctx != rcu_dereference(task->perf_event_ctxp)) {
208 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
212 if (!atomic_inc_not_zero(&ctx->refcount)) {
213 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
222 * Get the context for a task and increment its pin_count so it
223 * can't get swapped to another task. This also increments its
224 * reference count so that the context can't get freed.
226 static struct perf_event_context *perf_pin_task_context(struct task_struct *task)
228 struct perf_event_context *ctx;
231 ctx = perf_lock_task_context(task, &flags);
234 raw_spin_unlock_irqrestore(&ctx->lock, flags);
239 static void perf_unpin_context(struct perf_event_context *ctx)
243 raw_spin_lock_irqsave(&ctx->lock, flags);
245 raw_spin_unlock_irqrestore(&ctx->lock, flags);
249 static inline u64 perf_clock(void)
251 return cpu_clock(smp_processor_id());
255 * Update the record of the current time in a context.
257 static void update_context_time(struct perf_event_context *ctx)
259 u64 now = perf_clock();
261 ctx->time += now - ctx->timestamp;
262 ctx->timestamp = now;
266 * Update the total_time_enabled and total_time_running fields for a event.
268 static void update_event_times(struct perf_event *event)
270 struct perf_event_context *ctx = event->ctx;
273 if (event->state < PERF_EVENT_STATE_INACTIVE ||
274 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
280 run_end = event->tstamp_stopped;
282 event->total_time_enabled = run_end - event->tstamp_enabled;
284 if (event->state == PERF_EVENT_STATE_INACTIVE)
285 run_end = event->tstamp_stopped;
289 event->total_time_running = run_end - event->tstamp_running;
292 static struct list_head *
293 ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
295 if (event->attr.pinned)
296 return &ctx->pinned_groups;
298 return &ctx->flexible_groups;
302 * Add a event from the lists for its context.
303 * Must be called with ctx->mutex and ctx->lock held.
306 list_add_event(struct perf_event *event, struct perf_event_context *ctx)
308 struct perf_event *group_leader = event->group_leader;
311 * Depending on whether it is a standalone or sibling event,
312 * add it straight to the context's event list, or to the group
313 * leader's sibling list:
315 if (group_leader == event) {
316 struct list_head *list;
318 if (is_software_event(event))
319 event->group_flags |= PERF_GROUP_SOFTWARE;
321 list = ctx_group_list(event, ctx);
322 list_add_tail(&event->group_entry, list);
324 if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
325 !is_software_event(event))
326 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
328 list_add_tail(&event->group_entry, &group_leader->sibling_list);
329 group_leader->nr_siblings++;
332 list_add_rcu(&event->event_entry, &ctx->event_list);
334 if (event->attr.inherit_stat)
339 * Remove a event from the lists for its context.
340 * Must be called with ctx->mutex and ctx->lock held.
343 list_del_event(struct perf_event *event, struct perf_event_context *ctx)
345 struct perf_event *sibling, *tmp;
347 if (list_empty(&event->group_entry))
350 if (event->attr.inherit_stat)
353 list_del_init(&event->group_entry);
354 list_del_rcu(&event->event_entry);
356 if (event->group_leader != event)
357 event->group_leader->nr_siblings--;
359 update_event_times(event);
362 * If event was in error state, then keep it
363 * that way, otherwise bogus counts will be
364 * returned on read(). The only way to get out
365 * of error state is by explicit re-enabling
368 if (event->state > PERF_EVENT_STATE_OFF)
369 event->state = PERF_EVENT_STATE_OFF;
372 * If this was a group event with sibling events then
373 * upgrade the siblings to singleton events by adding them
374 * to the context list directly:
376 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
377 struct list_head *list;
379 list = ctx_group_list(event, ctx);
380 list_move_tail(&sibling->group_entry, list);
381 sibling->group_leader = sibling;
383 /* Inherit group flags from the previous leader */
384 sibling->group_flags = event->group_flags;
389 event_sched_out(struct perf_event *event,
390 struct perf_cpu_context *cpuctx,
391 struct perf_event_context *ctx)
393 if (event->state != PERF_EVENT_STATE_ACTIVE)
396 event->state = PERF_EVENT_STATE_INACTIVE;
397 if (event->pending_disable) {
398 event->pending_disable = 0;
399 event->state = PERF_EVENT_STATE_OFF;
401 event->tstamp_stopped = ctx->time;
402 event->pmu->disable(event);
405 if (!is_software_event(event))
406 cpuctx->active_oncpu--;
408 if (event->attr.exclusive || !cpuctx->active_oncpu)
409 cpuctx->exclusive = 0;
413 group_sched_out(struct perf_event *group_event,
414 struct perf_cpu_context *cpuctx,
415 struct perf_event_context *ctx)
417 struct perf_event *event;
419 if (group_event->state != PERF_EVENT_STATE_ACTIVE)
422 event_sched_out(group_event, cpuctx, ctx);
425 * Schedule out siblings (if any):
427 list_for_each_entry(event, &group_event->sibling_list, group_entry)
428 event_sched_out(event, cpuctx, ctx);
430 if (group_event->attr.exclusive)
431 cpuctx->exclusive = 0;
435 * Cross CPU call to remove a performance event
437 * We disable the event on the hardware level first. After that we
438 * remove it from the context list.
440 static void __perf_event_remove_from_context(void *info)
442 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
443 struct perf_event *event = info;
444 struct perf_event_context *ctx = event->ctx;
447 * If this is a task context, we need to check whether it is
448 * the current task context of this cpu. If not it has been
449 * scheduled out before the smp call arrived.
451 if (ctx->task && cpuctx->task_ctx != ctx)
454 raw_spin_lock(&ctx->lock);
456 * Protect the list operation against NMI by disabling the
457 * events on a global level.
461 event_sched_out(event, cpuctx, ctx);
463 list_del_event(event, ctx);
467 * Allow more per task events with respect to the
470 cpuctx->max_pertask =
471 min(perf_max_events - ctx->nr_events,
472 perf_max_events - perf_reserved_percpu);
476 raw_spin_unlock(&ctx->lock);
481 * Remove the event from a task's (or a CPU's) list of events.
483 * Must be called with ctx->mutex held.
485 * CPU events are removed with a smp call. For task events we only
486 * call when the task is on a CPU.
488 * If event->ctx is a cloned context, callers must make sure that
489 * every task struct that event->ctx->task could possibly point to
490 * remains valid. This is OK when called from perf_release since
491 * that only calls us on the top-level context, which can't be a clone.
492 * When called from perf_event_exit_task, it's OK because the
493 * context has been detached from its task.
495 static void perf_event_remove_from_context(struct perf_event *event)
497 struct perf_event_context *ctx = event->ctx;
498 struct task_struct *task = ctx->task;
502 * Per cpu events are removed via an smp call and
503 * the removal is always successful.
505 smp_call_function_single(event->cpu,
506 __perf_event_remove_from_context,
512 task_oncpu_function_call(task, __perf_event_remove_from_context,
515 raw_spin_lock_irq(&ctx->lock);
517 * If the context is active we need to retry the smp call.
519 if (ctx->nr_active && !list_empty(&event->group_entry)) {
520 raw_spin_unlock_irq(&ctx->lock);
525 * The lock prevents that this context is scheduled in so we
526 * can remove the event safely, if the call above did not
529 if (!list_empty(&event->group_entry))
530 list_del_event(event, ctx);
531 raw_spin_unlock_irq(&ctx->lock);
535 * Update total_time_enabled and total_time_running for all events in a group.
537 static void update_group_times(struct perf_event *leader)
539 struct perf_event *event;
541 update_event_times(leader);
542 list_for_each_entry(event, &leader->sibling_list, group_entry)
543 update_event_times(event);
547 * Cross CPU call to disable a performance event
549 static void __perf_event_disable(void *info)
551 struct perf_event *event = info;
552 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
553 struct perf_event_context *ctx = event->ctx;
556 * If this is a per-task event, need to check whether this
557 * event's task is the current task on this cpu.
559 if (ctx->task && cpuctx->task_ctx != ctx)
562 raw_spin_lock(&ctx->lock);
565 * If the event is on, turn it off.
566 * If it is in error state, leave it in error state.
568 if (event->state >= PERF_EVENT_STATE_INACTIVE) {
569 update_context_time(ctx);
570 update_group_times(event);
571 if (event == event->group_leader)
572 group_sched_out(event, cpuctx, ctx);
574 event_sched_out(event, cpuctx, ctx);
575 event->state = PERF_EVENT_STATE_OFF;
578 raw_spin_unlock(&ctx->lock);
584 * If event->ctx is a cloned context, callers must make sure that
585 * every task struct that event->ctx->task could possibly point to
586 * remains valid. This condition is satisifed when called through
587 * perf_event_for_each_child or perf_event_for_each because they
588 * hold the top-level event's child_mutex, so any descendant that
589 * goes to exit will block in sync_child_event.
590 * When called from perf_pending_event it's OK because event->ctx
591 * is the current context on this CPU and preemption is disabled,
592 * hence we can't get into perf_event_task_sched_out for this context.
594 void perf_event_disable(struct perf_event *event)
596 struct perf_event_context *ctx = event->ctx;
597 struct task_struct *task = ctx->task;
601 * Disable the event on the cpu that it's on
603 smp_call_function_single(event->cpu, __perf_event_disable,
609 task_oncpu_function_call(task, __perf_event_disable, event);
611 raw_spin_lock_irq(&ctx->lock);
613 * If the event is still active, we need to retry the cross-call.
615 if (event->state == PERF_EVENT_STATE_ACTIVE) {
616 raw_spin_unlock_irq(&ctx->lock);
621 * Since we have the lock this context can't be scheduled
622 * in, so we can change the state safely.
624 if (event->state == PERF_EVENT_STATE_INACTIVE) {
625 update_group_times(event);
626 event->state = PERF_EVENT_STATE_OFF;
629 raw_spin_unlock_irq(&ctx->lock);
633 event_sched_in(struct perf_event *event,
634 struct perf_cpu_context *cpuctx,
635 struct perf_event_context *ctx,
638 if (event->state <= PERF_EVENT_STATE_OFF)
641 event->state = PERF_EVENT_STATE_ACTIVE;
642 event->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
644 * The new state must be visible before we turn it on in the hardware:
648 if (event->pmu->enable(event)) {
649 event->state = PERF_EVENT_STATE_INACTIVE;
654 event->tstamp_running += ctx->time - event->tstamp_stopped;
656 if (!is_software_event(event))
657 cpuctx->active_oncpu++;
660 if (event->attr.exclusive)
661 cpuctx->exclusive = 1;
667 group_sched_in(struct perf_event *group_event,
668 struct perf_cpu_context *cpuctx,
669 struct perf_event_context *ctx,
672 struct perf_event *event, *partial_group;
675 if (group_event->state == PERF_EVENT_STATE_OFF)
678 ret = hw_perf_group_sched_in(group_event, cpuctx, ctx, cpu);
680 return ret < 0 ? ret : 0;
682 if (event_sched_in(group_event, cpuctx, ctx, cpu))
686 * Schedule in siblings as one group (if any):
688 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
689 if (event_sched_in(event, cpuctx, ctx, cpu)) {
690 partial_group = event;
699 * Groups can be scheduled in as one unit only, so undo any
700 * partial group before returning:
702 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
703 if (event == partial_group)
705 event_sched_out(event, cpuctx, ctx);
707 event_sched_out(group_event, cpuctx, ctx);
713 * Work out whether we can put this event group on the CPU now.
715 static int group_can_go_on(struct perf_event *event,
716 struct perf_cpu_context *cpuctx,
720 * Groups consisting entirely of software events can always go on.
722 if (event->group_flags & PERF_GROUP_SOFTWARE)
725 * If an exclusive group is already on, no other hardware
728 if (cpuctx->exclusive)
731 * If this group is exclusive and there are already
732 * events on the CPU, it can't go on.
734 if (event->attr.exclusive && cpuctx->active_oncpu)
737 * Otherwise, try to add it if all previous groups were able
743 static void add_event_to_ctx(struct perf_event *event,
744 struct perf_event_context *ctx)
746 list_add_event(event, ctx);
747 event->tstamp_enabled = ctx->time;
748 event->tstamp_running = ctx->time;
749 event->tstamp_stopped = ctx->time;
753 * Cross CPU call to install and enable a performance event
755 * Must be called with ctx->mutex held
757 static void __perf_install_in_context(void *info)
759 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
760 struct perf_event *event = info;
761 struct perf_event_context *ctx = event->ctx;
762 struct perf_event *leader = event->group_leader;
763 int cpu = smp_processor_id();
767 * If this is a task context, we need to check whether it is
768 * the current task context of this cpu. If not it has been
769 * scheduled out before the smp call arrived.
770 * Or possibly this is the right context but it isn't
771 * on this cpu because it had no events.
773 if (ctx->task && cpuctx->task_ctx != ctx) {
774 if (cpuctx->task_ctx || ctx->task != current)
776 cpuctx->task_ctx = ctx;
779 raw_spin_lock(&ctx->lock);
781 update_context_time(ctx);
784 * Protect the list operation against NMI by disabling the
785 * events on a global level. NOP for non NMI based events.
789 add_event_to_ctx(event, ctx);
791 if (event->cpu != -1 && event->cpu != smp_processor_id())
795 * Don't put the event on if it is disabled or if
796 * it is in a group and the group isn't on.
798 if (event->state != PERF_EVENT_STATE_INACTIVE ||
799 (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE))
803 * An exclusive event can't go on if there are already active
804 * hardware events, and no hardware event can go on if there
805 * is already an exclusive event on.
807 if (!group_can_go_on(event, cpuctx, 1))
810 err = event_sched_in(event, cpuctx, ctx, cpu);
814 * This event couldn't go on. If it is in a group
815 * then we have to pull the whole group off.
816 * If the event group is pinned then put it in error state.
819 group_sched_out(leader, cpuctx, ctx);
820 if (leader->attr.pinned) {
821 update_group_times(leader);
822 leader->state = PERF_EVENT_STATE_ERROR;
826 if (!err && !ctx->task && cpuctx->max_pertask)
827 cpuctx->max_pertask--;
832 raw_spin_unlock(&ctx->lock);
836 * Attach a performance event to a context
838 * First we add the event to the list with the hardware enable bit
839 * in event->hw_config cleared.
841 * If the event is attached to a task which is on a CPU we use a smp
842 * call to enable it in the task context. The task might have been
843 * scheduled away, but we check this in the smp call again.
845 * Must be called with ctx->mutex held.
848 perf_install_in_context(struct perf_event_context *ctx,
849 struct perf_event *event,
852 struct task_struct *task = ctx->task;
856 * Per cpu events are installed via an smp call and
857 * the install is always successful.
859 smp_call_function_single(cpu, __perf_install_in_context,
865 task_oncpu_function_call(task, __perf_install_in_context,
868 raw_spin_lock_irq(&ctx->lock);
870 * we need to retry the smp call.
872 if (ctx->is_active && list_empty(&event->group_entry)) {
873 raw_spin_unlock_irq(&ctx->lock);
878 * The lock prevents that this context is scheduled in so we
879 * can add the event safely, if it the call above did not
882 if (list_empty(&event->group_entry))
883 add_event_to_ctx(event, ctx);
884 raw_spin_unlock_irq(&ctx->lock);
888 * Put a event into inactive state and update time fields.
889 * Enabling the leader of a group effectively enables all
890 * the group members that aren't explicitly disabled, so we
891 * have to update their ->tstamp_enabled also.
892 * Note: this works for group members as well as group leaders
893 * since the non-leader members' sibling_lists will be empty.
895 static void __perf_event_mark_enabled(struct perf_event *event,
896 struct perf_event_context *ctx)
898 struct perf_event *sub;
900 event->state = PERF_EVENT_STATE_INACTIVE;
901 event->tstamp_enabled = ctx->time - event->total_time_enabled;
902 list_for_each_entry(sub, &event->sibling_list, group_entry)
903 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
904 sub->tstamp_enabled =
905 ctx->time - sub->total_time_enabled;
909 * Cross CPU call to enable a performance event
911 static void __perf_event_enable(void *info)
913 struct perf_event *event = info;
914 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
915 struct perf_event_context *ctx = event->ctx;
916 struct perf_event *leader = event->group_leader;
920 * If this is a per-task event, need to check whether this
921 * event's task is the current task on this cpu.
923 if (ctx->task && cpuctx->task_ctx != ctx) {
924 if (cpuctx->task_ctx || ctx->task != current)
926 cpuctx->task_ctx = ctx;
929 raw_spin_lock(&ctx->lock);
931 update_context_time(ctx);
933 if (event->state >= PERF_EVENT_STATE_INACTIVE)
935 __perf_event_mark_enabled(event, ctx);
937 if (event->cpu != -1 && event->cpu != smp_processor_id())
941 * If the event is in a group and isn't the group leader,
942 * then don't put it on unless the group is on.
944 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
947 if (!group_can_go_on(event, cpuctx, 1)) {
952 err = group_sched_in(event, cpuctx, ctx,
955 err = event_sched_in(event, cpuctx, ctx,
962 * If this event can't go on and it's part of a
963 * group, then the whole group has to come off.
966 group_sched_out(leader, cpuctx, ctx);
967 if (leader->attr.pinned) {
968 update_group_times(leader);
969 leader->state = PERF_EVENT_STATE_ERROR;
974 raw_spin_unlock(&ctx->lock);
980 * If event->ctx is a cloned context, callers must make sure that
981 * every task struct that event->ctx->task could possibly point to
982 * remains valid. This condition is satisfied when called through
983 * perf_event_for_each_child or perf_event_for_each as described
984 * for perf_event_disable.
986 void perf_event_enable(struct perf_event *event)
988 struct perf_event_context *ctx = event->ctx;
989 struct task_struct *task = ctx->task;
993 * Enable the event on the cpu that it's on
995 smp_call_function_single(event->cpu, __perf_event_enable,
1000 raw_spin_lock_irq(&ctx->lock);
1001 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1005 * If the event is in error state, clear that first.
1006 * That way, if we see the event in error state below, we
1007 * know that it has gone back into error state, as distinct
1008 * from the task having been scheduled away before the
1009 * cross-call arrived.
1011 if (event->state == PERF_EVENT_STATE_ERROR)
1012 event->state = PERF_EVENT_STATE_OFF;
1015 raw_spin_unlock_irq(&ctx->lock);
1016 task_oncpu_function_call(task, __perf_event_enable, event);
1018 raw_spin_lock_irq(&ctx->lock);
1021 * If the context is active and the event is still off,
1022 * we need to retry the cross-call.
1024 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF)
1028 * Since we have the lock this context can't be scheduled
1029 * in, so we can change the state safely.
1031 if (event->state == PERF_EVENT_STATE_OFF)
1032 __perf_event_mark_enabled(event, ctx);
1035 raw_spin_unlock_irq(&ctx->lock);
1038 static int perf_event_refresh(struct perf_event *event, int refresh)
1041 * not supported on inherited events
1043 if (event->attr.inherit)
1046 atomic_add(refresh, &event->event_limit);
1047 perf_event_enable(event);
1052 void __perf_event_sched_out(struct perf_event_context *ctx,
1053 struct perf_cpu_context *cpuctx)
1055 struct perf_event *event;
1057 raw_spin_lock(&ctx->lock);
1059 if (likely(!ctx->nr_events))
1061 update_context_time(ctx);
1064 if (ctx->nr_active) {
1065 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
1066 group_sched_out(event, cpuctx, ctx);
1068 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
1069 group_sched_out(event, cpuctx, ctx);
1073 raw_spin_unlock(&ctx->lock);
1077 * Test whether two contexts are equivalent, i.e. whether they
1078 * have both been cloned from the same version of the same context
1079 * and they both have the same number of enabled events.
1080 * If the number of enabled events is the same, then the set
1081 * of enabled events should be the same, because these are both
1082 * inherited contexts, therefore we can't access individual events
1083 * in them directly with an fd; we can only enable/disable all
1084 * events via prctl, or enable/disable all events in a family
1085 * via ioctl, which will have the same effect on both contexts.
1087 static int context_equiv(struct perf_event_context *ctx1,
1088 struct perf_event_context *ctx2)
1090 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
1091 && ctx1->parent_gen == ctx2->parent_gen
1092 && !ctx1->pin_count && !ctx2->pin_count;
1095 static void __perf_event_sync_stat(struct perf_event *event,
1096 struct perf_event *next_event)
1100 if (!event->attr.inherit_stat)
1104 * Update the event value, we cannot use perf_event_read()
1105 * because we're in the middle of a context switch and have IRQs
1106 * disabled, which upsets smp_call_function_single(), however
1107 * we know the event must be on the current CPU, therefore we
1108 * don't need to use it.
1110 switch (event->state) {
1111 case PERF_EVENT_STATE_ACTIVE:
1112 event->pmu->read(event);
1115 case PERF_EVENT_STATE_INACTIVE:
1116 update_event_times(event);
1124 * In order to keep per-task stats reliable we need to flip the event
1125 * values when we flip the contexts.
1127 value = atomic64_read(&next_event->count);
1128 value = atomic64_xchg(&event->count, value);
1129 atomic64_set(&next_event->count, value);
1131 swap(event->total_time_enabled, next_event->total_time_enabled);
1132 swap(event->total_time_running, next_event->total_time_running);
1135 * Since we swizzled the values, update the user visible data too.
1137 perf_event_update_userpage(event);
1138 perf_event_update_userpage(next_event);
1141 #define list_next_entry(pos, member) \
1142 list_entry(pos->member.next, typeof(*pos), member)
1144 static void perf_event_sync_stat(struct perf_event_context *ctx,
1145 struct perf_event_context *next_ctx)
1147 struct perf_event *event, *next_event;
1152 update_context_time(ctx);
1154 event = list_first_entry(&ctx->event_list,
1155 struct perf_event, event_entry);
1157 next_event = list_first_entry(&next_ctx->event_list,
1158 struct perf_event, event_entry);
1160 while (&event->event_entry != &ctx->event_list &&
1161 &next_event->event_entry != &next_ctx->event_list) {
1163 __perf_event_sync_stat(event, next_event);
1165 event = list_next_entry(event, event_entry);
1166 next_event = list_next_entry(next_event, event_entry);
1171 * Called from scheduler to remove the events of the current task,
1172 * with interrupts disabled.
1174 * We stop each event and update the event value in event->count.
1176 * This does not protect us against NMI, but disable()
1177 * sets the disabled bit in the control field of event _before_
1178 * accessing the event control register. If a NMI hits, then it will
1179 * not restart the event.
1181 void perf_event_task_sched_out(struct task_struct *task,
1182 struct task_struct *next)
1184 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1185 struct perf_event_context *ctx = task->perf_event_ctxp;
1186 struct perf_event_context *next_ctx;
1187 struct perf_event_context *parent;
1188 struct pt_regs *regs;
1191 regs = task_pt_regs(task);
1192 perf_sw_event(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 1, regs, 0);
1194 if (likely(!ctx || !cpuctx->task_ctx))
1198 parent = rcu_dereference(ctx->parent_ctx);
1199 next_ctx = next->perf_event_ctxp;
1200 if (parent && next_ctx &&
1201 rcu_dereference(next_ctx->parent_ctx) == parent) {
1203 * Looks like the two contexts are clones, so we might be
1204 * able to optimize the context switch. We lock both
1205 * contexts and check that they are clones under the
1206 * lock (including re-checking that neither has been
1207 * uncloned in the meantime). It doesn't matter which
1208 * order we take the locks because no other cpu could
1209 * be trying to lock both of these tasks.
1211 raw_spin_lock(&ctx->lock);
1212 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
1213 if (context_equiv(ctx, next_ctx)) {
1215 * XXX do we need a memory barrier of sorts
1216 * wrt to rcu_dereference() of perf_event_ctxp
1218 task->perf_event_ctxp = next_ctx;
1219 next->perf_event_ctxp = ctx;
1221 next_ctx->task = task;
1224 perf_event_sync_stat(ctx, next_ctx);
1226 raw_spin_unlock(&next_ctx->lock);
1227 raw_spin_unlock(&ctx->lock);
1232 __perf_event_sched_out(ctx, cpuctx);
1233 cpuctx->task_ctx = NULL;
1238 * Called with IRQs disabled
1240 static void __perf_event_task_sched_out(struct perf_event_context *ctx)
1242 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1244 if (!cpuctx->task_ctx)
1247 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
1250 __perf_event_sched_out(ctx, cpuctx);
1251 cpuctx->task_ctx = NULL;
1255 * Called with IRQs disabled
1257 static void perf_event_cpu_sched_out(struct perf_cpu_context *cpuctx)
1259 __perf_event_sched_out(&cpuctx->ctx, cpuctx);
1263 __perf_event_sched_in(struct perf_event_context *ctx,
1264 struct perf_cpu_context *cpuctx)
1266 int cpu = smp_processor_id();
1267 struct perf_event *event;
1270 raw_spin_lock(&ctx->lock);
1272 if (likely(!ctx->nr_events))
1275 ctx->timestamp = perf_clock();
1280 * First go through the list and put on any pinned groups
1281 * in order to give them the best chance of going on.
1283 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
1284 if (event->state <= PERF_EVENT_STATE_OFF)
1286 if (event->cpu != -1 && event->cpu != cpu)
1289 if (group_can_go_on(event, cpuctx, 1))
1290 group_sched_in(event, cpuctx, ctx, cpu);
1293 * If this pinned group hasn't been scheduled,
1294 * put it in error state.
1296 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1297 update_group_times(event);
1298 event->state = PERF_EVENT_STATE_ERROR;
1302 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
1303 /* Ignore events in OFF or ERROR state */
1304 if (event->state <= PERF_EVENT_STATE_OFF)
1307 * Listen to the 'cpu' scheduling filter constraint
1310 if (event->cpu != -1 && event->cpu != cpu)
1313 if (group_can_go_on(event, cpuctx, can_add_hw))
1314 if (group_sched_in(event, cpuctx, ctx, cpu))
1319 raw_spin_unlock(&ctx->lock);
1323 * Called from scheduler to add the events of the current task
1324 * with interrupts disabled.
1326 * We restore the event value and then enable it.
1328 * This does not protect us against NMI, but enable()
1329 * sets the enabled bit in the control field of event _before_
1330 * accessing the event control register. If a NMI hits, then it will
1331 * keep the event running.
1333 void perf_event_task_sched_in(struct task_struct *task)
1335 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1336 struct perf_event_context *ctx = task->perf_event_ctxp;
1340 if (cpuctx->task_ctx == ctx)
1342 __perf_event_sched_in(ctx, cpuctx);
1343 cpuctx->task_ctx = ctx;
1346 static void perf_event_cpu_sched_in(struct perf_cpu_context *cpuctx)
1348 struct perf_event_context *ctx = &cpuctx->ctx;
1350 __perf_event_sched_in(ctx, cpuctx);
1353 #define MAX_INTERRUPTS (~0ULL)
1355 static void perf_log_throttle(struct perf_event *event, int enable);
1357 static void perf_adjust_period(struct perf_event *event, u64 events)
1359 struct hw_perf_event *hwc = &event->hw;
1360 u64 period, sample_period;
1363 events *= hwc->sample_period;
1364 period = div64_u64(events, event->attr.sample_freq);
1366 delta = (s64)(period - hwc->sample_period);
1367 delta = (delta + 7) / 8; /* low pass filter */
1369 sample_period = hwc->sample_period + delta;
1374 hwc->sample_period = sample_period;
1377 static void perf_ctx_adjust_freq(struct perf_event_context *ctx)
1379 struct perf_event *event;
1380 struct hw_perf_event *hwc;
1381 u64 interrupts, freq;
1383 raw_spin_lock(&ctx->lock);
1384 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
1385 if (event->state != PERF_EVENT_STATE_ACTIVE)
1388 if (event->cpu != -1 && event->cpu != smp_processor_id())
1393 interrupts = hwc->interrupts;
1394 hwc->interrupts = 0;
1397 * unthrottle events on the tick
1399 if (interrupts == MAX_INTERRUPTS) {
1400 perf_log_throttle(event, 1);
1401 event->pmu->unthrottle(event);
1402 interrupts = 2*sysctl_perf_event_sample_rate/HZ;
1405 if (!event->attr.freq || !event->attr.sample_freq)
1409 * if the specified freq < HZ then we need to skip ticks
1411 if (event->attr.sample_freq < HZ) {
1412 freq = event->attr.sample_freq;
1414 hwc->freq_count += freq;
1415 hwc->freq_interrupts += interrupts;
1417 if (hwc->freq_count < HZ)
1420 interrupts = hwc->freq_interrupts;
1421 hwc->freq_interrupts = 0;
1422 hwc->freq_count -= HZ;
1426 perf_adjust_period(event, freq * interrupts);
1429 * In order to avoid being stalled by an (accidental) huge
1430 * sample period, force reset the sample period if we didn't
1431 * get any events in this freq period.
1435 event->pmu->disable(event);
1436 atomic64_set(&hwc->period_left, 0);
1437 event->pmu->enable(event);
1441 raw_spin_unlock(&ctx->lock);
1445 * Round-robin a context's events:
1447 static void rotate_ctx(struct perf_event_context *ctx)
1449 if (!ctx->nr_events)
1452 raw_spin_lock(&ctx->lock);
1454 /* Rotate the first entry last of non-pinned groups */
1457 list_rotate_left(&ctx->flexible_groups);
1461 raw_spin_unlock(&ctx->lock);
1464 void perf_event_task_tick(struct task_struct *curr)
1466 struct perf_cpu_context *cpuctx;
1467 struct perf_event_context *ctx;
1469 if (!atomic_read(&nr_events))
1472 cpuctx = &__get_cpu_var(perf_cpu_context);
1473 ctx = curr->perf_event_ctxp;
1475 perf_ctx_adjust_freq(&cpuctx->ctx);
1477 perf_ctx_adjust_freq(ctx);
1479 perf_event_cpu_sched_out(cpuctx);
1481 __perf_event_task_sched_out(ctx);
1483 rotate_ctx(&cpuctx->ctx);
1487 perf_event_cpu_sched_in(cpuctx);
1489 perf_event_task_sched_in(curr);
1492 static int event_enable_on_exec(struct perf_event *event,
1493 struct perf_event_context *ctx)
1495 if (!event->attr.enable_on_exec)
1498 event->attr.enable_on_exec = 0;
1499 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1502 __perf_event_mark_enabled(event, ctx);
1508 * Enable all of a task's events that have been marked enable-on-exec.
1509 * This expects task == current.
1511 static void perf_event_enable_on_exec(struct task_struct *task)
1513 struct perf_event_context *ctx;
1514 struct perf_event *event;
1515 unsigned long flags;
1519 local_irq_save(flags);
1520 ctx = task->perf_event_ctxp;
1521 if (!ctx || !ctx->nr_events)
1524 __perf_event_task_sched_out(ctx);
1526 raw_spin_lock(&ctx->lock);
1528 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
1529 ret = event_enable_on_exec(event, ctx);
1534 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
1535 ret = event_enable_on_exec(event, ctx);
1541 * Unclone this context if we enabled any event.
1546 raw_spin_unlock(&ctx->lock);
1548 perf_event_task_sched_in(task);
1550 local_irq_restore(flags);
1554 * Cross CPU call to read the hardware event
1556 static void __perf_event_read(void *info)
1558 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1559 struct perf_event *event = info;
1560 struct perf_event_context *ctx = event->ctx;
1563 * If this is a task context, we need to check whether it is
1564 * the current task context of this cpu. If not it has been
1565 * scheduled out before the smp call arrived. In that case
1566 * event->count would have been updated to a recent sample
1567 * when the event was scheduled out.
1569 if (ctx->task && cpuctx->task_ctx != ctx)
1572 raw_spin_lock(&ctx->lock);
1573 update_context_time(ctx);
1574 update_event_times(event);
1575 raw_spin_unlock(&ctx->lock);
1577 event->pmu->read(event);
1580 static u64 perf_event_read(struct perf_event *event)
1583 * If event is enabled and currently active on a CPU, update the
1584 * value in the event structure:
1586 if (event->state == PERF_EVENT_STATE_ACTIVE) {
1587 smp_call_function_single(event->oncpu,
1588 __perf_event_read, event, 1);
1589 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
1590 struct perf_event_context *ctx = event->ctx;
1591 unsigned long flags;
1593 raw_spin_lock_irqsave(&ctx->lock, flags);
1594 update_context_time(ctx);
1595 update_event_times(event);
1596 raw_spin_unlock_irqrestore(&ctx->lock, flags);
1599 return atomic64_read(&event->count);
1603 * Initialize the perf_event context in a task_struct:
1606 __perf_event_init_context(struct perf_event_context *ctx,
1607 struct task_struct *task)
1609 raw_spin_lock_init(&ctx->lock);
1610 mutex_init(&ctx->mutex);
1611 INIT_LIST_HEAD(&ctx->pinned_groups);
1612 INIT_LIST_HEAD(&ctx->flexible_groups);
1613 INIT_LIST_HEAD(&ctx->event_list);
1614 atomic_set(&ctx->refcount, 1);
1618 static struct perf_event_context *find_get_context(pid_t pid, int cpu)
1620 struct perf_event_context *ctx;
1621 struct perf_cpu_context *cpuctx;
1622 struct task_struct *task;
1623 unsigned long flags;
1626 if (pid == -1 && cpu != -1) {
1627 /* Must be root to operate on a CPU event: */
1628 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
1629 return ERR_PTR(-EACCES);
1631 if (cpu < 0 || cpu >= nr_cpumask_bits)
1632 return ERR_PTR(-EINVAL);
1635 * We could be clever and allow to attach a event to an
1636 * offline CPU and activate it when the CPU comes up, but
1639 if (!cpu_online(cpu))
1640 return ERR_PTR(-ENODEV);
1642 cpuctx = &per_cpu(perf_cpu_context, cpu);
1653 task = find_task_by_vpid(pid);
1655 get_task_struct(task);
1659 return ERR_PTR(-ESRCH);
1662 * Can't attach events to a dying task.
1665 if (task->flags & PF_EXITING)
1668 /* Reuse ptrace permission checks for now. */
1670 if (!ptrace_may_access(task, PTRACE_MODE_READ))
1674 ctx = perf_lock_task_context(task, &flags);
1677 raw_spin_unlock_irqrestore(&ctx->lock, flags);
1681 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
1685 __perf_event_init_context(ctx, task);
1687 if (cmpxchg(&task->perf_event_ctxp, NULL, ctx)) {
1689 * We raced with some other task; use
1690 * the context they set.
1695 get_task_struct(task);
1698 put_task_struct(task);
1702 put_task_struct(task);
1703 return ERR_PTR(err);
1706 static void perf_event_free_filter(struct perf_event *event);
1708 static void free_event_rcu(struct rcu_head *head)
1710 struct perf_event *event;
1712 event = container_of(head, struct perf_event, rcu_head);
1714 put_pid_ns(event->ns);
1715 perf_event_free_filter(event);
1719 static void perf_pending_sync(struct perf_event *event);
1721 static void free_event(struct perf_event *event)
1723 perf_pending_sync(event);
1725 if (!event->parent) {
1726 atomic_dec(&nr_events);
1727 if (event->attr.mmap)
1728 atomic_dec(&nr_mmap_events);
1729 if (event->attr.comm)
1730 atomic_dec(&nr_comm_events);
1731 if (event->attr.task)
1732 atomic_dec(&nr_task_events);
1735 if (event->output) {
1736 fput(event->output->filp);
1737 event->output = NULL;
1741 event->destroy(event);
1743 put_ctx(event->ctx);
1744 call_rcu(&event->rcu_head, free_event_rcu);
1747 int perf_event_release_kernel(struct perf_event *event)
1749 struct perf_event_context *ctx = event->ctx;
1751 WARN_ON_ONCE(ctx->parent_ctx);
1752 mutex_lock(&ctx->mutex);
1753 perf_event_remove_from_context(event);
1754 mutex_unlock(&ctx->mutex);
1756 mutex_lock(&event->owner->perf_event_mutex);
1757 list_del_init(&event->owner_entry);
1758 mutex_unlock(&event->owner->perf_event_mutex);
1759 put_task_struct(event->owner);
1765 EXPORT_SYMBOL_GPL(perf_event_release_kernel);
1768 * Called when the last reference to the file is gone.
1770 static int perf_release(struct inode *inode, struct file *file)
1772 struct perf_event *event = file->private_data;
1774 file->private_data = NULL;
1776 return perf_event_release_kernel(event);
1779 static int perf_event_read_size(struct perf_event *event)
1781 int entry = sizeof(u64); /* value */
1785 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1786 size += sizeof(u64);
1788 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1789 size += sizeof(u64);
1791 if (event->attr.read_format & PERF_FORMAT_ID)
1792 entry += sizeof(u64);
1794 if (event->attr.read_format & PERF_FORMAT_GROUP) {
1795 nr += event->group_leader->nr_siblings;
1796 size += sizeof(u64);
1804 u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
1806 struct perf_event *child;
1812 mutex_lock(&event->child_mutex);
1813 total += perf_event_read(event);
1814 *enabled += event->total_time_enabled +
1815 atomic64_read(&event->child_total_time_enabled);
1816 *running += event->total_time_running +
1817 atomic64_read(&event->child_total_time_running);
1819 list_for_each_entry(child, &event->child_list, child_list) {
1820 total += perf_event_read(child);
1821 *enabled += child->total_time_enabled;
1822 *running += child->total_time_running;
1824 mutex_unlock(&event->child_mutex);
1828 EXPORT_SYMBOL_GPL(perf_event_read_value);
1830 static int perf_event_read_group(struct perf_event *event,
1831 u64 read_format, char __user *buf)
1833 struct perf_event *leader = event->group_leader, *sub;
1834 int n = 0, size = 0, ret = -EFAULT;
1835 struct perf_event_context *ctx = leader->ctx;
1837 u64 count, enabled, running;
1839 mutex_lock(&ctx->mutex);
1840 count = perf_event_read_value(leader, &enabled, &running);
1842 values[n++] = 1 + leader->nr_siblings;
1843 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1844 values[n++] = enabled;
1845 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1846 values[n++] = running;
1847 values[n++] = count;
1848 if (read_format & PERF_FORMAT_ID)
1849 values[n++] = primary_event_id(leader);
1851 size = n * sizeof(u64);
1853 if (copy_to_user(buf, values, size))
1858 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
1861 values[n++] = perf_event_read_value(sub, &enabled, &running);
1862 if (read_format & PERF_FORMAT_ID)
1863 values[n++] = primary_event_id(sub);
1865 size = n * sizeof(u64);
1867 if (copy_to_user(buf + ret, values, size)) {
1875 mutex_unlock(&ctx->mutex);
1880 static int perf_event_read_one(struct perf_event *event,
1881 u64 read_format, char __user *buf)
1883 u64 enabled, running;
1887 values[n++] = perf_event_read_value(event, &enabled, &running);
1888 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1889 values[n++] = enabled;
1890 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1891 values[n++] = running;
1892 if (read_format & PERF_FORMAT_ID)
1893 values[n++] = primary_event_id(event);
1895 if (copy_to_user(buf, values, n * sizeof(u64)))
1898 return n * sizeof(u64);
1902 * Read the performance event - simple non blocking version for now
1905 perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
1907 u64 read_format = event->attr.read_format;
1911 * Return end-of-file for a read on a event that is in
1912 * error state (i.e. because it was pinned but it couldn't be
1913 * scheduled on to the CPU at some point).
1915 if (event->state == PERF_EVENT_STATE_ERROR)
1918 if (count < perf_event_read_size(event))
1921 WARN_ON_ONCE(event->ctx->parent_ctx);
1922 if (read_format & PERF_FORMAT_GROUP)
1923 ret = perf_event_read_group(event, read_format, buf);
1925 ret = perf_event_read_one(event, read_format, buf);
1931 perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
1933 struct perf_event *event = file->private_data;
1935 return perf_read_hw(event, buf, count);
1938 static unsigned int perf_poll(struct file *file, poll_table *wait)
1940 struct perf_event *event = file->private_data;
1941 struct perf_mmap_data *data;
1942 unsigned int events = POLL_HUP;
1945 data = rcu_dereference(event->data);
1947 events = atomic_xchg(&data->poll, 0);
1950 poll_wait(file, &event->waitq, wait);
1955 static void perf_event_reset(struct perf_event *event)
1957 (void)perf_event_read(event);
1958 atomic64_set(&event->count, 0);
1959 perf_event_update_userpage(event);
1963 * Holding the top-level event's child_mutex means that any
1964 * descendant process that has inherited this event will block
1965 * in sync_child_event if it goes to exit, thus satisfying the
1966 * task existence requirements of perf_event_enable/disable.
1968 static void perf_event_for_each_child(struct perf_event *event,
1969 void (*func)(struct perf_event *))
1971 struct perf_event *child;
1973 WARN_ON_ONCE(event->ctx->parent_ctx);
1974 mutex_lock(&event->child_mutex);
1976 list_for_each_entry(child, &event->child_list, child_list)
1978 mutex_unlock(&event->child_mutex);
1981 static void perf_event_for_each(struct perf_event *event,
1982 void (*func)(struct perf_event *))
1984 struct perf_event_context *ctx = event->ctx;
1985 struct perf_event *sibling;
1987 WARN_ON_ONCE(ctx->parent_ctx);
1988 mutex_lock(&ctx->mutex);
1989 event = event->group_leader;
1991 perf_event_for_each_child(event, func);
1993 list_for_each_entry(sibling, &event->sibling_list, group_entry)
1994 perf_event_for_each_child(event, func);
1995 mutex_unlock(&ctx->mutex);
1998 static int perf_event_period(struct perf_event *event, u64 __user *arg)
2000 struct perf_event_context *ctx = event->ctx;
2005 if (!event->attr.sample_period)
2008 size = copy_from_user(&value, arg, sizeof(value));
2009 if (size != sizeof(value))
2015 raw_spin_lock_irq(&ctx->lock);
2016 if (event->attr.freq) {
2017 if (value > sysctl_perf_event_sample_rate) {
2022 event->attr.sample_freq = value;
2024 event->attr.sample_period = value;
2025 event->hw.sample_period = value;
2028 raw_spin_unlock_irq(&ctx->lock);
2033 static int perf_event_set_output(struct perf_event *event, int output_fd);
2034 static int perf_event_set_filter(struct perf_event *event, void __user *arg);
2036 static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2038 struct perf_event *event = file->private_data;
2039 void (*func)(struct perf_event *);
2043 case PERF_EVENT_IOC_ENABLE:
2044 func = perf_event_enable;
2046 case PERF_EVENT_IOC_DISABLE:
2047 func = perf_event_disable;
2049 case PERF_EVENT_IOC_RESET:
2050 func = perf_event_reset;
2053 case PERF_EVENT_IOC_REFRESH:
2054 return perf_event_refresh(event, arg);
2056 case PERF_EVENT_IOC_PERIOD:
2057 return perf_event_period(event, (u64 __user *)arg);
2059 case PERF_EVENT_IOC_SET_OUTPUT:
2060 return perf_event_set_output(event, arg);
2062 case PERF_EVENT_IOC_SET_FILTER:
2063 return perf_event_set_filter(event, (void __user *)arg);
2069 if (flags & PERF_IOC_FLAG_GROUP)
2070 perf_event_for_each(event, func);
2072 perf_event_for_each_child(event, func);
2077 int perf_event_task_enable(void)
2079 struct perf_event *event;
2081 mutex_lock(¤t->perf_event_mutex);
2082 list_for_each_entry(event, ¤t->perf_event_list, owner_entry)
2083 perf_event_for_each_child(event, perf_event_enable);
2084 mutex_unlock(¤t->perf_event_mutex);
2089 int perf_event_task_disable(void)
2091 struct perf_event *event;
2093 mutex_lock(¤t->perf_event_mutex);
2094 list_for_each_entry(event, ¤t->perf_event_list, owner_entry)
2095 perf_event_for_each_child(event, perf_event_disable);
2096 mutex_unlock(¤t->perf_event_mutex);
2101 #ifndef PERF_EVENT_INDEX_OFFSET
2102 # define PERF_EVENT_INDEX_OFFSET 0
2105 static int perf_event_index(struct perf_event *event)
2107 if (event->state != PERF_EVENT_STATE_ACTIVE)
2110 return event->hw.idx + 1 - PERF_EVENT_INDEX_OFFSET;
2114 * Callers need to ensure there can be no nesting of this function, otherwise
2115 * the seqlock logic goes bad. We can not serialize this because the arch
2116 * code calls this from NMI context.
2118 void perf_event_update_userpage(struct perf_event *event)
2120 struct perf_event_mmap_page *userpg;
2121 struct perf_mmap_data *data;
2124 data = rcu_dereference(event->data);
2128 userpg = data->user_page;
2131 * Disable preemption so as to not let the corresponding user-space
2132 * spin too long if we get preempted.
2137 userpg->index = perf_event_index(event);
2138 userpg->offset = atomic64_read(&event->count);
2139 if (event->state == PERF_EVENT_STATE_ACTIVE)
2140 userpg->offset -= atomic64_read(&event->hw.prev_count);
2142 userpg->time_enabled = event->total_time_enabled +
2143 atomic64_read(&event->child_total_time_enabled);
2145 userpg->time_running = event->total_time_running +
2146 atomic64_read(&event->child_total_time_running);
2155 static unsigned long perf_data_size(struct perf_mmap_data *data)
2157 return data->nr_pages << (PAGE_SHIFT + data->data_order);
2160 #ifndef CONFIG_PERF_USE_VMALLOC
2163 * Back perf_mmap() with regular GFP_KERNEL-0 pages.
2166 static struct page *
2167 perf_mmap_to_page(struct perf_mmap_data *data, unsigned long pgoff)
2169 if (pgoff > data->nr_pages)
2173 return virt_to_page(data->user_page);
2175 return virt_to_page(data->data_pages[pgoff - 1]);
2178 static struct perf_mmap_data *
2179 perf_mmap_data_alloc(struct perf_event *event, int nr_pages)
2181 struct perf_mmap_data *data;
2185 WARN_ON(atomic_read(&event->mmap_count));
2187 size = sizeof(struct perf_mmap_data);
2188 size += nr_pages * sizeof(void *);
2190 data = kzalloc(size, GFP_KERNEL);
2194 data->user_page = (void *)get_zeroed_page(GFP_KERNEL);
2195 if (!data->user_page)
2196 goto fail_user_page;
2198 for (i = 0; i < nr_pages; i++) {
2199 data->data_pages[i] = (void *)get_zeroed_page(GFP_KERNEL);
2200 if (!data->data_pages[i])
2201 goto fail_data_pages;
2204 data->data_order = 0;
2205 data->nr_pages = nr_pages;
2210 for (i--; i >= 0; i--)
2211 free_page((unsigned long)data->data_pages[i]);
2213 free_page((unsigned long)data->user_page);
2222 static void perf_mmap_free_page(unsigned long addr)
2224 struct page *page = virt_to_page((void *)addr);
2226 page->mapping = NULL;
2230 static void perf_mmap_data_free(struct perf_mmap_data *data)
2234 perf_mmap_free_page((unsigned long)data->user_page);
2235 for (i = 0; i < data->nr_pages; i++)
2236 perf_mmap_free_page((unsigned long)data->data_pages[i]);
2243 * Back perf_mmap() with vmalloc memory.
2245 * Required for architectures that have d-cache aliasing issues.
2248 static struct page *
2249 perf_mmap_to_page(struct perf_mmap_data *data, unsigned long pgoff)
2251 if (pgoff > (1UL << data->data_order))
2254 return vmalloc_to_page((void *)data->user_page + pgoff * PAGE_SIZE);
2257 static void perf_mmap_unmark_page(void *addr)
2259 struct page *page = vmalloc_to_page(addr);
2261 page->mapping = NULL;
2264 static void perf_mmap_data_free_work(struct work_struct *work)
2266 struct perf_mmap_data *data;
2270 data = container_of(work, struct perf_mmap_data, work);
2271 nr = 1 << data->data_order;
2273 base = data->user_page;
2274 for (i = 0; i < nr + 1; i++)
2275 perf_mmap_unmark_page(base + (i * PAGE_SIZE));
2281 static void perf_mmap_data_free(struct perf_mmap_data *data)
2283 schedule_work(&data->work);
2286 static struct perf_mmap_data *
2287 perf_mmap_data_alloc(struct perf_event *event, int nr_pages)
2289 struct perf_mmap_data *data;
2293 WARN_ON(atomic_read(&event->mmap_count));
2295 size = sizeof(struct perf_mmap_data);
2296 size += sizeof(void *);
2298 data = kzalloc(size, GFP_KERNEL);
2302 INIT_WORK(&data->work, perf_mmap_data_free_work);
2304 all_buf = vmalloc_user((nr_pages + 1) * PAGE_SIZE);
2308 data->user_page = all_buf;
2309 data->data_pages[0] = all_buf + PAGE_SIZE;
2310 data->data_order = ilog2(nr_pages);
2324 static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
2326 struct perf_event *event = vma->vm_file->private_data;
2327 struct perf_mmap_data *data;
2328 int ret = VM_FAULT_SIGBUS;
2330 if (vmf->flags & FAULT_FLAG_MKWRITE) {
2331 if (vmf->pgoff == 0)
2337 data = rcu_dereference(event->data);
2341 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
2344 vmf->page = perf_mmap_to_page(data, vmf->pgoff);
2348 get_page(vmf->page);
2349 vmf->page->mapping = vma->vm_file->f_mapping;
2350 vmf->page->index = vmf->pgoff;
2360 perf_mmap_data_init(struct perf_event *event, struct perf_mmap_data *data)
2362 long max_size = perf_data_size(data);
2364 atomic_set(&data->lock, -1);
2366 if (event->attr.watermark) {
2367 data->watermark = min_t(long, max_size,
2368 event->attr.wakeup_watermark);
2371 if (!data->watermark)
2372 data->watermark = max_size / 2;
2375 rcu_assign_pointer(event->data, data);
2378 static void perf_mmap_data_free_rcu(struct rcu_head *rcu_head)
2380 struct perf_mmap_data *data;
2382 data = container_of(rcu_head, struct perf_mmap_data, rcu_head);
2383 perf_mmap_data_free(data);
2386 static void perf_mmap_data_release(struct perf_event *event)
2388 struct perf_mmap_data *data = event->data;
2390 WARN_ON(atomic_read(&event->mmap_count));
2392 rcu_assign_pointer(event->data, NULL);
2393 call_rcu(&data->rcu_head, perf_mmap_data_free_rcu);
2396 static void perf_mmap_open(struct vm_area_struct *vma)
2398 struct perf_event *event = vma->vm_file->private_data;
2400 atomic_inc(&event->mmap_count);
2403 static void perf_mmap_close(struct vm_area_struct *vma)
2405 struct perf_event *event = vma->vm_file->private_data;
2407 WARN_ON_ONCE(event->ctx->parent_ctx);
2408 if (atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) {
2409 unsigned long size = perf_data_size(event->data);
2410 struct user_struct *user = current_user();
2412 atomic_long_sub((size >> PAGE_SHIFT) + 1, &user->locked_vm);
2413 vma->vm_mm->locked_vm -= event->data->nr_locked;
2414 perf_mmap_data_release(event);
2415 mutex_unlock(&event->mmap_mutex);
2419 static const struct vm_operations_struct perf_mmap_vmops = {
2420 .open = perf_mmap_open,
2421 .close = perf_mmap_close,
2422 .fault = perf_mmap_fault,
2423 .page_mkwrite = perf_mmap_fault,
2426 static int perf_mmap(struct file *file, struct vm_area_struct *vma)
2428 struct perf_event *event = file->private_data;
2429 unsigned long user_locked, user_lock_limit;
2430 struct user_struct *user = current_user();
2431 unsigned long locked, lock_limit;
2432 struct perf_mmap_data *data;
2433 unsigned long vma_size;
2434 unsigned long nr_pages;
2435 long user_extra, extra;
2438 if (!(vma->vm_flags & VM_SHARED))
2441 vma_size = vma->vm_end - vma->vm_start;
2442 nr_pages = (vma_size / PAGE_SIZE) - 1;
2445 * If we have data pages ensure they're a power-of-two number, so we
2446 * can do bitmasks instead of modulo.
2448 if (nr_pages != 0 && !is_power_of_2(nr_pages))
2451 if (vma_size != PAGE_SIZE * (1 + nr_pages))
2454 if (vma->vm_pgoff != 0)
2457 WARN_ON_ONCE(event->ctx->parent_ctx);
2458 mutex_lock(&event->mmap_mutex);
2459 if (event->output) {
2464 if (atomic_inc_not_zero(&event->mmap_count)) {
2465 if (nr_pages != event->data->nr_pages)
2470 user_extra = nr_pages + 1;
2471 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
2474 * Increase the limit linearly with more CPUs:
2476 user_lock_limit *= num_online_cpus();
2478 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
2481 if (user_locked > user_lock_limit)
2482 extra = user_locked - user_lock_limit;
2484 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
2485 lock_limit >>= PAGE_SHIFT;
2486 locked = vma->vm_mm->locked_vm + extra;
2488 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
2489 !capable(CAP_IPC_LOCK)) {
2494 WARN_ON(event->data);
2496 data = perf_mmap_data_alloc(event, nr_pages);
2502 perf_mmap_data_init(event, data);
2504 atomic_set(&event->mmap_count, 1);
2505 atomic_long_add(user_extra, &user->locked_vm);
2506 vma->vm_mm->locked_vm += extra;
2507 event->data->nr_locked = extra;
2508 if (vma->vm_flags & VM_WRITE)
2509 event->data->writable = 1;
2512 mutex_unlock(&event->mmap_mutex);
2514 vma->vm_flags |= VM_RESERVED;
2515 vma->vm_ops = &perf_mmap_vmops;
2520 static int perf_fasync(int fd, struct file *filp, int on)
2522 struct inode *inode = filp->f_path.dentry->d_inode;
2523 struct perf_event *event = filp->private_data;
2526 mutex_lock(&inode->i_mutex);
2527 retval = fasync_helper(fd, filp, on, &event->fasync);
2528 mutex_unlock(&inode->i_mutex);
2536 static const struct file_operations perf_fops = {
2537 .release = perf_release,
2540 .unlocked_ioctl = perf_ioctl,
2541 .compat_ioctl = perf_ioctl,
2543 .fasync = perf_fasync,
2549 * If there's data, ensure we set the poll() state and publish everything
2550 * to user-space before waking everybody up.
2553 void perf_event_wakeup(struct perf_event *event)
2555 wake_up_all(&event->waitq);
2557 if (event->pending_kill) {
2558 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
2559 event->pending_kill = 0;
2566 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
2568 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
2569 * single linked list and use cmpxchg() to add entries lockless.
2572 static void perf_pending_event(struct perf_pending_entry *entry)
2574 struct perf_event *event = container_of(entry,
2575 struct perf_event, pending);
2577 if (event->pending_disable) {
2578 event->pending_disable = 0;
2579 __perf_event_disable(event);
2582 if (event->pending_wakeup) {
2583 event->pending_wakeup = 0;
2584 perf_event_wakeup(event);
2588 #define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
2590 static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
2594 static void perf_pending_queue(struct perf_pending_entry *entry,
2595 void (*func)(struct perf_pending_entry *))
2597 struct perf_pending_entry **head;
2599 if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
2604 head = &get_cpu_var(perf_pending_head);
2607 entry->next = *head;
2608 } while (cmpxchg(head, entry->next, entry) != entry->next);
2610 set_perf_event_pending();
2612 put_cpu_var(perf_pending_head);
2615 static int __perf_pending_run(void)
2617 struct perf_pending_entry *list;
2620 list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
2621 while (list != PENDING_TAIL) {
2622 void (*func)(struct perf_pending_entry *);
2623 struct perf_pending_entry *entry = list;
2630 * Ensure we observe the unqueue before we issue the wakeup,
2631 * so that we won't be waiting forever.
2632 * -- see perf_not_pending().
2643 static inline int perf_not_pending(struct perf_event *event)
2646 * If we flush on whatever cpu we run, there is a chance we don't
2650 __perf_pending_run();
2654 * Ensure we see the proper queue state before going to sleep
2655 * so that we do not miss the wakeup. -- see perf_pending_handle()
2658 return event->pending.next == NULL;
2661 static void perf_pending_sync(struct perf_event *event)
2663 wait_event(event->waitq, perf_not_pending(event));
2666 void perf_event_do_pending(void)
2668 __perf_pending_run();
2672 * Callchain support -- arch specific
2675 __weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
2683 static bool perf_output_space(struct perf_mmap_data *data, unsigned long tail,
2684 unsigned long offset, unsigned long head)
2688 if (!data->writable)
2691 mask = perf_data_size(data) - 1;
2693 offset = (offset - tail) & mask;
2694 head = (head - tail) & mask;
2696 if ((int)(head - offset) < 0)
2702 static void perf_output_wakeup(struct perf_output_handle *handle)
2704 atomic_set(&handle->data->poll, POLL_IN);
2707 handle->event->pending_wakeup = 1;
2708 perf_pending_queue(&handle->event->pending,
2709 perf_pending_event);
2711 perf_event_wakeup(handle->event);
2715 * Curious locking construct.
2717 * We need to ensure a later event_id doesn't publish a head when a former
2718 * event_id isn't done writing. However since we need to deal with NMIs we
2719 * cannot fully serialize things.
2721 * What we do is serialize between CPUs so we only have to deal with NMI
2722 * nesting on a single CPU.
2724 * We only publish the head (and generate a wakeup) when the outer-most
2725 * event_id completes.
2727 static void perf_output_lock(struct perf_output_handle *handle)
2729 struct perf_mmap_data *data = handle->data;
2730 int cur, cpu = get_cpu();
2735 cur = atomic_cmpxchg(&data->lock, -1, cpu);
2747 static void perf_output_unlock(struct perf_output_handle *handle)
2749 struct perf_mmap_data *data = handle->data;
2753 data->done_head = data->head;
2755 if (!handle->locked)
2760 * The xchg implies a full barrier that ensures all writes are done
2761 * before we publish the new head, matched by a rmb() in userspace when
2762 * reading this position.
2764 while ((head = atomic_long_xchg(&data->done_head, 0)))
2765 data->user_page->data_head = head;
2768 * NMI can happen here, which means we can miss a done_head update.
2771 cpu = atomic_xchg(&data->lock, -1);
2772 WARN_ON_ONCE(cpu != smp_processor_id());
2775 * Therefore we have to validate we did not indeed do so.
2777 if (unlikely(atomic_long_read(&data->done_head))) {
2779 * Since we had it locked, we can lock it again.
2781 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
2787 if (atomic_xchg(&data->wakeup, 0))
2788 perf_output_wakeup(handle);
2793 void perf_output_copy(struct perf_output_handle *handle,
2794 const void *buf, unsigned int len)
2796 unsigned int pages_mask;
2797 unsigned long offset;
2801 offset = handle->offset;
2802 pages_mask = handle->data->nr_pages - 1;
2803 pages = handle->data->data_pages;
2806 unsigned long page_offset;
2807 unsigned long page_size;
2810 nr = (offset >> PAGE_SHIFT) & pages_mask;
2811 page_size = 1UL << (handle->data->data_order + PAGE_SHIFT);
2812 page_offset = offset & (page_size - 1);
2813 size = min_t(unsigned int, page_size - page_offset, len);
2815 memcpy(pages[nr] + page_offset, buf, size);
2822 handle->offset = offset;
2825 * Check we didn't copy past our reservation window, taking the
2826 * possible unsigned int wrap into account.
2828 WARN_ON_ONCE(((long)(handle->head - handle->offset)) < 0);
2831 int perf_output_begin(struct perf_output_handle *handle,
2832 struct perf_event *event, unsigned int size,
2833 int nmi, int sample)
2835 struct perf_event *output_event;
2836 struct perf_mmap_data *data;
2837 unsigned long tail, offset, head;
2840 struct perf_event_header header;
2847 * For inherited events we send all the output towards the parent.
2850 event = event->parent;
2852 output_event = rcu_dereference(event->output);
2854 event = output_event;
2856 data = rcu_dereference(event->data);
2860 handle->data = data;
2861 handle->event = event;
2863 handle->sample = sample;
2865 if (!data->nr_pages)
2868 have_lost = atomic_read(&data->lost);
2870 size += sizeof(lost_event);
2872 perf_output_lock(handle);
2876 * Userspace could choose to issue a mb() before updating the
2877 * tail pointer. So that all reads will be completed before the
2880 tail = ACCESS_ONCE(data->user_page->data_tail);
2882 offset = head = atomic_long_read(&data->head);
2884 if (unlikely(!perf_output_space(data, tail, offset, head)))
2886 } while (atomic_long_cmpxchg(&data->head, offset, head) != offset);
2888 handle->offset = offset;
2889 handle->head = head;
2891 if (head - tail > data->watermark)
2892 atomic_set(&data->wakeup, 1);
2895 lost_event.header.type = PERF_RECORD_LOST;
2896 lost_event.header.misc = 0;
2897 lost_event.header.size = sizeof(lost_event);
2898 lost_event.id = event->id;
2899 lost_event.lost = atomic_xchg(&data->lost, 0);
2901 perf_output_put(handle, lost_event);
2907 atomic_inc(&data->lost);
2908 perf_output_unlock(handle);
2915 void perf_output_end(struct perf_output_handle *handle)
2917 struct perf_event *event = handle->event;
2918 struct perf_mmap_data *data = handle->data;
2920 int wakeup_events = event->attr.wakeup_events;
2922 if (handle->sample && wakeup_events) {
2923 int events = atomic_inc_return(&data->events);
2924 if (events >= wakeup_events) {
2925 atomic_sub(wakeup_events, &data->events);
2926 atomic_set(&data->wakeup, 1);
2930 perf_output_unlock(handle);
2934 static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
2937 * only top level events have the pid namespace they were created in
2940 event = event->parent;
2942 return task_tgid_nr_ns(p, event->ns);
2945 static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
2948 * only top level events have the pid namespace they were created in
2951 event = event->parent;
2953 return task_pid_nr_ns(p, event->ns);
2956 static void perf_output_read_one(struct perf_output_handle *handle,
2957 struct perf_event *event)
2959 u64 read_format = event->attr.read_format;
2963 values[n++] = atomic64_read(&event->count);
2964 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
2965 values[n++] = event->total_time_enabled +
2966 atomic64_read(&event->child_total_time_enabled);
2968 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
2969 values[n++] = event->total_time_running +
2970 atomic64_read(&event->child_total_time_running);
2972 if (read_format & PERF_FORMAT_ID)
2973 values[n++] = primary_event_id(event);
2975 perf_output_copy(handle, values, n * sizeof(u64));
2979 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
2981 static void perf_output_read_group(struct perf_output_handle *handle,
2982 struct perf_event *event)
2984 struct perf_event *leader = event->group_leader, *sub;
2985 u64 read_format = event->attr.read_format;
2989 values[n++] = 1 + leader->nr_siblings;
2991 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2992 values[n++] = leader->total_time_enabled;
2994 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2995 values[n++] = leader->total_time_running;
2997 if (leader != event)
2998 leader->pmu->read(leader);
3000 values[n++] = atomic64_read(&leader->count);
3001 if (read_format & PERF_FORMAT_ID)
3002 values[n++] = primary_event_id(leader);
3004 perf_output_copy(handle, values, n * sizeof(u64));
3006 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
3010 sub->pmu->read(sub);
3012 values[n++] = atomic64_read(&sub->count);
3013 if (read_format & PERF_FORMAT_ID)
3014 values[n++] = primary_event_id(sub);
3016 perf_output_copy(handle, values, n * sizeof(u64));
3020 static void perf_output_read(struct perf_output_handle *handle,
3021 struct perf_event *event)
3023 if (event->attr.read_format & PERF_FORMAT_GROUP)
3024 perf_output_read_group(handle, event);
3026 perf_output_read_one(handle, event);
3029 void perf_output_sample(struct perf_output_handle *handle,
3030 struct perf_event_header *header,
3031 struct perf_sample_data *data,
3032 struct perf_event *event)
3034 u64 sample_type = data->type;
3036 perf_output_put(handle, *header);
3038 if (sample_type & PERF_SAMPLE_IP)
3039 perf_output_put(handle, data->ip);
3041 if (sample_type & PERF_SAMPLE_TID)
3042 perf_output_put(handle, data->tid_entry);
3044 if (sample_type & PERF_SAMPLE_TIME)
3045 perf_output_put(handle, data->time);
3047 if (sample_type & PERF_SAMPLE_ADDR)
3048 perf_output_put(handle, data->addr);
3050 if (sample_type & PERF_SAMPLE_ID)
3051 perf_output_put(handle, data->id);
3053 if (sample_type & PERF_SAMPLE_STREAM_ID)
3054 perf_output_put(handle, data->stream_id);
3056 if (sample_type & PERF_SAMPLE_CPU)
3057 perf_output_put(handle, data->cpu_entry);
3059 if (sample_type & PERF_SAMPLE_PERIOD)
3060 perf_output_put(handle, data->period);
3062 if (sample_type & PERF_SAMPLE_READ)
3063 perf_output_read(handle, event);
3065 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3066 if (data->callchain) {
3069 if (data->callchain)
3070 size += data->callchain->nr;
3072 size *= sizeof(u64);
3074 perf_output_copy(handle, data->callchain, size);
3077 perf_output_put(handle, nr);
3081 if (sample_type & PERF_SAMPLE_RAW) {
3083 perf_output_put(handle, data->raw->size);
3084 perf_output_copy(handle, data->raw->data,
3091 .size = sizeof(u32),
3094 perf_output_put(handle, raw);
3099 void perf_prepare_sample(struct perf_event_header *header,
3100 struct perf_sample_data *data,
3101 struct perf_event *event,
3102 struct pt_regs *regs)
3104 u64 sample_type = event->attr.sample_type;
3106 data->type = sample_type;
3108 header->type = PERF_RECORD_SAMPLE;
3109 header->size = sizeof(*header);
3112 header->misc |= perf_misc_flags(regs);
3114 if (sample_type & PERF_SAMPLE_IP) {
3115 data->ip = perf_instruction_pointer(regs);
3117 header->size += sizeof(data->ip);
3120 if (sample_type & PERF_SAMPLE_TID) {
3121 /* namespace issues */
3122 data->tid_entry.pid = perf_event_pid(event, current);
3123 data->tid_entry.tid = perf_event_tid(event, current);
3125 header->size += sizeof(data->tid_entry);
3128 if (sample_type & PERF_SAMPLE_TIME) {
3129 data->time = perf_clock();
3131 header->size += sizeof(data->time);
3134 if (sample_type & PERF_SAMPLE_ADDR)
3135 header->size += sizeof(data->addr);
3137 if (sample_type & PERF_SAMPLE_ID) {
3138 data->id = primary_event_id(event);
3140 header->size += sizeof(data->id);
3143 if (sample_type & PERF_SAMPLE_STREAM_ID) {
3144 data->stream_id = event->id;
3146 header->size += sizeof(data->stream_id);
3149 if (sample_type & PERF_SAMPLE_CPU) {
3150 data->cpu_entry.cpu = raw_smp_processor_id();
3151 data->cpu_entry.reserved = 0;
3153 header->size += sizeof(data->cpu_entry);
3156 if (sample_type & PERF_SAMPLE_PERIOD)
3157 header->size += sizeof(data->period);
3159 if (sample_type & PERF_SAMPLE_READ)
3160 header->size += perf_event_read_size(event);
3162 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3165 data->callchain = perf_callchain(regs);
3167 if (data->callchain)
3168 size += data->callchain->nr;
3170 header->size += size * sizeof(u64);
3173 if (sample_type & PERF_SAMPLE_RAW) {
3174 int size = sizeof(u32);
3177 size += data->raw->size;
3179 size += sizeof(u32);
3181 WARN_ON_ONCE(size & (sizeof(u64)-1));
3182 header->size += size;
3186 static void perf_event_output(struct perf_event *event, int nmi,
3187 struct perf_sample_data *data,
3188 struct pt_regs *regs)
3190 struct perf_output_handle handle;
3191 struct perf_event_header header;
3193 perf_prepare_sample(&header, data, event, regs);
3195 if (perf_output_begin(&handle, event, header.size, nmi, 1))
3198 perf_output_sample(&handle, &header, data, event);
3200 perf_output_end(&handle);
3207 struct perf_read_event {
3208 struct perf_event_header header;
3215 perf_event_read_event(struct perf_event *event,
3216 struct task_struct *task)
3218 struct perf_output_handle handle;
3219 struct perf_read_event read_event = {
3221 .type = PERF_RECORD_READ,
3223 .size = sizeof(read_event) + perf_event_read_size(event),
3225 .pid = perf_event_pid(event, task),
3226 .tid = perf_event_tid(event, task),
3230 ret = perf_output_begin(&handle, event, read_event.header.size, 0, 0);
3234 perf_output_put(&handle, read_event);
3235 perf_output_read(&handle, event);
3237 perf_output_end(&handle);
3241 * task tracking -- fork/exit
3243 * enabled by: attr.comm | attr.mmap | attr.task
3246 struct perf_task_event {
3247 struct task_struct *task;
3248 struct perf_event_context *task_ctx;
3251 struct perf_event_header header;
3261 static void perf_event_task_output(struct perf_event *event,
3262 struct perf_task_event *task_event)
3264 struct perf_output_handle handle;
3266 struct task_struct *task = task_event->task;
3269 size = task_event->event_id.header.size;
3270 ret = perf_output_begin(&handle, event, size, 0, 0);
3275 task_event->event_id.pid = perf_event_pid(event, task);
3276 task_event->event_id.ppid = perf_event_pid(event, current);
3278 task_event->event_id.tid = perf_event_tid(event, task);
3279 task_event->event_id.ptid = perf_event_tid(event, current);
3281 task_event->event_id.time = perf_clock();
3283 perf_output_put(&handle, task_event->event_id);
3285 perf_output_end(&handle);
3288 static int perf_event_task_match(struct perf_event *event)
3290 if (event->cpu != -1 && event->cpu != smp_processor_id())
3293 if (event->attr.comm || event->attr.mmap || event->attr.task)
3299 static void perf_event_task_ctx(struct perf_event_context *ctx,
3300 struct perf_task_event *task_event)
3302 struct perf_event *event;
3304 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3305 if (perf_event_task_match(event))
3306 perf_event_task_output(event, task_event);
3310 static void perf_event_task_event(struct perf_task_event *task_event)
3312 struct perf_cpu_context *cpuctx;
3313 struct perf_event_context *ctx = task_event->task_ctx;
3316 cpuctx = &get_cpu_var(perf_cpu_context);
3317 perf_event_task_ctx(&cpuctx->ctx, task_event);
3319 ctx = rcu_dereference(task_event->task->perf_event_ctxp);
3321 perf_event_task_ctx(ctx, task_event);
3322 put_cpu_var(perf_cpu_context);
3326 static void perf_event_task(struct task_struct *task,
3327 struct perf_event_context *task_ctx,
3330 struct perf_task_event task_event;
3332 if (!atomic_read(&nr_comm_events) &&
3333 !atomic_read(&nr_mmap_events) &&
3334 !atomic_read(&nr_task_events))
3337 task_event = (struct perf_task_event){
3339 .task_ctx = task_ctx,
3342 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
3344 .size = sizeof(task_event.event_id),
3353 perf_event_task_event(&task_event);
3356 void perf_event_fork(struct task_struct *task)
3358 perf_event_task(task, NULL, 1);
3365 struct perf_comm_event {
3366 struct task_struct *task;
3371 struct perf_event_header header;
3378 static void perf_event_comm_output(struct perf_event *event,
3379 struct perf_comm_event *comm_event)
3381 struct perf_output_handle handle;
3382 int size = comm_event->event_id.header.size;
3383 int ret = perf_output_begin(&handle, event, size, 0, 0);
3388 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
3389 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
3391 perf_output_put(&handle, comm_event->event_id);
3392 perf_output_copy(&handle, comm_event->comm,
3393 comm_event->comm_size);
3394 perf_output_end(&handle);
3397 static int perf_event_comm_match(struct perf_event *event)
3399 if (event->cpu != -1 && event->cpu != smp_processor_id())
3402 if (event->attr.comm)
3408 static void perf_event_comm_ctx(struct perf_event_context *ctx,
3409 struct perf_comm_event *comm_event)
3411 struct perf_event *event;
3413 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3414 if (perf_event_comm_match(event))
3415 perf_event_comm_output(event, comm_event);
3419 static void perf_event_comm_event(struct perf_comm_event *comm_event)
3421 struct perf_cpu_context *cpuctx;
3422 struct perf_event_context *ctx;
3424 char comm[TASK_COMM_LEN];
3426 memset(comm, 0, sizeof(comm));
3427 strlcpy(comm, comm_event->task->comm, sizeof(comm));
3428 size = ALIGN(strlen(comm)+1, sizeof(u64));
3430 comm_event->comm = comm;
3431 comm_event->comm_size = size;
3433 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
3436 cpuctx = &get_cpu_var(perf_cpu_context);
3437 perf_event_comm_ctx(&cpuctx->ctx, comm_event);
3438 ctx = rcu_dereference(current->perf_event_ctxp);
3440 perf_event_comm_ctx(ctx, comm_event);
3441 put_cpu_var(perf_cpu_context);
3445 void perf_event_comm(struct task_struct *task)
3447 struct perf_comm_event comm_event;
3449 if (task->perf_event_ctxp)
3450 perf_event_enable_on_exec(task);
3452 if (!atomic_read(&nr_comm_events))
3455 comm_event = (struct perf_comm_event){
3461 .type = PERF_RECORD_COMM,
3470 perf_event_comm_event(&comm_event);
3477 struct perf_mmap_event {
3478 struct vm_area_struct *vma;
3480 const char *file_name;
3484 struct perf_event_header header;
3494 static void perf_event_mmap_output(struct perf_event *event,
3495 struct perf_mmap_event *mmap_event)
3497 struct perf_output_handle handle;
3498 int size = mmap_event->event_id.header.size;
3499 int ret = perf_output_begin(&handle, event, size, 0, 0);
3504 mmap_event->event_id.pid = perf_event_pid(event, current);
3505 mmap_event->event_id.tid = perf_event_tid(event, current);
3507 perf_output_put(&handle, mmap_event->event_id);
3508 perf_output_copy(&handle, mmap_event->file_name,
3509 mmap_event->file_size);
3510 perf_output_end(&handle);
3513 static int perf_event_mmap_match(struct perf_event *event,
3514 struct perf_mmap_event *mmap_event)
3516 if (event->cpu != -1 && event->cpu != smp_processor_id())
3519 if (event->attr.mmap)
3525 static void perf_event_mmap_ctx(struct perf_event_context *ctx,
3526 struct perf_mmap_event *mmap_event)
3528 struct perf_event *event;
3530 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3531 if (perf_event_mmap_match(event, mmap_event))
3532 perf_event_mmap_output(event, mmap_event);
3536 static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
3538 struct perf_cpu_context *cpuctx;
3539 struct perf_event_context *ctx;
3540 struct vm_area_struct *vma = mmap_event->vma;
3541 struct file *file = vma->vm_file;
3547 memset(tmp, 0, sizeof(tmp));
3551 * d_path works from the end of the buffer backwards, so we
3552 * need to add enough zero bytes after the string to handle
3553 * the 64bit alignment we do later.
3555 buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
3557 name = strncpy(tmp, "//enomem", sizeof(tmp));
3560 name = d_path(&file->f_path, buf, PATH_MAX);
3562 name = strncpy(tmp, "//toolong", sizeof(tmp));
3566 if (arch_vma_name(mmap_event->vma)) {
3567 name = strncpy(tmp, arch_vma_name(mmap_event->vma),
3573 name = strncpy(tmp, "[vdso]", sizeof(tmp));
3577 name = strncpy(tmp, "//anon", sizeof(tmp));
3582 size = ALIGN(strlen(name)+1, sizeof(u64));
3584 mmap_event->file_name = name;
3585 mmap_event->file_size = size;
3587 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
3590 cpuctx = &get_cpu_var(perf_cpu_context);
3591 perf_event_mmap_ctx(&cpuctx->ctx, mmap_event);
3592 ctx = rcu_dereference(current->perf_event_ctxp);
3594 perf_event_mmap_ctx(ctx, mmap_event);
3595 put_cpu_var(perf_cpu_context);
3601 void __perf_event_mmap(struct vm_area_struct *vma)
3603 struct perf_mmap_event mmap_event;
3605 if (!atomic_read(&nr_mmap_events))
3608 mmap_event = (struct perf_mmap_event){
3614 .type = PERF_RECORD_MMAP,
3620 .start = vma->vm_start,
3621 .len = vma->vm_end - vma->vm_start,
3622 .pgoff = vma->vm_pgoff,
3626 perf_event_mmap_event(&mmap_event);
3630 * IRQ throttle logging
3633 static void perf_log_throttle(struct perf_event *event, int enable)
3635 struct perf_output_handle handle;
3639 struct perf_event_header header;
3643 } throttle_event = {
3645 .type = PERF_RECORD_THROTTLE,
3647 .size = sizeof(throttle_event),
3649 .time = perf_clock(),
3650 .id = primary_event_id(event),
3651 .stream_id = event->id,
3655 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
3657 ret = perf_output_begin(&handle, event, sizeof(throttle_event), 1, 0);
3661 perf_output_put(&handle, throttle_event);
3662 perf_output_end(&handle);
3666 * Generic event overflow handling, sampling.
3669 static int __perf_event_overflow(struct perf_event *event, int nmi,
3670 int throttle, struct perf_sample_data *data,
3671 struct pt_regs *regs)
3673 int events = atomic_read(&event->event_limit);
3674 struct hw_perf_event *hwc = &event->hw;
3677 throttle = (throttle && event->pmu->unthrottle != NULL);
3682 if (hwc->interrupts != MAX_INTERRUPTS) {
3684 if (HZ * hwc->interrupts >
3685 (u64)sysctl_perf_event_sample_rate) {
3686 hwc->interrupts = MAX_INTERRUPTS;
3687 perf_log_throttle(event, 0);
3692 * Keep re-disabling events even though on the previous
3693 * pass we disabled it - just in case we raced with a
3694 * sched-in and the event got enabled again:
3700 if (event->attr.freq) {
3701 u64 now = perf_clock();
3702 s64 delta = now - hwc->freq_stamp;
3704 hwc->freq_stamp = now;
3706 if (delta > 0 && delta < TICK_NSEC)
3707 perf_adjust_period(event, NSEC_PER_SEC / (int)delta);
3711 * XXX event_limit might not quite work as expected on inherited
3715 event->pending_kill = POLL_IN;
3716 if (events && atomic_dec_and_test(&event->event_limit)) {
3718 event->pending_kill = POLL_HUP;
3720 event->pending_disable = 1;
3721 perf_pending_queue(&event->pending,
3722 perf_pending_event);
3724 perf_event_disable(event);
3727 if (event->overflow_handler)
3728 event->overflow_handler(event, nmi, data, regs);
3730 perf_event_output(event, nmi, data, regs);
3735 int perf_event_overflow(struct perf_event *event, int nmi,
3736 struct perf_sample_data *data,
3737 struct pt_regs *regs)
3739 return __perf_event_overflow(event, nmi, 1, data, regs);
3743 * Generic software event infrastructure
3747 * We directly increment event->count and keep a second value in
3748 * event->hw.period_left to count intervals. This period event
3749 * is kept in the range [-sample_period, 0] so that we can use the
3753 static u64 perf_swevent_set_period(struct perf_event *event)
3755 struct hw_perf_event *hwc = &event->hw;
3756 u64 period = hwc->last_period;
3760 hwc->last_period = hwc->sample_period;
3763 old = val = atomic64_read(&hwc->period_left);
3767 nr = div64_u64(period + val, period);
3768 offset = nr * period;
3770 if (atomic64_cmpxchg(&hwc->period_left, old, val) != old)
3776 static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
3777 int nmi, struct perf_sample_data *data,
3778 struct pt_regs *regs)
3780 struct hw_perf_event *hwc = &event->hw;
3783 data->period = event->hw.last_period;
3785 overflow = perf_swevent_set_period(event);
3787 if (hwc->interrupts == MAX_INTERRUPTS)
3790 for (; overflow; overflow--) {
3791 if (__perf_event_overflow(event, nmi, throttle,
3794 * We inhibit the overflow from happening when
3795 * hwc->interrupts == MAX_INTERRUPTS.
3803 static void perf_swevent_unthrottle(struct perf_event *event)
3806 * Nothing to do, we already reset hwc->interrupts.
3810 static void perf_swevent_add(struct perf_event *event, u64 nr,
3811 int nmi, struct perf_sample_data *data,
3812 struct pt_regs *regs)
3814 struct hw_perf_event *hwc = &event->hw;
3816 atomic64_add(nr, &event->count);
3821 if (!hwc->sample_period)
3824 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
3825 return perf_swevent_overflow(event, 1, nmi, data, regs);
3827 if (atomic64_add_negative(nr, &hwc->period_left))
3830 perf_swevent_overflow(event, 0, nmi, data, regs);
3833 static int perf_swevent_is_counting(struct perf_event *event)
3836 * The event is active, we're good!
3838 if (event->state == PERF_EVENT_STATE_ACTIVE)
3842 * The event is off/error, not counting.
3844 if (event->state != PERF_EVENT_STATE_INACTIVE)
3848 * The event is inactive, if the context is active
3849 * we're part of a group that didn't make it on the 'pmu',
3852 if (event->ctx->is_active)
3856 * We're inactive and the context is too, this means the
3857 * task is scheduled out, we're counting events that happen
3858 * to us, like migration events.
3863 static int perf_tp_event_match(struct perf_event *event,
3864 struct perf_sample_data *data);
3866 static int perf_exclude_event(struct perf_event *event,
3867 struct pt_regs *regs)
3870 if (event->attr.exclude_user && user_mode(regs))
3873 if (event->attr.exclude_kernel && !user_mode(regs))
3880 static int perf_swevent_match(struct perf_event *event,
3881 enum perf_type_id type,
3883 struct perf_sample_data *data,
3884 struct pt_regs *regs)
3886 if (event->cpu != -1 && event->cpu != smp_processor_id())
3889 if (!perf_swevent_is_counting(event))
3892 if (event->attr.type != type)
3895 if (event->attr.config != event_id)
3898 if (perf_exclude_event(event, regs))
3901 if (event->attr.type == PERF_TYPE_TRACEPOINT &&
3902 !perf_tp_event_match(event, data))
3908 static void perf_swevent_ctx_event(struct perf_event_context *ctx,
3909 enum perf_type_id type,
3910 u32 event_id, u64 nr, int nmi,
3911 struct perf_sample_data *data,
3912 struct pt_regs *regs)
3914 struct perf_event *event;
3916 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3917 if (perf_swevent_match(event, type, event_id, data, regs))
3918 perf_swevent_add(event, nr, nmi, data, regs);
3922 int perf_swevent_get_recursion_context(void)
3924 struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
3931 else if (in_softirq())
3936 if (cpuctx->recursion[rctx]) {
3937 put_cpu_var(perf_cpu_context);
3941 cpuctx->recursion[rctx]++;
3946 EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
3948 void perf_swevent_put_recursion_context(int rctx)
3950 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
3952 cpuctx->recursion[rctx]--;
3953 put_cpu_var(perf_cpu_context);
3955 EXPORT_SYMBOL_GPL(perf_swevent_put_recursion_context);
3957 static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
3959 struct perf_sample_data *data,
3960 struct pt_regs *regs)
3962 struct perf_cpu_context *cpuctx;
3963 struct perf_event_context *ctx;
3965 cpuctx = &__get_cpu_var(perf_cpu_context);
3967 perf_swevent_ctx_event(&cpuctx->ctx, type, event_id,
3968 nr, nmi, data, regs);
3970 * doesn't really matter which of the child contexts the
3971 * events ends up in.
3973 ctx = rcu_dereference(current->perf_event_ctxp);
3975 perf_swevent_ctx_event(ctx, type, event_id, nr, nmi, data, regs);
3979 void __perf_sw_event(u32 event_id, u64 nr, int nmi,
3980 struct pt_regs *regs, u64 addr)
3982 struct perf_sample_data data;
3985 rctx = perf_swevent_get_recursion_context();
3992 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, nmi, &data, regs);
3994 perf_swevent_put_recursion_context(rctx);
3997 static void perf_swevent_read(struct perf_event *event)
4001 static int perf_swevent_enable(struct perf_event *event)
4003 struct hw_perf_event *hwc = &event->hw;
4005 if (hwc->sample_period) {
4006 hwc->last_period = hwc->sample_period;
4007 perf_swevent_set_period(event);
4012 static void perf_swevent_disable(struct perf_event *event)
4016 static const struct pmu perf_ops_generic = {
4017 .enable = perf_swevent_enable,
4018 .disable = perf_swevent_disable,
4019 .read = perf_swevent_read,
4020 .unthrottle = perf_swevent_unthrottle,
4024 * hrtimer based swevent callback
4027 static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
4029 enum hrtimer_restart ret = HRTIMER_RESTART;
4030 struct perf_sample_data data;
4031 struct pt_regs *regs;
4032 struct perf_event *event;
4035 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
4036 event->pmu->read(event);
4040 data.period = event->hw.last_period;
4041 regs = get_irq_regs();
4043 * In case we exclude kernel IPs or are somehow not in interrupt
4044 * context, provide the next best thing, the user IP.
4046 if ((event->attr.exclude_kernel || !regs) &&
4047 !event->attr.exclude_user)
4048 regs = task_pt_regs(current);
4051 if (!(event->attr.exclude_idle && current->pid == 0))
4052 if (perf_event_overflow(event, 0, &data, regs))
4053 ret = HRTIMER_NORESTART;
4056 period = max_t(u64, 10000, event->hw.sample_period);
4057 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
4062 static void perf_swevent_start_hrtimer(struct perf_event *event)
4064 struct hw_perf_event *hwc = &event->hw;
4066 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
4067 hwc->hrtimer.function = perf_swevent_hrtimer;
4068 if (hwc->sample_period) {
4071 if (hwc->remaining) {
4072 if (hwc->remaining < 0)
4075 period = hwc->remaining;
4078 period = max_t(u64, 10000, hwc->sample_period);
4080 __hrtimer_start_range_ns(&hwc->hrtimer,
4081 ns_to_ktime(period), 0,
4082 HRTIMER_MODE_REL, 0);
4086 static void perf_swevent_cancel_hrtimer(struct perf_event *event)
4088 struct hw_perf_event *hwc = &event->hw;
4090 if (hwc->sample_period) {
4091 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
4092 hwc->remaining = ktime_to_ns(remaining);
4094 hrtimer_cancel(&hwc->hrtimer);
4099 * Software event: cpu wall time clock
4102 static void cpu_clock_perf_event_update(struct perf_event *event)
4104 int cpu = raw_smp_processor_id();
4108 now = cpu_clock(cpu);
4109 prev = atomic64_xchg(&event->hw.prev_count, now);
4110 atomic64_add(now - prev, &event->count);
4113 static int cpu_clock_perf_event_enable(struct perf_event *event)
4115 struct hw_perf_event *hwc = &event->hw;
4116 int cpu = raw_smp_processor_id();
4118 atomic64_set(&hwc->prev_count, cpu_clock(cpu));
4119 perf_swevent_start_hrtimer(event);
4124 static void cpu_clock_perf_event_disable(struct perf_event *event)
4126 perf_swevent_cancel_hrtimer(event);
4127 cpu_clock_perf_event_update(event);
4130 static void cpu_clock_perf_event_read(struct perf_event *event)
4132 cpu_clock_perf_event_update(event);
4135 static const struct pmu perf_ops_cpu_clock = {
4136 .enable = cpu_clock_perf_event_enable,
4137 .disable = cpu_clock_perf_event_disable,
4138 .read = cpu_clock_perf_event_read,
4142 * Software event: task time clock
4145 static void task_clock_perf_event_update(struct perf_event *event, u64 now)
4150 prev = atomic64_xchg(&event->hw.prev_count, now);
4152 atomic64_add(delta, &event->count);
4155 static int task_clock_perf_event_enable(struct perf_event *event)
4157 struct hw_perf_event *hwc = &event->hw;
4160 now = event->ctx->time;
4162 atomic64_set(&hwc->prev_count, now);
4164 perf_swevent_start_hrtimer(event);
4169 static void task_clock_perf_event_disable(struct perf_event *event)
4171 perf_swevent_cancel_hrtimer(event);
4172 task_clock_perf_event_update(event, event->ctx->time);
4176 static void task_clock_perf_event_read(struct perf_event *event)
4181 update_context_time(event->ctx);
4182 time = event->ctx->time;
4184 u64 now = perf_clock();
4185 u64 delta = now - event->ctx->timestamp;
4186 time = event->ctx->time + delta;
4189 task_clock_perf_event_update(event, time);
4192 static const struct pmu perf_ops_task_clock = {
4193 .enable = task_clock_perf_event_enable,
4194 .disable = task_clock_perf_event_disable,
4195 .read = task_clock_perf_event_read,
4198 #ifdef CONFIG_EVENT_TRACING
4200 void perf_tp_event(int event_id, u64 addr, u64 count, void *record,
4203 struct perf_raw_record raw = {
4208 struct perf_sample_data data = {
4213 struct pt_regs *regs = get_irq_regs();
4216 regs = task_pt_regs(current);
4218 /* Trace events already protected against recursion */
4219 do_perf_sw_event(PERF_TYPE_TRACEPOINT, event_id, count, 1,
4222 EXPORT_SYMBOL_GPL(perf_tp_event);
4224 static int perf_tp_event_match(struct perf_event *event,
4225 struct perf_sample_data *data)
4227 void *record = data->raw->data;
4229 if (likely(!event->filter) || filter_match_preds(event->filter, record))
4234 static void tp_perf_event_destroy(struct perf_event *event)
4236 ftrace_profile_disable(event->attr.config);
4239 static const struct pmu *tp_perf_event_init(struct perf_event *event)
4242 * Raw tracepoint data is a severe data leak, only allow root to
4245 if ((event->attr.sample_type & PERF_SAMPLE_RAW) &&
4246 perf_paranoid_tracepoint_raw() &&
4247 !capable(CAP_SYS_ADMIN))
4248 return ERR_PTR(-EPERM);
4250 if (ftrace_profile_enable(event->attr.config))
4253 event->destroy = tp_perf_event_destroy;
4255 return &perf_ops_generic;
4258 static int perf_event_set_filter(struct perf_event *event, void __user *arg)
4263 if (event->attr.type != PERF_TYPE_TRACEPOINT)
4266 filter_str = strndup_user(arg, PAGE_SIZE);
4267 if (IS_ERR(filter_str))
4268 return PTR_ERR(filter_str);
4270 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
4276 static void perf_event_free_filter(struct perf_event *event)
4278 ftrace_profile_free_filter(event);
4283 static int perf_tp_event_match(struct perf_event *event,
4284 struct perf_sample_data *data)
4289 static const struct pmu *tp_perf_event_init(struct perf_event *event)
4294 static int perf_event_set_filter(struct perf_event *event, void __user *arg)
4299 static void perf_event_free_filter(struct perf_event *event)
4303 #endif /* CONFIG_EVENT_TRACING */
4305 #ifdef CONFIG_HAVE_HW_BREAKPOINT
4306 static void bp_perf_event_destroy(struct perf_event *event)
4308 release_bp_slot(event);
4311 static const struct pmu *bp_perf_event_init(struct perf_event *bp)
4315 err = register_perf_hw_breakpoint(bp);
4317 return ERR_PTR(err);
4319 bp->destroy = bp_perf_event_destroy;
4321 return &perf_ops_bp;
4324 void perf_bp_event(struct perf_event *bp, void *data)
4326 struct perf_sample_data sample;
4327 struct pt_regs *regs = data;
4330 sample.addr = bp->attr.bp_addr;
4332 if (!perf_exclude_event(bp, regs))
4333 perf_swevent_add(bp, 1, 1, &sample, regs);
4336 static const struct pmu *bp_perf_event_init(struct perf_event *bp)
4341 void perf_bp_event(struct perf_event *bp, void *regs)
4346 atomic_t perf_swevent_enabled[PERF_COUNT_SW_MAX];
4348 static void sw_perf_event_destroy(struct perf_event *event)
4350 u64 event_id = event->attr.config;
4352 WARN_ON(event->parent);
4354 atomic_dec(&perf_swevent_enabled[event_id]);
4357 static const struct pmu *sw_perf_event_init(struct perf_event *event)
4359 const struct pmu *pmu = NULL;
4360 u64 event_id = event->attr.config;
4363 * Software events (currently) can't in general distinguish
4364 * between user, kernel and hypervisor events.
4365 * However, context switches and cpu migrations are considered
4366 * to be kernel events, and page faults are never hypervisor
4370 case PERF_COUNT_SW_CPU_CLOCK:
4371 pmu = &perf_ops_cpu_clock;
4374 case PERF_COUNT_SW_TASK_CLOCK:
4376 * If the user instantiates this as a per-cpu event,
4377 * use the cpu_clock event instead.
4379 if (event->ctx->task)
4380 pmu = &perf_ops_task_clock;
4382 pmu = &perf_ops_cpu_clock;
4385 case PERF_COUNT_SW_PAGE_FAULTS:
4386 case PERF_COUNT_SW_PAGE_FAULTS_MIN:
4387 case PERF_COUNT_SW_PAGE_FAULTS_MAJ:
4388 case PERF_COUNT_SW_CONTEXT_SWITCHES:
4389 case PERF_COUNT_SW_CPU_MIGRATIONS:
4390 case PERF_COUNT_SW_ALIGNMENT_FAULTS:
4391 case PERF_COUNT_SW_EMULATION_FAULTS:
4392 if (!event->parent) {
4393 atomic_inc(&perf_swevent_enabled[event_id]);
4394 event->destroy = sw_perf_event_destroy;
4396 pmu = &perf_ops_generic;
4404 * Allocate and initialize a event structure
4406 static struct perf_event *
4407 perf_event_alloc(struct perf_event_attr *attr,
4409 struct perf_event_context *ctx,
4410 struct perf_event *group_leader,
4411 struct perf_event *parent_event,
4412 perf_overflow_handler_t overflow_handler,
4415 const struct pmu *pmu;
4416 struct perf_event *event;
4417 struct hw_perf_event *hwc;
4420 event = kzalloc(sizeof(*event), gfpflags);
4422 return ERR_PTR(-ENOMEM);
4425 * Single events are their own group leaders, with an
4426 * empty sibling list:
4429 group_leader = event;
4431 mutex_init(&event->child_mutex);
4432 INIT_LIST_HEAD(&event->child_list);
4434 INIT_LIST_HEAD(&event->group_entry);
4435 INIT_LIST_HEAD(&event->event_entry);
4436 INIT_LIST_HEAD(&event->sibling_list);
4437 init_waitqueue_head(&event->waitq);
4439 mutex_init(&event->mmap_mutex);
4442 event->attr = *attr;
4443 event->group_leader = group_leader;
4448 event->parent = parent_event;
4450 event->ns = get_pid_ns(current->nsproxy->pid_ns);
4451 event->id = atomic64_inc_return(&perf_event_id);
4453 event->state = PERF_EVENT_STATE_INACTIVE;
4455 if (!overflow_handler && parent_event)
4456 overflow_handler = parent_event->overflow_handler;
4458 event->overflow_handler = overflow_handler;
4461 event->state = PERF_EVENT_STATE_OFF;
4466 hwc->sample_period = attr->sample_period;
4467 if (attr->freq && attr->sample_freq)
4468 hwc->sample_period = 1;
4469 hwc->last_period = hwc->sample_period;
4471 atomic64_set(&hwc->period_left, hwc->sample_period);
4474 * we currently do not support PERF_FORMAT_GROUP on inherited events
4476 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
4479 switch (attr->type) {
4481 case PERF_TYPE_HARDWARE:
4482 case PERF_TYPE_HW_CACHE:
4483 pmu = hw_perf_event_init(event);
4486 case PERF_TYPE_SOFTWARE:
4487 pmu = sw_perf_event_init(event);
4490 case PERF_TYPE_TRACEPOINT:
4491 pmu = tp_perf_event_init(event);
4494 case PERF_TYPE_BREAKPOINT:
4495 pmu = bp_perf_event_init(event);
4506 else if (IS_ERR(pmu))
4511 put_pid_ns(event->ns);
4513 return ERR_PTR(err);
4518 if (!event->parent) {
4519 atomic_inc(&nr_events);
4520 if (event->attr.mmap)
4521 atomic_inc(&nr_mmap_events);
4522 if (event->attr.comm)
4523 atomic_inc(&nr_comm_events);
4524 if (event->attr.task)
4525 atomic_inc(&nr_task_events);
4531 static int perf_copy_attr(struct perf_event_attr __user *uattr,
4532 struct perf_event_attr *attr)
4537 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
4541 * zero the full structure, so that a short copy will be nice.
4543 memset(attr, 0, sizeof(*attr));
4545 ret = get_user(size, &uattr->size);
4549 if (size > PAGE_SIZE) /* silly large */
4552 if (!size) /* abi compat */
4553 size = PERF_ATTR_SIZE_VER0;
4555 if (size < PERF_ATTR_SIZE_VER0)
4559 * If we're handed a bigger struct than we know of,
4560 * ensure all the unknown bits are 0 - i.e. new
4561 * user-space does not rely on any kernel feature
4562 * extensions we dont know about yet.
4564 if (size > sizeof(*attr)) {
4565 unsigned char __user *addr;
4566 unsigned char __user *end;
4569 addr = (void __user *)uattr + sizeof(*attr);
4570 end = (void __user *)uattr + size;
4572 for (; addr < end; addr++) {
4573 ret = get_user(val, addr);
4579 size = sizeof(*attr);
4582 ret = copy_from_user(attr, uattr, size);
4587 * If the type exists, the corresponding creation will verify
4590 if (attr->type >= PERF_TYPE_MAX)
4593 if (attr->__reserved_1 || attr->__reserved_2)
4596 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
4599 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
4606 put_user(sizeof(*attr), &uattr->size);
4611 static int perf_event_set_output(struct perf_event *event, int output_fd)
4613 struct perf_event *output_event = NULL;
4614 struct file *output_file = NULL;
4615 struct perf_event *old_output;
4616 int fput_needed = 0;
4622 output_file = fget_light(output_fd, &fput_needed);
4626 if (output_file->f_op != &perf_fops)
4629 output_event = output_file->private_data;
4631 /* Don't chain output fds */
4632 if (output_event->output)
4635 /* Don't set an output fd when we already have an output channel */
4639 atomic_long_inc(&output_file->f_count);
4642 mutex_lock(&event->mmap_mutex);
4643 old_output = event->output;
4644 rcu_assign_pointer(event->output, output_event);
4645 mutex_unlock(&event->mmap_mutex);
4649 * we need to make sure no existing perf_output_*()
4650 * is still referencing this event.
4653 fput(old_output->filp);
4658 fput_light(output_file, fput_needed);
4663 * sys_perf_event_open - open a performance event, associate it to a task/cpu
4665 * @attr_uptr: event_id type attributes for monitoring/sampling
4668 * @group_fd: group leader event fd
4670 SYSCALL_DEFINE5(perf_event_open,
4671 struct perf_event_attr __user *, attr_uptr,
4672 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
4674 struct perf_event *event, *group_leader;
4675 struct perf_event_attr attr;
4676 struct perf_event_context *ctx;
4677 struct file *event_file = NULL;
4678 struct file *group_file = NULL;
4679 int fput_needed = 0;
4680 int fput_needed2 = 0;
4683 /* for future expandability... */
4684 if (flags & ~(PERF_FLAG_FD_NO_GROUP | PERF_FLAG_FD_OUTPUT))
4687 err = perf_copy_attr(attr_uptr, &attr);
4691 if (!attr.exclude_kernel) {
4692 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
4697 if (attr.sample_freq > sysctl_perf_event_sample_rate)
4702 * Get the target context (task or percpu):
4704 ctx = find_get_context(pid, cpu);
4706 return PTR_ERR(ctx);
4709 * Look up the group leader (we will attach this event to it):
4711 group_leader = NULL;
4712 if (group_fd != -1 && !(flags & PERF_FLAG_FD_NO_GROUP)) {
4714 group_file = fget_light(group_fd, &fput_needed);
4716 goto err_put_context;
4717 if (group_file->f_op != &perf_fops)
4718 goto err_put_context;
4720 group_leader = group_file->private_data;
4722 * Do not allow a recursive hierarchy (this new sibling
4723 * becoming part of another group-sibling):
4725 if (group_leader->group_leader != group_leader)
4726 goto err_put_context;
4728 * Do not allow to attach to a group in a different
4729 * task or CPU context:
4731 if (group_leader->ctx != ctx)
4732 goto err_put_context;
4734 * Only a group leader can be exclusive or pinned
4736 if (attr.exclusive || attr.pinned)
4737 goto err_put_context;
4740 event = perf_event_alloc(&attr, cpu, ctx, group_leader,
4741 NULL, NULL, GFP_KERNEL);
4742 err = PTR_ERR(event);
4744 goto err_put_context;
4746 err = anon_inode_getfd("[perf_event]", &perf_fops, event, O_RDWR);
4748 goto err_free_put_context;
4750 event_file = fget_light(err, &fput_needed2);
4752 goto err_free_put_context;
4754 if (flags & PERF_FLAG_FD_OUTPUT) {
4755 err = perf_event_set_output(event, group_fd);
4757 goto err_fput_free_put_context;
4760 event->filp = event_file;
4761 WARN_ON_ONCE(ctx->parent_ctx);
4762 mutex_lock(&ctx->mutex);
4763 perf_install_in_context(ctx, event, cpu);
4765 mutex_unlock(&ctx->mutex);
4767 event->owner = current;
4768 get_task_struct(current);
4769 mutex_lock(¤t->perf_event_mutex);
4770 list_add_tail(&event->owner_entry, ¤t->perf_event_list);
4771 mutex_unlock(¤t->perf_event_mutex);
4773 err_fput_free_put_context:
4774 fput_light(event_file, fput_needed2);
4776 err_free_put_context:
4784 fput_light(group_file, fput_needed);
4790 * perf_event_create_kernel_counter
4792 * @attr: attributes of the counter to create
4793 * @cpu: cpu in which the counter is bound
4794 * @pid: task to profile
4797 perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
4799 perf_overflow_handler_t overflow_handler)
4801 struct perf_event *event;
4802 struct perf_event_context *ctx;
4806 * Get the target context (task or percpu):
4809 ctx = find_get_context(pid, cpu);
4815 event = perf_event_alloc(attr, cpu, ctx, NULL,
4816 NULL, overflow_handler, GFP_KERNEL);
4817 if (IS_ERR(event)) {
4818 err = PTR_ERR(event);
4819 goto err_put_context;
4823 WARN_ON_ONCE(ctx->parent_ctx);
4824 mutex_lock(&ctx->mutex);
4825 perf_install_in_context(ctx, event, cpu);
4827 mutex_unlock(&ctx->mutex);
4829 event->owner = current;
4830 get_task_struct(current);
4831 mutex_lock(¤t->perf_event_mutex);
4832 list_add_tail(&event->owner_entry, ¤t->perf_event_list);
4833 mutex_unlock(¤t->perf_event_mutex);
4840 return ERR_PTR(err);
4842 EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
4845 * inherit a event from parent task to child task:
4847 static struct perf_event *
4848 inherit_event(struct perf_event *parent_event,
4849 struct task_struct *parent,
4850 struct perf_event_context *parent_ctx,
4851 struct task_struct *child,
4852 struct perf_event *group_leader,
4853 struct perf_event_context *child_ctx)
4855 struct perf_event *child_event;
4858 * Instead of creating recursive hierarchies of events,
4859 * we link inherited events back to the original parent,
4860 * which has a filp for sure, which we use as the reference
4863 if (parent_event->parent)
4864 parent_event = parent_event->parent;
4866 child_event = perf_event_alloc(&parent_event->attr,
4867 parent_event->cpu, child_ctx,
4868 group_leader, parent_event,
4870 if (IS_ERR(child_event))
4875 * Make the child state follow the state of the parent event,
4876 * not its attr.disabled bit. We hold the parent's mutex,
4877 * so we won't race with perf_event_{en, dis}able_family.
4879 if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
4880 child_event->state = PERF_EVENT_STATE_INACTIVE;
4882 child_event->state = PERF_EVENT_STATE_OFF;
4884 if (parent_event->attr.freq)
4885 child_event->hw.sample_period = parent_event->hw.sample_period;
4887 child_event->overflow_handler = parent_event->overflow_handler;
4890 * Link it up in the child's context:
4892 add_event_to_ctx(child_event, child_ctx);
4895 * Get a reference to the parent filp - we will fput it
4896 * when the child event exits. This is safe to do because
4897 * we are in the parent and we know that the filp still
4898 * exists and has a nonzero count:
4900 atomic_long_inc(&parent_event->filp->f_count);
4903 * Link this into the parent event's child list
4905 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
4906 mutex_lock(&parent_event->child_mutex);
4907 list_add_tail(&child_event->child_list, &parent_event->child_list);
4908 mutex_unlock(&parent_event->child_mutex);
4913 static int inherit_group(struct perf_event *parent_event,
4914 struct task_struct *parent,
4915 struct perf_event_context *parent_ctx,
4916 struct task_struct *child,
4917 struct perf_event_context *child_ctx)
4919 struct perf_event *leader;
4920 struct perf_event *sub;
4921 struct perf_event *child_ctr;
4923 leader = inherit_event(parent_event, parent, parent_ctx,
4924 child, NULL, child_ctx);
4926 return PTR_ERR(leader);
4927 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
4928 child_ctr = inherit_event(sub, parent, parent_ctx,
4929 child, leader, child_ctx);
4930 if (IS_ERR(child_ctr))
4931 return PTR_ERR(child_ctr);
4936 static void sync_child_event(struct perf_event *child_event,
4937 struct task_struct *child)
4939 struct perf_event *parent_event = child_event->parent;
4942 if (child_event->attr.inherit_stat)
4943 perf_event_read_event(child_event, child);
4945 child_val = atomic64_read(&child_event->count);
4948 * Add back the child's count to the parent's count:
4950 atomic64_add(child_val, &parent_event->count);
4951 atomic64_add(child_event->total_time_enabled,
4952 &parent_event->child_total_time_enabled);
4953 atomic64_add(child_event->total_time_running,
4954 &parent_event->child_total_time_running);
4957 * Remove this event from the parent's list
4959 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
4960 mutex_lock(&parent_event->child_mutex);
4961 list_del_init(&child_event->child_list);
4962 mutex_unlock(&parent_event->child_mutex);
4965 * Release the parent event, if this was the last
4968 fput(parent_event->filp);
4972 __perf_event_exit_task(struct perf_event *child_event,
4973 struct perf_event_context *child_ctx,
4974 struct task_struct *child)
4976 struct perf_event *parent_event;
4978 perf_event_remove_from_context(child_event);
4980 parent_event = child_event->parent;
4982 * It can happen that parent exits first, and has events
4983 * that are still around due to the child reference. These
4984 * events need to be zapped - but otherwise linger.
4987 sync_child_event(child_event, child);
4988 free_event(child_event);
4993 * When a child task exits, feed back event values to parent events.
4995 void perf_event_exit_task(struct task_struct *child)
4997 struct perf_event *child_event, *tmp;
4998 struct perf_event_context *child_ctx;
4999 unsigned long flags;
5001 if (likely(!child->perf_event_ctxp)) {
5002 perf_event_task(child, NULL, 0);
5006 local_irq_save(flags);
5008 * We can't reschedule here because interrupts are disabled,
5009 * and either child is current or it is a task that can't be
5010 * scheduled, so we are now safe from rescheduling changing
5013 child_ctx = child->perf_event_ctxp;
5014 __perf_event_task_sched_out(child_ctx);
5017 * Take the context lock here so that if find_get_context is
5018 * reading child->perf_event_ctxp, we wait until it has
5019 * incremented the context's refcount before we do put_ctx below.
5021 raw_spin_lock(&child_ctx->lock);
5022 child->perf_event_ctxp = NULL;
5024 * If this context is a clone; unclone it so it can't get
5025 * swapped to another process while we're removing all
5026 * the events from it.
5028 unclone_ctx(child_ctx);
5029 update_context_time(child_ctx);
5030 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
5033 * Report the task dead after unscheduling the events so that we
5034 * won't get any samples after PERF_RECORD_EXIT. We can however still
5035 * get a few PERF_RECORD_READ events.
5037 perf_event_task(child, child_ctx, 0);
5040 * We can recurse on the same lock type through:
5042 * __perf_event_exit_task()
5043 * sync_child_event()
5044 * fput(parent_event->filp)
5046 * mutex_lock(&ctx->mutex)
5048 * But since its the parent context it won't be the same instance.
5050 mutex_lock_nested(&child_ctx->mutex, SINGLE_DEPTH_NESTING);
5053 list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
5055 __perf_event_exit_task(child_event, child_ctx, child);
5057 list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
5059 __perf_event_exit_task(child_event, child_ctx, child);
5062 * If the last event was a group event, it will have appended all
5063 * its siblings to the list, but we obtained 'tmp' before that which
5064 * will still point to the list head terminating the iteration.
5066 if (!list_empty(&child_ctx->pinned_groups) ||
5067 !list_empty(&child_ctx->flexible_groups))
5070 mutex_unlock(&child_ctx->mutex);
5075 static void perf_free_event(struct perf_event *event,
5076 struct perf_event_context *ctx)
5078 struct perf_event *parent = event->parent;
5080 if (WARN_ON_ONCE(!parent))
5083 mutex_lock(&parent->child_mutex);
5084 list_del_init(&event->child_list);
5085 mutex_unlock(&parent->child_mutex);
5089 list_del_event(event, ctx);
5094 * free an unexposed, unused context as created by inheritance by
5095 * init_task below, used by fork() in case of fail.
5097 void perf_event_free_task(struct task_struct *task)
5099 struct perf_event_context *ctx = task->perf_event_ctxp;
5100 struct perf_event *event, *tmp;
5105 mutex_lock(&ctx->mutex);
5107 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
5108 perf_free_event(event, ctx);
5110 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
5112 perf_free_event(event, ctx);
5114 if (!list_empty(&ctx->pinned_groups) ||
5115 !list_empty(&ctx->flexible_groups))
5118 mutex_unlock(&ctx->mutex);
5124 inherit_task_group(struct perf_event *event, struct task_struct *parent,
5125 struct perf_event_context *parent_ctx,
5126 struct task_struct *child,
5130 struct perf_event_context *child_ctx = child->perf_event_ctxp;
5132 if (!event->attr.inherit) {
5139 * This is executed from the parent task context, so
5140 * inherit events that have been marked for cloning.
5141 * First allocate and initialize a context for the
5145 child_ctx = kzalloc(sizeof(struct perf_event_context),
5150 __perf_event_init_context(child_ctx, child);
5151 child->perf_event_ctxp = child_ctx;
5152 get_task_struct(child);
5155 ret = inherit_group(event, parent, parent_ctx,
5166 * Initialize the perf_event context in task_struct
5168 int perf_event_init_task(struct task_struct *child)
5170 struct perf_event_context *child_ctx, *parent_ctx;
5171 struct perf_event_context *cloned_ctx;
5172 struct perf_event *event;
5173 struct task_struct *parent = current;
5174 int inherited_all = 1;
5177 child->perf_event_ctxp = NULL;
5179 mutex_init(&child->perf_event_mutex);
5180 INIT_LIST_HEAD(&child->perf_event_list);
5182 if (likely(!parent->perf_event_ctxp))
5186 * If the parent's context is a clone, pin it so it won't get
5189 parent_ctx = perf_pin_task_context(parent);
5192 * No need to check if parent_ctx != NULL here; since we saw
5193 * it non-NULL earlier, the only reason for it to become NULL
5194 * is if we exit, and since we're currently in the middle of
5195 * a fork we can't be exiting at the same time.
5199 * Lock the parent list. No need to lock the child - not PID
5200 * hashed yet and not running, so nobody can access it.
5202 mutex_lock(&parent_ctx->mutex);
5205 * We dont have to disable NMIs - we are only looking at
5206 * the list, not manipulating it:
5208 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
5209 ret = inherit_task_group(event, parent, parent_ctx, child,
5215 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
5216 ret = inherit_task_group(event, parent, parent_ctx, child,
5222 child_ctx = child->perf_event_ctxp;
5224 if (child_ctx && inherited_all) {
5226 * Mark the child context as a clone of the parent
5227 * context, or of whatever the parent is a clone of.
5228 * Note that if the parent is a clone, it could get
5229 * uncloned at any point, but that doesn't matter
5230 * because the list of events and the generation
5231 * count can't have changed since we took the mutex.
5233 cloned_ctx = rcu_dereference(parent_ctx->parent_ctx);
5235 child_ctx->parent_ctx = cloned_ctx;
5236 child_ctx->parent_gen = parent_ctx->parent_gen;
5238 child_ctx->parent_ctx = parent_ctx;
5239 child_ctx->parent_gen = parent_ctx->generation;
5241 get_ctx(child_ctx->parent_ctx);
5244 mutex_unlock(&parent_ctx->mutex);
5246 perf_unpin_context(parent_ctx);
5251 static void __cpuinit perf_event_init_cpu(int cpu)
5253 struct perf_cpu_context *cpuctx;
5255 cpuctx = &per_cpu(perf_cpu_context, cpu);
5256 __perf_event_init_context(&cpuctx->ctx, NULL);
5258 spin_lock(&perf_resource_lock);
5259 cpuctx->max_pertask = perf_max_events - perf_reserved_percpu;
5260 spin_unlock(&perf_resource_lock);
5262 hw_perf_event_setup(cpu);
5265 #ifdef CONFIG_HOTPLUG_CPU
5266 static void __perf_event_exit_cpu(void *info)
5268 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
5269 struct perf_event_context *ctx = &cpuctx->ctx;
5270 struct perf_event *event, *tmp;
5272 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
5273 __perf_event_remove_from_context(event);
5274 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, group_entry)
5275 __perf_event_remove_from_context(event);
5277 static void perf_event_exit_cpu(int cpu)
5279 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
5280 struct perf_event_context *ctx = &cpuctx->ctx;
5282 mutex_lock(&ctx->mutex);
5283 smp_call_function_single(cpu, __perf_event_exit_cpu, NULL, 1);
5284 mutex_unlock(&ctx->mutex);
5287 static inline void perf_event_exit_cpu(int cpu) { }
5290 static int __cpuinit
5291 perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
5293 unsigned int cpu = (long)hcpu;
5297 case CPU_UP_PREPARE:
5298 case CPU_UP_PREPARE_FROZEN:
5299 perf_event_init_cpu(cpu);
5303 case CPU_ONLINE_FROZEN:
5304 hw_perf_event_setup_online(cpu);
5307 case CPU_DOWN_PREPARE:
5308 case CPU_DOWN_PREPARE_FROZEN:
5309 perf_event_exit_cpu(cpu);
5320 * This has to have a higher priority than migration_notifier in sched.c.
5322 static struct notifier_block __cpuinitdata perf_cpu_nb = {
5323 .notifier_call = perf_cpu_notify,
5327 void __init perf_event_init(void)
5329 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
5330 (void *)(long)smp_processor_id());
5331 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_ONLINE,
5332 (void *)(long)smp_processor_id());
5333 register_cpu_notifier(&perf_cpu_nb);
5336 static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
5338 return sprintf(buf, "%d\n", perf_reserved_percpu);
5342 perf_set_reserve_percpu(struct sysdev_class *class,
5346 struct perf_cpu_context *cpuctx;
5350 err = strict_strtoul(buf, 10, &val);
5353 if (val > perf_max_events)
5356 spin_lock(&perf_resource_lock);
5357 perf_reserved_percpu = val;
5358 for_each_online_cpu(cpu) {
5359 cpuctx = &per_cpu(perf_cpu_context, cpu);
5360 raw_spin_lock_irq(&cpuctx->ctx.lock);
5361 mpt = min(perf_max_events - cpuctx->ctx.nr_events,
5362 perf_max_events - perf_reserved_percpu);
5363 cpuctx->max_pertask = mpt;
5364 raw_spin_unlock_irq(&cpuctx->ctx.lock);
5366 spin_unlock(&perf_resource_lock);
5371 static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
5373 return sprintf(buf, "%d\n", perf_overcommit);
5377 perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
5382 err = strict_strtoul(buf, 10, &val);
5388 spin_lock(&perf_resource_lock);
5389 perf_overcommit = val;
5390 spin_unlock(&perf_resource_lock);
5395 static SYSDEV_CLASS_ATTR(
5398 perf_show_reserve_percpu,
5399 perf_set_reserve_percpu
5402 static SYSDEV_CLASS_ATTR(
5405 perf_show_overcommit,
5409 static struct attribute *perfclass_attrs[] = {
5410 &attr_reserve_percpu.attr,
5411 &attr_overcommit.attr,
5415 static struct attribute_group perfclass_attr_group = {
5416 .attrs = perfclass_attrs,
5417 .name = "perf_events",
5420 static int __init perf_event_sysfs_init(void)
5422 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
5423 &perfclass_attr_group);
5425 device_initcall(perf_event_sysfs_init);