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 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 spin_lock_irqsave(&ctx->lock, *flags);
207 if (ctx != rcu_dereference(task->perf_event_ctxp)) {
208 spin_unlock_irqrestore(&ctx->lock, *flags);
212 if (!atomic_inc_not_zero(&ctx->refcount)) {
213 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 spin_unlock_irqrestore(&ctx->lock, flags);
239 static void perf_unpin_context(struct perf_event_context *ctx)
243 spin_lock_irqsave(&ctx->lock, flags);
245 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)
277 event->total_time_enabled = ctx->time - event->tstamp_enabled;
279 if (event->state == PERF_EVENT_STATE_INACTIVE)
280 run_end = event->tstamp_stopped;
284 event->total_time_running = run_end - event->tstamp_running;
288 * Add a event from the lists for its context.
289 * Must be called with ctx->mutex and ctx->lock held.
292 list_add_event(struct perf_event *event, struct perf_event_context *ctx)
294 struct perf_event *group_leader = event->group_leader;
297 * Depending on whether it is a standalone or sibling event,
298 * add it straight to the context's event list, or to the group
299 * leader's sibling list:
301 if (group_leader == event)
302 list_add_tail(&event->group_entry, &ctx->group_list);
304 list_add_tail(&event->group_entry, &group_leader->sibling_list);
305 group_leader->nr_siblings++;
308 list_add_rcu(&event->event_entry, &ctx->event_list);
310 if (event->attr.inherit_stat)
315 * Remove a event from the lists for its context.
316 * Must be called with ctx->mutex and ctx->lock held.
319 list_del_event(struct perf_event *event, struct perf_event_context *ctx)
321 struct perf_event *sibling, *tmp;
323 if (list_empty(&event->group_entry))
326 if (event->attr.inherit_stat)
329 list_del_init(&event->group_entry);
330 list_del_rcu(&event->event_entry);
332 if (event->group_leader != event)
333 event->group_leader->nr_siblings--;
335 update_event_times(event);
336 event->state = PERF_EVENT_STATE_OFF;
339 * If this was a group event with sibling events then
340 * upgrade the siblings to singleton events by adding them
341 * to the context list directly:
343 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
345 list_move_tail(&sibling->group_entry, &ctx->group_list);
346 sibling->group_leader = sibling;
351 event_sched_out(struct perf_event *event,
352 struct perf_cpu_context *cpuctx,
353 struct perf_event_context *ctx)
355 if (event->state != PERF_EVENT_STATE_ACTIVE)
358 event->state = PERF_EVENT_STATE_INACTIVE;
359 if (event->pending_disable) {
360 event->pending_disable = 0;
361 event->state = PERF_EVENT_STATE_OFF;
363 event->tstamp_stopped = ctx->time;
364 event->pmu->disable(event);
367 if (!is_software_event(event))
368 cpuctx->active_oncpu--;
370 if (event->attr.exclusive || !cpuctx->active_oncpu)
371 cpuctx->exclusive = 0;
375 group_sched_out(struct perf_event *group_event,
376 struct perf_cpu_context *cpuctx,
377 struct perf_event_context *ctx)
379 struct perf_event *event;
381 if (group_event->state != PERF_EVENT_STATE_ACTIVE)
384 event_sched_out(group_event, cpuctx, ctx);
387 * Schedule out siblings (if any):
389 list_for_each_entry(event, &group_event->sibling_list, group_entry)
390 event_sched_out(event, cpuctx, ctx);
392 if (group_event->attr.exclusive)
393 cpuctx->exclusive = 0;
397 * Cross CPU call to remove a performance event
399 * We disable the event on the hardware level first. After that we
400 * remove it from the context list.
402 static void __perf_event_remove_from_context(void *info)
404 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
405 struct perf_event *event = info;
406 struct perf_event_context *ctx = event->ctx;
409 * If this is a task context, we need to check whether it is
410 * the current task context of this cpu. If not it has been
411 * scheduled out before the smp call arrived.
413 if (ctx->task && cpuctx->task_ctx != ctx)
416 spin_lock(&ctx->lock);
418 * Protect the list operation against NMI by disabling the
419 * events on a global level.
423 event_sched_out(event, cpuctx, ctx);
425 list_del_event(event, ctx);
429 * Allow more per task events with respect to the
432 cpuctx->max_pertask =
433 min(perf_max_events - ctx->nr_events,
434 perf_max_events - perf_reserved_percpu);
438 spin_unlock(&ctx->lock);
443 * Remove the event from a task's (or a CPU's) list of events.
445 * Must be called with ctx->mutex held.
447 * CPU events are removed with a smp call. For task events we only
448 * call when the task is on a CPU.
450 * If event->ctx is a cloned context, callers must make sure that
451 * every task struct that event->ctx->task could possibly point to
452 * remains valid. This is OK when called from perf_release since
453 * that only calls us on the top-level context, which can't be a clone.
454 * When called from perf_event_exit_task, it's OK because the
455 * context has been detached from its task.
457 static void perf_event_remove_from_context(struct perf_event *event)
459 struct perf_event_context *ctx = event->ctx;
460 struct task_struct *task = ctx->task;
464 * Per cpu events are removed via an smp call and
465 * the removal is always sucessful.
467 smp_call_function_single(event->cpu,
468 __perf_event_remove_from_context,
474 task_oncpu_function_call(task, __perf_event_remove_from_context,
477 spin_lock_irq(&ctx->lock);
479 * If the context is active we need to retry the smp call.
481 if (ctx->nr_active && !list_empty(&event->group_entry)) {
482 spin_unlock_irq(&ctx->lock);
487 * The lock prevents that this context is scheduled in so we
488 * can remove the event safely, if the call above did not
491 if (!list_empty(&event->group_entry))
492 list_del_event(event, ctx);
493 spin_unlock_irq(&ctx->lock);
497 * Update total_time_enabled and total_time_running for all events in a group.
499 static void update_group_times(struct perf_event *leader)
501 struct perf_event *event;
503 update_event_times(leader);
504 list_for_each_entry(event, &leader->sibling_list, group_entry)
505 update_event_times(event);
509 * Cross CPU call to disable a performance event
511 static void __perf_event_disable(void *info)
513 struct perf_event *event = info;
514 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
515 struct perf_event_context *ctx = event->ctx;
518 * If this is a per-task event, need to check whether this
519 * event's task is the current task on this cpu.
521 if (ctx->task && cpuctx->task_ctx != ctx)
524 spin_lock(&ctx->lock);
527 * If the event is on, turn it off.
528 * If it is in error state, leave it in error state.
530 if (event->state >= PERF_EVENT_STATE_INACTIVE) {
531 update_context_time(ctx);
532 update_group_times(event);
533 if (event == event->group_leader)
534 group_sched_out(event, cpuctx, ctx);
536 event_sched_out(event, cpuctx, ctx);
537 event->state = PERF_EVENT_STATE_OFF;
540 spin_unlock(&ctx->lock);
546 * If event->ctx is a cloned context, callers must make sure that
547 * every task struct that event->ctx->task could possibly point to
548 * remains valid. This condition is satisifed when called through
549 * perf_event_for_each_child or perf_event_for_each because they
550 * hold the top-level event's child_mutex, so any descendant that
551 * goes to exit will block in sync_child_event.
552 * When called from perf_pending_event it's OK because event->ctx
553 * is the current context on this CPU and preemption is disabled,
554 * hence we can't get into perf_event_task_sched_out for this context.
556 static void perf_event_disable(struct perf_event *event)
558 struct perf_event_context *ctx = event->ctx;
559 struct task_struct *task = ctx->task;
563 * Disable the event on the cpu that it's on
565 smp_call_function_single(event->cpu, __perf_event_disable,
571 task_oncpu_function_call(task, __perf_event_disable, event);
573 spin_lock_irq(&ctx->lock);
575 * If the event is still active, we need to retry the cross-call.
577 if (event->state == PERF_EVENT_STATE_ACTIVE) {
578 spin_unlock_irq(&ctx->lock);
583 * Since we have the lock this context can't be scheduled
584 * in, so we can change the state safely.
586 if (event->state == PERF_EVENT_STATE_INACTIVE) {
587 update_group_times(event);
588 event->state = PERF_EVENT_STATE_OFF;
591 spin_unlock_irq(&ctx->lock);
595 event_sched_in(struct perf_event *event,
596 struct perf_cpu_context *cpuctx,
597 struct perf_event_context *ctx,
600 if (event->state <= PERF_EVENT_STATE_OFF)
603 event->state = PERF_EVENT_STATE_ACTIVE;
604 event->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
606 * The new state must be visible before we turn it on in the hardware:
610 if (event->pmu->enable(event)) {
611 event->state = PERF_EVENT_STATE_INACTIVE;
616 event->tstamp_running += ctx->time - event->tstamp_stopped;
618 if (!is_software_event(event))
619 cpuctx->active_oncpu++;
622 if (event->attr.exclusive)
623 cpuctx->exclusive = 1;
629 group_sched_in(struct perf_event *group_event,
630 struct perf_cpu_context *cpuctx,
631 struct perf_event_context *ctx,
634 struct perf_event *event, *partial_group;
637 if (group_event->state == PERF_EVENT_STATE_OFF)
640 ret = hw_perf_group_sched_in(group_event, cpuctx, ctx, cpu);
642 return ret < 0 ? ret : 0;
644 if (event_sched_in(group_event, cpuctx, ctx, cpu))
648 * Schedule in siblings as one group (if any):
650 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
651 if (event_sched_in(event, cpuctx, ctx, cpu)) {
652 partial_group = event;
661 * Groups can be scheduled in as one unit only, so undo any
662 * partial group before returning:
664 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
665 if (event == partial_group)
667 event_sched_out(event, cpuctx, ctx);
669 event_sched_out(group_event, cpuctx, ctx);
675 * Return 1 for a group consisting entirely of software events,
676 * 0 if the group contains any hardware events.
678 static int is_software_only_group(struct perf_event *leader)
680 struct perf_event *event;
682 if (!is_software_event(leader))
685 list_for_each_entry(event, &leader->sibling_list, group_entry)
686 if (!is_software_event(event))
693 * Work out whether we can put this event group on the CPU now.
695 static int group_can_go_on(struct perf_event *event,
696 struct perf_cpu_context *cpuctx,
700 * Groups consisting entirely of software events can always go on.
702 if (is_software_only_group(event))
705 * If an exclusive group is already on, no other hardware
708 if (cpuctx->exclusive)
711 * If this group is exclusive and there are already
712 * events on the CPU, it can't go on.
714 if (event->attr.exclusive && cpuctx->active_oncpu)
717 * Otherwise, try to add it if all previous groups were able
723 static void add_event_to_ctx(struct perf_event *event,
724 struct perf_event_context *ctx)
726 list_add_event(event, ctx);
727 event->tstamp_enabled = ctx->time;
728 event->tstamp_running = ctx->time;
729 event->tstamp_stopped = ctx->time;
733 * Cross CPU call to install and enable a performance event
735 * Must be called with ctx->mutex held
737 static void __perf_install_in_context(void *info)
739 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
740 struct perf_event *event = info;
741 struct perf_event_context *ctx = event->ctx;
742 struct perf_event *leader = event->group_leader;
743 int cpu = smp_processor_id();
747 * If this is a task context, we need to check whether it is
748 * the current task context of this cpu. If not it has been
749 * scheduled out before the smp call arrived.
750 * Or possibly this is the right context but it isn't
751 * on this cpu because it had no events.
753 if (ctx->task && cpuctx->task_ctx != ctx) {
754 if (cpuctx->task_ctx || ctx->task != current)
756 cpuctx->task_ctx = ctx;
759 spin_lock(&ctx->lock);
761 update_context_time(ctx);
764 * Protect the list operation against NMI by disabling the
765 * events on a global level. NOP for non NMI based events.
769 add_event_to_ctx(event, ctx);
772 * Don't put the event on if it is disabled or if
773 * it is in a group and the group isn't on.
775 if (event->state != PERF_EVENT_STATE_INACTIVE ||
776 (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE))
780 * An exclusive event can't go on if there are already active
781 * hardware events, and no hardware event can go on if there
782 * is already an exclusive event on.
784 if (!group_can_go_on(event, cpuctx, 1))
787 err = event_sched_in(event, cpuctx, ctx, cpu);
791 * This event couldn't go on. If it is in a group
792 * then we have to pull the whole group off.
793 * If the event group is pinned then put it in error state.
796 group_sched_out(leader, cpuctx, ctx);
797 if (leader->attr.pinned) {
798 update_group_times(leader);
799 leader->state = PERF_EVENT_STATE_ERROR;
803 if (!err && !ctx->task && cpuctx->max_pertask)
804 cpuctx->max_pertask--;
809 spin_unlock(&ctx->lock);
813 * Attach a performance event to a context
815 * First we add the event to the list with the hardware enable bit
816 * in event->hw_config cleared.
818 * If the event is attached to a task which is on a CPU we use a smp
819 * call to enable it in the task context. The task might have been
820 * scheduled away, but we check this in the smp call again.
822 * Must be called with ctx->mutex held.
825 perf_install_in_context(struct perf_event_context *ctx,
826 struct perf_event *event,
829 struct task_struct *task = ctx->task;
833 * Per cpu events are installed via an smp call and
834 * the install is always sucessful.
836 smp_call_function_single(cpu, __perf_install_in_context,
842 task_oncpu_function_call(task, __perf_install_in_context,
845 spin_lock_irq(&ctx->lock);
847 * we need to retry the smp call.
849 if (ctx->is_active && list_empty(&event->group_entry)) {
850 spin_unlock_irq(&ctx->lock);
855 * The lock prevents that this context is scheduled in so we
856 * can add the event safely, if it the call above did not
859 if (list_empty(&event->group_entry))
860 add_event_to_ctx(event, ctx);
861 spin_unlock_irq(&ctx->lock);
865 * Put a event into inactive state and update time fields.
866 * Enabling the leader of a group effectively enables all
867 * the group members that aren't explicitly disabled, so we
868 * have to update their ->tstamp_enabled also.
869 * Note: this works for group members as well as group leaders
870 * since the non-leader members' sibling_lists will be empty.
872 static void __perf_event_mark_enabled(struct perf_event *event,
873 struct perf_event_context *ctx)
875 struct perf_event *sub;
877 event->state = PERF_EVENT_STATE_INACTIVE;
878 event->tstamp_enabled = ctx->time - event->total_time_enabled;
879 list_for_each_entry(sub, &event->sibling_list, group_entry)
880 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
881 sub->tstamp_enabled =
882 ctx->time - sub->total_time_enabled;
886 * Cross CPU call to enable a performance event
888 static void __perf_event_enable(void *info)
890 struct perf_event *event = info;
891 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
892 struct perf_event_context *ctx = event->ctx;
893 struct perf_event *leader = event->group_leader;
897 * If this is a per-task event, need to check whether this
898 * event's task is the current task on this cpu.
900 if (ctx->task && cpuctx->task_ctx != ctx) {
901 if (cpuctx->task_ctx || ctx->task != current)
903 cpuctx->task_ctx = ctx;
906 spin_lock(&ctx->lock);
908 update_context_time(ctx);
910 if (event->state >= PERF_EVENT_STATE_INACTIVE)
912 __perf_event_mark_enabled(event, ctx);
915 * If the event is in a group and isn't the group leader,
916 * then don't put it on unless the group is on.
918 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
921 if (!group_can_go_on(event, cpuctx, 1)) {
926 err = group_sched_in(event, cpuctx, ctx,
929 err = event_sched_in(event, cpuctx, ctx,
936 * If this event can't go on and it's part of a
937 * group, then the whole group has to come off.
940 group_sched_out(leader, cpuctx, ctx);
941 if (leader->attr.pinned) {
942 update_group_times(leader);
943 leader->state = PERF_EVENT_STATE_ERROR;
948 spin_unlock(&ctx->lock);
954 * If event->ctx is a cloned context, callers must make sure that
955 * every task struct that event->ctx->task could possibly point to
956 * remains valid. This condition is satisfied when called through
957 * perf_event_for_each_child or perf_event_for_each as described
958 * for perf_event_disable.
960 static void perf_event_enable(struct perf_event *event)
962 struct perf_event_context *ctx = event->ctx;
963 struct task_struct *task = ctx->task;
967 * Enable the event on the cpu that it's on
969 smp_call_function_single(event->cpu, __perf_event_enable,
974 spin_lock_irq(&ctx->lock);
975 if (event->state >= PERF_EVENT_STATE_INACTIVE)
979 * If the event is in error state, clear that first.
980 * That way, if we see the event in error state below, we
981 * know that it has gone back into error state, as distinct
982 * from the task having been scheduled away before the
983 * cross-call arrived.
985 if (event->state == PERF_EVENT_STATE_ERROR)
986 event->state = PERF_EVENT_STATE_OFF;
989 spin_unlock_irq(&ctx->lock);
990 task_oncpu_function_call(task, __perf_event_enable, event);
992 spin_lock_irq(&ctx->lock);
995 * If the context is active and the event is still off,
996 * we need to retry the cross-call.
998 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF)
1002 * Since we have the lock this context can't be scheduled
1003 * in, so we can change the state safely.
1005 if (event->state == PERF_EVENT_STATE_OFF)
1006 __perf_event_mark_enabled(event, ctx);
1009 spin_unlock_irq(&ctx->lock);
1012 static int perf_event_refresh(struct perf_event *event, int refresh)
1015 * not supported on inherited events
1017 if (event->attr.inherit)
1020 atomic_add(refresh, &event->event_limit);
1021 perf_event_enable(event);
1026 void __perf_event_sched_out(struct perf_event_context *ctx,
1027 struct perf_cpu_context *cpuctx)
1029 struct perf_event *event;
1031 spin_lock(&ctx->lock);
1033 if (likely(!ctx->nr_events))
1035 update_context_time(ctx);
1038 if (ctx->nr_active) {
1039 list_for_each_entry(event, &ctx->group_list, group_entry)
1040 group_sched_out(event, cpuctx, ctx);
1044 spin_unlock(&ctx->lock);
1048 * Test whether two contexts are equivalent, i.e. whether they
1049 * have both been cloned from the same version of the same context
1050 * and they both have the same number of enabled events.
1051 * If the number of enabled events is the same, then the set
1052 * of enabled events should be the same, because these are both
1053 * inherited contexts, therefore we can't access individual events
1054 * in them directly with an fd; we can only enable/disable all
1055 * events via prctl, or enable/disable all events in a family
1056 * via ioctl, which will have the same effect on both contexts.
1058 static int context_equiv(struct perf_event_context *ctx1,
1059 struct perf_event_context *ctx2)
1061 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
1062 && ctx1->parent_gen == ctx2->parent_gen
1063 && !ctx1->pin_count && !ctx2->pin_count;
1066 static void __perf_event_sync_stat(struct perf_event *event,
1067 struct perf_event *next_event)
1071 if (!event->attr.inherit_stat)
1075 * Update the event value, we cannot use perf_event_read()
1076 * because we're in the middle of a context switch and have IRQs
1077 * disabled, which upsets smp_call_function_single(), however
1078 * we know the event must be on the current CPU, therefore we
1079 * don't need to use it.
1081 switch (event->state) {
1082 case PERF_EVENT_STATE_ACTIVE:
1083 event->pmu->read(event);
1086 case PERF_EVENT_STATE_INACTIVE:
1087 update_event_times(event);
1095 * In order to keep per-task stats reliable we need to flip the event
1096 * values when we flip the contexts.
1098 value = atomic64_read(&next_event->count);
1099 value = atomic64_xchg(&event->count, value);
1100 atomic64_set(&next_event->count, value);
1102 swap(event->total_time_enabled, next_event->total_time_enabled);
1103 swap(event->total_time_running, next_event->total_time_running);
1106 * Since we swizzled the values, update the user visible data too.
1108 perf_event_update_userpage(event);
1109 perf_event_update_userpage(next_event);
1112 #define list_next_entry(pos, member) \
1113 list_entry(pos->member.next, typeof(*pos), member)
1115 static void perf_event_sync_stat(struct perf_event_context *ctx,
1116 struct perf_event_context *next_ctx)
1118 struct perf_event *event, *next_event;
1123 update_context_time(ctx);
1125 event = list_first_entry(&ctx->event_list,
1126 struct perf_event, event_entry);
1128 next_event = list_first_entry(&next_ctx->event_list,
1129 struct perf_event, event_entry);
1131 while (&event->event_entry != &ctx->event_list &&
1132 &next_event->event_entry != &next_ctx->event_list) {
1134 __perf_event_sync_stat(event, next_event);
1136 event = list_next_entry(event, event_entry);
1137 next_event = list_next_entry(next_event, event_entry);
1142 * Called from scheduler to remove the events of the current task,
1143 * with interrupts disabled.
1145 * We stop each event and update the event value in event->count.
1147 * This does not protect us against NMI, but disable()
1148 * sets the disabled bit in the control field of event _before_
1149 * accessing the event control register. If a NMI hits, then it will
1150 * not restart the event.
1152 void perf_event_task_sched_out(struct task_struct *task,
1153 struct task_struct *next, int cpu)
1155 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
1156 struct perf_event_context *ctx = task->perf_event_ctxp;
1157 struct perf_event_context *next_ctx;
1158 struct perf_event_context *parent;
1159 struct pt_regs *regs;
1162 regs = task_pt_regs(task);
1163 perf_sw_event(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 1, regs, 0);
1165 if (likely(!ctx || !cpuctx->task_ctx))
1169 parent = rcu_dereference(ctx->parent_ctx);
1170 next_ctx = next->perf_event_ctxp;
1171 if (parent && next_ctx &&
1172 rcu_dereference(next_ctx->parent_ctx) == parent) {
1174 * Looks like the two contexts are clones, so we might be
1175 * able to optimize the context switch. We lock both
1176 * contexts and check that they are clones under the
1177 * lock (including re-checking that neither has been
1178 * uncloned in the meantime). It doesn't matter which
1179 * order we take the locks because no other cpu could
1180 * be trying to lock both of these tasks.
1182 spin_lock(&ctx->lock);
1183 spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
1184 if (context_equiv(ctx, next_ctx)) {
1186 * XXX do we need a memory barrier of sorts
1187 * wrt to rcu_dereference() of perf_event_ctxp
1189 task->perf_event_ctxp = next_ctx;
1190 next->perf_event_ctxp = ctx;
1192 next_ctx->task = task;
1195 perf_event_sync_stat(ctx, next_ctx);
1197 spin_unlock(&next_ctx->lock);
1198 spin_unlock(&ctx->lock);
1203 __perf_event_sched_out(ctx, cpuctx);
1204 cpuctx->task_ctx = NULL;
1209 * Called with IRQs disabled
1211 static void __perf_event_task_sched_out(struct perf_event_context *ctx)
1213 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1215 if (!cpuctx->task_ctx)
1218 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
1221 __perf_event_sched_out(ctx, cpuctx);
1222 cpuctx->task_ctx = NULL;
1226 * Called with IRQs disabled
1228 static void perf_event_cpu_sched_out(struct perf_cpu_context *cpuctx)
1230 __perf_event_sched_out(&cpuctx->ctx, cpuctx);
1234 __perf_event_sched_in(struct perf_event_context *ctx,
1235 struct perf_cpu_context *cpuctx, int cpu)
1237 struct perf_event *event;
1240 spin_lock(&ctx->lock);
1242 if (likely(!ctx->nr_events))
1245 ctx->timestamp = perf_clock();
1250 * First go through the list and put on any pinned groups
1251 * in order to give them the best chance of going on.
1253 list_for_each_entry(event, &ctx->group_list, group_entry) {
1254 if (event->state <= PERF_EVENT_STATE_OFF ||
1255 !event->attr.pinned)
1257 if (event->cpu != -1 && event->cpu != cpu)
1260 if (group_can_go_on(event, cpuctx, 1))
1261 group_sched_in(event, cpuctx, ctx, cpu);
1264 * If this pinned group hasn't been scheduled,
1265 * put it in error state.
1267 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1268 update_group_times(event);
1269 event->state = PERF_EVENT_STATE_ERROR;
1273 list_for_each_entry(event, &ctx->group_list, group_entry) {
1275 * Ignore events in OFF or ERROR state, and
1276 * ignore pinned events since we did them already.
1278 if (event->state <= PERF_EVENT_STATE_OFF ||
1283 * Listen to the 'cpu' scheduling filter constraint
1286 if (event->cpu != -1 && event->cpu != cpu)
1289 if (group_can_go_on(event, cpuctx, can_add_hw))
1290 if (group_sched_in(event, cpuctx, ctx, cpu))
1295 spin_unlock(&ctx->lock);
1299 * Called from scheduler to add the events of the current task
1300 * with interrupts disabled.
1302 * We restore the event value and then enable it.
1304 * This does not protect us against NMI, but enable()
1305 * sets the enabled bit in the control field of event _before_
1306 * accessing the event control register. If a NMI hits, then it will
1307 * keep the event running.
1309 void perf_event_task_sched_in(struct task_struct *task, int cpu)
1311 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
1312 struct perf_event_context *ctx = task->perf_event_ctxp;
1316 if (cpuctx->task_ctx == ctx)
1318 __perf_event_sched_in(ctx, cpuctx, cpu);
1319 cpuctx->task_ctx = ctx;
1322 static void perf_event_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu)
1324 struct perf_event_context *ctx = &cpuctx->ctx;
1326 __perf_event_sched_in(ctx, cpuctx, cpu);
1329 #define MAX_INTERRUPTS (~0ULL)
1331 static void perf_log_throttle(struct perf_event *event, int enable);
1333 static void perf_adjust_period(struct perf_event *event, u64 events)
1335 struct hw_perf_event *hwc = &event->hw;
1336 u64 period, sample_period;
1339 events *= hwc->sample_period;
1340 period = div64_u64(events, event->attr.sample_freq);
1342 delta = (s64)(period - hwc->sample_period);
1343 delta = (delta + 7) / 8; /* low pass filter */
1345 sample_period = hwc->sample_period + delta;
1350 hwc->sample_period = sample_period;
1353 static void perf_ctx_adjust_freq(struct perf_event_context *ctx)
1355 struct perf_event *event;
1356 struct hw_perf_event *hwc;
1357 u64 interrupts, freq;
1359 spin_lock(&ctx->lock);
1360 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
1361 if (event->state != PERF_EVENT_STATE_ACTIVE)
1366 interrupts = hwc->interrupts;
1367 hwc->interrupts = 0;
1370 * unthrottle events on the tick
1372 if (interrupts == MAX_INTERRUPTS) {
1373 perf_log_throttle(event, 1);
1374 event->pmu->unthrottle(event);
1375 interrupts = 2*sysctl_perf_event_sample_rate/HZ;
1378 if (!event->attr.freq || !event->attr.sample_freq)
1382 * if the specified freq < HZ then we need to skip ticks
1384 if (event->attr.sample_freq < HZ) {
1385 freq = event->attr.sample_freq;
1387 hwc->freq_count += freq;
1388 hwc->freq_interrupts += interrupts;
1390 if (hwc->freq_count < HZ)
1393 interrupts = hwc->freq_interrupts;
1394 hwc->freq_interrupts = 0;
1395 hwc->freq_count -= HZ;
1399 perf_adjust_period(event, freq * interrupts);
1402 * In order to avoid being stalled by an (accidental) huge
1403 * sample period, force reset the sample period if we didn't
1404 * get any events in this freq period.
1408 event->pmu->disable(event);
1409 atomic64_set(&hwc->period_left, 0);
1410 event->pmu->enable(event);
1414 spin_unlock(&ctx->lock);
1418 * Round-robin a context's events:
1420 static void rotate_ctx(struct perf_event_context *ctx)
1422 struct perf_event *event;
1424 if (!ctx->nr_events)
1427 spin_lock(&ctx->lock);
1429 * Rotate the first entry last (works just fine for group events too):
1432 list_for_each_entry(event, &ctx->group_list, group_entry) {
1433 list_move_tail(&event->group_entry, &ctx->group_list);
1438 spin_unlock(&ctx->lock);
1441 void perf_event_task_tick(struct task_struct *curr, int cpu)
1443 struct perf_cpu_context *cpuctx;
1444 struct perf_event_context *ctx;
1446 if (!atomic_read(&nr_events))
1449 cpuctx = &per_cpu(perf_cpu_context, cpu);
1450 ctx = curr->perf_event_ctxp;
1452 perf_ctx_adjust_freq(&cpuctx->ctx);
1454 perf_ctx_adjust_freq(ctx);
1456 perf_event_cpu_sched_out(cpuctx);
1458 __perf_event_task_sched_out(ctx);
1460 rotate_ctx(&cpuctx->ctx);
1464 perf_event_cpu_sched_in(cpuctx, cpu);
1466 perf_event_task_sched_in(curr, cpu);
1470 * Enable all of a task's events that have been marked enable-on-exec.
1471 * This expects task == current.
1473 static void perf_event_enable_on_exec(struct task_struct *task)
1475 struct perf_event_context *ctx;
1476 struct perf_event *event;
1477 unsigned long flags;
1480 local_irq_save(flags);
1481 ctx = task->perf_event_ctxp;
1482 if (!ctx || !ctx->nr_events)
1485 __perf_event_task_sched_out(ctx);
1487 spin_lock(&ctx->lock);
1489 list_for_each_entry(event, &ctx->group_list, group_entry) {
1490 if (!event->attr.enable_on_exec)
1492 event->attr.enable_on_exec = 0;
1493 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1495 __perf_event_mark_enabled(event, ctx);
1500 * Unclone this context if we enabled any event.
1505 spin_unlock(&ctx->lock);
1507 perf_event_task_sched_in(task, smp_processor_id());
1509 local_irq_restore(flags);
1513 * Cross CPU call to read the hardware event
1515 static void __perf_event_read(void *info)
1517 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1518 struct perf_event *event = info;
1519 struct perf_event_context *ctx = event->ctx;
1522 * If this is a task context, we need to check whether it is
1523 * the current task context of this cpu. If not it has been
1524 * scheduled out before the smp call arrived. In that case
1525 * event->count would have been updated to a recent sample
1526 * when the event was scheduled out.
1528 if (ctx->task && cpuctx->task_ctx != ctx)
1531 spin_lock(&ctx->lock);
1532 update_context_time(ctx);
1533 update_event_times(event);
1534 spin_unlock(&ctx->lock);
1536 event->pmu->read(event);
1539 static u64 perf_event_read(struct perf_event *event)
1542 * If event is enabled and currently active on a CPU, update the
1543 * value in the event structure:
1545 if (event->state == PERF_EVENT_STATE_ACTIVE) {
1546 smp_call_function_single(event->oncpu,
1547 __perf_event_read, event, 1);
1548 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
1549 struct perf_event_context *ctx = event->ctx;
1550 unsigned long flags;
1552 spin_lock_irqsave(&ctx->lock, flags);
1553 update_context_time(ctx);
1554 update_event_times(event);
1555 spin_unlock_irqrestore(&ctx->lock, flags);
1558 return atomic64_read(&event->count);
1562 * Initialize the perf_event context in a task_struct:
1565 __perf_event_init_context(struct perf_event_context *ctx,
1566 struct task_struct *task)
1568 memset(ctx, 0, sizeof(*ctx));
1569 spin_lock_init(&ctx->lock);
1570 mutex_init(&ctx->mutex);
1571 INIT_LIST_HEAD(&ctx->group_list);
1572 INIT_LIST_HEAD(&ctx->event_list);
1573 atomic_set(&ctx->refcount, 1);
1577 static struct perf_event_context *find_get_context(pid_t pid, int cpu)
1579 struct perf_event_context *ctx;
1580 struct perf_cpu_context *cpuctx;
1581 struct task_struct *task;
1582 unsigned long flags;
1586 * If cpu is not a wildcard then this is a percpu event:
1589 /* Must be root to operate on a CPU event: */
1590 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
1591 return ERR_PTR(-EACCES);
1593 if (cpu < 0 || cpu > num_possible_cpus())
1594 return ERR_PTR(-EINVAL);
1597 * We could be clever and allow to attach a event to an
1598 * offline CPU and activate it when the CPU comes up, but
1601 if (!cpu_isset(cpu, cpu_online_map))
1602 return ERR_PTR(-ENODEV);
1604 cpuctx = &per_cpu(perf_cpu_context, cpu);
1615 task = find_task_by_vpid(pid);
1617 get_task_struct(task);
1621 return ERR_PTR(-ESRCH);
1624 * Can't attach events to a dying task.
1627 if (task->flags & PF_EXITING)
1630 /* Reuse ptrace permission checks for now. */
1632 if (!ptrace_may_access(task, PTRACE_MODE_READ))
1636 ctx = perf_lock_task_context(task, &flags);
1639 spin_unlock_irqrestore(&ctx->lock, flags);
1643 ctx = kmalloc(sizeof(struct perf_event_context), GFP_KERNEL);
1647 __perf_event_init_context(ctx, task);
1649 if (cmpxchg(&task->perf_event_ctxp, NULL, ctx)) {
1651 * We raced with some other task; use
1652 * the context they set.
1657 get_task_struct(task);
1660 put_task_struct(task);
1664 put_task_struct(task);
1665 return ERR_PTR(err);
1668 static void perf_event_free_filter(struct perf_event *event);
1670 static void free_event_rcu(struct rcu_head *head)
1672 struct perf_event *event;
1674 event = container_of(head, struct perf_event, rcu_head);
1676 put_pid_ns(event->ns);
1677 perf_event_free_filter(event);
1681 static void perf_pending_sync(struct perf_event *event);
1683 static void free_event(struct perf_event *event)
1685 perf_pending_sync(event);
1687 if (!event->parent) {
1688 atomic_dec(&nr_events);
1689 if (event->attr.mmap)
1690 atomic_dec(&nr_mmap_events);
1691 if (event->attr.comm)
1692 atomic_dec(&nr_comm_events);
1693 if (event->attr.task)
1694 atomic_dec(&nr_task_events);
1697 if (event->output) {
1698 fput(event->output->filp);
1699 event->output = NULL;
1703 event->destroy(event);
1705 put_ctx(event->ctx);
1706 call_rcu(&event->rcu_head, free_event_rcu);
1709 int perf_event_release_kernel(struct perf_event *event)
1711 struct perf_event_context *ctx = event->ctx;
1713 WARN_ON_ONCE(ctx->parent_ctx);
1714 mutex_lock(&ctx->mutex);
1715 perf_event_remove_from_context(event);
1716 mutex_unlock(&ctx->mutex);
1718 mutex_lock(&event->owner->perf_event_mutex);
1719 list_del_init(&event->owner_entry);
1720 mutex_unlock(&event->owner->perf_event_mutex);
1721 put_task_struct(event->owner);
1727 EXPORT_SYMBOL_GPL(perf_event_release_kernel);
1730 * Called when the last reference to the file is gone.
1732 static int perf_release(struct inode *inode, struct file *file)
1734 struct perf_event *event = file->private_data;
1736 file->private_data = NULL;
1738 return perf_event_release_kernel(event);
1741 static int perf_event_read_size(struct perf_event *event)
1743 int entry = sizeof(u64); /* value */
1747 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1748 size += sizeof(u64);
1750 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1751 size += sizeof(u64);
1753 if (event->attr.read_format & PERF_FORMAT_ID)
1754 entry += sizeof(u64);
1756 if (event->attr.read_format & PERF_FORMAT_GROUP) {
1757 nr += event->group_leader->nr_siblings;
1758 size += sizeof(u64);
1766 u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
1768 struct perf_event *child;
1774 mutex_lock(&event->child_mutex);
1775 total += perf_event_read(event);
1776 *enabled += event->total_time_enabled +
1777 atomic64_read(&event->child_total_time_enabled);
1778 *running += event->total_time_running +
1779 atomic64_read(&event->child_total_time_running);
1781 list_for_each_entry(child, &event->child_list, child_list) {
1782 total += perf_event_read(child);
1783 *enabled += child->total_time_enabled;
1784 *running += child->total_time_running;
1786 mutex_unlock(&event->child_mutex);
1790 EXPORT_SYMBOL_GPL(perf_event_read_value);
1792 static int perf_event_read_group(struct perf_event *event,
1793 u64 read_format, char __user *buf)
1795 struct perf_event *leader = event->group_leader, *sub;
1796 int n = 0, size = 0, ret = -EFAULT;
1797 struct perf_event_context *ctx = leader->ctx;
1799 u64 count, enabled, running;
1801 mutex_lock(&ctx->mutex);
1802 count = perf_event_read_value(leader, &enabled, &running);
1804 values[n++] = 1 + leader->nr_siblings;
1805 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1806 values[n++] = enabled;
1807 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1808 values[n++] = running;
1809 values[n++] = count;
1810 if (read_format & PERF_FORMAT_ID)
1811 values[n++] = primary_event_id(leader);
1813 size = n * sizeof(u64);
1815 if (copy_to_user(buf, values, size))
1820 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
1823 values[n++] = perf_event_read_value(sub, &enabled, &running);
1824 if (read_format & PERF_FORMAT_ID)
1825 values[n++] = primary_event_id(sub);
1827 size = n * sizeof(u64);
1829 if (copy_to_user(buf + size, values, size)) {
1837 mutex_unlock(&ctx->mutex);
1842 static int perf_event_read_one(struct perf_event *event,
1843 u64 read_format, char __user *buf)
1845 u64 enabled, running;
1849 values[n++] = perf_event_read_value(event, &enabled, &running);
1850 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1851 values[n++] = enabled;
1852 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1853 values[n++] = running;
1854 if (read_format & PERF_FORMAT_ID)
1855 values[n++] = primary_event_id(event);
1857 if (copy_to_user(buf, values, n * sizeof(u64)))
1860 return n * sizeof(u64);
1864 * Read the performance event - simple non blocking version for now
1867 perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
1869 u64 read_format = event->attr.read_format;
1873 * Return end-of-file for a read on a event that is in
1874 * error state (i.e. because it was pinned but it couldn't be
1875 * scheduled on to the CPU at some point).
1877 if (event->state == PERF_EVENT_STATE_ERROR)
1880 if (count < perf_event_read_size(event))
1883 WARN_ON_ONCE(event->ctx->parent_ctx);
1884 if (read_format & PERF_FORMAT_GROUP)
1885 ret = perf_event_read_group(event, read_format, buf);
1887 ret = perf_event_read_one(event, read_format, buf);
1893 perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
1895 struct perf_event *event = file->private_data;
1897 return perf_read_hw(event, buf, count);
1900 static unsigned int perf_poll(struct file *file, poll_table *wait)
1902 struct perf_event *event = file->private_data;
1903 struct perf_mmap_data *data;
1904 unsigned int events = POLL_HUP;
1907 data = rcu_dereference(event->data);
1909 events = atomic_xchg(&data->poll, 0);
1912 poll_wait(file, &event->waitq, wait);
1917 static void perf_event_reset(struct perf_event *event)
1919 (void)perf_event_read(event);
1920 atomic64_set(&event->count, 0);
1921 perf_event_update_userpage(event);
1925 * Holding the top-level event's child_mutex means that any
1926 * descendant process that has inherited this event will block
1927 * in sync_child_event if it goes to exit, thus satisfying the
1928 * task existence requirements of perf_event_enable/disable.
1930 static void perf_event_for_each_child(struct perf_event *event,
1931 void (*func)(struct perf_event *))
1933 struct perf_event *child;
1935 WARN_ON_ONCE(event->ctx->parent_ctx);
1936 mutex_lock(&event->child_mutex);
1938 list_for_each_entry(child, &event->child_list, child_list)
1940 mutex_unlock(&event->child_mutex);
1943 static void perf_event_for_each(struct perf_event *event,
1944 void (*func)(struct perf_event *))
1946 struct perf_event_context *ctx = event->ctx;
1947 struct perf_event *sibling;
1949 WARN_ON_ONCE(ctx->parent_ctx);
1950 mutex_lock(&ctx->mutex);
1951 event = event->group_leader;
1953 perf_event_for_each_child(event, func);
1955 list_for_each_entry(sibling, &event->sibling_list, group_entry)
1956 perf_event_for_each_child(event, func);
1957 mutex_unlock(&ctx->mutex);
1960 static int perf_event_period(struct perf_event *event, u64 __user *arg)
1962 struct perf_event_context *ctx = event->ctx;
1967 if (!event->attr.sample_period)
1970 size = copy_from_user(&value, arg, sizeof(value));
1971 if (size != sizeof(value))
1977 spin_lock_irq(&ctx->lock);
1978 if (event->attr.freq) {
1979 if (value > sysctl_perf_event_sample_rate) {
1984 event->attr.sample_freq = value;
1986 event->attr.sample_period = value;
1987 event->hw.sample_period = value;
1990 spin_unlock_irq(&ctx->lock);
1995 static int perf_event_set_output(struct perf_event *event, int output_fd);
1996 static int perf_event_set_filter(struct perf_event *event, void __user *arg);
1998 static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2000 struct perf_event *event = file->private_data;
2001 void (*func)(struct perf_event *);
2005 case PERF_EVENT_IOC_ENABLE:
2006 func = perf_event_enable;
2008 case PERF_EVENT_IOC_DISABLE:
2009 func = perf_event_disable;
2011 case PERF_EVENT_IOC_RESET:
2012 func = perf_event_reset;
2015 case PERF_EVENT_IOC_REFRESH:
2016 return perf_event_refresh(event, arg);
2018 case PERF_EVENT_IOC_PERIOD:
2019 return perf_event_period(event, (u64 __user *)arg);
2021 case PERF_EVENT_IOC_SET_OUTPUT:
2022 return perf_event_set_output(event, arg);
2024 case PERF_EVENT_IOC_SET_FILTER:
2025 return perf_event_set_filter(event, (void __user *)arg);
2031 if (flags & PERF_IOC_FLAG_GROUP)
2032 perf_event_for_each(event, func);
2034 perf_event_for_each_child(event, func);
2039 int perf_event_task_enable(void)
2041 struct perf_event *event;
2043 mutex_lock(¤t->perf_event_mutex);
2044 list_for_each_entry(event, ¤t->perf_event_list, owner_entry)
2045 perf_event_for_each_child(event, perf_event_enable);
2046 mutex_unlock(¤t->perf_event_mutex);
2051 int perf_event_task_disable(void)
2053 struct perf_event *event;
2055 mutex_lock(¤t->perf_event_mutex);
2056 list_for_each_entry(event, ¤t->perf_event_list, owner_entry)
2057 perf_event_for_each_child(event, perf_event_disable);
2058 mutex_unlock(¤t->perf_event_mutex);
2063 #ifndef PERF_EVENT_INDEX_OFFSET
2064 # define PERF_EVENT_INDEX_OFFSET 0
2067 static int perf_event_index(struct perf_event *event)
2069 if (event->state != PERF_EVENT_STATE_ACTIVE)
2072 return event->hw.idx + 1 - PERF_EVENT_INDEX_OFFSET;
2076 * Callers need to ensure there can be no nesting of this function, otherwise
2077 * the seqlock logic goes bad. We can not serialize this because the arch
2078 * code calls this from NMI context.
2080 void perf_event_update_userpage(struct perf_event *event)
2082 struct perf_event_mmap_page *userpg;
2083 struct perf_mmap_data *data;
2086 data = rcu_dereference(event->data);
2090 userpg = data->user_page;
2093 * Disable preemption so as to not let the corresponding user-space
2094 * spin too long if we get preempted.
2099 userpg->index = perf_event_index(event);
2100 userpg->offset = atomic64_read(&event->count);
2101 if (event->state == PERF_EVENT_STATE_ACTIVE)
2102 userpg->offset -= atomic64_read(&event->hw.prev_count);
2104 userpg->time_enabled = event->total_time_enabled +
2105 atomic64_read(&event->child_total_time_enabled);
2107 userpg->time_running = event->total_time_running +
2108 atomic64_read(&event->child_total_time_running);
2117 static unsigned long perf_data_size(struct perf_mmap_data *data)
2119 return data->nr_pages << (PAGE_SHIFT + data->data_order);
2122 #ifndef CONFIG_PERF_USE_VMALLOC
2125 * Back perf_mmap() with regular GFP_KERNEL-0 pages.
2128 static struct page *
2129 perf_mmap_to_page(struct perf_mmap_data *data, unsigned long pgoff)
2131 if (pgoff > data->nr_pages)
2135 return virt_to_page(data->user_page);
2137 return virt_to_page(data->data_pages[pgoff - 1]);
2140 static struct perf_mmap_data *
2141 perf_mmap_data_alloc(struct perf_event *event, int nr_pages)
2143 struct perf_mmap_data *data;
2147 WARN_ON(atomic_read(&event->mmap_count));
2149 size = sizeof(struct perf_mmap_data);
2150 size += nr_pages * sizeof(void *);
2152 data = kzalloc(size, GFP_KERNEL);
2156 data->user_page = (void *)get_zeroed_page(GFP_KERNEL);
2157 if (!data->user_page)
2158 goto fail_user_page;
2160 for (i = 0; i < nr_pages; i++) {
2161 data->data_pages[i] = (void *)get_zeroed_page(GFP_KERNEL);
2162 if (!data->data_pages[i])
2163 goto fail_data_pages;
2166 data->data_order = 0;
2167 data->nr_pages = nr_pages;
2172 for (i--; i >= 0; i--)
2173 free_page((unsigned long)data->data_pages[i]);
2175 free_page((unsigned long)data->user_page);
2184 static void perf_mmap_free_page(unsigned long addr)
2186 struct page *page = virt_to_page((void *)addr);
2188 page->mapping = NULL;
2192 static void perf_mmap_data_free(struct perf_mmap_data *data)
2196 perf_mmap_free_page((unsigned long)data->user_page);
2197 for (i = 0; i < data->nr_pages; i++)
2198 perf_mmap_free_page((unsigned long)data->data_pages[i]);
2204 * Back perf_mmap() with vmalloc memory.
2206 * Required for architectures that have d-cache aliasing issues.
2209 static struct page *
2210 perf_mmap_to_page(struct perf_mmap_data *data, unsigned long pgoff)
2212 if (pgoff > (1UL << data->data_order))
2215 return vmalloc_to_page((void *)data->user_page + pgoff * PAGE_SIZE);
2218 static void perf_mmap_unmark_page(void *addr)
2220 struct page *page = vmalloc_to_page(addr);
2222 page->mapping = NULL;
2225 static void perf_mmap_data_free_work(struct work_struct *work)
2227 struct perf_mmap_data *data;
2231 data = container_of(work, struct perf_mmap_data, work);
2232 nr = 1 << data->data_order;
2234 base = data->user_page;
2235 for (i = 0; i < nr + 1; i++)
2236 perf_mmap_unmark_page(base + (i * PAGE_SIZE));
2241 static void perf_mmap_data_free(struct perf_mmap_data *data)
2243 schedule_work(&data->work);
2246 static struct perf_mmap_data *
2247 perf_mmap_data_alloc(struct perf_event *event, int nr_pages)
2249 struct perf_mmap_data *data;
2253 WARN_ON(atomic_read(&event->mmap_count));
2255 size = sizeof(struct perf_mmap_data);
2256 size += sizeof(void *);
2258 data = kzalloc(size, GFP_KERNEL);
2262 INIT_WORK(&data->work, perf_mmap_data_free_work);
2264 all_buf = vmalloc_user((nr_pages + 1) * PAGE_SIZE);
2268 data->user_page = all_buf;
2269 data->data_pages[0] = all_buf + PAGE_SIZE;
2270 data->data_order = ilog2(nr_pages);
2284 static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
2286 struct perf_event *event = vma->vm_file->private_data;
2287 struct perf_mmap_data *data;
2288 int ret = VM_FAULT_SIGBUS;
2290 if (vmf->flags & FAULT_FLAG_MKWRITE) {
2291 if (vmf->pgoff == 0)
2297 data = rcu_dereference(event->data);
2301 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
2304 vmf->page = perf_mmap_to_page(data, vmf->pgoff);
2308 get_page(vmf->page);
2309 vmf->page->mapping = vma->vm_file->f_mapping;
2310 vmf->page->index = vmf->pgoff;
2320 perf_mmap_data_init(struct perf_event *event, struct perf_mmap_data *data)
2322 long max_size = perf_data_size(data);
2324 atomic_set(&data->lock, -1);
2326 if (event->attr.watermark) {
2327 data->watermark = min_t(long, max_size,
2328 event->attr.wakeup_watermark);
2331 if (!data->watermark)
2332 data->watermark = max_size / 2;
2335 rcu_assign_pointer(event->data, data);
2338 static void perf_mmap_data_free_rcu(struct rcu_head *rcu_head)
2340 struct perf_mmap_data *data;
2342 data = container_of(rcu_head, struct perf_mmap_data, rcu_head);
2343 perf_mmap_data_free(data);
2347 static void perf_mmap_data_release(struct perf_event *event)
2349 struct perf_mmap_data *data = event->data;
2351 WARN_ON(atomic_read(&event->mmap_count));
2353 rcu_assign_pointer(event->data, NULL);
2354 call_rcu(&data->rcu_head, perf_mmap_data_free_rcu);
2357 static void perf_mmap_open(struct vm_area_struct *vma)
2359 struct perf_event *event = vma->vm_file->private_data;
2361 atomic_inc(&event->mmap_count);
2364 static void perf_mmap_close(struct vm_area_struct *vma)
2366 struct perf_event *event = vma->vm_file->private_data;
2368 WARN_ON_ONCE(event->ctx->parent_ctx);
2369 if (atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) {
2370 unsigned long size = perf_data_size(event->data);
2371 struct user_struct *user = current_user();
2373 atomic_long_sub((size >> PAGE_SHIFT) + 1, &user->locked_vm);
2374 vma->vm_mm->locked_vm -= event->data->nr_locked;
2375 perf_mmap_data_release(event);
2376 mutex_unlock(&event->mmap_mutex);
2380 static const struct vm_operations_struct perf_mmap_vmops = {
2381 .open = perf_mmap_open,
2382 .close = perf_mmap_close,
2383 .fault = perf_mmap_fault,
2384 .page_mkwrite = perf_mmap_fault,
2387 static int perf_mmap(struct file *file, struct vm_area_struct *vma)
2389 struct perf_event *event = file->private_data;
2390 unsigned long user_locked, user_lock_limit;
2391 struct user_struct *user = current_user();
2392 unsigned long locked, lock_limit;
2393 struct perf_mmap_data *data;
2394 unsigned long vma_size;
2395 unsigned long nr_pages;
2396 long user_extra, extra;
2399 if (!(vma->vm_flags & VM_SHARED))
2402 vma_size = vma->vm_end - vma->vm_start;
2403 nr_pages = (vma_size / PAGE_SIZE) - 1;
2406 * If we have data pages ensure they're a power-of-two number, so we
2407 * can do bitmasks instead of modulo.
2409 if (nr_pages != 0 && !is_power_of_2(nr_pages))
2412 if (vma_size != PAGE_SIZE * (1 + nr_pages))
2415 if (vma->vm_pgoff != 0)
2418 WARN_ON_ONCE(event->ctx->parent_ctx);
2419 mutex_lock(&event->mmap_mutex);
2420 if (event->output) {
2425 if (atomic_inc_not_zero(&event->mmap_count)) {
2426 if (nr_pages != event->data->nr_pages)
2431 user_extra = nr_pages + 1;
2432 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
2435 * Increase the limit linearly with more CPUs:
2437 user_lock_limit *= num_online_cpus();
2439 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
2442 if (user_locked > user_lock_limit)
2443 extra = user_locked - user_lock_limit;
2445 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
2446 lock_limit >>= PAGE_SHIFT;
2447 locked = vma->vm_mm->locked_vm + extra;
2449 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
2450 !capable(CAP_IPC_LOCK)) {
2455 WARN_ON(event->data);
2457 data = perf_mmap_data_alloc(event, nr_pages);
2463 perf_mmap_data_init(event, data);
2465 atomic_set(&event->mmap_count, 1);
2466 atomic_long_add(user_extra, &user->locked_vm);
2467 vma->vm_mm->locked_vm += extra;
2468 event->data->nr_locked = extra;
2469 if (vma->vm_flags & VM_WRITE)
2470 event->data->writable = 1;
2473 mutex_unlock(&event->mmap_mutex);
2475 vma->vm_flags |= VM_RESERVED;
2476 vma->vm_ops = &perf_mmap_vmops;
2481 static int perf_fasync(int fd, struct file *filp, int on)
2483 struct inode *inode = filp->f_path.dentry->d_inode;
2484 struct perf_event *event = filp->private_data;
2487 mutex_lock(&inode->i_mutex);
2488 retval = fasync_helper(fd, filp, on, &event->fasync);
2489 mutex_unlock(&inode->i_mutex);
2497 static const struct file_operations perf_fops = {
2498 .release = perf_release,
2501 .unlocked_ioctl = perf_ioctl,
2502 .compat_ioctl = perf_ioctl,
2504 .fasync = perf_fasync,
2510 * If there's data, ensure we set the poll() state and publish everything
2511 * to user-space before waking everybody up.
2514 void perf_event_wakeup(struct perf_event *event)
2516 wake_up_all(&event->waitq);
2518 if (event->pending_kill) {
2519 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
2520 event->pending_kill = 0;
2527 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
2529 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
2530 * single linked list and use cmpxchg() to add entries lockless.
2533 static void perf_pending_event(struct perf_pending_entry *entry)
2535 struct perf_event *event = container_of(entry,
2536 struct perf_event, pending);
2538 if (event->pending_disable) {
2539 event->pending_disable = 0;
2540 __perf_event_disable(event);
2543 if (event->pending_wakeup) {
2544 event->pending_wakeup = 0;
2545 perf_event_wakeup(event);
2549 #define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
2551 static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
2555 static void perf_pending_queue(struct perf_pending_entry *entry,
2556 void (*func)(struct perf_pending_entry *))
2558 struct perf_pending_entry **head;
2560 if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
2565 head = &get_cpu_var(perf_pending_head);
2568 entry->next = *head;
2569 } while (cmpxchg(head, entry->next, entry) != entry->next);
2571 set_perf_event_pending();
2573 put_cpu_var(perf_pending_head);
2576 static int __perf_pending_run(void)
2578 struct perf_pending_entry *list;
2581 list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
2582 while (list != PENDING_TAIL) {
2583 void (*func)(struct perf_pending_entry *);
2584 struct perf_pending_entry *entry = list;
2591 * Ensure we observe the unqueue before we issue the wakeup,
2592 * so that we won't be waiting forever.
2593 * -- see perf_not_pending().
2604 static inline int perf_not_pending(struct perf_event *event)
2607 * If we flush on whatever cpu we run, there is a chance we don't
2611 __perf_pending_run();
2615 * Ensure we see the proper queue state before going to sleep
2616 * so that we do not miss the wakeup. -- see perf_pending_handle()
2619 return event->pending.next == NULL;
2622 static void perf_pending_sync(struct perf_event *event)
2624 wait_event(event->waitq, perf_not_pending(event));
2627 void perf_event_do_pending(void)
2629 __perf_pending_run();
2633 * Callchain support -- arch specific
2636 __weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
2644 static bool perf_output_space(struct perf_mmap_data *data, unsigned long tail,
2645 unsigned long offset, unsigned long head)
2649 if (!data->writable)
2652 mask = perf_data_size(data) - 1;
2654 offset = (offset - tail) & mask;
2655 head = (head - tail) & mask;
2657 if ((int)(head - offset) < 0)
2663 static void perf_output_wakeup(struct perf_output_handle *handle)
2665 atomic_set(&handle->data->poll, POLL_IN);
2668 handle->event->pending_wakeup = 1;
2669 perf_pending_queue(&handle->event->pending,
2670 perf_pending_event);
2672 perf_event_wakeup(handle->event);
2676 * Curious locking construct.
2678 * We need to ensure a later event_id doesn't publish a head when a former
2679 * event_id isn't done writing. However since we need to deal with NMIs we
2680 * cannot fully serialize things.
2682 * What we do is serialize between CPUs so we only have to deal with NMI
2683 * nesting on a single CPU.
2685 * We only publish the head (and generate a wakeup) when the outer-most
2686 * event_id completes.
2688 static void perf_output_lock(struct perf_output_handle *handle)
2690 struct perf_mmap_data *data = handle->data;
2691 int cur, cpu = get_cpu();
2696 cur = atomic_cmpxchg(&data->lock, -1, cpu);
2708 static void perf_output_unlock(struct perf_output_handle *handle)
2710 struct perf_mmap_data *data = handle->data;
2714 data->done_head = data->head;
2716 if (!handle->locked)
2721 * The xchg implies a full barrier that ensures all writes are done
2722 * before we publish the new head, matched by a rmb() in userspace when
2723 * reading this position.
2725 while ((head = atomic_long_xchg(&data->done_head, 0)))
2726 data->user_page->data_head = head;
2729 * NMI can happen here, which means we can miss a done_head update.
2732 cpu = atomic_xchg(&data->lock, -1);
2733 WARN_ON_ONCE(cpu != smp_processor_id());
2736 * Therefore we have to validate we did not indeed do so.
2738 if (unlikely(atomic_long_read(&data->done_head))) {
2740 * Since we had it locked, we can lock it again.
2742 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
2748 if (atomic_xchg(&data->wakeup, 0))
2749 perf_output_wakeup(handle);
2754 void perf_output_copy(struct perf_output_handle *handle,
2755 const void *buf, unsigned int len)
2757 unsigned int pages_mask;
2758 unsigned long offset;
2762 offset = handle->offset;
2763 pages_mask = handle->data->nr_pages - 1;
2764 pages = handle->data->data_pages;
2767 unsigned long page_offset;
2768 unsigned long page_size;
2771 nr = (offset >> PAGE_SHIFT) & pages_mask;
2772 page_size = 1UL << (handle->data->data_order + PAGE_SHIFT);
2773 page_offset = offset & (page_size - 1);
2774 size = min_t(unsigned int, page_size - page_offset, len);
2776 memcpy(pages[nr] + page_offset, buf, size);
2783 handle->offset = offset;
2786 * Check we didn't copy past our reservation window, taking the
2787 * possible unsigned int wrap into account.
2789 WARN_ON_ONCE(((long)(handle->head - handle->offset)) < 0);
2792 int perf_output_begin(struct perf_output_handle *handle,
2793 struct perf_event *event, unsigned int size,
2794 int nmi, int sample)
2796 struct perf_event *output_event;
2797 struct perf_mmap_data *data;
2798 unsigned long tail, offset, head;
2801 struct perf_event_header header;
2808 * For inherited events we send all the output towards the parent.
2811 event = event->parent;
2813 output_event = rcu_dereference(event->output);
2815 event = output_event;
2817 data = rcu_dereference(event->data);
2821 handle->data = data;
2822 handle->event = event;
2824 handle->sample = sample;
2826 if (!data->nr_pages)
2829 have_lost = atomic_read(&data->lost);
2831 size += sizeof(lost_event);
2833 perf_output_lock(handle);
2837 * Userspace could choose to issue a mb() before updating the
2838 * tail pointer. So that all reads will be completed before the
2841 tail = ACCESS_ONCE(data->user_page->data_tail);
2843 offset = head = atomic_long_read(&data->head);
2845 if (unlikely(!perf_output_space(data, tail, offset, head)))
2847 } while (atomic_long_cmpxchg(&data->head, offset, head) != offset);
2849 handle->offset = offset;
2850 handle->head = head;
2852 if (head - tail > data->watermark)
2853 atomic_set(&data->wakeup, 1);
2856 lost_event.header.type = PERF_RECORD_LOST;
2857 lost_event.header.misc = 0;
2858 lost_event.header.size = sizeof(lost_event);
2859 lost_event.id = event->id;
2860 lost_event.lost = atomic_xchg(&data->lost, 0);
2862 perf_output_put(handle, lost_event);
2868 atomic_inc(&data->lost);
2869 perf_output_unlock(handle);
2876 void perf_output_end(struct perf_output_handle *handle)
2878 struct perf_event *event = handle->event;
2879 struct perf_mmap_data *data = handle->data;
2881 int wakeup_events = event->attr.wakeup_events;
2883 if (handle->sample && wakeup_events) {
2884 int events = atomic_inc_return(&data->events);
2885 if (events >= wakeup_events) {
2886 atomic_sub(wakeup_events, &data->events);
2887 atomic_set(&data->wakeup, 1);
2891 perf_output_unlock(handle);
2895 static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
2898 * only top level events have the pid namespace they were created in
2901 event = event->parent;
2903 return task_tgid_nr_ns(p, event->ns);
2906 static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
2909 * only top level events have the pid namespace they were created in
2912 event = event->parent;
2914 return task_pid_nr_ns(p, event->ns);
2917 static void perf_output_read_one(struct perf_output_handle *handle,
2918 struct perf_event *event)
2920 u64 read_format = event->attr.read_format;
2924 values[n++] = atomic64_read(&event->count);
2925 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
2926 values[n++] = event->total_time_enabled +
2927 atomic64_read(&event->child_total_time_enabled);
2929 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
2930 values[n++] = event->total_time_running +
2931 atomic64_read(&event->child_total_time_running);
2933 if (read_format & PERF_FORMAT_ID)
2934 values[n++] = primary_event_id(event);
2936 perf_output_copy(handle, values, n * sizeof(u64));
2940 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
2942 static void perf_output_read_group(struct perf_output_handle *handle,
2943 struct perf_event *event)
2945 struct perf_event *leader = event->group_leader, *sub;
2946 u64 read_format = event->attr.read_format;
2950 values[n++] = 1 + leader->nr_siblings;
2952 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2953 values[n++] = leader->total_time_enabled;
2955 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2956 values[n++] = leader->total_time_running;
2958 if (leader != event)
2959 leader->pmu->read(leader);
2961 values[n++] = atomic64_read(&leader->count);
2962 if (read_format & PERF_FORMAT_ID)
2963 values[n++] = primary_event_id(leader);
2965 perf_output_copy(handle, values, n * sizeof(u64));
2967 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
2971 sub->pmu->read(sub);
2973 values[n++] = atomic64_read(&sub->count);
2974 if (read_format & PERF_FORMAT_ID)
2975 values[n++] = primary_event_id(sub);
2977 perf_output_copy(handle, values, n * sizeof(u64));
2981 static void perf_output_read(struct perf_output_handle *handle,
2982 struct perf_event *event)
2984 if (event->attr.read_format & PERF_FORMAT_GROUP)
2985 perf_output_read_group(handle, event);
2987 perf_output_read_one(handle, event);
2990 void perf_output_sample(struct perf_output_handle *handle,
2991 struct perf_event_header *header,
2992 struct perf_sample_data *data,
2993 struct perf_event *event)
2995 u64 sample_type = data->type;
2997 perf_output_put(handle, *header);
2999 if (sample_type & PERF_SAMPLE_IP)
3000 perf_output_put(handle, data->ip);
3002 if (sample_type & PERF_SAMPLE_TID)
3003 perf_output_put(handle, data->tid_entry);
3005 if (sample_type & PERF_SAMPLE_TIME)
3006 perf_output_put(handle, data->time);
3008 if (sample_type & PERF_SAMPLE_ADDR)
3009 perf_output_put(handle, data->addr);
3011 if (sample_type & PERF_SAMPLE_ID)
3012 perf_output_put(handle, data->id);
3014 if (sample_type & PERF_SAMPLE_STREAM_ID)
3015 perf_output_put(handle, data->stream_id);
3017 if (sample_type & PERF_SAMPLE_CPU)
3018 perf_output_put(handle, data->cpu_entry);
3020 if (sample_type & PERF_SAMPLE_PERIOD)
3021 perf_output_put(handle, data->period);
3023 if (sample_type & PERF_SAMPLE_READ)
3024 perf_output_read(handle, event);
3026 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3027 if (data->callchain) {
3030 if (data->callchain)
3031 size += data->callchain->nr;
3033 size *= sizeof(u64);
3035 perf_output_copy(handle, data->callchain, size);
3038 perf_output_put(handle, nr);
3042 if (sample_type & PERF_SAMPLE_RAW) {
3044 perf_output_put(handle, data->raw->size);
3045 perf_output_copy(handle, data->raw->data,
3052 .size = sizeof(u32),
3055 perf_output_put(handle, raw);
3060 void perf_prepare_sample(struct perf_event_header *header,
3061 struct perf_sample_data *data,
3062 struct perf_event *event,
3063 struct pt_regs *regs)
3065 u64 sample_type = event->attr.sample_type;
3067 data->type = sample_type;
3069 header->type = PERF_RECORD_SAMPLE;
3070 header->size = sizeof(*header);
3073 header->misc |= perf_misc_flags(regs);
3075 if (sample_type & PERF_SAMPLE_IP) {
3076 data->ip = perf_instruction_pointer(regs);
3078 header->size += sizeof(data->ip);
3081 if (sample_type & PERF_SAMPLE_TID) {
3082 /* namespace issues */
3083 data->tid_entry.pid = perf_event_pid(event, current);
3084 data->tid_entry.tid = perf_event_tid(event, current);
3086 header->size += sizeof(data->tid_entry);
3089 if (sample_type & PERF_SAMPLE_TIME) {
3090 data->time = perf_clock();
3092 header->size += sizeof(data->time);
3095 if (sample_type & PERF_SAMPLE_ADDR)
3096 header->size += sizeof(data->addr);
3098 if (sample_type & PERF_SAMPLE_ID) {
3099 data->id = primary_event_id(event);
3101 header->size += sizeof(data->id);
3104 if (sample_type & PERF_SAMPLE_STREAM_ID) {
3105 data->stream_id = event->id;
3107 header->size += sizeof(data->stream_id);
3110 if (sample_type & PERF_SAMPLE_CPU) {
3111 data->cpu_entry.cpu = raw_smp_processor_id();
3112 data->cpu_entry.reserved = 0;
3114 header->size += sizeof(data->cpu_entry);
3117 if (sample_type & PERF_SAMPLE_PERIOD)
3118 header->size += sizeof(data->period);
3120 if (sample_type & PERF_SAMPLE_READ)
3121 header->size += perf_event_read_size(event);
3123 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3126 data->callchain = perf_callchain(regs);
3128 if (data->callchain)
3129 size += data->callchain->nr;
3131 header->size += size * sizeof(u64);
3134 if (sample_type & PERF_SAMPLE_RAW) {
3135 int size = sizeof(u32);
3138 size += data->raw->size;
3140 size += sizeof(u32);
3142 WARN_ON_ONCE(size & (sizeof(u64)-1));
3143 header->size += size;
3147 static void perf_event_output(struct perf_event *event, int nmi,
3148 struct perf_sample_data *data,
3149 struct pt_regs *regs)
3151 struct perf_output_handle handle;
3152 struct perf_event_header header;
3154 perf_prepare_sample(&header, data, event, regs);
3156 if (perf_output_begin(&handle, event, header.size, nmi, 1))
3159 perf_output_sample(&handle, &header, data, event);
3161 perf_output_end(&handle);
3168 struct perf_read_event {
3169 struct perf_event_header header;
3176 perf_event_read_event(struct perf_event *event,
3177 struct task_struct *task)
3179 struct perf_output_handle handle;
3180 struct perf_read_event read_event = {
3182 .type = PERF_RECORD_READ,
3184 .size = sizeof(read_event) + perf_event_read_size(event),
3186 .pid = perf_event_pid(event, task),
3187 .tid = perf_event_tid(event, task),
3191 ret = perf_output_begin(&handle, event, read_event.header.size, 0, 0);
3195 perf_output_put(&handle, read_event);
3196 perf_output_read(&handle, event);
3198 perf_output_end(&handle);
3202 * task tracking -- fork/exit
3204 * enabled by: attr.comm | attr.mmap | attr.task
3207 struct perf_task_event {
3208 struct task_struct *task;
3209 struct perf_event_context *task_ctx;
3212 struct perf_event_header header;
3222 static void perf_event_task_output(struct perf_event *event,
3223 struct perf_task_event *task_event)
3225 struct perf_output_handle handle;
3227 struct task_struct *task = task_event->task;
3230 size = task_event->event_id.header.size;
3231 ret = perf_output_begin(&handle, event, size, 0, 0);
3236 task_event->event_id.pid = perf_event_pid(event, task);
3237 task_event->event_id.ppid = perf_event_pid(event, current);
3239 task_event->event_id.tid = perf_event_tid(event, task);
3240 task_event->event_id.ptid = perf_event_tid(event, current);
3242 task_event->event_id.time = perf_clock();
3244 perf_output_put(&handle, task_event->event_id);
3246 perf_output_end(&handle);
3249 static int perf_event_task_match(struct perf_event *event)
3251 if (event->attr.comm || event->attr.mmap || event->attr.task)
3257 static void perf_event_task_ctx(struct perf_event_context *ctx,
3258 struct perf_task_event *task_event)
3260 struct perf_event *event;
3262 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3263 if (perf_event_task_match(event))
3264 perf_event_task_output(event, task_event);
3268 static void perf_event_task_event(struct perf_task_event *task_event)
3270 struct perf_cpu_context *cpuctx;
3271 struct perf_event_context *ctx = task_event->task_ctx;
3274 cpuctx = &get_cpu_var(perf_cpu_context);
3275 perf_event_task_ctx(&cpuctx->ctx, task_event);
3276 put_cpu_var(perf_cpu_context);
3279 ctx = rcu_dereference(task_event->task->perf_event_ctxp);
3281 perf_event_task_ctx(ctx, task_event);
3285 static void perf_event_task(struct task_struct *task,
3286 struct perf_event_context *task_ctx,
3289 struct perf_task_event task_event;
3291 if (!atomic_read(&nr_comm_events) &&
3292 !atomic_read(&nr_mmap_events) &&
3293 !atomic_read(&nr_task_events))
3296 task_event = (struct perf_task_event){
3298 .task_ctx = task_ctx,
3301 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
3303 .size = sizeof(task_event.event_id),
3312 perf_event_task_event(&task_event);
3315 void perf_event_fork(struct task_struct *task)
3317 perf_event_task(task, NULL, 1);
3324 struct perf_comm_event {
3325 struct task_struct *task;
3330 struct perf_event_header header;
3337 static void perf_event_comm_output(struct perf_event *event,
3338 struct perf_comm_event *comm_event)
3340 struct perf_output_handle handle;
3341 int size = comm_event->event_id.header.size;
3342 int ret = perf_output_begin(&handle, event, size, 0, 0);
3347 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
3348 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
3350 perf_output_put(&handle, comm_event->event_id);
3351 perf_output_copy(&handle, comm_event->comm,
3352 comm_event->comm_size);
3353 perf_output_end(&handle);
3356 static int perf_event_comm_match(struct perf_event *event)
3358 if (event->attr.comm)
3364 static void perf_event_comm_ctx(struct perf_event_context *ctx,
3365 struct perf_comm_event *comm_event)
3367 struct perf_event *event;
3369 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3370 if (perf_event_comm_match(event))
3371 perf_event_comm_output(event, comm_event);
3375 static void perf_event_comm_event(struct perf_comm_event *comm_event)
3377 struct perf_cpu_context *cpuctx;
3378 struct perf_event_context *ctx;
3380 char comm[TASK_COMM_LEN];
3382 memset(comm, 0, sizeof(comm));
3383 strlcpy(comm, comm_event->task->comm, sizeof(comm));
3384 size = ALIGN(strlen(comm)+1, sizeof(u64));
3386 comm_event->comm = comm;
3387 comm_event->comm_size = size;
3389 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
3392 cpuctx = &get_cpu_var(perf_cpu_context);
3393 perf_event_comm_ctx(&cpuctx->ctx, comm_event);
3394 put_cpu_var(perf_cpu_context);
3397 * doesn't really matter which of the child contexts the
3398 * events ends up in.
3400 ctx = rcu_dereference(current->perf_event_ctxp);
3402 perf_event_comm_ctx(ctx, comm_event);
3406 void perf_event_comm(struct task_struct *task)
3408 struct perf_comm_event comm_event;
3410 if (task->perf_event_ctxp)
3411 perf_event_enable_on_exec(task);
3413 if (!atomic_read(&nr_comm_events))
3416 comm_event = (struct perf_comm_event){
3422 .type = PERF_RECORD_COMM,
3431 perf_event_comm_event(&comm_event);
3438 struct perf_mmap_event {
3439 struct vm_area_struct *vma;
3441 const char *file_name;
3445 struct perf_event_header header;
3455 static void perf_event_mmap_output(struct perf_event *event,
3456 struct perf_mmap_event *mmap_event)
3458 struct perf_output_handle handle;
3459 int size = mmap_event->event_id.header.size;
3460 int ret = perf_output_begin(&handle, event, size, 0, 0);
3465 mmap_event->event_id.pid = perf_event_pid(event, current);
3466 mmap_event->event_id.tid = perf_event_tid(event, current);
3468 perf_output_put(&handle, mmap_event->event_id);
3469 perf_output_copy(&handle, mmap_event->file_name,
3470 mmap_event->file_size);
3471 perf_output_end(&handle);
3474 static int perf_event_mmap_match(struct perf_event *event,
3475 struct perf_mmap_event *mmap_event)
3477 if (event->attr.mmap)
3483 static void perf_event_mmap_ctx(struct perf_event_context *ctx,
3484 struct perf_mmap_event *mmap_event)
3486 struct perf_event *event;
3488 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3489 if (perf_event_mmap_match(event, mmap_event))
3490 perf_event_mmap_output(event, mmap_event);
3494 static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
3496 struct perf_cpu_context *cpuctx;
3497 struct perf_event_context *ctx;
3498 struct vm_area_struct *vma = mmap_event->vma;
3499 struct file *file = vma->vm_file;
3505 memset(tmp, 0, sizeof(tmp));
3509 * d_path works from the end of the buffer backwards, so we
3510 * need to add enough zero bytes after the string to handle
3511 * the 64bit alignment we do later.
3513 buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
3515 name = strncpy(tmp, "//enomem", sizeof(tmp));
3518 name = d_path(&file->f_path, buf, PATH_MAX);
3520 name = strncpy(tmp, "//toolong", sizeof(tmp));
3524 if (arch_vma_name(mmap_event->vma)) {
3525 name = strncpy(tmp, arch_vma_name(mmap_event->vma),
3531 name = strncpy(tmp, "[vdso]", sizeof(tmp));
3535 name = strncpy(tmp, "//anon", sizeof(tmp));
3540 size = ALIGN(strlen(name)+1, sizeof(u64));
3542 mmap_event->file_name = name;
3543 mmap_event->file_size = size;
3545 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
3548 cpuctx = &get_cpu_var(perf_cpu_context);
3549 perf_event_mmap_ctx(&cpuctx->ctx, mmap_event);
3550 put_cpu_var(perf_cpu_context);
3553 * doesn't really matter which of the child contexts the
3554 * events ends up in.
3556 ctx = rcu_dereference(current->perf_event_ctxp);
3558 perf_event_mmap_ctx(ctx, mmap_event);
3564 void __perf_event_mmap(struct vm_area_struct *vma)
3566 struct perf_mmap_event mmap_event;
3568 if (!atomic_read(&nr_mmap_events))
3571 mmap_event = (struct perf_mmap_event){
3577 .type = PERF_RECORD_MMAP,
3583 .start = vma->vm_start,
3584 .len = vma->vm_end - vma->vm_start,
3585 .pgoff = vma->vm_pgoff,
3589 perf_event_mmap_event(&mmap_event);
3593 * IRQ throttle logging
3596 static void perf_log_throttle(struct perf_event *event, int enable)
3598 struct perf_output_handle handle;
3602 struct perf_event_header header;
3606 } throttle_event = {
3608 .type = PERF_RECORD_THROTTLE,
3610 .size = sizeof(throttle_event),
3612 .time = perf_clock(),
3613 .id = primary_event_id(event),
3614 .stream_id = event->id,
3618 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
3620 ret = perf_output_begin(&handle, event, sizeof(throttle_event), 1, 0);
3624 perf_output_put(&handle, throttle_event);
3625 perf_output_end(&handle);
3629 * Generic event overflow handling, sampling.
3632 static int __perf_event_overflow(struct perf_event *event, int nmi,
3633 int throttle, struct perf_sample_data *data,
3634 struct pt_regs *regs)
3636 int events = atomic_read(&event->event_limit);
3637 struct hw_perf_event *hwc = &event->hw;
3640 throttle = (throttle && event->pmu->unthrottle != NULL);
3645 if (hwc->interrupts != MAX_INTERRUPTS) {
3647 if (HZ * hwc->interrupts >
3648 (u64)sysctl_perf_event_sample_rate) {
3649 hwc->interrupts = MAX_INTERRUPTS;
3650 perf_log_throttle(event, 0);
3655 * Keep re-disabling events even though on the previous
3656 * pass we disabled it - just in case we raced with a
3657 * sched-in and the event got enabled again:
3663 if (event->attr.freq) {
3664 u64 now = perf_clock();
3665 s64 delta = now - hwc->freq_stamp;
3667 hwc->freq_stamp = now;
3669 if (delta > 0 && delta < TICK_NSEC)
3670 perf_adjust_period(event, NSEC_PER_SEC / (int)delta);
3674 * XXX event_limit might not quite work as expected on inherited
3678 event->pending_kill = POLL_IN;
3679 if (events && atomic_dec_and_test(&event->event_limit)) {
3681 event->pending_kill = POLL_HUP;
3683 event->pending_disable = 1;
3684 perf_pending_queue(&event->pending,
3685 perf_pending_event);
3687 perf_event_disable(event);
3690 if (event->overflow_handler)
3691 event->overflow_handler(event, nmi, data, regs);
3693 perf_event_output(event, nmi, data, regs);
3698 int perf_event_overflow(struct perf_event *event, int nmi,
3699 struct perf_sample_data *data,
3700 struct pt_regs *regs)
3702 return __perf_event_overflow(event, nmi, 1, data, regs);
3706 * Generic software event infrastructure
3710 * We directly increment event->count and keep a second value in
3711 * event->hw.period_left to count intervals. This period event
3712 * is kept in the range [-sample_period, 0] so that we can use the
3716 static u64 perf_swevent_set_period(struct perf_event *event)
3718 struct hw_perf_event *hwc = &event->hw;
3719 u64 period = hwc->last_period;
3723 hwc->last_period = hwc->sample_period;
3726 old = val = atomic64_read(&hwc->period_left);
3730 nr = div64_u64(period + val, period);
3731 offset = nr * period;
3733 if (atomic64_cmpxchg(&hwc->period_left, old, val) != old)
3739 static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
3740 int nmi, struct perf_sample_data *data,
3741 struct pt_regs *regs)
3743 struct hw_perf_event *hwc = &event->hw;
3746 data->period = event->hw.last_period;
3748 overflow = perf_swevent_set_period(event);
3750 if (hwc->interrupts == MAX_INTERRUPTS)
3753 for (; overflow; overflow--) {
3754 if (__perf_event_overflow(event, nmi, throttle,
3757 * We inhibit the overflow from happening when
3758 * hwc->interrupts == MAX_INTERRUPTS.
3766 static void perf_swevent_unthrottle(struct perf_event *event)
3769 * Nothing to do, we already reset hwc->interrupts.
3773 static void perf_swevent_add(struct perf_event *event, u64 nr,
3774 int nmi, struct perf_sample_data *data,
3775 struct pt_regs *regs)
3777 struct hw_perf_event *hwc = &event->hw;
3779 atomic64_add(nr, &event->count);
3784 if (!hwc->sample_period)
3787 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
3788 return perf_swevent_overflow(event, 1, nmi, data, regs);
3790 if (atomic64_add_negative(nr, &hwc->period_left))
3793 perf_swevent_overflow(event, 0, nmi, data, regs);
3796 static int perf_swevent_is_counting(struct perf_event *event)
3799 * The event is active, we're good!
3801 if (event->state == PERF_EVENT_STATE_ACTIVE)
3805 * The event is off/error, not counting.
3807 if (event->state != PERF_EVENT_STATE_INACTIVE)
3811 * The event is inactive, if the context is active
3812 * we're part of a group that didn't make it on the 'pmu',
3815 if (event->ctx->is_active)
3819 * We're inactive and the context is too, this means the
3820 * task is scheduled out, we're counting events that happen
3821 * to us, like migration events.
3826 static int perf_tp_event_match(struct perf_event *event,
3827 struct perf_sample_data *data);
3829 static int perf_swevent_match(struct perf_event *event,
3830 enum perf_type_id type,
3832 struct perf_sample_data *data,
3833 struct pt_regs *regs)
3835 if (!perf_swevent_is_counting(event))
3838 if (event->attr.type != type)
3840 if (event->attr.config != event_id)
3844 if (event->attr.exclude_user && user_mode(regs))
3847 if (event->attr.exclude_kernel && !user_mode(regs))
3851 if (event->attr.type == PERF_TYPE_TRACEPOINT &&
3852 !perf_tp_event_match(event, data))
3858 static void perf_swevent_ctx_event(struct perf_event_context *ctx,
3859 enum perf_type_id type,
3860 u32 event_id, u64 nr, int nmi,
3861 struct perf_sample_data *data,
3862 struct pt_regs *regs)
3864 struct perf_event *event;
3866 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3867 if (perf_swevent_match(event, type, event_id, data, regs))
3868 perf_swevent_add(event, nr, nmi, data, regs);
3873 * Must be called with preemption disabled
3875 int perf_swevent_get_recursion_context(int **recursion)
3877 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
3880 *recursion = &cpuctx->recursion[3];
3882 *recursion = &cpuctx->recursion[2];
3883 else if (in_softirq())
3884 *recursion = &cpuctx->recursion[1];
3886 *recursion = &cpuctx->recursion[0];
3895 EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
3897 void perf_swevent_put_recursion_context(int *recursion)
3901 EXPORT_SYMBOL_GPL(perf_swevent_put_recursion_context);
3903 static void __do_perf_sw_event(enum perf_type_id type, u32 event_id,
3905 struct perf_sample_data *data,
3906 struct pt_regs *regs)
3908 struct perf_event_context *ctx;
3909 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
3912 perf_swevent_ctx_event(&cpuctx->ctx, type, event_id,
3913 nr, nmi, data, regs);
3915 * doesn't really matter which of the child contexts the
3916 * events ends up in.
3918 ctx = rcu_dereference(current->perf_event_ctxp);
3920 perf_swevent_ctx_event(ctx, type, event_id, nr, nmi, data, regs);
3924 static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
3926 struct perf_sample_data *data,
3927 struct pt_regs *regs)
3933 if (perf_swevent_get_recursion_context(&recursion))
3936 __do_perf_sw_event(type, event_id, nr, nmi, data, regs);
3938 perf_swevent_put_recursion_context(recursion);
3943 void __perf_sw_event(u32 event_id, u64 nr, int nmi,
3944 struct pt_regs *regs, u64 addr)
3946 struct perf_sample_data data;
3951 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, nmi, &data, regs);
3954 static void perf_swevent_read(struct perf_event *event)
3958 static int perf_swevent_enable(struct perf_event *event)
3960 struct hw_perf_event *hwc = &event->hw;
3962 if (hwc->sample_period) {
3963 hwc->last_period = hwc->sample_period;
3964 perf_swevent_set_period(event);
3969 static void perf_swevent_disable(struct perf_event *event)
3973 static const struct pmu perf_ops_generic = {
3974 .enable = perf_swevent_enable,
3975 .disable = perf_swevent_disable,
3976 .read = perf_swevent_read,
3977 .unthrottle = perf_swevent_unthrottle,
3981 * hrtimer based swevent callback
3984 static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
3986 enum hrtimer_restart ret = HRTIMER_RESTART;
3987 struct perf_sample_data data;
3988 struct pt_regs *regs;
3989 struct perf_event *event;
3992 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
3993 event->pmu->read(event);
3996 regs = get_irq_regs();
3998 * In case we exclude kernel IPs or are somehow not in interrupt
3999 * context, provide the next best thing, the user IP.
4001 if ((event->attr.exclude_kernel || !regs) &&
4002 !event->attr.exclude_user)
4003 regs = task_pt_regs(current);
4006 if (!(event->attr.exclude_idle && current->pid == 0))
4007 if (perf_event_overflow(event, 0, &data, regs))
4008 ret = HRTIMER_NORESTART;
4011 period = max_t(u64, 10000, event->hw.sample_period);
4012 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
4017 static void perf_swevent_start_hrtimer(struct perf_event *event)
4019 struct hw_perf_event *hwc = &event->hw;
4021 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
4022 hwc->hrtimer.function = perf_swevent_hrtimer;
4023 if (hwc->sample_period) {
4026 if (hwc->remaining) {
4027 if (hwc->remaining < 0)
4030 period = hwc->remaining;
4033 period = max_t(u64, 10000, hwc->sample_period);
4035 __hrtimer_start_range_ns(&hwc->hrtimer,
4036 ns_to_ktime(period), 0,
4037 HRTIMER_MODE_REL, 0);
4041 static void perf_swevent_cancel_hrtimer(struct perf_event *event)
4043 struct hw_perf_event *hwc = &event->hw;
4045 if (hwc->sample_period) {
4046 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
4047 hwc->remaining = ktime_to_ns(remaining);
4049 hrtimer_cancel(&hwc->hrtimer);
4054 * Software event: cpu wall time clock
4057 static void cpu_clock_perf_event_update(struct perf_event *event)
4059 int cpu = raw_smp_processor_id();
4063 now = cpu_clock(cpu);
4064 prev = atomic64_read(&event->hw.prev_count);
4065 atomic64_set(&event->hw.prev_count, now);
4066 atomic64_add(now - prev, &event->count);
4069 static int cpu_clock_perf_event_enable(struct perf_event *event)
4071 struct hw_perf_event *hwc = &event->hw;
4072 int cpu = raw_smp_processor_id();
4074 atomic64_set(&hwc->prev_count, cpu_clock(cpu));
4075 perf_swevent_start_hrtimer(event);
4080 static void cpu_clock_perf_event_disable(struct perf_event *event)
4082 perf_swevent_cancel_hrtimer(event);
4083 cpu_clock_perf_event_update(event);
4086 static void cpu_clock_perf_event_read(struct perf_event *event)
4088 cpu_clock_perf_event_update(event);
4091 static const struct pmu perf_ops_cpu_clock = {
4092 .enable = cpu_clock_perf_event_enable,
4093 .disable = cpu_clock_perf_event_disable,
4094 .read = cpu_clock_perf_event_read,
4098 * Software event: task time clock
4101 static void task_clock_perf_event_update(struct perf_event *event, u64 now)
4106 prev = atomic64_xchg(&event->hw.prev_count, now);
4108 atomic64_add(delta, &event->count);
4111 static int task_clock_perf_event_enable(struct perf_event *event)
4113 struct hw_perf_event *hwc = &event->hw;
4116 now = event->ctx->time;
4118 atomic64_set(&hwc->prev_count, now);
4120 perf_swevent_start_hrtimer(event);
4125 static void task_clock_perf_event_disable(struct perf_event *event)
4127 perf_swevent_cancel_hrtimer(event);
4128 task_clock_perf_event_update(event, event->ctx->time);
4132 static void task_clock_perf_event_read(struct perf_event *event)
4137 update_context_time(event->ctx);
4138 time = event->ctx->time;
4140 u64 now = perf_clock();
4141 u64 delta = now - event->ctx->timestamp;
4142 time = event->ctx->time + delta;
4145 task_clock_perf_event_update(event, time);
4148 static const struct pmu perf_ops_task_clock = {
4149 .enable = task_clock_perf_event_enable,
4150 .disable = task_clock_perf_event_disable,
4151 .read = task_clock_perf_event_read,
4154 #ifdef CONFIG_EVENT_PROFILE
4156 void perf_tp_event(int event_id, u64 addr, u64 count, void *record,
4159 struct perf_raw_record raw = {
4164 struct perf_sample_data data = {
4169 struct pt_regs *regs = get_irq_regs();
4172 regs = task_pt_regs(current);
4174 /* Trace events already protected against recursion */
4175 __do_perf_sw_event(PERF_TYPE_TRACEPOINT, event_id, count, 1,
4178 EXPORT_SYMBOL_GPL(perf_tp_event);
4180 static int perf_tp_event_match(struct perf_event *event,
4181 struct perf_sample_data *data)
4183 void *record = data->raw->data;
4185 if (likely(!event->filter) || filter_match_preds(event->filter, record))
4190 static void tp_perf_event_destroy(struct perf_event *event)
4192 ftrace_profile_disable(event->attr.config);
4195 static const struct pmu *tp_perf_event_init(struct perf_event *event)
4198 * Raw tracepoint data is a severe data leak, only allow root to
4201 if ((event->attr.sample_type & PERF_SAMPLE_RAW) &&
4202 perf_paranoid_tracepoint_raw() &&
4203 !capable(CAP_SYS_ADMIN))
4204 return ERR_PTR(-EPERM);
4206 if (ftrace_profile_enable(event->attr.config))
4209 event->destroy = tp_perf_event_destroy;
4211 return &perf_ops_generic;
4214 static int perf_event_set_filter(struct perf_event *event, void __user *arg)
4219 if (event->attr.type != PERF_TYPE_TRACEPOINT)
4222 filter_str = strndup_user(arg, PAGE_SIZE);
4223 if (IS_ERR(filter_str))
4224 return PTR_ERR(filter_str);
4226 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
4232 static void perf_event_free_filter(struct perf_event *event)
4234 ftrace_profile_free_filter(event);
4239 static int perf_tp_event_match(struct perf_event *event,
4240 struct perf_sample_data *data)
4245 static const struct pmu *tp_perf_event_init(struct perf_event *event)
4250 static int perf_event_set_filter(struct perf_event *event, void __user *arg)
4255 static void perf_event_free_filter(struct perf_event *event)
4259 #endif /* CONFIG_EVENT_PROFILE */
4261 #ifdef CONFIG_HAVE_HW_BREAKPOINT
4262 static void bp_perf_event_destroy(struct perf_event *event)
4264 release_bp_slot(event);
4267 static const struct pmu *bp_perf_event_init(struct perf_event *bp)
4271 * The breakpoint is already filled if we haven't created the counter
4272 * through perf syscall
4273 * FIXME: manage to get trigerred to NULL if it comes from syscalls
4276 err = register_perf_hw_breakpoint(bp);
4278 err = __register_perf_hw_breakpoint(bp);
4280 return ERR_PTR(err);
4282 bp->destroy = bp_perf_event_destroy;
4284 return &perf_ops_bp;
4287 void perf_bp_event(struct perf_event *bp, void *regs)
4292 static void bp_perf_event_destroy(struct perf_event *event)
4296 static const struct pmu *bp_perf_event_init(struct perf_event *bp)
4301 void perf_bp_event(struct perf_event *bp, void *regs)
4306 atomic_t perf_swevent_enabled[PERF_COUNT_SW_MAX];
4308 static void sw_perf_event_destroy(struct perf_event *event)
4310 u64 event_id = event->attr.config;
4312 WARN_ON(event->parent);
4314 atomic_dec(&perf_swevent_enabled[event_id]);
4317 static const struct pmu *sw_perf_event_init(struct perf_event *event)
4319 const struct pmu *pmu = NULL;
4320 u64 event_id = event->attr.config;
4323 * Software events (currently) can't in general distinguish
4324 * between user, kernel and hypervisor events.
4325 * However, context switches and cpu migrations are considered
4326 * to be kernel events, and page faults are never hypervisor
4330 case PERF_COUNT_SW_CPU_CLOCK:
4331 pmu = &perf_ops_cpu_clock;
4334 case PERF_COUNT_SW_TASK_CLOCK:
4336 * If the user instantiates this as a per-cpu event,
4337 * use the cpu_clock event instead.
4339 if (event->ctx->task)
4340 pmu = &perf_ops_task_clock;
4342 pmu = &perf_ops_cpu_clock;
4345 case PERF_COUNT_SW_PAGE_FAULTS:
4346 case PERF_COUNT_SW_PAGE_FAULTS_MIN:
4347 case PERF_COUNT_SW_PAGE_FAULTS_MAJ:
4348 case PERF_COUNT_SW_CONTEXT_SWITCHES:
4349 case PERF_COUNT_SW_CPU_MIGRATIONS:
4350 case PERF_COUNT_SW_ALIGNMENT_FAULTS:
4351 case PERF_COUNT_SW_EMULATION_FAULTS:
4352 if (!event->parent) {
4353 atomic_inc(&perf_swevent_enabled[event_id]);
4354 event->destroy = sw_perf_event_destroy;
4356 pmu = &perf_ops_generic;
4364 * Allocate and initialize a event structure
4366 static struct perf_event *
4367 perf_event_alloc(struct perf_event_attr *attr,
4369 struct perf_event_context *ctx,
4370 struct perf_event *group_leader,
4371 struct perf_event *parent_event,
4372 perf_callback_t callback,
4375 const struct pmu *pmu;
4376 struct perf_event *event;
4377 struct hw_perf_event *hwc;
4380 event = kzalloc(sizeof(*event), gfpflags);
4382 return ERR_PTR(-ENOMEM);
4385 * Single events are their own group leaders, with an
4386 * empty sibling list:
4389 group_leader = event;
4391 mutex_init(&event->child_mutex);
4392 INIT_LIST_HEAD(&event->child_list);
4394 INIT_LIST_HEAD(&event->group_entry);
4395 INIT_LIST_HEAD(&event->event_entry);
4396 INIT_LIST_HEAD(&event->sibling_list);
4397 init_waitqueue_head(&event->waitq);
4399 mutex_init(&event->mmap_mutex);
4402 event->attr = *attr;
4403 event->group_leader = group_leader;
4408 event->parent = parent_event;
4410 event->ns = get_pid_ns(current->nsproxy->pid_ns);
4411 event->id = atomic64_inc_return(&perf_event_id);
4413 event->state = PERF_EVENT_STATE_INACTIVE;
4415 if (!callback && parent_event)
4416 callback = parent_event->callback;
4418 event->callback = callback;
4421 event->state = PERF_EVENT_STATE_OFF;
4426 hwc->sample_period = attr->sample_period;
4427 if (attr->freq && attr->sample_freq)
4428 hwc->sample_period = 1;
4429 hwc->last_period = hwc->sample_period;
4431 atomic64_set(&hwc->period_left, hwc->sample_period);
4434 * we currently do not support PERF_FORMAT_GROUP on inherited events
4436 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
4439 switch (attr->type) {
4441 case PERF_TYPE_HARDWARE:
4442 case PERF_TYPE_HW_CACHE:
4443 pmu = hw_perf_event_init(event);
4446 case PERF_TYPE_SOFTWARE:
4447 pmu = sw_perf_event_init(event);
4450 case PERF_TYPE_TRACEPOINT:
4451 pmu = tp_perf_event_init(event);
4454 case PERF_TYPE_BREAKPOINT:
4455 pmu = bp_perf_event_init(event);
4466 else if (IS_ERR(pmu))
4471 put_pid_ns(event->ns);
4473 return ERR_PTR(err);
4478 if (!event->parent) {
4479 atomic_inc(&nr_events);
4480 if (event->attr.mmap)
4481 atomic_inc(&nr_mmap_events);
4482 if (event->attr.comm)
4483 atomic_inc(&nr_comm_events);
4484 if (event->attr.task)
4485 atomic_inc(&nr_task_events);
4491 static int perf_copy_attr(struct perf_event_attr __user *uattr,
4492 struct perf_event_attr *attr)
4497 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
4501 * zero the full structure, so that a short copy will be nice.
4503 memset(attr, 0, sizeof(*attr));
4505 ret = get_user(size, &uattr->size);
4509 if (size > PAGE_SIZE) /* silly large */
4512 if (!size) /* abi compat */
4513 size = PERF_ATTR_SIZE_VER0;
4515 if (size < PERF_ATTR_SIZE_VER0)
4519 * If we're handed a bigger struct than we know of,
4520 * ensure all the unknown bits are 0 - i.e. new
4521 * user-space does not rely on any kernel feature
4522 * extensions we dont know about yet.
4524 if (size > sizeof(*attr)) {
4525 unsigned char __user *addr;
4526 unsigned char __user *end;
4529 addr = (void __user *)uattr + sizeof(*attr);
4530 end = (void __user *)uattr + size;
4532 for (; addr < end; addr++) {
4533 ret = get_user(val, addr);
4539 size = sizeof(*attr);
4542 ret = copy_from_user(attr, uattr, size);
4547 * If the type exists, the corresponding creation will verify
4550 if (attr->type >= PERF_TYPE_MAX)
4553 if (attr->__reserved_1 || attr->__reserved_2 || attr->__reserved_3)
4556 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
4559 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
4566 put_user(sizeof(*attr), &uattr->size);
4571 static int perf_event_set_output(struct perf_event *event, int output_fd)
4573 struct perf_event *output_event = NULL;
4574 struct file *output_file = NULL;
4575 struct perf_event *old_output;
4576 int fput_needed = 0;
4582 output_file = fget_light(output_fd, &fput_needed);
4586 if (output_file->f_op != &perf_fops)
4589 output_event = output_file->private_data;
4591 /* Don't chain output fds */
4592 if (output_event->output)
4595 /* Don't set an output fd when we already have an output channel */
4599 atomic_long_inc(&output_file->f_count);
4602 mutex_lock(&event->mmap_mutex);
4603 old_output = event->output;
4604 rcu_assign_pointer(event->output, output_event);
4605 mutex_unlock(&event->mmap_mutex);
4609 * we need to make sure no existing perf_output_*()
4610 * is still referencing this event.
4613 fput(old_output->filp);
4618 fput_light(output_file, fput_needed);
4623 * sys_perf_event_open - open a performance event, associate it to a task/cpu
4625 * @attr_uptr: event_id type attributes for monitoring/sampling
4628 * @group_fd: group leader event fd
4630 SYSCALL_DEFINE5(perf_event_open,
4631 struct perf_event_attr __user *, attr_uptr,
4632 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
4634 struct perf_event *event, *group_leader;
4635 struct perf_event_attr attr;
4636 struct perf_event_context *ctx;
4637 struct file *event_file = NULL;
4638 struct file *group_file = NULL;
4639 int fput_needed = 0;
4640 int fput_needed2 = 0;
4643 /* for future expandability... */
4644 if (flags & ~(PERF_FLAG_FD_NO_GROUP | PERF_FLAG_FD_OUTPUT))
4647 err = perf_copy_attr(attr_uptr, &attr);
4651 if (!attr.exclude_kernel) {
4652 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
4657 if (attr.sample_freq > sysctl_perf_event_sample_rate)
4662 * Get the target context (task or percpu):
4664 ctx = find_get_context(pid, cpu);
4666 return PTR_ERR(ctx);
4669 * Look up the group leader (we will attach this event to it):
4671 group_leader = NULL;
4672 if (group_fd != -1 && !(flags & PERF_FLAG_FD_NO_GROUP)) {
4674 group_file = fget_light(group_fd, &fput_needed);
4676 goto err_put_context;
4677 if (group_file->f_op != &perf_fops)
4678 goto err_put_context;
4680 group_leader = group_file->private_data;
4682 * Do not allow a recursive hierarchy (this new sibling
4683 * becoming part of another group-sibling):
4685 if (group_leader->group_leader != group_leader)
4686 goto err_put_context;
4688 * Do not allow to attach to a group in a different
4689 * task or CPU context:
4691 if (group_leader->ctx != ctx)
4692 goto err_put_context;
4694 * Only a group leader can be exclusive or pinned
4696 if (attr.exclusive || attr.pinned)
4697 goto err_put_context;
4700 event = perf_event_alloc(&attr, cpu, ctx, group_leader,
4701 NULL, NULL, GFP_KERNEL);
4702 err = PTR_ERR(event);
4704 goto err_put_context;
4706 err = anon_inode_getfd("[perf_event]", &perf_fops, event, 0);
4708 goto err_free_put_context;
4710 event_file = fget_light(err, &fput_needed2);
4712 goto err_free_put_context;
4714 if (flags & PERF_FLAG_FD_OUTPUT) {
4715 err = perf_event_set_output(event, group_fd);
4717 goto err_fput_free_put_context;
4720 event->filp = event_file;
4721 WARN_ON_ONCE(ctx->parent_ctx);
4722 mutex_lock(&ctx->mutex);
4723 perf_install_in_context(ctx, event, cpu);
4725 mutex_unlock(&ctx->mutex);
4727 event->owner = current;
4728 get_task_struct(current);
4729 mutex_lock(¤t->perf_event_mutex);
4730 list_add_tail(&event->owner_entry, ¤t->perf_event_list);
4731 mutex_unlock(¤t->perf_event_mutex);
4733 err_fput_free_put_context:
4734 fput_light(event_file, fput_needed2);
4736 err_free_put_context:
4744 fput_light(group_file, fput_needed);
4750 * perf_event_create_kernel_counter
4752 * @attr: attributes of the counter to create
4753 * @cpu: cpu in which the counter is bound
4754 * @pid: task to profile
4757 perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
4758 pid_t pid, perf_callback_t callback)
4760 struct perf_event *event;
4761 struct perf_event_context *ctx;
4765 * Get the target context (task or percpu):
4768 ctx = find_get_context(pid, cpu);
4772 event = perf_event_alloc(attr, cpu, ctx, NULL,
4773 NULL, callback, GFP_KERNEL);
4774 err = PTR_ERR(event);
4776 goto err_put_context;
4779 WARN_ON_ONCE(ctx->parent_ctx);
4780 mutex_lock(&ctx->mutex);
4781 perf_install_in_context(ctx, event, cpu);
4783 mutex_unlock(&ctx->mutex);
4785 event->owner = current;
4786 get_task_struct(current);
4787 mutex_lock(¤t->perf_event_mutex);
4788 list_add_tail(&event->owner_entry, ¤t->perf_event_list);
4789 mutex_unlock(¤t->perf_event_mutex);
4799 EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
4802 * inherit a event from parent task to child task:
4804 static struct perf_event *
4805 inherit_event(struct perf_event *parent_event,
4806 struct task_struct *parent,
4807 struct perf_event_context *parent_ctx,
4808 struct task_struct *child,
4809 struct perf_event *group_leader,
4810 struct perf_event_context *child_ctx)
4812 struct perf_event *child_event;
4815 * Instead of creating recursive hierarchies of events,
4816 * we link inherited events back to the original parent,
4817 * which has a filp for sure, which we use as the reference
4820 if (parent_event->parent)
4821 parent_event = parent_event->parent;
4823 child_event = perf_event_alloc(&parent_event->attr,
4824 parent_event->cpu, child_ctx,
4825 group_leader, parent_event,
4827 if (IS_ERR(child_event))
4832 * Make the child state follow the state of the parent event,
4833 * not its attr.disabled bit. We hold the parent's mutex,
4834 * so we won't race with perf_event_{en, dis}able_family.
4836 if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
4837 child_event->state = PERF_EVENT_STATE_INACTIVE;
4839 child_event->state = PERF_EVENT_STATE_OFF;
4841 if (parent_event->attr.freq)
4842 child_event->hw.sample_period = parent_event->hw.sample_period;
4844 child_event->overflow_handler = parent_event->overflow_handler;
4847 * Link it up in the child's context:
4849 add_event_to_ctx(child_event, child_ctx);
4852 * Get a reference to the parent filp - we will fput it
4853 * when the child event exits. This is safe to do because
4854 * we are in the parent and we know that the filp still
4855 * exists and has a nonzero count:
4857 atomic_long_inc(&parent_event->filp->f_count);
4860 * Link this into the parent event's child list
4862 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
4863 mutex_lock(&parent_event->child_mutex);
4864 list_add_tail(&child_event->child_list, &parent_event->child_list);
4865 mutex_unlock(&parent_event->child_mutex);
4870 static int inherit_group(struct perf_event *parent_event,
4871 struct task_struct *parent,
4872 struct perf_event_context *parent_ctx,
4873 struct task_struct *child,
4874 struct perf_event_context *child_ctx)
4876 struct perf_event *leader;
4877 struct perf_event *sub;
4878 struct perf_event *child_ctr;
4880 leader = inherit_event(parent_event, parent, parent_ctx,
4881 child, NULL, child_ctx);
4883 return PTR_ERR(leader);
4884 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
4885 child_ctr = inherit_event(sub, parent, parent_ctx,
4886 child, leader, child_ctx);
4887 if (IS_ERR(child_ctr))
4888 return PTR_ERR(child_ctr);
4893 static void sync_child_event(struct perf_event *child_event,
4894 struct task_struct *child)
4896 struct perf_event *parent_event = child_event->parent;
4899 if (child_event->attr.inherit_stat)
4900 perf_event_read_event(child_event, child);
4902 child_val = atomic64_read(&child_event->count);
4905 * Add back the child's count to the parent's count:
4907 atomic64_add(child_val, &parent_event->count);
4908 atomic64_add(child_event->total_time_enabled,
4909 &parent_event->child_total_time_enabled);
4910 atomic64_add(child_event->total_time_running,
4911 &parent_event->child_total_time_running);
4914 * Remove this event from the parent's list
4916 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
4917 mutex_lock(&parent_event->child_mutex);
4918 list_del_init(&child_event->child_list);
4919 mutex_unlock(&parent_event->child_mutex);
4922 * Release the parent event, if this was the last
4925 fput(parent_event->filp);
4929 __perf_event_exit_task(struct perf_event *child_event,
4930 struct perf_event_context *child_ctx,
4931 struct task_struct *child)
4933 struct perf_event *parent_event;
4935 perf_event_remove_from_context(child_event);
4937 parent_event = child_event->parent;
4939 * It can happen that parent exits first, and has events
4940 * that are still around due to the child reference. These
4941 * events need to be zapped - but otherwise linger.
4944 sync_child_event(child_event, child);
4945 free_event(child_event);
4950 * When a child task exits, feed back event values to parent events.
4952 void perf_event_exit_task(struct task_struct *child)
4954 struct perf_event *child_event, *tmp;
4955 struct perf_event_context *child_ctx;
4956 unsigned long flags;
4958 if (likely(!child->perf_event_ctxp)) {
4959 perf_event_task(child, NULL, 0);
4963 local_irq_save(flags);
4965 * We can't reschedule here because interrupts are disabled,
4966 * and either child is current or it is a task that can't be
4967 * scheduled, so we are now safe from rescheduling changing
4970 child_ctx = child->perf_event_ctxp;
4971 __perf_event_task_sched_out(child_ctx);
4974 * Take the context lock here so that if find_get_context is
4975 * reading child->perf_event_ctxp, we wait until it has
4976 * incremented the context's refcount before we do put_ctx below.
4978 spin_lock(&child_ctx->lock);
4979 child->perf_event_ctxp = NULL;
4981 * If this context is a clone; unclone it so it can't get
4982 * swapped to another process while we're removing all
4983 * the events from it.
4985 unclone_ctx(child_ctx);
4986 update_context_time(child_ctx);
4987 spin_unlock_irqrestore(&child_ctx->lock, flags);
4990 * Report the task dead after unscheduling the events so that we
4991 * won't get any samples after PERF_RECORD_EXIT. We can however still
4992 * get a few PERF_RECORD_READ events.
4994 perf_event_task(child, child_ctx, 0);
4997 * We can recurse on the same lock type through:
4999 * __perf_event_exit_task()
5000 * sync_child_event()
5001 * fput(parent_event->filp)
5003 * mutex_lock(&ctx->mutex)
5005 * But since its the parent context it won't be the same instance.
5007 mutex_lock_nested(&child_ctx->mutex, SINGLE_DEPTH_NESTING);
5010 list_for_each_entry_safe(child_event, tmp, &child_ctx->group_list,
5012 __perf_event_exit_task(child_event, child_ctx, child);
5015 * If the last event was a group event, it will have appended all
5016 * its siblings to the list, but we obtained 'tmp' before that which
5017 * will still point to the list head terminating the iteration.
5019 if (!list_empty(&child_ctx->group_list))
5022 mutex_unlock(&child_ctx->mutex);
5028 * free an unexposed, unused context as created by inheritance by
5029 * init_task below, used by fork() in case of fail.
5031 void perf_event_free_task(struct task_struct *task)
5033 struct perf_event_context *ctx = task->perf_event_ctxp;
5034 struct perf_event *event, *tmp;
5039 mutex_lock(&ctx->mutex);
5041 list_for_each_entry_safe(event, tmp, &ctx->group_list, group_entry) {
5042 struct perf_event *parent = event->parent;
5044 if (WARN_ON_ONCE(!parent))
5047 mutex_lock(&parent->child_mutex);
5048 list_del_init(&event->child_list);
5049 mutex_unlock(&parent->child_mutex);
5053 list_del_event(event, ctx);
5057 if (!list_empty(&ctx->group_list))
5060 mutex_unlock(&ctx->mutex);
5066 * Initialize the perf_event context in task_struct
5068 int perf_event_init_task(struct task_struct *child)
5070 struct perf_event_context *child_ctx, *parent_ctx;
5071 struct perf_event_context *cloned_ctx;
5072 struct perf_event *event;
5073 struct task_struct *parent = current;
5074 int inherited_all = 1;
5077 child->perf_event_ctxp = NULL;
5079 mutex_init(&child->perf_event_mutex);
5080 INIT_LIST_HEAD(&child->perf_event_list);
5082 if (likely(!parent->perf_event_ctxp))
5086 * This is executed from the parent task context, so inherit
5087 * events that have been marked for cloning.
5088 * First allocate and initialize a context for the child.
5091 child_ctx = kmalloc(sizeof(struct perf_event_context), GFP_KERNEL);
5095 __perf_event_init_context(child_ctx, child);
5096 child->perf_event_ctxp = child_ctx;
5097 get_task_struct(child);
5100 * If the parent's context is a clone, pin it so it won't get
5103 parent_ctx = perf_pin_task_context(parent);
5106 * No need to check if parent_ctx != NULL here; since we saw
5107 * it non-NULL earlier, the only reason for it to become NULL
5108 * is if we exit, and since we're currently in the middle of
5109 * a fork we can't be exiting at the same time.
5113 * Lock the parent list. No need to lock the child - not PID
5114 * hashed yet and not running, so nobody can access it.
5116 mutex_lock(&parent_ctx->mutex);
5119 * We dont have to disable NMIs - we are only looking at
5120 * the list, not manipulating it:
5122 list_for_each_entry(event, &parent_ctx->group_list, group_entry) {
5124 if (!event->attr.inherit) {
5129 ret = inherit_group(event, parent, parent_ctx,
5137 if (inherited_all) {
5139 * Mark the child context as a clone of the parent
5140 * context, or of whatever the parent is a clone of.
5141 * Note that if the parent is a clone, it could get
5142 * uncloned at any point, but that doesn't matter
5143 * because the list of events and the generation
5144 * count can't have changed since we took the mutex.
5146 cloned_ctx = rcu_dereference(parent_ctx->parent_ctx);
5148 child_ctx->parent_ctx = cloned_ctx;
5149 child_ctx->parent_gen = parent_ctx->parent_gen;
5151 child_ctx->parent_ctx = parent_ctx;
5152 child_ctx->parent_gen = parent_ctx->generation;
5154 get_ctx(child_ctx->parent_ctx);
5157 mutex_unlock(&parent_ctx->mutex);
5159 perf_unpin_context(parent_ctx);
5164 static void __cpuinit perf_event_init_cpu(int cpu)
5166 struct perf_cpu_context *cpuctx;
5168 cpuctx = &per_cpu(perf_cpu_context, cpu);
5169 __perf_event_init_context(&cpuctx->ctx, NULL);
5171 spin_lock(&perf_resource_lock);
5172 cpuctx->max_pertask = perf_max_events - perf_reserved_percpu;
5173 spin_unlock(&perf_resource_lock);
5175 hw_perf_event_setup(cpu);
5178 #ifdef CONFIG_HOTPLUG_CPU
5179 static void __perf_event_exit_cpu(void *info)
5181 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
5182 struct perf_event_context *ctx = &cpuctx->ctx;
5183 struct perf_event *event, *tmp;
5185 list_for_each_entry_safe(event, tmp, &ctx->group_list, group_entry)
5186 __perf_event_remove_from_context(event);
5188 static void perf_event_exit_cpu(int cpu)
5190 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
5191 struct perf_event_context *ctx = &cpuctx->ctx;
5193 mutex_lock(&ctx->mutex);
5194 smp_call_function_single(cpu, __perf_event_exit_cpu, NULL, 1);
5195 mutex_unlock(&ctx->mutex);
5198 static inline void perf_event_exit_cpu(int cpu) { }
5201 static int __cpuinit
5202 perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
5204 unsigned int cpu = (long)hcpu;
5208 case CPU_UP_PREPARE:
5209 case CPU_UP_PREPARE_FROZEN:
5210 perf_event_init_cpu(cpu);
5214 case CPU_ONLINE_FROZEN:
5215 hw_perf_event_setup_online(cpu);
5218 case CPU_DOWN_PREPARE:
5219 case CPU_DOWN_PREPARE_FROZEN:
5220 perf_event_exit_cpu(cpu);
5231 * This has to have a higher priority than migration_notifier in sched.c.
5233 static struct notifier_block __cpuinitdata perf_cpu_nb = {
5234 .notifier_call = perf_cpu_notify,
5238 void __init perf_event_init(void)
5240 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
5241 (void *)(long)smp_processor_id());
5242 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_ONLINE,
5243 (void *)(long)smp_processor_id());
5244 register_cpu_notifier(&perf_cpu_nb);
5247 static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
5249 return sprintf(buf, "%d\n", perf_reserved_percpu);
5253 perf_set_reserve_percpu(struct sysdev_class *class,
5257 struct perf_cpu_context *cpuctx;
5261 err = strict_strtoul(buf, 10, &val);
5264 if (val > perf_max_events)
5267 spin_lock(&perf_resource_lock);
5268 perf_reserved_percpu = val;
5269 for_each_online_cpu(cpu) {
5270 cpuctx = &per_cpu(perf_cpu_context, cpu);
5271 spin_lock_irq(&cpuctx->ctx.lock);
5272 mpt = min(perf_max_events - cpuctx->ctx.nr_events,
5273 perf_max_events - perf_reserved_percpu);
5274 cpuctx->max_pertask = mpt;
5275 spin_unlock_irq(&cpuctx->ctx.lock);
5277 spin_unlock(&perf_resource_lock);
5282 static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
5284 return sprintf(buf, "%d\n", perf_overcommit);
5288 perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
5293 err = strict_strtoul(buf, 10, &val);
5299 spin_lock(&perf_resource_lock);
5300 perf_overcommit = val;
5301 spin_unlock(&perf_resource_lock);
5306 static SYSDEV_CLASS_ATTR(
5309 perf_show_reserve_percpu,
5310 perf_set_reserve_percpu
5313 static SYSDEV_CLASS_ATTR(
5316 perf_show_overcommit,
5320 static struct attribute *perfclass_attrs[] = {
5321 &attr_reserve_percpu.attr,
5322 &attr_overcommit.attr,
5326 static struct attribute_group perfclass_attr_group = {
5327 .attrs = perfclass_attrs,
5328 .name = "perf_events",
5331 static int __init perf_event_sysfs_init(void)
5333 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
5334 &perfclass_attr_group);
5336 device_initcall(perf_event_sysfs_init);