usb: musb: am35x: give it a context structure
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / kernel / perf_event.c
blob517d827f498281da50210aa76a02b4693116154a
1 /*
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
12 #include <linux/fs.h>
13 #include <linux/mm.h>
14 #include <linux/cpu.h>
15 #include <linux/smp.h>
16 #include <linux/file.h>
17 #include <linux/poll.h>
18 #include <linux/slab.h>
19 #include <linux/hash.h>
20 #include <linux/sysfs.h>
21 #include <linux/dcache.h>
22 #include <linux/percpu.h>
23 #include <linux/ptrace.h>
24 #include <linux/vmstat.h>
25 #include <linux/vmalloc.h>
26 #include <linux/hardirq.h>
27 #include <linux/rculist.h>
28 #include <linux/uaccess.h>
29 #include <linux/syscalls.h>
30 #include <linux/anon_inodes.h>
31 #include <linux/kernel_stat.h>
32 #include <linux/perf_event.h>
33 #include <linux/ftrace_event.h>
35 #include <asm/irq_regs.h>
37 atomic_t perf_task_events __read_mostly;
38 static atomic_t nr_mmap_events __read_mostly;
39 static atomic_t nr_comm_events __read_mostly;
40 static atomic_t nr_task_events __read_mostly;
42 static LIST_HEAD(pmus);
43 static DEFINE_MUTEX(pmus_lock);
44 static struct srcu_struct pmus_srcu;
47 * perf event paranoia level:
48 * -1 - not paranoid at all
49 * 0 - disallow raw tracepoint access for unpriv
50 * 1 - disallow cpu events for unpriv
51 * 2 - disallow kernel profiling for unpriv
53 int sysctl_perf_event_paranoid __read_mostly = 1;
55 int sysctl_perf_event_mlock __read_mostly = 512; /* 'free' kb per user */
58 * max perf event sample rate
60 int sysctl_perf_event_sample_rate __read_mostly = 100000;
62 static atomic64_t perf_event_id;
64 void __weak perf_event_print_debug(void) { }
66 extern __weak const char *perf_pmu_name(void)
68 return "pmu";
71 void perf_pmu_disable(struct pmu *pmu)
73 int *count = this_cpu_ptr(pmu->pmu_disable_count);
74 if (!(*count)++)
75 pmu->pmu_disable(pmu);
78 void perf_pmu_enable(struct pmu *pmu)
80 int *count = this_cpu_ptr(pmu->pmu_disable_count);
81 if (!--(*count))
82 pmu->pmu_enable(pmu);
85 static DEFINE_PER_CPU(struct list_head, rotation_list);
88 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
89 * because they're strictly cpu affine and rotate_start is called with IRQs
90 * disabled, while rotate_context is called from IRQ context.
92 static void perf_pmu_rotate_start(struct pmu *pmu)
94 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
95 struct list_head *head = &__get_cpu_var(rotation_list);
97 WARN_ON(!irqs_disabled());
99 if (list_empty(&cpuctx->rotation_list))
100 list_add(&cpuctx->rotation_list, head);
103 static void get_ctx(struct perf_event_context *ctx)
105 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
108 static void free_ctx(struct rcu_head *head)
110 struct perf_event_context *ctx;
112 ctx = container_of(head, struct perf_event_context, rcu_head);
113 kfree(ctx);
116 static void put_ctx(struct perf_event_context *ctx)
118 if (atomic_dec_and_test(&ctx->refcount)) {
119 if (ctx->parent_ctx)
120 put_ctx(ctx->parent_ctx);
121 if (ctx->task)
122 put_task_struct(ctx->task);
123 call_rcu(&ctx->rcu_head, free_ctx);
127 static void unclone_ctx(struct perf_event_context *ctx)
129 if (ctx->parent_ctx) {
130 put_ctx(ctx->parent_ctx);
131 ctx->parent_ctx = NULL;
136 * If we inherit events we want to return the parent event id
137 * to userspace.
139 static u64 primary_event_id(struct perf_event *event)
141 u64 id = event->id;
143 if (event->parent)
144 id = event->parent->id;
146 return id;
150 * Get the perf_event_context for a task and lock it.
151 * This has to cope with with the fact that until it is locked,
152 * the context could get moved to another task.
154 static struct perf_event_context *
155 perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
157 struct perf_event_context *ctx;
159 rcu_read_lock();
160 retry:
161 ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
162 if (ctx) {
164 * If this context is a clone of another, it might
165 * get swapped for another underneath us by
166 * perf_event_task_sched_out, though the
167 * rcu_read_lock() protects us from any context
168 * getting freed. Lock the context and check if it
169 * got swapped before we could get the lock, and retry
170 * if so. If we locked the right context, then it
171 * can't get swapped on us any more.
173 raw_spin_lock_irqsave(&ctx->lock, *flags);
174 if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
175 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
176 goto retry;
179 if (!atomic_inc_not_zero(&ctx->refcount)) {
180 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
181 ctx = NULL;
184 rcu_read_unlock();
185 return ctx;
189 * Get the context for a task and increment its pin_count so it
190 * can't get swapped to another task. This also increments its
191 * reference count so that the context can't get freed.
193 static struct perf_event_context *
194 perf_pin_task_context(struct task_struct *task, int ctxn)
196 struct perf_event_context *ctx;
197 unsigned long flags;
199 ctx = perf_lock_task_context(task, ctxn, &flags);
200 if (ctx) {
201 ++ctx->pin_count;
202 raw_spin_unlock_irqrestore(&ctx->lock, flags);
204 return ctx;
207 static void perf_unpin_context(struct perf_event_context *ctx)
209 unsigned long flags;
211 raw_spin_lock_irqsave(&ctx->lock, flags);
212 --ctx->pin_count;
213 raw_spin_unlock_irqrestore(&ctx->lock, flags);
214 put_ctx(ctx);
217 static inline u64 perf_clock(void)
219 return local_clock();
223 * Update the record of the current time in a context.
225 static void update_context_time(struct perf_event_context *ctx)
227 u64 now = perf_clock();
229 ctx->time += now - ctx->timestamp;
230 ctx->timestamp = now;
234 * Update the total_time_enabled and total_time_running fields for a event.
236 static void update_event_times(struct perf_event *event)
238 struct perf_event_context *ctx = event->ctx;
239 u64 run_end;
241 if (event->state < PERF_EVENT_STATE_INACTIVE ||
242 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
243 return;
245 if (ctx->is_active)
246 run_end = ctx->time;
247 else
248 run_end = event->tstamp_stopped;
250 event->total_time_enabled = run_end - event->tstamp_enabled;
252 if (event->state == PERF_EVENT_STATE_INACTIVE)
253 run_end = event->tstamp_stopped;
254 else
255 run_end = ctx->time;
257 event->total_time_running = run_end - event->tstamp_running;
261 * Update total_time_enabled and total_time_running for all events in a group.
263 static void update_group_times(struct perf_event *leader)
265 struct perf_event *event;
267 update_event_times(leader);
268 list_for_each_entry(event, &leader->sibling_list, group_entry)
269 update_event_times(event);
272 static struct list_head *
273 ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
275 if (event->attr.pinned)
276 return &ctx->pinned_groups;
277 else
278 return &ctx->flexible_groups;
282 * Add a event from the lists for its context.
283 * Must be called with ctx->mutex and ctx->lock held.
285 static void
286 list_add_event(struct perf_event *event, struct perf_event_context *ctx)
288 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
289 event->attach_state |= PERF_ATTACH_CONTEXT;
292 * If we're a stand alone event or group leader, we go to the context
293 * list, group events are kept attached to the group so that
294 * perf_group_detach can, at all times, locate all siblings.
296 if (event->group_leader == event) {
297 struct list_head *list;
299 if (is_software_event(event))
300 event->group_flags |= PERF_GROUP_SOFTWARE;
302 list = ctx_group_list(event, ctx);
303 list_add_tail(&event->group_entry, list);
306 list_add_rcu(&event->event_entry, &ctx->event_list);
307 if (!ctx->nr_events)
308 perf_pmu_rotate_start(ctx->pmu);
309 ctx->nr_events++;
310 if (event->attr.inherit_stat)
311 ctx->nr_stat++;
314 static void perf_group_attach(struct perf_event *event)
316 struct perf_event *group_leader = event->group_leader;
319 * We can have double attach due to group movement in perf_event_open.
321 if (event->attach_state & PERF_ATTACH_GROUP)
322 return;
324 event->attach_state |= PERF_ATTACH_GROUP;
326 if (group_leader == event)
327 return;
329 if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
330 !is_software_event(event))
331 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
333 list_add_tail(&event->group_entry, &group_leader->sibling_list);
334 group_leader->nr_siblings++;
338 * Remove a event from the lists for its context.
339 * Must be called with ctx->mutex and ctx->lock held.
341 static void
342 list_del_event(struct perf_event *event, struct perf_event_context *ctx)
345 * We can have double detach due to exit/hot-unplug + close.
347 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
348 return;
350 event->attach_state &= ~PERF_ATTACH_CONTEXT;
352 ctx->nr_events--;
353 if (event->attr.inherit_stat)
354 ctx->nr_stat--;
356 list_del_rcu(&event->event_entry);
358 if (event->group_leader == event)
359 list_del_init(&event->group_entry);
361 update_group_times(event);
364 * If event was in error state, then keep it
365 * that way, otherwise bogus counts will be
366 * returned on read(). The only way to get out
367 * of error state is by explicit re-enabling
368 * of the event
370 if (event->state > PERF_EVENT_STATE_OFF)
371 event->state = PERF_EVENT_STATE_OFF;
374 static void perf_group_detach(struct perf_event *event)
376 struct perf_event *sibling, *tmp;
377 struct list_head *list = NULL;
380 * We can have double detach due to exit/hot-unplug + close.
382 if (!(event->attach_state & PERF_ATTACH_GROUP))
383 return;
385 event->attach_state &= ~PERF_ATTACH_GROUP;
388 * If this is a sibling, remove it from its group.
390 if (event->group_leader != event) {
391 list_del_init(&event->group_entry);
392 event->group_leader->nr_siblings--;
393 return;
396 if (!list_empty(&event->group_entry))
397 list = &event->group_entry;
400 * If this was a group event with sibling events then
401 * upgrade the siblings to singleton events by adding them
402 * to whatever list we are on.
404 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
405 if (list)
406 list_move_tail(&sibling->group_entry, list);
407 sibling->group_leader = sibling;
409 /* Inherit group flags from the previous leader */
410 sibling->group_flags = event->group_flags;
414 static inline int
415 event_filter_match(struct perf_event *event)
417 return event->cpu == -1 || event->cpu == smp_processor_id();
420 static void
421 event_sched_out(struct perf_event *event,
422 struct perf_cpu_context *cpuctx,
423 struct perf_event_context *ctx)
425 u64 delta;
427 * An event which could not be activated because of
428 * filter mismatch still needs to have its timings
429 * maintained, otherwise bogus information is return
430 * via read() for time_enabled, time_running:
432 if (event->state == PERF_EVENT_STATE_INACTIVE
433 && !event_filter_match(event)) {
434 delta = ctx->time - event->tstamp_stopped;
435 event->tstamp_running += delta;
436 event->tstamp_stopped = ctx->time;
439 if (event->state != PERF_EVENT_STATE_ACTIVE)
440 return;
442 event->state = PERF_EVENT_STATE_INACTIVE;
443 if (event->pending_disable) {
444 event->pending_disable = 0;
445 event->state = PERF_EVENT_STATE_OFF;
447 event->tstamp_stopped = ctx->time;
448 event->pmu->del(event, 0);
449 event->oncpu = -1;
451 if (!is_software_event(event))
452 cpuctx->active_oncpu--;
453 ctx->nr_active--;
454 if (event->attr.exclusive || !cpuctx->active_oncpu)
455 cpuctx->exclusive = 0;
458 static void
459 group_sched_out(struct perf_event *group_event,
460 struct perf_cpu_context *cpuctx,
461 struct perf_event_context *ctx)
463 struct perf_event *event;
464 int state = group_event->state;
466 event_sched_out(group_event, cpuctx, ctx);
469 * Schedule out siblings (if any):
471 list_for_each_entry(event, &group_event->sibling_list, group_entry)
472 event_sched_out(event, cpuctx, ctx);
474 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
475 cpuctx->exclusive = 0;
478 static inline struct perf_cpu_context *
479 __get_cpu_context(struct perf_event_context *ctx)
481 return this_cpu_ptr(ctx->pmu->pmu_cpu_context);
485 * Cross CPU call to remove a performance event
487 * We disable the event on the hardware level first. After that we
488 * remove it from the context list.
490 static void __perf_event_remove_from_context(void *info)
492 struct perf_event *event = info;
493 struct perf_event_context *ctx = event->ctx;
494 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
497 * If this is a task context, we need to check whether it is
498 * the current task context of this cpu. If not it has been
499 * scheduled out before the smp call arrived.
501 if (ctx->task && cpuctx->task_ctx != ctx)
502 return;
504 raw_spin_lock(&ctx->lock);
506 event_sched_out(event, cpuctx, ctx);
508 list_del_event(event, ctx);
510 raw_spin_unlock(&ctx->lock);
515 * Remove the event from a task's (or a CPU's) list of events.
517 * Must be called with ctx->mutex held.
519 * CPU events are removed with a smp call. For task events we only
520 * call when the task is on a CPU.
522 * If event->ctx is a cloned context, callers must make sure that
523 * every task struct that event->ctx->task could possibly point to
524 * remains valid. This is OK when called from perf_release since
525 * that only calls us on the top-level context, which can't be a clone.
526 * When called from perf_event_exit_task, it's OK because the
527 * context has been detached from its task.
529 static void perf_event_remove_from_context(struct perf_event *event)
531 struct perf_event_context *ctx = event->ctx;
532 struct task_struct *task = ctx->task;
534 if (!task) {
536 * Per cpu events are removed via an smp call and
537 * the removal is always successful.
539 smp_call_function_single(event->cpu,
540 __perf_event_remove_from_context,
541 event, 1);
542 return;
545 retry:
546 task_oncpu_function_call(task, __perf_event_remove_from_context,
547 event);
549 raw_spin_lock_irq(&ctx->lock);
551 * If the context is active we need to retry the smp call.
553 if (ctx->nr_active && !list_empty(&event->group_entry)) {
554 raw_spin_unlock_irq(&ctx->lock);
555 goto retry;
559 * The lock prevents that this context is scheduled in so we
560 * can remove the event safely, if the call above did not
561 * succeed.
563 if (!list_empty(&event->group_entry))
564 list_del_event(event, ctx);
565 raw_spin_unlock_irq(&ctx->lock);
569 * Cross CPU call to disable a performance event
571 static void __perf_event_disable(void *info)
573 struct perf_event *event = info;
574 struct perf_event_context *ctx = event->ctx;
575 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
578 * If this is a per-task event, need to check whether this
579 * event's task is the current task on this cpu.
581 if (ctx->task && cpuctx->task_ctx != ctx)
582 return;
584 raw_spin_lock(&ctx->lock);
587 * If the event is on, turn it off.
588 * If it is in error state, leave it in error state.
590 if (event->state >= PERF_EVENT_STATE_INACTIVE) {
591 update_context_time(ctx);
592 update_group_times(event);
593 if (event == event->group_leader)
594 group_sched_out(event, cpuctx, ctx);
595 else
596 event_sched_out(event, cpuctx, ctx);
597 event->state = PERF_EVENT_STATE_OFF;
600 raw_spin_unlock(&ctx->lock);
604 * Disable a event.
606 * If event->ctx is a cloned context, callers must make sure that
607 * every task struct that event->ctx->task could possibly point to
608 * remains valid. This condition is satisifed when called through
609 * perf_event_for_each_child or perf_event_for_each because they
610 * hold the top-level event's child_mutex, so any descendant that
611 * goes to exit will block in sync_child_event.
612 * When called from perf_pending_event it's OK because event->ctx
613 * is the current context on this CPU and preemption is disabled,
614 * hence we can't get into perf_event_task_sched_out for this context.
616 void perf_event_disable(struct perf_event *event)
618 struct perf_event_context *ctx = event->ctx;
619 struct task_struct *task = ctx->task;
621 if (!task) {
623 * Disable the event on the cpu that it's on
625 smp_call_function_single(event->cpu, __perf_event_disable,
626 event, 1);
627 return;
630 retry:
631 task_oncpu_function_call(task, __perf_event_disable, event);
633 raw_spin_lock_irq(&ctx->lock);
635 * If the event is still active, we need to retry the cross-call.
637 if (event->state == PERF_EVENT_STATE_ACTIVE) {
638 raw_spin_unlock_irq(&ctx->lock);
639 goto retry;
643 * Since we have the lock this context can't be scheduled
644 * in, so we can change the state safely.
646 if (event->state == PERF_EVENT_STATE_INACTIVE) {
647 update_group_times(event);
648 event->state = PERF_EVENT_STATE_OFF;
651 raw_spin_unlock_irq(&ctx->lock);
654 static int
655 event_sched_in(struct perf_event *event,
656 struct perf_cpu_context *cpuctx,
657 struct perf_event_context *ctx)
659 if (event->state <= PERF_EVENT_STATE_OFF)
660 return 0;
662 event->state = PERF_EVENT_STATE_ACTIVE;
663 event->oncpu = smp_processor_id();
665 * The new state must be visible before we turn it on in the hardware:
667 smp_wmb();
669 if (event->pmu->add(event, PERF_EF_START)) {
670 event->state = PERF_EVENT_STATE_INACTIVE;
671 event->oncpu = -1;
672 return -EAGAIN;
675 event->tstamp_running += ctx->time - event->tstamp_stopped;
677 if (!is_software_event(event))
678 cpuctx->active_oncpu++;
679 ctx->nr_active++;
681 if (event->attr.exclusive)
682 cpuctx->exclusive = 1;
684 return 0;
687 static int
688 group_sched_in(struct perf_event *group_event,
689 struct perf_cpu_context *cpuctx,
690 struct perf_event_context *ctx)
692 struct perf_event *event, *partial_group = NULL;
693 struct pmu *pmu = group_event->pmu;
694 u64 now = ctx->time;
695 bool simulate = false;
697 if (group_event->state == PERF_EVENT_STATE_OFF)
698 return 0;
700 pmu->start_txn(pmu);
702 if (event_sched_in(group_event, cpuctx, ctx)) {
703 pmu->cancel_txn(pmu);
704 return -EAGAIN;
708 * Schedule in siblings as one group (if any):
710 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
711 if (event_sched_in(event, cpuctx, ctx)) {
712 partial_group = event;
713 goto group_error;
717 if (!pmu->commit_txn(pmu))
718 return 0;
720 group_error:
722 * Groups can be scheduled in as one unit only, so undo any
723 * partial group before returning:
724 * The events up to the failed event are scheduled out normally,
725 * tstamp_stopped will be updated.
727 * The failed events and the remaining siblings need to have
728 * their timings updated as if they had gone thru event_sched_in()
729 * and event_sched_out(). This is required to get consistent timings
730 * across the group. This also takes care of the case where the group
731 * could never be scheduled by ensuring tstamp_stopped is set to mark
732 * the time the event was actually stopped, such that time delta
733 * calculation in update_event_times() is correct.
735 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
736 if (event == partial_group)
737 simulate = true;
739 if (simulate) {
740 event->tstamp_running += now - event->tstamp_stopped;
741 event->tstamp_stopped = now;
742 } else {
743 event_sched_out(event, cpuctx, ctx);
746 event_sched_out(group_event, cpuctx, ctx);
748 pmu->cancel_txn(pmu);
750 return -EAGAIN;
754 * Work out whether we can put this event group on the CPU now.
756 static int group_can_go_on(struct perf_event *event,
757 struct perf_cpu_context *cpuctx,
758 int can_add_hw)
761 * Groups consisting entirely of software events can always go on.
763 if (event->group_flags & PERF_GROUP_SOFTWARE)
764 return 1;
766 * If an exclusive group is already on, no other hardware
767 * events can go on.
769 if (cpuctx->exclusive)
770 return 0;
772 * If this group is exclusive and there are already
773 * events on the CPU, it can't go on.
775 if (event->attr.exclusive && cpuctx->active_oncpu)
776 return 0;
778 * Otherwise, try to add it if all previous groups were able
779 * to go on.
781 return can_add_hw;
784 static void add_event_to_ctx(struct perf_event *event,
785 struct perf_event_context *ctx)
787 list_add_event(event, ctx);
788 perf_group_attach(event);
789 event->tstamp_enabled = ctx->time;
790 event->tstamp_running = ctx->time;
791 event->tstamp_stopped = ctx->time;
795 * Cross CPU call to install and enable a performance event
797 * Must be called with ctx->mutex held
799 static void __perf_install_in_context(void *info)
801 struct perf_event *event = info;
802 struct perf_event_context *ctx = event->ctx;
803 struct perf_event *leader = event->group_leader;
804 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
805 int err;
808 * If this is a task context, we need to check whether it is
809 * the current task context of this cpu. If not it has been
810 * scheduled out before the smp call arrived.
811 * Or possibly this is the right context but it isn't
812 * on this cpu because it had no events.
814 if (ctx->task && cpuctx->task_ctx != ctx) {
815 if (cpuctx->task_ctx || ctx->task != current)
816 return;
817 cpuctx->task_ctx = ctx;
820 raw_spin_lock(&ctx->lock);
821 ctx->is_active = 1;
822 update_context_time(ctx);
824 add_event_to_ctx(event, ctx);
826 if (event->cpu != -1 && event->cpu != smp_processor_id())
827 goto unlock;
830 * Don't put the event on if it is disabled or if
831 * it is in a group and the group isn't on.
833 if (event->state != PERF_EVENT_STATE_INACTIVE ||
834 (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE))
835 goto unlock;
838 * An exclusive event can't go on if there are already active
839 * hardware events, and no hardware event can go on if there
840 * is already an exclusive event on.
842 if (!group_can_go_on(event, cpuctx, 1))
843 err = -EEXIST;
844 else
845 err = event_sched_in(event, cpuctx, ctx);
847 if (err) {
849 * This event couldn't go on. If it is in a group
850 * then we have to pull the whole group off.
851 * If the event group is pinned then put it in error state.
853 if (leader != event)
854 group_sched_out(leader, cpuctx, ctx);
855 if (leader->attr.pinned) {
856 update_group_times(leader);
857 leader->state = PERF_EVENT_STATE_ERROR;
861 unlock:
862 raw_spin_unlock(&ctx->lock);
866 * Attach a performance event to a context
868 * First we add the event to the list with the hardware enable bit
869 * in event->hw_config cleared.
871 * If the event is attached to a task which is on a CPU we use a smp
872 * call to enable it in the task context. The task might have been
873 * scheduled away, but we check this in the smp call again.
875 * Must be called with ctx->mutex held.
877 static void
878 perf_install_in_context(struct perf_event_context *ctx,
879 struct perf_event *event,
880 int cpu)
882 struct task_struct *task = ctx->task;
884 event->ctx = ctx;
886 if (!task) {
888 * Per cpu events are installed via an smp call and
889 * the install is always successful.
891 smp_call_function_single(cpu, __perf_install_in_context,
892 event, 1);
893 return;
896 retry:
897 task_oncpu_function_call(task, __perf_install_in_context,
898 event);
900 raw_spin_lock_irq(&ctx->lock);
902 * we need to retry the smp call.
904 if (ctx->is_active && list_empty(&event->group_entry)) {
905 raw_spin_unlock_irq(&ctx->lock);
906 goto retry;
910 * The lock prevents that this context is scheduled in so we
911 * can add the event safely, if it the call above did not
912 * succeed.
914 if (list_empty(&event->group_entry))
915 add_event_to_ctx(event, ctx);
916 raw_spin_unlock_irq(&ctx->lock);
920 * Put a event into inactive state and update time fields.
921 * Enabling the leader of a group effectively enables all
922 * the group members that aren't explicitly disabled, so we
923 * have to update their ->tstamp_enabled also.
924 * Note: this works for group members as well as group leaders
925 * since the non-leader members' sibling_lists will be empty.
927 static void __perf_event_mark_enabled(struct perf_event *event,
928 struct perf_event_context *ctx)
930 struct perf_event *sub;
932 event->state = PERF_EVENT_STATE_INACTIVE;
933 event->tstamp_enabled = ctx->time - event->total_time_enabled;
934 list_for_each_entry(sub, &event->sibling_list, group_entry) {
935 if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
936 sub->tstamp_enabled =
937 ctx->time - sub->total_time_enabled;
943 * Cross CPU call to enable a performance event
945 static void __perf_event_enable(void *info)
947 struct perf_event *event = info;
948 struct perf_event_context *ctx = event->ctx;
949 struct perf_event *leader = event->group_leader;
950 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
951 int err;
954 * If this is a per-task event, need to check whether this
955 * event's task is the current task on this cpu.
957 if (ctx->task && cpuctx->task_ctx != ctx) {
958 if (cpuctx->task_ctx || ctx->task != current)
959 return;
960 cpuctx->task_ctx = ctx;
963 raw_spin_lock(&ctx->lock);
964 ctx->is_active = 1;
965 update_context_time(ctx);
967 if (event->state >= PERF_EVENT_STATE_INACTIVE)
968 goto unlock;
969 __perf_event_mark_enabled(event, ctx);
971 if (event->cpu != -1 && event->cpu != smp_processor_id())
972 goto unlock;
975 * If the event is in a group and isn't the group leader,
976 * then don't put it on unless the group is on.
978 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
979 goto unlock;
981 if (!group_can_go_on(event, cpuctx, 1)) {
982 err = -EEXIST;
983 } else {
984 if (event == leader)
985 err = group_sched_in(event, cpuctx, ctx);
986 else
987 err = event_sched_in(event, cpuctx, ctx);
990 if (err) {
992 * If this event can't go on and it's part of a
993 * group, then the whole group has to come off.
995 if (leader != event)
996 group_sched_out(leader, cpuctx, ctx);
997 if (leader->attr.pinned) {
998 update_group_times(leader);
999 leader->state = PERF_EVENT_STATE_ERROR;
1003 unlock:
1004 raw_spin_unlock(&ctx->lock);
1008 * Enable a event.
1010 * If event->ctx is a cloned context, callers must make sure that
1011 * every task struct that event->ctx->task could possibly point to
1012 * remains valid. This condition is satisfied when called through
1013 * perf_event_for_each_child or perf_event_for_each as described
1014 * for perf_event_disable.
1016 void perf_event_enable(struct perf_event *event)
1018 struct perf_event_context *ctx = event->ctx;
1019 struct task_struct *task = ctx->task;
1021 if (!task) {
1023 * Enable the event on the cpu that it's on
1025 smp_call_function_single(event->cpu, __perf_event_enable,
1026 event, 1);
1027 return;
1030 raw_spin_lock_irq(&ctx->lock);
1031 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1032 goto out;
1035 * If the event is in error state, clear that first.
1036 * That way, if we see the event in error state below, we
1037 * know that it has gone back into error state, as distinct
1038 * from the task having been scheduled away before the
1039 * cross-call arrived.
1041 if (event->state == PERF_EVENT_STATE_ERROR)
1042 event->state = PERF_EVENT_STATE_OFF;
1044 retry:
1045 raw_spin_unlock_irq(&ctx->lock);
1046 task_oncpu_function_call(task, __perf_event_enable, event);
1048 raw_spin_lock_irq(&ctx->lock);
1051 * If the context is active and the event is still off,
1052 * we need to retry the cross-call.
1054 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF)
1055 goto retry;
1058 * Since we have the lock this context can't be scheduled
1059 * in, so we can change the state safely.
1061 if (event->state == PERF_EVENT_STATE_OFF)
1062 __perf_event_mark_enabled(event, ctx);
1064 out:
1065 raw_spin_unlock_irq(&ctx->lock);
1068 static int perf_event_refresh(struct perf_event *event, int refresh)
1071 * not supported on inherited events
1073 if (event->attr.inherit)
1074 return -EINVAL;
1076 atomic_add(refresh, &event->event_limit);
1077 perf_event_enable(event);
1079 return 0;
1082 enum event_type_t {
1083 EVENT_FLEXIBLE = 0x1,
1084 EVENT_PINNED = 0x2,
1085 EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
1088 static void ctx_sched_out(struct perf_event_context *ctx,
1089 struct perf_cpu_context *cpuctx,
1090 enum event_type_t event_type)
1092 struct perf_event *event;
1094 raw_spin_lock(&ctx->lock);
1095 perf_pmu_disable(ctx->pmu);
1096 ctx->is_active = 0;
1097 if (likely(!ctx->nr_events))
1098 goto out;
1099 update_context_time(ctx);
1101 if (!ctx->nr_active)
1102 goto out;
1104 if (event_type & EVENT_PINNED) {
1105 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
1106 group_sched_out(event, cpuctx, ctx);
1109 if (event_type & EVENT_FLEXIBLE) {
1110 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
1111 group_sched_out(event, cpuctx, ctx);
1113 out:
1114 perf_pmu_enable(ctx->pmu);
1115 raw_spin_unlock(&ctx->lock);
1119 * Test whether two contexts are equivalent, i.e. whether they
1120 * have both been cloned from the same version of the same context
1121 * and they both have the same number of enabled events.
1122 * If the number of enabled events is the same, then the set
1123 * of enabled events should be the same, because these are both
1124 * inherited contexts, therefore we can't access individual events
1125 * in them directly with an fd; we can only enable/disable all
1126 * events via prctl, or enable/disable all events in a family
1127 * via ioctl, which will have the same effect on both contexts.
1129 static int context_equiv(struct perf_event_context *ctx1,
1130 struct perf_event_context *ctx2)
1132 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
1133 && ctx1->parent_gen == ctx2->parent_gen
1134 && !ctx1->pin_count && !ctx2->pin_count;
1137 static void __perf_event_sync_stat(struct perf_event *event,
1138 struct perf_event *next_event)
1140 u64 value;
1142 if (!event->attr.inherit_stat)
1143 return;
1146 * Update the event value, we cannot use perf_event_read()
1147 * because we're in the middle of a context switch and have IRQs
1148 * disabled, which upsets smp_call_function_single(), however
1149 * we know the event must be on the current CPU, therefore we
1150 * don't need to use it.
1152 switch (event->state) {
1153 case PERF_EVENT_STATE_ACTIVE:
1154 event->pmu->read(event);
1155 /* fall-through */
1157 case PERF_EVENT_STATE_INACTIVE:
1158 update_event_times(event);
1159 break;
1161 default:
1162 break;
1166 * In order to keep per-task stats reliable we need to flip the event
1167 * values when we flip the contexts.
1169 value = local64_read(&next_event->count);
1170 value = local64_xchg(&event->count, value);
1171 local64_set(&next_event->count, value);
1173 swap(event->total_time_enabled, next_event->total_time_enabled);
1174 swap(event->total_time_running, next_event->total_time_running);
1177 * Since we swizzled the values, update the user visible data too.
1179 perf_event_update_userpage(event);
1180 perf_event_update_userpage(next_event);
1183 #define list_next_entry(pos, member) \
1184 list_entry(pos->member.next, typeof(*pos), member)
1186 static void perf_event_sync_stat(struct perf_event_context *ctx,
1187 struct perf_event_context *next_ctx)
1189 struct perf_event *event, *next_event;
1191 if (!ctx->nr_stat)
1192 return;
1194 update_context_time(ctx);
1196 event = list_first_entry(&ctx->event_list,
1197 struct perf_event, event_entry);
1199 next_event = list_first_entry(&next_ctx->event_list,
1200 struct perf_event, event_entry);
1202 while (&event->event_entry != &ctx->event_list &&
1203 &next_event->event_entry != &next_ctx->event_list) {
1205 __perf_event_sync_stat(event, next_event);
1207 event = list_next_entry(event, event_entry);
1208 next_event = list_next_entry(next_event, event_entry);
1212 void perf_event_context_sched_out(struct task_struct *task, int ctxn,
1213 struct task_struct *next)
1215 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
1216 struct perf_event_context *next_ctx;
1217 struct perf_event_context *parent;
1218 struct perf_cpu_context *cpuctx;
1219 int do_switch = 1;
1221 if (likely(!ctx))
1222 return;
1224 cpuctx = __get_cpu_context(ctx);
1225 if (!cpuctx->task_ctx)
1226 return;
1228 rcu_read_lock();
1229 parent = rcu_dereference(ctx->parent_ctx);
1230 next_ctx = next->perf_event_ctxp[ctxn];
1231 if (parent && next_ctx &&
1232 rcu_dereference(next_ctx->parent_ctx) == parent) {
1234 * Looks like the two contexts are clones, so we might be
1235 * able to optimize the context switch. We lock both
1236 * contexts and check that they are clones under the
1237 * lock (including re-checking that neither has been
1238 * uncloned in the meantime). It doesn't matter which
1239 * order we take the locks because no other cpu could
1240 * be trying to lock both of these tasks.
1242 raw_spin_lock(&ctx->lock);
1243 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
1244 if (context_equiv(ctx, next_ctx)) {
1246 * XXX do we need a memory barrier of sorts
1247 * wrt to rcu_dereference() of perf_event_ctxp
1249 task->perf_event_ctxp[ctxn] = next_ctx;
1250 next->perf_event_ctxp[ctxn] = ctx;
1251 ctx->task = next;
1252 next_ctx->task = task;
1253 do_switch = 0;
1255 perf_event_sync_stat(ctx, next_ctx);
1257 raw_spin_unlock(&next_ctx->lock);
1258 raw_spin_unlock(&ctx->lock);
1260 rcu_read_unlock();
1262 if (do_switch) {
1263 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
1264 cpuctx->task_ctx = NULL;
1268 #define for_each_task_context_nr(ctxn) \
1269 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
1272 * Called from scheduler to remove the events of the current task,
1273 * with interrupts disabled.
1275 * We stop each event and update the event value in event->count.
1277 * This does not protect us against NMI, but disable()
1278 * sets the disabled bit in the control field of event _before_
1279 * accessing the event control register. If a NMI hits, then it will
1280 * not restart the event.
1282 void __perf_event_task_sched_out(struct task_struct *task,
1283 struct task_struct *next)
1285 int ctxn;
1287 perf_sw_event(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 1, NULL, 0);
1289 for_each_task_context_nr(ctxn)
1290 perf_event_context_sched_out(task, ctxn, next);
1293 static void task_ctx_sched_out(struct perf_event_context *ctx,
1294 enum event_type_t event_type)
1296 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1298 if (!cpuctx->task_ctx)
1299 return;
1301 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
1302 return;
1304 ctx_sched_out(ctx, cpuctx, event_type);
1305 cpuctx->task_ctx = NULL;
1309 * Called with IRQs disabled
1311 static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
1312 enum event_type_t event_type)
1314 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
1317 static void
1318 ctx_pinned_sched_in(struct perf_event_context *ctx,
1319 struct perf_cpu_context *cpuctx)
1321 struct perf_event *event;
1323 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
1324 if (event->state <= PERF_EVENT_STATE_OFF)
1325 continue;
1326 if (event->cpu != -1 && event->cpu != smp_processor_id())
1327 continue;
1329 if (group_can_go_on(event, cpuctx, 1))
1330 group_sched_in(event, cpuctx, ctx);
1333 * If this pinned group hasn't been scheduled,
1334 * put it in error state.
1336 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1337 update_group_times(event);
1338 event->state = PERF_EVENT_STATE_ERROR;
1343 static void
1344 ctx_flexible_sched_in(struct perf_event_context *ctx,
1345 struct perf_cpu_context *cpuctx)
1347 struct perf_event *event;
1348 int can_add_hw = 1;
1350 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
1351 /* Ignore events in OFF or ERROR state */
1352 if (event->state <= PERF_EVENT_STATE_OFF)
1353 continue;
1355 * Listen to the 'cpu' scheduling filter constraint
1356 * of events:
1358 if (event->cpu != -1 && event->cpu != smp_processor_id())
1359 continue;
1361 if (group_can_go_on(event, cpuctx, can_add_hw)) {
1362 if (group_sched_in(event, cpuctx, ctx))
1363 can_add_hw = 0;
1368 static void
1369 ctx_sched_in(struct perf_event_context *ctx,
1370 struct perf_cpu_context *cpuctx,
1371 enum event_type_t event_type)
1373 raw_spin_lock(&ctx->lock);
1374 ctx->is_active = 1;
1375 if (likely(!ctx->nr_events))
1376 goto out;
1378 ctx->timestamp = perf_clock();
1381 * First go through the list and put on any pinned groups
1382 * in order to give them the best chance of going on.
1384 if (event_type & EVENT_PINNED)
1385 ctx_pinned_sched_in(ctx, cpuctx);
1387 /* Then walk through the lower prio flexible groups */
1388 if (event_type & EVENT_FLEXIBLE)
1389 ctx_flexible_sched_in(ctx, cpuctx);
1391 out:
1392 raw_spin_unlock(&ctx->lock);
1395 static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
1396 enum event_type_t event_type)
1398 struct perf_event_context *ctx = &cpuctx->ctx;
1400 ctx_sched_in(ctx, cpuctx, event_type);
1403 static void task_ctx_sched_in(struct perf_event_context *ctx,
1404 enum event_type_t event_type)
1406 struct perf_cpu_context *cpuctx;
1408 cpuctx = __get_cpu_context(ctx);
1409 if (cpuctx->task_ctx == ctx)
1410 return;
1412 ctx_sched_in(ctx, cpuctx, event_type);
1413 cpuctx->task_ctx = ctx;
1416 void perf_event_context_sched_in(struct perf_event_context *ctx)
1418 struct perf_cpu_context *cpuctx;
1420 cpuctx = __get_cpu_context(ctx);
1421 if (cpuctx->task_ctx == ctx)
1422 return;
1424 perf_pmu_disable(ctx->pmu);
1426 * We want to keep the following priority order:
1427 * cpu pinned (that don't need to move), task pinned,
1428 * cpu flexible, task flexible.
1430 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
1432 ctx_sched_in(ctx, cpuctx, EVENT_PINNED);
1433 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE);
1434 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE);
1436 cpuctx->task_ctx = ctx;
1439 * Since these rotations are per-cpu, we need to ensure the
1440 * cpu-context we got scheduled on is actually rotating.
1442 perf_pmu_rotate_start(ctx->pmu);
1443 perf_pmu_enable(ctx->pmu);
1447 * Called from scheduler to add the events of the current task
1448 * with interrupts disabled.
1450 * We restore the event value and then enable it.
1452 * This does not protect us against NMI, but enable()
1453 * sets the enabled bit in the control field of event _before_
1454 * accessing the event control register. If a NMI hits, then it will
1455 * keep the event running.
1457 void __perf_event_task_sched_in(struct task_struct *task)
1459 struct perf_event_context *ctx;
1460 int ctxn;
1462 for_each_task_context_nr(ctxn) {
1463 ctx = task->perf_event_ctxp[ctxn];
1464 if (likely(!ctx))
1465 continue;
1467 perf_event_context_sched_in(ctx);
1471 #define MAX_INTERRUPTS (~0ULL)
1473 static void perf_log_throttle(struct perf_event *event, int enable);
1475 static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
1477 u64 frequency = event->attr.sample_freq;
1478 u64 sec = NSEC_PER_SEC;
1479 u64 divisor, dividend;
1481 int count_fls, nsec_fls, frequency_fls, sec_fls;
1483 count_fls = fls64(count);
1484 nsec_fls = fls64(nsec);
1485 frequency_fls = fls64(frequency);
1486 sec_fls = 30;
1489 * We got @count in @nsec, with a target of sample_freq HZ
1490 * the target period becomes:
1492 * @count * 10^9
1493 * period = -------------------
1494 * @nsec * sample_freq
1499 * Reduce accuracy by one bit such that @a and @b converge
1500 * to a similar magnitude.
1502 #define REDUCE_FLS(a, b) \
1503 do { \
1504 if (a##_fls > b##_fls) { \
1505 a >>= 1; \
1506 a##_fls--; \
1507 } else { \
1508 b >>= 1; \
1509 b##_fls--; \
1511 } while (0)
1514 * Reduce accuracy until either term fits in a u64, then proceed with
1515 * the other, so that finally we can do a u64/u64 division.
1517 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
1518 REDUCE_FLS(nsec, frequency);
1519 REDUCE_FLS(sec, count);
1522 if (count_fls + sec_fls > 64) {
1523 divisor = nsec * frequency;
1525 while (count_fls + sec_fls > 64) {
1526 REDUCE_FLS(count, sec);
1527 divisor >>= 1;
1530 dividend = count * sec;
1531 } else {
1532 dividend = count * sec;
1534 while (nsec_fls + frequency_fls > 64) {
1535 REDUCE_FLS(nsec, frequency);
1536 dividend >>= 1;
1539 divisor = nsec * frequency;
1542 if (!divisor)
1543 return dividend;
1545 return div64_u64(dividend, divisor);
1548 static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count)
1550 struct hw_perf_event *hwc = &event->hw;
1551 s64 period, sample_period;
1552 s64 delta;
1554 period = perf_calculate_period(event, nsec, count);
1556 delta = (s64)(period - hwc->sample_period);
1557 delta = (delta + 7) / 8; /* low pass filter */
1559 sample_period = hwc->sample_period + delta;
1561 if (!sample_period)
1562 sample_period = 1;
1564 hwc->sample_period = sample_period;
1566 if (local64_read(&hwc->period_left) > 8*sample_period) {
1567 event->pmu->stop(event, PERF_EF_UPDATE);
1568 local64_set(&hwc->period_left, 0);
1569 event->pmu->start(event, PERF_EF_RELOAD);
1573 static void perf_ctx_adjust_freq(struct perf_event_context *ctx, u64 period)
1575 struct perf_event *event;
1576 struct hw_perf_event *hwc;
1577 u64 interrupts, now;
1578 s64 delta;
1580 raw_spin_lock(&ctx->lock);
1581 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
1582 if (event->state != PERF_EVENT_STATE_ACTIVE)
1583 continue;
1585 if (event->cpu != -1 && event->cpu != smp_processor_id())
1586 continue;
1588 hwc = &event->hw;
1590 interrupts = hwc->interrupts;
1591 hwc->interrupts = 0;
1594 * unthrottle events on the tick
1596 if (interrupts == MAX_INTERRUPTS) {
1597 perf_log_throttle(event, 1);
1598 event->pmu->start(event, 0);
1601 if (!event->attr.freq || !event->attr.sample_freq)
1602 continue;
1604 event->pmu->read(event);
1605 now = local64_read(&event->count);
1606 delta = now - hwc->freq_count_stamp;
1607 hwc->freq_count_stamp = now;
1609 if (delta > 0)
1610 perf_adjust_period(event, period, delta);
1612 raw_spin_unlock(&ctx->lock);
1616 * Round-robin a context's events:
1618 static void rotate_ctx(struct perf_event_context *ctx)
1620 raw_spin_lock(&ctx->lock);
1622 /* Rotate the first entry last of non-pinned groups */
1623 list_rotate_left(&ctx->flexible_groups);
1625 raw_spin_unlock(&ctx->lock);
1629 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
1630 * because they're strictly cpu affine and rotate_start is called with IRQs
1631 * disabled, while rotate_context is called from IRQ context.
1633 static void perf_rotate_context(struct perf_cpu_context *cpuctx)
1635 u64 interval = (u64)cpuctx->jiffies_interval * TICK_NSEC;
1636 struct perf_event_context *ctx = NULL;
1637 int rotate = 0, remove = 1;
1639 if (cpuctx->ctx.nr_events) {
1640 remove = 0;
1641 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
1642 rotate = 1;
1645 ctx = cpuctx->task_ctx;
1646 if (ctx && ctx->nr_events) {
1647 remove = 0;
1648 if (ctx->nr_events != ctx->nr_active)
1649 rotate = 1;
1652 perf_pmu_disable(cpuctx->ctx.pmu);
1653 perf_ctx_adjust_freq(&cpuctx->ctx, interval);
1654 if (ctx)
1655 perf_ctx_adjust_freq(ctx, interval);
1657 if (!rotate)
1658 goto done;
1660 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
1661 if (ctx)
1662 task_ctx_sched_out(ctx, EVENT_FLEXIBLE);
1664 rotate_ctx(&cpuctx->ctx);
1665 if (ctx)
1666 rotate_ctx(ctx);
1668 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE);
1669 if (ctx)
1670 task_ctx_sched_in(ctx, EVENT_FLEXIBLE);
1672 done:
1673 if (remove)
1674 list_del_init(&cpuctx->rotation_list);
1676 perf_pmu_enable(cpuctx->ctx.pmu);
1679 void perf_event_task_tick(void)
1681 struct list_head *head = &__get_cpu_var(rotation_list);
1682 struct perf_cpu_context *cpuctx, *tmp;
1684 WARN_ON(!irqs_disabled());
1686 list_for_each_entry_safe(cpuctx, tmp, head, rotation_list) {
1687 if (cpuctx->jiffies_interval == 1 ||
1688 !(jiffies % cpuctx->jiffies_interval))
1689 perf_rotate_context(cpuctx);
1693 static int event_enable_on_exec(struct perf_event *event,
1694 struct perf_event_context *ctx)
1696 if (!event->attr.enable_on_exec)
1697 return 0;
1699 event->attr.enable_on_exec = 0;
1700 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1701 return 0;
1703 __perf_event_mark_enabled(event, ctx);
1705 return 1;
1709 * Enable all of a task's events that have been marked enable-on-exec.
1710 * This expects task == current.
1712 static void perf_event_enable_on_exec(struct perf_event_context *ctx)
1714 struct perf_event *event;
1715 unsigned long flags;
1716 int enabled = 0;
1717 int ret;
1719 local_irq_save(flags);
1720 if (!ctx || !ctx->nr_events)
1721 goto out;
1723 task_ctx_sched_out(ctx, EVENT_ALL);
1725 raw_spin_lock(&ctx->lock);
1727 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
1728 ret = event_enable_on_exec(event, ctx);
1729 if (ret)
1730 enabled = 1;
1733 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
1734 ret = event_enable_on_exec(event, ctx);
1735 if (ret)
1736 enabled = 1;
1740 * Unclone this context if we enabled any event.
1742 if (enabled)
1743 unclone_ctx(ctx);
1745 raw_spin_unlock(&ctx->lock);
1747 perf_event_context_sched_in(ctx);
1748 out:
1749 local_irq_restore(flags);
1753 * Cross CPU call to read the hardware event
1755 static void __perf_event_read(void *info)
1757 struct perf_event *event = info;
1758 struct perf_event_context *ctx = event->ctx;
1759 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1762 * If this is a task context, we need to check whether it is
1763 * the current task context of this cpu. If not it has been
1764 * scheduled out before the smp call arrived. In that case
1765 * event->count would have been updated to a recent sample
1766 * when the event was scheduled out.
1768 if (ctx->task && cpuctx->task_ctx != ctx)
1769 return;
1771 raw_spin_lock(&ctx->lock);
1772 update_context_time(ctx);
1773 update_event_times(event);
1774 raw_spin_unlock(&ctx->lock);
1776 event->pmu->read(event);
1779 static inline u64 perf_event_count(struct perf_event *event)
1781 return local64_read(&event->count) + atomic64_read(&event->child_count);
1784 static u64 perf_event_read(struct perf_event *event)
1787 * If event is enabled and currently active on a CPU, update the
1788 * value in the event structure:
1790 if (event->state == PERF_EVENT_STATE_ACTIVE) {
1791 smp_call_function_single(event->oncpu,
1792 __perf_event_read, event, 1);
1793 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
1794 struct perf_event_context *ctx = event->ctx;
1795 unsigned long flags;
1797 raw_spin_lock_irqsave(&ctx->lock, flags);
1799 * may read while context is not active
1800 * (e.g., thread is blocked), in that case
1801 * we cannot update context time
1803 if (ctx->is_active)
1804 update_context_time(ctx);
1805 update_event_times(event);
1806 raw_spin_unlock_irqrestore(&ctx->lock, flags);
1809 return perf_event_count(event);
1813 * Callchain support
1816 struct callchain_cpus_entries {
1817 struct rcu_head rcu_head;
1818 struct perf_callchain_entry *cpu_entries[0];
1821 static DEFINE_PER_CPU(int, callchain_recursion[PERF_NR_CONTEXTS]);
1822 static atomic_t nr_callchain_events;
1823 static DEFINE_MUTEX(callchain_mutex);
1824 struct callchain_cpus_entries *callchain_cpus_entries;
1827 __weak void perf_callchain_kernel(struct perf_callchain_entry *entry,
1828 struct pt_regs *regs)
1832 __weak void perf_callchain_user(struct perf_callchain_entry *entry,
1833 struct pt_regs *regs)
1837 static void release_callchain_buffers_rcu(struct rcu_head *head)
1839 struct callchain_cpus_entries *entries;
1840 int cpu;
1842 entries = container_of(head, struct callchain_cpus_entries, rcu_head);
1844 for_each_possible_cpu(cpu)
1845 kfree(entries->cpu_entries[cpu]);
1847 kfree(entries);
1850 static void release_callchain_buffers(void)
1852 struct callchain_cpus_entries *entries;
1854 entries = callchain_cpus_entries;
1855 rcu_assign_pointer(callchain_cpus_entries, NULL);
1856 call_rcu(&entries->rcu_head, release_callchain_buffers_rcu);
1859 static int alloc_callchain_buffers(void)
1861 int cpu;
1862 int size;
1863 struct callchain_cpus_entries *entries;
1866 * We can't use the percpu allocation API for data that can be
1867 * accessed from NMI. Use a temporary manual per cpu allocation
1868 * until that gets sorted out.
1870 size = sizeof(*entries) + sizeof(struct perf_callchain_entry *) *
1871 num_possible_cpus();
1873 entries = kzalloc(size, GFP_KERNEL);
1874 if (!entries)
1875 return -ENOMEM;
1877 size = sizeof(struct perf_callchain_entry) * PERF_NR_CONTEXTS;
1879 for_each_possible_cpu(cpu) {
1880 entries->cpu_entries[cpu] = kmalloc_node(size, GFP_KERNEL,
1881 cpu_to_node(cpu));
1882 if (!entries->cpu_entries[cpu])
1883 goto fail;
1886 rcu_assign_pointer(callchain_cpus_entries, entries);
1888 return 0;
1890 fail:
1891 for_each_possible_cpu(cpu)
1892 kfree(entries->cpu_entries[cpu]);
1893 kfree(entries);
1895 return -ENOMEM;
1898 static int get_callchain_buffers(void)
1900 int err = 0;
1901 int count;
1903 mutex_lock(&callchain_mutex);
1905 count = atomic_inc_return(&nr_callchain_events);
1906 if (WARN_ON_ONCE(count < 1)) {
1907 err = -EINVAL;
1908 goto exit;
1911 if (count > 1) {
1912 /* If the allocation failed, give up */
1913 if (!callchain_cpus_entries)
1914 err = -ENOMEM;
1915 goto exit;
1918 err = alloc_callchain_buffers();
1919 if (err)
1920 release_callchain_buffers();
1921 exit:
1922 mutex_unlock(&callchain_mutex);
1924 return err;
1927 static void put_callchain_buffers(void)
1929 if (atomic_dec_and_mutex_lock(&nr_callchain_events, &callchain_mutex)) {
1930 release_callchain_buffers();
1931 mutex_unlock(&callchain_mutex);
1935 static int get_recursion_context(int *recursion)
1937 int rctx;
1939 if (in_nmi())
1940 rctx = 3;
1941 else if (in_irq())
1942 rctx = 2;
1943 else if (in_softirq())
1944 rctx = 1;
1945 else
1946 rctx = 0;
1948 if (recursion[rctx])
1949 return -1;
1951 recursion[rctx]++;
1952 barrier();
1954 return rctx;
1957 static inline void put_recursion_context(int *recursion, int rctx)
1959 barrier();
1960 recursion[rctx]--;
1963 static struct perf_callchain_entry *get_callchain_entry(int *rctx)
1965 int cpu;
1966 struct callchain_cpus_entries *entries;
1968 *rctx = get_recursion_context(__get_cpu_var(callchain_recursion));
1969 if (*rctx == -1)
1970 return NULL;
1972 entries = rcu_dereference(callchain_cpus_entries);
1973 if (!entries)
1974 return NULL;
1976 cpu = smp_processor_id();
1978 return &entries->cpu_entries[cpu][*rctx];
1981 static void
1982 put_callchain_entry(int rctx)
1984 put_recursion_context(__get_cpu_var(callchain_recursion), rctx);
1987 static struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
1989 int rctx;
1990 struct perf_callchain_entry *entry;
1993 entry = get_callchain_entry(&rctx);
1994 if (rctx == -1)
1995 return NULL;
1997 if (!entry)
1998 goto exit_put;
2000 entry->nr = 0;
2002 if (!user_mode(regs)) {
2003 perf_callchain_store(entry, PERF_CONTEXT_KERNEL);
2004 perf_callchain_kernel(entry, regs);
2005 if (current->mm)
2006 regs = task_pt_regs(current);
2007 else
2008 regs = NULL;
2011 if (regs) {
2012 perf_callchain_store(entry, PERF_CONTEXT_USER);
2013 perf_callchain_user(entry, regs);
2016 exit_put:
2017 put_callchain_entry(rctx);
2019 return entry;
2023 * Initialize the perf_event context in a task_struct:
2025 static void __perf_event_init_context(struct perf_event_context *ctx)
2027 raw_spin_lock_init(&ctx->lock);
2028 mutex_init(&ctx->mutex);
2029 INIT_LIST_HEAD(&ctx->pinned_groups);
2030 INIT_LIST_HEAD(&ctx->flexible_groups);
2031 INIT_LIST_HEAD(&ctx->event_list);
2032 atomic_set(&ctx->refcount, 1);
2035 static struct perf_event_context *
2036 alloc_perf_context(struct pmu *pmu, struct task_struct *task)
2038 struct perf_event_context *ctx;
2040 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
2041 if (!ctx)
2042 return NULL;
2044 __perf_event_init_context(ctx);
2045 if (task) {
2046 ctx->task = task;
2047 get_task_struct(task);
2049 ctx->pmu = pmu;
2051 return ctx;
2054 static struct task_struct *
2055 find_lively_task_by_vpid(pid_t vpid)
2057 struct task_struct *task;
2058 int err;
2060 rcu_read_lock();
2061 if (!vpid)
2062 task = current;
2063 else
2064 task = find_task_by_vpid(vpid);
2065 if (task)
2066 get_task_struct(task);
2067 rcu_read_unlock();
2069 if (!task)
2070 return ERR_PTR(-ESRCH);
2073 * Can't attach events to a dying task.
2075 err = -ESRCH;
2076 if (task->flags & PF_EXITING)
2077 goto errout;
2079 /* Reuse ptrace permission checks for now. */
2080 err = -EACCES;
2081 if (!ptrace_may_access(task, PTRACE_MODE_READ))
2082 goto errout;
2084 return task;
2085 errout:
2086 put_task_struct(task);
2087 return ERR_PTR(err);
2091 static struct perf_event_context *
2092 find_get_context(struct pmu *pmu, struct task_struct *task, int cpu)
2094 struct perf_event_context *ctx;
2095 struct perf_cpu_context *cpuctx;
2096 unsigned long flags;
2097 int ctxn, err;
2099 if (!task && cpu != -1) {
2100 /* Must be root to operate on a CPU event: */
2101 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
2102 return ERR_PTR(-EACCES);
2104 if (cpu < 0 || cpu >= nr_cpumask_bits)
2105 return ERR_PTR(-EINVAL);
2108 * We could be clever and allow to attach a event to an
2109 * offline CPU and activate it when the CPU comes up, but
2110 * that's for later.
2112 if (!cpu_online(cpu))
2113 return ERR_PTR(-ENODEV);
2115 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
2116 ctx = &cpuctx->ctx;
2117 get_ctx(ctx);
2119 return ctx;
2122 err = -EINVAL;
2123 ctxn = pmu->task_ctx_nr;
2124 if (ctxn < 0)
2125 goto errout;
2127 retry:
2128 ctx = perf_lock_task_context(task, ctxn, &flags);
2129 if (ctx) {
2130 unclone_ctx(ctx);
2131 raw_spin_unlock_irqrestore(&ctx->lock, flags);
2134 if (!ctx) {
2135 ctx = alloc_perf_context(pmu, task);
2136 err = -ENOMEM;
2137 if (!ctx)
2138 goto errout;
2140 get_ctx(ctx);
2142 if (cmpxchg(&task->perf_event_ctxp[ctxn], NULL, ctx)) {
2144 * We raced with some other task; use
2145 * the context they set.
2147 put_task_struct(task);
2148 kfree(ctx);
2149 goto retry;
2153 return ctx;
2155 errout:
2156 return ERR_PTR(err);
2159 static void perf_event_free_filter(struct perf_event *event);
2161 static void free_event_rcu(struct rcu_head *head)
2163 struct perf_event *event;
2165 event = container_of(head, struct perf_event, rcu_head);
2166 if (event->ns)
2167 put_pid_ns(event->ns);
2168 perf_event_free_filter(event);
2169 kfree(event);
2172 static void perf_buffer_put(struct perf_buffer *buffer);
2174 static void free_event(struct perf_event *event)
2176 irq_work_sync(&event->pending);
2178 if (!event->parent) {
2179 if (event->attach_state & PERF_ATTACH_TASK)
2180 jump_label_dec(&perf_task_events);
2181 if (event->attr.mmap || event->attr.mmap_data)
2182 atomic_dec(&nr_mmap_events);
2183 if (event->attr.comm)
2184 atomic_dec(&nr_comm_events);
2185 if (event->attr.task)
2186 atomic_dec(&nr_task_events);
2187 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
2188 put_callchain_buffers();
2191 if (event->buffer) {
2192 perf_buffer_put(event->buffer);
2193 event->buffer = NULL;
2196 if (event->destroy)
2197 event->destroy(event);
2199 if (event->ctx)
2200 put_ctx(event->ctx);
2202 call_rcu(&event->rcu_head, free_event_rcu);
2205 int perf_event_release_kernel(struct perf_event *event)
2207 struct perf_event_context *ctx = event->ctx;
2210 * Remove from the PMU, can't get re-enabled since we got
2211 * here because the last ref went.
2213 perf_event_disable(event);
2215 WARN_ON_ONCE(ctx->parent_ctx);
2217 * There are two ways this annotation is useful:
2219 * 1) there is a lock recursion from perf_event_exit_task
2220 * see the comment there.
2222 * 2) there is a lock-inversion with mmap_sem through
2223 * perf_event_read_group(), which takes faults while
2224 * holding ctx->mutex, however this is called after
2225 * the last filedesc died, so there is no possibility
2226 * to trigger the AB-BA case.
2228 mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
2229 raw_spin_lock_irq(&ctx->lock);
2230 perf_group_detach(event);
2231 list_del_event(event, ctx);
2232 raw_spin_unlock_irq(&ctx->lock);
2233 mutex_unlock(&ctx->mutex);
2235 mutex_lock(&event->owner->perf_event_mutex);
2236 list_del_init(&event->owner_entry);
2237 mutex_unlock(&event->owner->perf_event_mutex);
2238 put_task_struct(event->owner);
2240 free_event(event);
2242 return 0;
2244 EXPORT_SYMBOL_GPL(perf_event_release_kernel);
2247 * Called when the last reference to the file is gone.
2249 static int perf_release(struct inode *inode, struct file *file)
2251 struct perf_event *event = file->private_data;
2253 file->private_data = NULL;
2255 return perf_event_release_kernel(event);
2258 static int perf_event_read_size(struct perf_event *event)
2260 int entry = sizeof(u64); /* value */
2261 int size = 0;
2262 int nr = 1;
2264 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2265 size += sizeof(u64);
2267 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2268 size += sizeof(u64);
2270 if (event->attr.read_format & PERF_FORMAT_ID)
2271 entry += sizeof(u64);
2273 if (event->attr.read_format & PERF_FORMAT_GROUP) {
2274 nr += event->group_leader->nr_siblings;
2275 size += sizeof(u64);
2278 size += entry * nr;
2280 return size;
2283 u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
2285 struct perf_event *child;
2286 u64 total = 0;
2288 *enabled = 0;
2289 *running = 0;
2291 mutex_lock(&event->child_mutex);
2292 total += perf_event_read(event);
2293 *enabled += event->total_time_enabled +
2294 atomic64_read(&event->child_total_time_enabled);
2295 *running += event->total_time_running +
2296 atomic64_read(&event->child_total_time_running);
2298 list_for_each_entry(child, &event->child_list, child_list) {
2299 total += perf_event_read(child);
2300 *enabled += child->total_time_enabled;
2301 *running += child->total_time_running;
2303 mutex_unlock(&event->child_mutex);
2305 return total;
2307 EXPORT_SYMBOL_GPL(perf_event_read_value);
2309 static int perf_event_read_group(struct perf_event *event,
2310 u64 read_format, char __user *buf)
2312 struct perf_event *leader = event->group_leader, *sub;
2313 int n = 0, size = 0, ret = -EFAULT;
2314 struct perf_event_context *ctx = leader->ctx;
2315 u64 values[5];
2316 u64 count, enabled, running;
2318 mutex_lock(&ctx->mutex);
2319 count = perf_event_read_value(leader, &enabled, &running);
2321 values[n++] = 1 + leader->nr_siblings;
2322 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2323 values[n++] = enabled;
2324 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2325 values[n++] = running;
2326 values[n++] = count;
2327 if (read_format & PERF_FORMAT_ID)
2328 values[n++] = primary_event_id(leader);
2330 size = n * sizeof(u64);
2332 if (copy_to_user(buf, values, size))
2333 goto unlock;
2335 ret = size;
2337 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
2338 n = 0;
2340 values[n++] = perf_event_read_value(sub, &enabled, &running);
2341 if (read_format & PERF_FORMAT_ID)
2342 values[n++] = primary_event_id(sub);
2344 size = n * sizeof(u64);
2346 if (copy_to_user(buf + ret, values, size)) {
2347 ret = -EFAULT;
2348 goto unlock;
2351 ret += size;
2353 unlock:
2354 mutex_unlock(&ctx->mutex);
2356 return ret;
2359 static int perf_event_read_one(struct perf_event *event,
2360 u64 read_format, char __user *buf)
2362 u64 enabled, running;
2363 u64 values[4];
2364 int n = 0;
2366 values[n++] = perf_event_read_value(event, &enabled, &running);
2367 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2368 values[n++] = enabled;
2369 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2370 values[n++] = running;
2371 if (read_format & PERF_FORMAT_ID)
2372 values[n++] = primary_event_id(event);
2374 if (copy_to_user(buf, values, n * sizeof(u64)))
2375 return -EFAULT;
2377 return n * sizeof(u64);
2381 * Read the performance event - simple non blocking version for now
2383 static ssize_t
2384 perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
2386 u64 read_format = event->attr.read_format;
2387 int ret;
2390 * Return end-of-file for a read on a event that is in
2391 * error state (i.e. because it was pinned but it couldn't be
2392 * scheduled on to the CPU at some point).
2394 if (event->state == PERF_EVENT_STATE_ERROR)
2395 return 0;
2397 if (count < perf_event_read_size(event))
2398 return -ENOSPC;
2400 WARN_ON_ONCE(event->ctx->parent_ctx);
2401 if (read_format & PERF_FORMAT_GROUP)
2402 ret = perf_event_read_group(event, read_format, buf);
2403 else
2404 ret = perf_event_read_one(event, read_format, buf);
2406 return ret;
2409 static ssize_t
2410 perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
2412 struct perf_event *event = file->private_data;
2414 return perf_read_hw(event, buf, count);
2417 static unsigned int perf_poll(struct file *file, poll_table *wait)
2419 struct perf_event *event = file->private_data;
2420 struct perf_buffer *buffer;
2421 unsigned int events = POLL_HUP;
2423 rcu_read_lock();
2424 buffer = rcu_dereference(event->buffer);
2425 if (buffer)
2426 events = atomic_xchg(&buffer->poll, 0);
2427 rcu_read_unlock();
2429 poll_wait(file, &event->waitq, wait);
2431 return events;
2434 static void perf_event_reset(struct perf_event *event)
2436 (void)perf_event_read(event);
2437 local64_set(&event->count, 0);
2438 perf_event_update_userpage(event);
2442 * Holding the top-level event's child_mutex means that any
2443 * descendant process that has inherited this event will block
2444 * in sync_child_event if it goes to exit, thus satisfying the
2445 * task existence requirements of perf_event_enable/disable.
2447 static void perf_event_for_each_child(struct perf_event *event,
2448 void (*func)(struct perf_event *))
2450 struct perf_event *child;
2452 WARN_ON_ONCE(event->ctx->parent_ctx);
2453 mutex_lock(&event->child_mutex);
2454 func(event);
2455 list_for_each_entry(child, &event->child_list, child_list)
2456 func(child);
2457 mutex_unlock(&event->child_mutex);
2460 static void perf_event_for_each(struct perf_event *event,
2461 void (*func)(struct perf_event *))
2463 struct perf_event_context *ctx = event->ctx;
2464 struct perf_event *sibling;
2466 WARN_ON_ONCE(ctx->parent_ctx);
2467 mutex_lock(&ctx->mutex);
2468 event = event->group_leader;
2470 perf_event_for_each_child(event, func);
2471 func(event);
2472 list_for_each_entry(sibling, &event->sibling_list, group_entry)
2473 perf_event_for_each_child(event, func);
2474 mutex_unlock(&ctx->mutex);
2477 static int perf_event_period(struct perf_event *event, u64 __user *arg)
2479 struct perf_event_context *ctx = event->ctx;
2480 int ret = 0;
2481 u64 value;
2483 if (!event->attr.sample_period)
2484 return -EINVAL;
2486 if (copy_from_user(&value, arg, sizeof(value)))
2487 return -EFAULT;
2489 if (!value)
2490 return -EINVAL;
2492 raw_spin_lock_irq(&ctx->lock);
2493 if (event->attr.freq) {
2494 if (value > sysctl_perf_event_sample_rate) {
2495 ret = -EINVAL;
2496 goto unlock;
2499 event->attr.sample_freq = value;
2500 } else {
2501 event->attr.sample_period = value;
2502 event->hw.sample_period = value;
2504 unlock:
2505 raw_spin_unlock_irq(&ctx->lock);
2507 return ret;
2510 static const struct file_operations perf_fops;
2512 static struct perf_event *perf_fget_light(int fd, int *fput_needed)
2514 struct file *file;
2516 file = fget_light(fd, fput_needed);
2517 if (!file)
2518 return ERR_PTR(-EBADF);
2520 if (file->f_op != &perf_fops) {
2521 fput_light(file, *fput_needed);
2522 *fput_needed = 0;
2523 return ERR_PTR(-EBADF);
2526 return file->private_data;
2529 static int perf_event_set_output(struct perf_event *event,
2530 struct perf_event *output_event);
2531 static int perf_event_set_filter(struct perf_event *event, void __user *arg);
2533 static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2535 struct perf_event *event = file->private_data;
2536 void (*func)(struct perf_event *);
2537 u32 flags = arg;
2539 switch (cmd) {
2540 case PERF_EVENT_IOC_ENABLE:
2541 func = perf_event_enable;
2542 break;
2543 case PERF_EVENT_IOC_DISABLE:
2544 func = perf_event_disable;
2545 break;
2546 case PERF_EVENT_IOC_RESET:
2547 func = perf_event_reset;
2548 break;
2550 case PERF_EVENT_IOC_REFRESH:
2551 return perf_event_refresh(event, arg);
2553 case PERF_EVENT_IOC_PERIOD:
2554 return perf_event_period(event, (u64 __user *)arg);
2556 case PERF_EVENT_IOC_SET_OUTPUT:
2558 struct perf_event *output_event = NULL;
2559 int fput_needed = 0;
2560 int ret;
2562 if (arg != -1) {
2563 output_event = perf_fget_light(arg, &fput_needed);
2564 if (IS_ERR(output_event))
2565 return PTR_ERR(output_event);
2568 ret = perf_event_set_output(event, output_event);
2569 if (output_event)
2570 fput_light(output_event->filp, fput_needed);
2572 return ret;
2575 case PERF_EVENT_IOC_SET_FILTER:
2576 return perf_event_set_filter(event, (void __user *)arg);
2578 default:
2579 return -ENOTTY;
2582 if (flags & PERF_IOC_FLAG_GROUP)
2583 perf_event_for_each(event, func);
2584 else
2585 perf_event_for_each_child(event, func);
2587 return 0;
2590 int perf_event_task_enable(void)
2592 struct perf_event *event;
2594 mutex_lock(&current->perf_event_mutex);
2595 list_for_each_entry(event, &current->perf_event_list, owner_entry)
2596 perf_event_for_each_child(event, perf_event_enable);
2597 mutex_unlock(&current->perf_event_mutex);
2599 return 0;
2602 int perf_event_task_disable(void)
2604 struct perf_event *event;
2606 mutex_lock(&current->perf_event_mutex);
2607 list_for_each_entry(event, &current->perf_event_list, owner_entry)
2608 perf_event_for_each_child(event, perf_event_disable);
2609 mutex_unlock(&current->perf_event_mutex);
2611 return 0;
2614 #ifndef PERF_EVENT_INDEX_OFFSET
2615 # define PERF_EVENT_INDEX_OFFSET 0
2616 #endif
2618 static int perf_event_index(struct perf_event *event)
2620 if (event->hw.state & PERF_HES_STOPPED)
2621 return 0;
2623 if (event->state != PERF_EVENT_STATE_ACTIVE)
2624 return 0;
2626 return event->hw.idx + 1 - PERF_EVENT_INDEX_OFFSET;
2630 * Callers need to ensure there can be no nesting of this function, otherwise
2631 * the seqlock logic goes bad. We can not serialize this because the arch
2632 * code calls this from NMI context.
2634 void perf_event_update_userpage(struct perf_event *event)
2636 struct perf_event_mmap_page *userpg;
2637 struct perf_buffer *buffer;
2639 rcu_read_lock();
2640 buffer = rcu_dereference(event->buffer);
2641 if (!buffer)
2642 goto unlock;
2644 userpg = buffer->user_page;
2647 * Disable preemption so as to not let the corresponding user-space
2648 * spin too long if we get preempted.
2650 preempt_disable();
2651 ++userpg->lock;
2652 barrier();
2653 userpg->index = perf_event_index(event);
2654 userpg->offset = perf_event_count(event);
2655 if (event->state == PERF_EVENT_STATE_ACTIVE)
2656 userpg->offset -= local64_read(&event->hw.prev_count);
2658 userpg->time_enabled = event->total_time_enabled +
2659 atomic64_read(&event->child_total_time_enabled);
2661 userpg->time_running = event->total_time_running +
2662 atomic64_read(&event->child_total_time_running);
2664 barrier();
2665 ++userpg->lock;
2666 preempt_enable();
2667 unlock:
2668 rcu_read_unlock();
2671 static unsigned long perf_data_size(struct perf_buffer *buffer);
2673 static void
2674 perf_buffer_init(struct perf_buffer *buffer, long watermark, int flags)
2676 long max_size = perf_data_size(buffer);
2678 if (watermark)
2679 buffer->watermark = min(max_size, watermark);
2681 if (!buffer->watermark)
2682 buffer->watermark = max_size / 2;
2684 if (flags & PERF_BUFFER_WRITABLE)
2685 buffer->writable = 1;
2687 atomic_set(&buffer->refcount, 1);
2690 #ifndef CONFIG_PERF_USE_VMALLOC
2693 * Back perf_mmap() with regular GFP_KERNEL-0 pages.
2696 static struct page *
2697 perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
2699 if (pgoff > buffer->nr_pages)
2700 return NULL;
2702 if (pgoff == 0)
2703 return virt_to_page(buffer->user_page);
2705 return virt_to_page(buffer->data_pages[pgoff - 1]);
2708 static void *perf_mmap_alloc_page(int cpu)
2710 struct page *page;
2711 int node;
2713 node = (cpu == -1) ? cpu : cpu_to_node(cpu);
2714 page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
2715 if (!page)
2716 return NULL;
2718 return page_address(page);
2721 static struct perf_buffer *
2722 perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
2724 struct perf_buffer *buffer;
2725 unsigned long size;
2726 int i;
2728 size = sizeof(struct perf_buffer);
2729 size += nr_pages * sizeof(void *);
2731 buffer = kzalloc(size, GFP_KERNEL);
2732 if (!buffer)
2733 goto fail;
2735 buffer->user_page = perf_mmap_alloc_page(cpu);
2736 if (!buffer->user_page)
2737 goto fail_user_page;
2739 for (i = 0; i < nr_pages; i++) {
2740 buffer->data_pages[i] = perf_mmap_alloc_page(cpu);
2741 if (!buffer->data_pages[i])
2742 goto fail_data_pages;
2745 buffer->nr_pages = nr_pages;
2747 perf_buffer_init(buffer, watermark, flags);
2749 return buffer;
2751 fail_data_pages:
2752 for (i--; i >= 0; i--)
2753 free_page((unsigned long)buffer->data_pages[i]);
2755 free_page((unsigned long)buffer->user_page);
2757 fail_user_page:
2758 kfree(buffer);
2760 fail:
2761 return NULL;
2764 static void perf_mmap_free_page(unsigned long addr)
2766 struct page *page = virt_to_page((void *)addr);
2768 page->mapping = NULL;
2769 __free_page(page);
2772 static void perf_buffer_free(struct perf_buffer *buffer)
2774 int i;
2776 perf_mmap_free_page((unsigned long)buffer->user_page);
2777 for (i = 0; i < buffer->nr_pages; i++)
2778 perf_mmap_free_page((unsigned long)buffer->data_pages[i]);
2779 kfree(buffer);
2782 static inline int page_order(struct perf_buffer *buffer)
2784 return 0;
2787 #else
2790 * Back perf_mmap() with vmalloc memory.
2792 * Required for architectures that have d-cache aliasing issues.
2795 static inline int page_order(struct perf_buffer *buffer)
2797 return buffer->page_order;
2800 static struct page *
2801 perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
2803 if (pgoff > (1UL << page_order(buffer)))
2804 return NULL;
2806 return vmalloc_to_page((void *)buffer->user_page + pgoff * PAGE_SIZE);
2809 static void perf_mmap_unmark_page(void *addr)
2811 struct page *page = vmalloc_to_page(addr);
2813 page->mapping = NULL;
2816 static void perf_buffer_free_work(struct work_struct *work)
2818 struct perf_buffer *buffer;
2819 void *base;
2820 int i, nr;
2822 buffer = container_of(work, struct perf_buffer, work);
2823 nr = 1 << page_order(buffer);
2825 base = buffer->user_page;
2826 for (i = 0; i < nr + 1; i++)
2827 perf_mmap_unmark_page(base + (i * PAGE_SIZE));
2829 vfree(base);
2830 kfree(buffer);
2833 static void perf_buffer_free(struct perf_buffer *buffer)
2835 schedule_work(&buffer->work);
2838 static struct perf_buffer *
2839 perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
2841 struct perf_buffer *buffer;
2842 unsigned long size;
2843 void *all_buf;
2845 size = sizeof(struct perf_buffer);
2846 size += sizeof(void *);
2848 buffer = kzalloc(size, GFP_KERNEL);
2849 if (!buffer)
2850 goto fail;
2852 INIT_WORK(&buffer->work, perf_buffer_free_work);
2854 all_buf = vmalloc_user((nr_pages + 1) * PAGE_SIZE);
2855 if (!all_buf)
2856 goto fail_all_buf;
2858 buffer->user_page = all_buf;
2859 buffer->data_pages[0] = all_buf + PAGE_SIZE;
2860 buffer->page_order = ilog2(nr_pages);
2861 buffer->nr_pages = 1;
2863 perf_buffer_init(buffer, watermark, flags);
2865 return buffer;
2867 fail_all_buf:
2868 kfree(buffer);
2870 fail:
2871 return NULL;
2874 #endif
2876 static unsigned long perf_data_size(struct perf_buffer *buffer)
2878 return buffer->nr_pages << (PAGE_SHIFT + page_order(buffer));
2881 static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
2883 struct perf_event *event = vma->vm_file->private_data;
2884 struct perf_buffer *buffer;
2885 int ret = VM_FAULT_SIGBUS;
2887 if (vmf->flags & FAULT_FLAG_MKWRITE) {
2888 if (vmf->pgoff == 0)
2889 ret = 0;
2890 return ret;
2893 rcu_read_lock();
2894 buffer = rcu_dereference(event->buffer);
2895 if (!buffer)
2896 goto unlock;
2898 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
2899 goto unlock;
2901 vmf->page = perf_mmap_to_page(buffer, vmf->pgoff);
2902 if (!vmf->page)
2903 goto unlock;
2905 get_page(vmf->page);
2906 vmf->page->mapping = vma->vm_file->f_mapping;
2907 vmf->page->index = vmf->pgoff;
2909 ret = 0;
2910 unlock:
2911 rcu_read_unlock();
2913 return ret;
2916 static void perf_buffer_free_rcu(struct rcu_head *rcu_head)
2918 struct perf_buffer *buffer;
2920 buffer = container_of(rcu_head, struct perf_buffer, rcu_head);
2921 perf_buffer_free(buffer);
2924 static struct perf_buffer *perf_buffer_get(struct perf_event *event)
2926 struct perf_buffer *buffer;
2928 rcu_read_lock();
2929 buffer = rcu_dereference(event->buffer);
2930 if (buffer) {
2931 if (!atomic_inc_not_zero(&buffer->refcount))
2932 buffer = NULL;
2934 rcu_read_unlock();
2936 return buffer;
2939 static void perf_buffer_put(struct perf_buffer *buffer)
2941 if (!atomic_dec_and_test(&buffer->refcount))
2942 return;
2944 call_rcu(&buffer->rcu_head, perf_buffer_free_rcu);
2947 static void perf_mmap_open(struct vm_area_struct *vma)
2949 struct perf_event *event = vma->vm_file->private_data;
2951 atomic_inc(&event->mmap_count);
2954 static void perf_mmap_close(struct vm_area_struct *vma)
2956 struct perf_event *event = vma->vm_file->private_data;
2958 if (atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) {
2959 unsigned long size = perf_data_size(event->buffer);
2960 struct user_struct *user = event->mmap_user;
2961 struct perf_buffer *buffer = event->buffer;
2963 atomic_long_sub((size >> PAGE_SHIFT) + 1, &user->locked_vm);
2964 vma->vm_mm->locked_vm -= event->mmap_locked;
2965 rcu_assign_pointer(event->buffer, NULL);
2966 mutex_unlock(&event->mmap_mutex);
2968 perf_buffer_put(buffer);
2969 free_uid(user);
2973 static const struct vm_operations_struct perf_mmap_vmops = {
2974 .open = perf_mmap_open,
2975 .close = perf_mmap_close,
2976 .fault = perf_mmap_fault,
2977 .page_mkwrite = perf_mmap_fault,
2980 static int perf_mmap(struct file *file, struct vm_area_struct *vma)
2982 struct perf_event *event = file->private_data;
2983 unsigned long user_locked, user_lock_limit;
2984 struct user_struct *user = current_user();
2985 unsigned long locked, lock_limit;
2986 struct perf_buffer *buffer;
2987 unsigned long vma_size;
2988 unsigned long nr_pages;
2989 long user_extra, extra;
2990 int ret = 0, flags = 0;
2993 * Don't allow mmap() of inherited per-task counters. This would
2994 * create a performance issue due to all children writing to the
2995 * same buffer.
2997 if (event->cpu == -1 && event->attr.inherit)
2998 return -EINVAL;
3000 if (!(vma->vm_flags & VM_SHARED))
3001 return -EINVAL;
3003 vma_size = vma->vm_end - vma->vm_start;
3004 nr_pages = (vma_size / PAGE_SIZE) - 1;
3007 * If we have buffer pages ensure they're a power-of-two number, so we
3008 * can do bitmasks instead of modulo.
3010 if (nr_pages != 0 && !is_power_of_2(nr_pages))
3011 return -EINVAL;
3013 if (vma_size != PAGE_SIZE * (1 + nr_pages))
3014 return -EINVAL;
3016 if (vma->vm_pgoff != 0)
3017 return -EINVAL;
3019 WARN_ON_ONCE(event->ctx->parent_ctx);
3020 mutex_lock(&event->mmap_mutex);
3021 if (event->buffer) {
3022 if (event->buffer->nr_pages == nr_pages)
3023 atomic_inc(&event->buffer->refcount);
3024 else
3025 ret = -EINVAL;
3026 goto unlock;
3029 user_extra = nr_pages + 1;
3030 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
3033 * Increase the limit linearly with more CPUs:
3035 user_lock_limit *= num_online_cpus();
3037 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
3039 extra = 0;
3040 if (user_locked > user_lock_limit)
3041 extra = user_locked - user_lock_limit;
3043 lock_limit = rlimit(RLIMIT_MEMLOCK);
3044 lock_limit >>= PAGE_SHIFT;
3045 locked = vma->vm_mm->locked_vm + extra;
3047 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
3048 !capable(CAP_IPC_LOCK)) {
3049 ret = -EPERM;
3050 goto unlock;
3053 WARN_ON(event->buffer);
3055 if (vma->vm_flags & VM_WRITE)
3056 flags |= PERF_BUFFER_WRITABLE;
3058 buffer = perf_buffer_alloc(nr_pages, event->attr.wakeup_watermark,
3059 event->cpu, flags);
3060 if (!buffer) {
3061 ret = -ENOMEM;
3062 goto unlock;
3064 rcu_assign_pointer(event->buffer, buffer);
3066 atomic_long_add(user_extra, &user->locked_vm);
3067 event->mmap_locked = extra;
3068 event->mmap_user = get_current_user();
3069 vma->vm_mm->locked_vm += event->mmap_locked;
3071 unlock:
3072 if (!ret)
3073 atomic_inc(&event->mmap_count);
3074 mutex_unlock(&event->mmap_mutex);
3076 vma->vm_flags |= VM_RESERVED;
3077 vma->vm_ops = &perf_mmap_vmops;
3079 return ret;
3082 static int perf_fasync(int fd, struct file *filp, int on)
3084 struct inode *inode = filp->f_path.dentry->d_inode;
3085 struct perf_event *event = filp->private_data;
3086 int retval;
3088 mutex_lock(&inode->i_mutex);
3089 retval = fasync_helper(fd, filp, on, &event->fasync);
3090 mutex_unlock(&inode->i_mutex);
3092 if (retval < 0)
3093 return retval;
3095 return 0;
3098 static const struct file_operations perf_fops = {
3099 .llseek = no_llseek,
3100 .release = perf_release,
3101 .read = perf_read,
3102 .poll = perf_poll,
3103 .unlocked_ioctl = perf_ioctl,
3104 .compat_ioctl = perf_ioctl,
3105 .mmap = perf_mmap,
3106 .fasync = perf_fasync,
3110 * Perf event wakeup
3112 * If there's data, ensure we set the poll() state and publish everything
3113 * to user-space before waking everybody up.
3116 void perf_event_wakeup(struct perf_event *event)
3118 wake_up_all(&event->waitq);
3120 if (event->pending_kill) {
3121 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
3122 event->pending_kill = 0;
3126 static void perf_pending_event(struct irq_work *entry)
3128 struct perf_event *event = container_of(entry,
3129 struct perf_event, pending);
3131 if (event->pending_disable) {
3132 event->pending_disable = 0;
3133 __perf_event_disable(event);
3136 if (event->pending_wakeup) {
3137 event->pending_wakeup = 0;
3138 perf_event_wakeup(event);
3143 * We assume there is only KVM supporting the callbacks.
3144 * Later on, we might change it to a list if there is
3145 * another virtualization implementation supporting the callbacks.
3147 struct perf_guest_info_callbacks *perf_guest_cbs;
3149 int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3151 perf_guest_cbs = cbs;
3152 return 0;
3154 EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
3156 int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3158 perf_guest_cbs = NULL;
3159 return 0;
3161 EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
3164 * Output
3166 static bool perf_output_space(struct perf_buffer *buffer, unsigned long tail,
3167 unsigned long offset, unsigned long head)
3169 unsigned long mask;
3171 if (!buffer->writable)
3172 return true;
3174 mask = perf_data_size(buffer) - 1;
3176 offset = (offset - tail) & mask;
3177 head = (head - tail) & mask;
3179 if ((int)(head - offset) < 0)
3180 return false;
3182 return true;
3185 static void perf_output_wakeup(struct perf_output_handle *handle)
3187 atomic_set(&handle->buffer->poll, POLL_IN);
3189 if (handle->nmi) {
3190 handle->event->pending_wakeup = 1;
3191 irq_work_queue(&handle->event->pending);
3192 } else
3193 perf_event_wakeup(handle->event);
3197 * We need to ensure a later event_id doesn't publish a head when a former
3198 * event isn't done writing. However since we need to deal with NMIs we
3199 * cannot fully serialize things.
3201 * We only publish the head (and generate a wakeup) when the outer-most
3202 * event completes.
3204 static void perf_output_get_handle(struct perf_output_handle *handle)
3206 struct perf_buffer *buffer = handle->buffer;
3208 preempt_disable();
3209 local_inc(&buffer->nest);
3210 handle->wakeup = local_read(&buffer->wakeup);
3213 static void perf_output_put_handle(struct perf_output_handle *handle)
3215 struct perf_buffer *buffer = handle->buffer;
3216 unsigned long head;
3218 again:
3219 head = local_read(&buffer->head);
3222 * IRQ/NMI can happen here, which means we can miss a head update.
3225 if (!local_dec_and_test(&buffer->nest))
3226 goto out;
3229 * Publish the known good head. Rely on the full barrier implied
3230 * by atomic_dec_and_test() order the buffer->head read and this
3231 * write.
3233 buffer->user_page->data_head = head;
3236 * Now check if we missed an update, rely on the (compiler)
3237 * barrier in atomic_dec_and_test() to re-read buffer->head.
3239 if (unlikely(head != local_read(&buffer->head))) {
3240 local_inc(&buffer->nest);
3241 goto again;
3244 if (handle->wakeup != local_read(&buffer->wakeup))
3245 perf_output_wakeup(handle);
3247 out:
3248 preempt_enable();
3251 __always_inline void perf_output_copy(struct perf_output_handle *handle,
3252 const void *buf, unsigned int len)
3254 do {
3255 unsigned long size = min_t(unsigned long, handle->size, len);
3257 memcpy(handle->addr, buf, size);
3259 len -= size;
3260 handle->addr += size;
3261 buf += size;
3262 handle->size -= size;
3263 if (!handle->size) {
3264 struct perf_buffer *buffer = handle->buffer;
3266 handle->page++;
3267 handle->page &= buffer->nr_pages - 1;
3268 handle->addr = buffer->data_pages[handle->page];
3269 handle->size = PAGE_SIZE << page_order(buffer);
3271 } while (len);
3274 int perf_output_begin(struct perf_output_handle *handle,
3275 struct perf_event *event, unsigned int size,
3276 int nmi, int sample)
3278 struct perf_buffer *buffer;
3279 unsigned long tail, offset, head;
3280 int have_lost;
3281 struct {
3282 struct perf_event_header header;
3283 u64 id;
3284 u64 lost;
3285 } lost_event;
3287 rcu_read_lock();
3289 * For inherited events we send all the output towards the parent.
3291 if (event->parent)
3292 event = event->parent;
3294 buffer = rcu_dereference(event->buffer);
3295 if (!buffer)
3296 goto out;
3298 handle->buffer = buffer;
3299 handle->event = event;
3300 handle->nmi = nmi;
3301 handle->sample = sample;
3303 if (!buffer->nr_pages)
3304 goto out;
3306 have_lost = local_read(&buffer->lost);
3307 if (have_lost)
3308 size += sizeof(lost_event);
3310 perf_output_get_handle(handle);
3312 do {
3314 * Userspace could choose to issue a mb() before updating the
3315 * tail pointer. So that all reads will be completed before the
3316 * write is issued.
3318 tail = ACCESS_ONCE(buffer->user_page->data_tail);
3319 smp_rmb();
3320 offset = head = local_read(&buffer->head);
3321 head += size;
3322 if (unlikely(!perf_output_space(buffer, tail, offset, head)))
3323 goto fail;
3324 } while (local_cmpxchg(&buffer->head, offset, head) != offset);
3326 if (head - local_read(&buffer->wakeup) > buffer->watermark)
3327 local_add(buffer->watermark, &buffer->wakeup);
3329 handle->page = offset >> (PAGE_SHIFT + page_order(buffer));
3330 handle->page &= buffer->nr_pages - 1;
3331 handle->size = offset & ((PAGE_SIZE << page_order(buffer)) - 1);
3332 handle->addr = buffer->data_pages[handle->page];
3333 handle->addr += handle->size;
3334 handle->size = (PAGE_SIZE << page_order(buffer)) - handle->size;
3336 if (have_lost) {
3337 lost_event.header.type = PERF_RECORD_LOST;
3338 lost_event.header.misc = 0;
3339 lost_event.header.size = sizeof(lost_event);
3340 lost_event.id = event->id;
3341 lost_event.lost = local_xchg(&buffer->lost, 0);
3343 perf_output_put(handle, lost_event);
3346 return 0;
3348 fail:
3349 local_inc(&buffer->lost);
3350 perf_output_put_handle(handle);
3351 out:
3352 rcu_read_unlock();
3354 return -ENOSPC;
3357 void perf_output_end(struct perf_output_handle *handle)
3359 struct perf_event *event = handle->event;
3360 struct perf_buffer *buffer = handle->buffer;
3362 int wakeup_events = event->attr.wakeup_events;
3364 if (handle->sample && wakeup_events) {
3365 int events = local_inc_return(&buffer->events);
3366 if (events >= wakeup_events) {
3367 local_sub(wakeup_events, &buffer->events);
3368 local_inc(&buffer->wakeup);
3372 perf_output_put_handle(handle);
3373 rcu_read_unlock();
3376 static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
3379 * only top level events have the pid namespace they were created in
3381 if (event->parent)
3382 event = event->parent;
3384 return task_tgid_nr_ns(p, event->ns);
3387 static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
3390 * only top level events have the pid namespace they were created in
3392 if (event->parent)
3393 event = event->parent;
3395 return task_pid_nr_ns(p, event->ns);
3398 static void perf_output_read_one(struct perf_output_handle *handle,
3399 struct perf_event *event)
3401 u64 read_format = event->attr.read_format;
3402 u64 values[4];
3403 int n = 0;
3405 values[n++] = perf_event_count(event);
3406 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
3407 values[n++] = event->total_time_enabled +
3408 atomic64_read(&event->child_total_time_enabled);
3410 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
3411 values[n++] = event->total_time_running +
3412 atomic64_read(&event->child_total_time_running);
3414 if (read_format & PERF_FORMAT_ID)
3415 values[n++] = primary_event_id(event);
3417 perf_output_copy(handle, values, n * sizeof(u64));
3421 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
3423 static void perf_output_read_group(struct perf_output_handle *handle,
3424 struct perf_event *event)
3426 struct perf_event *leader = event->group_leader, *sub;
3427 u64 read_format = event->attr.read_format;
3428 u64 values[5];
3429 int n = 0;
3431 values[n++] = 1 + leader->nr_siblings;
3433 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3434 values[n++] = leader->total_time_enabled;
3436 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3437 values[n++] = leader->total_time_running;
3439 if (leader != event)
3440 leader->pmu->read(leader);
3442 values[n++] = perf_event_count(leader);
3443 if (read_format & PERF_FORMAT_ID)
3444 values[n++] = primary_event_id(leader);
3446 perf_output_copy(handle, values, n * sizeof(u64));
3448 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
3449 n = 0;
3451 if (sub != event)
3452 sub->pmu->read(sub);
3454 values[n++] = perf_event_count(sub);
3455 if (read_format & PERF_FORMAT_ID)
3456 values[n++] = primary_event_id(sub);
3458 perf_output_copy(handle, values, n * sizeof(u64));
3462 static void perf_output_read(struct perf_output_handle *handle,
3463 struct perf_event *event)
3465 if (event->attr.read_format & PERF_FORMAT_GROUP)
3466 perf_output_read_group(handle, event);
3467 else
3468 perf_output_read_one(handle, event);
3471 void perf_output_sample(struct perf_output_handle *handle,
3472 struct perf_event_header *header,
3473 struct perf_sample_data *data,
3474 struct perf_event *event)
3476 u64 sample_type = data->type;
3478 perf_output_put(handle, *header);
3480 if (sample_type & PERF_SAMPLE_IP)
3481 perf_output_put(handle, data->ip);
3483 if (sample_type & PERF_SAMPLE_TID)
3484 perf_output_put(handle, data->tid_entry);
3486 if (sample_type & PERF_SAMPLE_TIME)
3487 perf_output_put(handle, data->time);
3489 if (sample_type & PERF_SAMPLE_ADDR)
3490 perf_output_put(handle, data->addr);
3492 if (sample_type & PERF_SAMPLE_ID)
3493 perf_output_put(handle, data->id);
3495 if (sample_type & PERF_SAMPLE_STREAM_ID)
3496 perf_output_put(handle, data->stream_id);
3498 if (sample_type & PERF_SAMPLE_CPU)
3499 perf_output_put(handle, data->cpu_entry);
3501 if (sample_type & PERF_SAMPLE_PERIOD)
3502 perf_output_put(handle, data->period);
3504 if (sample_type & PERF_SAMPLE_READ)
3505 perf_output_read(handle, event);
3507 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3508 if (data->callchain) {
3509 int size = 1;
3511 if (data->callchain)
3512 size += data->callchain->nr;
3514 size *= sizeof(u64);
3516 perf_output_copy(handle, data->callchain, size);
3517 } else {
3518 u64 nr = 0;
3519 perf_output_put(handle, nr);
3523 if (sample_type & PERF_SAMPLE_RAW) {
3524 if (data->raw) {
3525 perf_output_put(handle, data->raw->size);
3526 perf_output_copy(handle, data->raw->data,
3527 data->raw->size);
3528 } else {
3529 struct {
3530 u32 size;
3531 u32 data;
3532 } raw = {
3533 .size = sizeof(u32),
3534 .data = 0,
3536 perf_output_put(handle, raw);
3541 void perf_prepare_sample(struct perf_event_header *header,
3542 struct perf_sample_data *data,
3543 struct perf_event *event,
3544 struct pt_regs *regs)
3546 u64 sample_type = event->attr.sample_type;
3548 data->type = sample_type;
3550 header->type = PERF_RECORD_SAMPLE;
3551 header->size = sizeof(*header);
3553 header->misc = 0;
3554 header->misc |= perf_misc_flags(regs);
3556 if (sample_type & PERF_SAMPLE_IP) {
3557 data->ip = perf_instruction_pointer(regs);
3559 header->size += sizeof(data->ip);
3562 if (sample_type & PERF_SAMPLE_TID) {
3563 /* namespace issues */
3564 data->tid_entry.pid = perf_event_pid(event, current);
3565 data->tid_entry.tid = perf_event_tid(event, current);
3567 header->size += sizeof(data->tid_entry);
3570 if (sample_type & PERF_SAMPLE_TIME) {
3571 data->time = perf_clock();
3573 header->size += sizeof(data->time);
3576 if (sample_type & PERF_SAMPLE_ADDR)
3577 header->size += sizeof(data->addr);
3579 if (sample_type & PERF_SAMPLE_ID) {
3580 data->id = primary_event_id(event);
3582 header->size += sizeof(data->id);
3585 if (sample_type & PERF_SAMPLE_STREAM_ID) {
3586 data->stream_id = event->id;
3588 header->size += sizeof(data->stream_id);
3591 if (sample_type & PERF_SAMPLE_CPU) {
3592 data->cpu_entry.cpu = raw_smp_processor_id();
3593 data->cpu_entry.reserved = 0;
3595 header->size += sizeof(data->cpu_entry);
3598 if (sample_type & PERF_SAMPLE_PERIOD)
3599 header->size += sizeof(data->period);
3601 if (sample_type & PERF_SAMPLE_READ)
3602 header->size += perf_event_read_size(event);
3604 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3605 int size = 1;
3607 data->callchain = perf_callchain(regs);
3609 if (data->callchain)
3610 size += data->callchain->nr;
3612 header->size += size * sizeof(u64);
3615 if (sample_type & PERF_SAMPLE_RAW) {
3616 int size = sizeof(u32);
3618 if (data->raw)
3619 size += data->raw->size;
3620 else
3621 size += sizeof(u32);
3623 WARN_ON_ONCE(size & (sizeof(u64)-1));
3624 header->size += size;
3628 static void perf_event_output(struct perf_event *event, int nmi,
3629 struct perf_sample_data *data,
3630 struct pt_regs *regs)
3632 struct perf_output_handle handle;
3633 struct perf_event_header header;
3635 /* protect the callchain buffers */
3636 rcu_read_lock();
3638 perf_prepare_sample(&header, data, event, regs);
3640 if (perf_output_begin(&handle, event, header.size, nmi, 1))
3641 goto exit;
3643 perf_output_sample(&handle, &header, data, event);
3645 perf_output_end(&handle);
3647 exit:
3648 rcu_read_unlock();
3652 * read event_id
3655 struct perf_read_event {
3656 struct perf_event_header header;
3658 u32 pid;
3659 u32 tid;
3662 static void
3663 perf_event_read_event(struct perf_event *event,
3664 struct task_struct *task)
3666 struct perf_output_handle handle;
3667 struct perf_read_event read_event = {
3668 .header = {
3669 .type = PERF_RECORD_READ,
3670 .misc = 0,
3671 .size = sizeof(read_event) + perf_event_read_size(event),
3673 .pid = perf_event_pid(event, task),
3674 .tid = perf_event_tid(event, task),
3676 int ret;
3678 ret = perf_output_begin(&handle, event, read_event.header.size, 0, 0);
3679 if (ret)
3680 return;
3682 perf_output_put(&handle, read_event);
3683 perf_output_read(&handle, event);
3685 perf_output_end(&handle);
3689 * task tracking -- fork/exit
3691 * enabled by: attr.comm | attr.mmap | attr.mmap_data | attr.task
3694 struct perf_task_event {
3695 struct task_struct *task;
3696 struct perf_event_context *task_ctx;
3698 struct {
3699 struct perf_event_header header;
3701 u32 pid;
3702 u32 ppid;
3703 u32 tid;
3704 u32 ptid;
3705 u64 time;
3706 } event_id;
3709 static void perf_event_task_output(struct perf_event *event,
3710 struct perf_task_event *task_event)
3712 struct perf_output_handle handle;
3713 struct task_struct *task = task_event->task;
3714 int size, ret;
3716 size = task_event->event_id.header.size;
3717 ret = perf_output_begin(&handle, event, size, 0, 0);
3719 if (ret)
3720 return;
3722 task_event->event_id.pid = perf_event_pid(event, task);
3723 task_event->event_id.ppid = perf_event_pid(event, current);
3725 task_event->event_id.tid = perf_event_tid(event, task);
3726 task_event->event_id.ptid = perf_event_tid(event, current);
3728 perf_output_put(&handle, task_event->event_id);
3730 perf_output_end(&handle);
3733 static int perf_event_task_match(struct perf_event *event)
3735 if (event->state < PERF_EVENT_STATE_INACTIVE)
3736 return 0;
3738 if (event->cpu != -1 && event->cpu != smp_processor_id())
3739 return 0;
3741 if (event->attr.comm || event->attr.mmap ||
3742 event->attr.mmap_data || event->attr.task)
3743 return 1;
3745 return 0;
3748 static void perf_event_task_ctx(struct perf_event_context *ctx,
3749 struct perf_task_event *task_event)
3751 struct perf_event *event;
3753 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3754 if (perf_event_task_match(event))
3755 perf_event_task_output(event, task_event);
3759 static void perf_event_task_event(struct perf_task_event *task_event)
3761 struct perf_cpu_context *cpuctx;
3762 struct perf_event_context *ctx;
3763 struct pmu *pmu;
3764 int ctxn;
3766 rcu_read_lock();
3767 list_for_each_entry_rcu(pmu, &pmus, entry) {
3768 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
3769 perf_event_task_ctx(&cpuctx->ctx, task_event);
3771 ctx = task_event->task_ctx;
3772 if (!ctx) {
3773 ctxn = pmu->task_ctx_nr;
3774 if (ctxn < 0)
3775 goto next;
3776 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
3778 if (ctx)
3779 perf_event_task_ctx(ctx, task_event);
3780 next:
3781 put_cpu_ptr(pmu->pmu_cpu_context);
3783 rcu_read_unlock();
3786 static void perf_event_task(struct task_struct *task,
3787 struct perf_event_context *task_ctx,
3788 int new)
3790 struct perf_task_event task_event;
3792 if (!atomic_read(&nr_comm_events) &&
3793 !atomic_read(&nr_mmap_events) &&
3794 !atomic_read(&nr_task_events))
3795 return;
3797 task_event = (struct perf_task_event){
3798 .task = task,
3799 .task_ctx = task_ctx,
3800 .event_id = {
3801 .header = {
3802 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
3803 .misc = 0,
3804 .size = sizeof(task_event.event_id),
3806 /* .pid */
3807 /* .ppid */
3808 /* .tid */
3809 /* .ptid */
3810 .time = perf_clock(),
3814 perf_event_task_event(&task_event);
3817 void perf_event_fork(struct task_struct *task)
3819 perf_event_task(task, NULL, 1);
3823 * comm tracking
3826 struct perf_comm_event {
3827 struct task_struct *task;
3828 char *comm;
3829 int comm_size;
3831 struct {
3832 struct perf_event_header header;
3834 u32 pid;
3835 u32 tid;
3836 } event_id;
3839 static void perf_event_comm_output(struct perf_event *event,
3840 struct perf_comm_event *comm_event)
3842 struct perf_output_handle handle;
3843 int size = comm_event->event_id.header.size;
3844 int ret = perf_output_begin(&handle, event, size, 0, 0);
3846 if (ret)
3847 return;
3849 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
3850 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
3852 perf_output_put(&handle, comm_event->event_id);
3853 perf_output_copy(&handle, comm_event->comm,
3854 comm_event->comm_size);
3855 perf_output_end(&handle);
3858 static int perf_event_comm_match(struct perf_event *event)
3860 if (event->state < PERF_EVENT_STATE_INACTIVE)
3861 return 0;
3863 if (event->cpu != -1 && event->cpu != smp_processor_id())
3864 return 0;
3866 if (event->attr.comm)
3867 return 1;
3869 return 0;
3872 static void perf_event_comm_ctx(struct perf_event_context *ctx,
3873 struct perf_comm_event *comm_event)
3875 struct perf_event *event;
3877 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3878 if (perf_event_comm_match(event))
3879 perf_event_comm_output(event, comm_event);
3883 static void perf_event_comm_event(struct perf_comm_event *comm_event)
3885 struct perf_cpu_context *cpuctx;
3886 struct perf_event_context *ctx;
3887 char comm[TASK_COMM_LEN];
3888 unsigned int size;
3889 struct pmu *pmu;
3890 int ctxn;
3892 memset(comm, 0, sizeof(comm));
3893 strlcpy(comm, comm_event->task->comm, sizeof(comm));
3894 size = ALIGN(strlen(comm)+1, sizeof(u64));
3896 comm_event->comm = comm;
3897 comm_event->comm_size = size;
3899 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
3901 rcu_read_lock();
3902 list_for_each_entry_rcu(pmu, &pmus, entry) {
3903 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
3904 perf_event_comm_ctx(&cpuctx->ctx, comm_event);
3906 ctxn = pmu->task_ctx_nr;
3907 if (ctxn < 0)
3908 goto next;
3910 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
3911 if (ctx)
3912 perf_event_comm_ctx(ctx, comm_event);
3913 next:
3914 put_cpu_ptr(pmu->pmu_cpu_context);
3916 rcu_read_unlock();
3919 void perf_event_comm(struct task_struct *task)
3921 struct perf_comm_event comm_event;
3922 struct perf_event_context *ctx;
3923 int ctxn;
3925 for_each_task_context_nr(ctxn) {
3926 ctx = task->perf_event_ctxp[ctxn];
3927 if (!ctx)
3928 continue;
3930 perf_event_enable_on_exec(ctx);
3933 if (!atomic_read(&nr_comm_events))
3934 return;
3936 comm_event = (struct perf_comm_event){
3937 .task = task,
3938 /* .comm */
3939 /* .comm_size */
3940 .event_id = {
3941 .header = {
3942 .type = PERF_RECORD_COMM,
3943 .misc = 0,
3944 /* .size */
3946 /* .pid */
3947 /* .tid */
3951 perf_event_comm_event(&comm_event);
3955 * mmap tracking
3958 struct perf_mmap_event {
3959 struct vm_area_struct *vma;
3961 const char *file_name;
3962 int file_size;
3964 struct {
3965 struct perf_event_header header;
3967 u32 pid;
3968 u32 tid;
3969 u64 start;
3970 u64 len;
3971 u64 pgoff;
3972 } event_id;
3975 static void perf_event_mmap_output(struct perf_event *event,
3976 struct perf_mmap_event *mmap_event)
3978 struct perf_output_handle handle;
3979 int size = mmap_event->event_id.header.size;
3980 int ret = perf_output_begin(&handle, event, size, 0, 0);
3982 if (ret)
3983 return;
3985 mmap_event->event_id.pid = perf_event_pid(event, current);
3986 mmap_event->event_id.tid = perf_event_tid(event, current);
3988 perf_output_put(&handle, mmap_event->event_id);
3989 perf_output_copy(&handle, mmap_event->file_name,
3990 mmap_event->file_size);
3991 perf_output_end(&handle);
3994 static int perf_event_mmap_match(struct perf_event *event,
3995 struct perf_mmap_event *mmap_event,
3996 int executable)
3998 if (event->state < PERF_EVENT_STATE_INACTIVE)
3999 return 0;
4001 if (event->cpu != -1 && event->cpu != smp_processor_id())
4002 return 0;
4004 if ((!executable && event->attr.mmap_data) ||
4005 (executable && event->attr.mmap))
4006 return 1;
4008 return 0;
4011 static void perf_event_mmap_ctx(struct perf_event_context *ctx,
4012 struct perf_mmap_event *mmap_event,
4013 int executable)
4015 struct perf_event *event;
4017 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4018 if (perf_event_mmap_match(event, mmap_event, executable))
4019 perf_event_mmap_output(event, mmap_event);
4023 static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
4025 struct perf_cpu_context *cpuctx;
4026 struct perf_event_context *ctx;
4027 struct vm_area_struct *vma = mmap_event->vma;
4028 struct file *file = vma->vm_file;
4029 unsigned int size;
4030 char tmp[16];
4031 char *buf = NULL;
4032 const char *name;
4033 struct pmu *pmu;
4034 int ctxn;
4036 memset(tmp, 0, sizeof(tmp));
4038 if (file) {
4040 * d_path works from the end of the buffer backwards, so we
4041 * need to add enough zero bytes after the string to handle
4042 * the 64bit alignment we do later.
4044 buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
4045 if (!buf) {
4046 name = strncpy(tmp, "//enomem", sizeof(tmp));
4047 goto got_name;
4049 name = d_path(&file->f_path, buf, PATH_MAX);
4050 if (IS_ERR(name)) {
4051 name = strncpy(tmp, "//toolong", sizeof(tmp));
4052 goto got_name;
4054 } else {
4055 if (arch_vma_name(mmap_event->vma)) {
4056 name = strncpy(tmp, arch_vma_name(mmap_event->vma),
4057 sizeof(tmp));
4058 goto got_name;
4061 if (!vma->vm_mm) {
4062 name = strncpy(tmp, "[vdso]", sizeof(tmp));
4063 goto got_name;
4064 } else if (vma->vm_start <= vma->vm_mm->start_brk &&
4065 vma->vm_end >= vma->vm_mm->brk) {
4066 name = strncpy(tmp, "[heap]", sizeof(tmp));
4067 goto got_name;
4068 } else if (vma->vm_start <= vma->vm_mm->start_stack &&
4069 vma->vm_end >= vma->vm_mm->start_stack) {
4070 name = strncpy(tmp, "[stack]", sizeof(tmp));
4071 goto got_name;
4074 name = strncpy(tmp, "//anon", sizeof(tmp));
4075 goto got_name;
4078 got_name:
4079 size = ALIGN(strlen(name)+1, sizeof(u64));
4081 mmap_event->file_name = name;
4082 mmap_event->file_size = size;
4084 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
4086 rcu_read_lock();
4087 list_for_each_entry_rcu(pmu, &pmus, entry) {
4088 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
4089 perf_event_mmap_ctx(&cpuctx->ctx, mmap_event,
4090 vma->vm_flags & VM_EXEC);
4092 ctxn = pmu->task_ctx_nr;
4093 if (ctxn < 0)
4094 goto next;
4096 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4097 if (ctx) {
4098 perf_event_mmap_ctx(ctx, mmap_event,
4099 vma->vm_flags & VM_EXEC);
4101 next:
4102 put_cpu_ptr(pmu->pmu_cpu_context);
4104 rcu_read_unlock();
4106 kfree(buf);
4109 void perf_event_mmap(struct vm_area_struct *vma)
4111 struct perf_mmap_event mmap_event;
4113 if (!atomic_read(&nr_mmap_events))
4114 return;
4116 mmap_event = (struct perf_mmap_event){
4117 .vma = vma,
4118 /* .file_name */
4119 /* .file_size */
4120 .event_id = {
4121 .header = {
4122 .type = PERF_RECORD_MMAP,
4123 .misc = PERF_RECORD_MISC_USER,
4124 /* .size */
4126 /* .pid */
4127 /* .tid */
4128 .start = vma->vm_start,
4129 .len = vma->vm_end - vma->vm_start,
4130 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
4134 perf_event_mmap_event(&mmap_event);
4138 * IRQ throttle logging
4141 static void perf_log_throttle(struct perf_event *event, int enable)
4143 struct perf_output_handle handle;
4144 int ret;
4146 struct {
4147 struct perf_event_header header;
4148 u64 time;
4149 u64 id;
4150 u64 stream_id;
4151 } throttle_event = {
4152 .header = {
4153 .type = PERF_RECORD_THROTTLE,
4154 .misc = 0,
4155 .size = sizeof(throttle_event),
4157 .time = perf_clock(),
4158 .id = primary_event_id(event),
4159 .stream_id = event->id,
4162 if (enable)
4163 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
4165 ret = perf_output_begin(&handle, event, sizeof(throttle_event), 1, 0);
4166 if (ret)
4167 return;
4169 perf_output_put(&handle, throttle_event);
4170 perf_output_end(&handle);
4174 * Generic event overflow handling, sampling.
4177 static int __perf_event_overflow(struct perf_event *event, int nmi,
4178 int throttle, struct perf_sample_data *data,
4179 struct pt_regs *regs)
4181 int events = atomic_read(&event->event_limit);
4182 struct hw_perf_event *hwc = &event->hw;
4183 int ret = 0;
4185 if (!throttle) {
4186 hwc->interrupts++;
4187 } else {
4188 if (hwc->interrupts != MAX_INTERRUPTS) {
4189 hwc->interrupts++;
4190 if (HZ * hwc->interrupts >
4191 (u64)sysctl_perf_event_sample_rate) {
4192 hwc->interrupts = MAX_INTERRUPTS;
4193 perf_log_throttle(event, 0);
4194 ret = 1;
4196 } else {
4198 * Keep re-disabling events even though on the previous
4199 * pass we disabled it - just in case we raced with a
4200 * sched-in and the event got enabled again:
4202 ret = 1;
4206 if (event->attr.freq) {
4207 u64 now = perf_clock();
4208 s64 delta = now - hwc->freq_time_stamp;
4210 hwc->freq_time_stamp = now;
4212 if (delta > 0 && delta < 2*TICK_NSEC)
4213 perf_adjust_period(event, delta, hwc->last_period);
4217 * XXX event_limit might not quite work as expected on inherited
4218 * events
4221 event->pending_kill = POLL_IN;
4222 if (events && atomic_dec_and_test(&event->event_limit)) {
4223 ret = 1;
4224 event->pending_kill = POLL_HUP;
4225 if (nmi) {
4226 event->pending_disable = 1;
4227 irq_work_queue(&event->pending);
4228 } else
4229 perf_event_disable(event);
4232 if (event->overflow_handler)
4233 event->overflow_handler(event, nmi, data, regs);
4234 else
4235 perf_event_output(event, nmi, data, regs);
4237 return ret;
4240 int perf_event_overflow(struct perf_event *event, int nmi,
4241 struct perf_sample_data *data,
4242 struct pt_regs *regs)
4244 return __perf_event_overflow(event, nmi, 1, data, regs);
4248 * Generic software event infrastructure
4251 struct swevent_htable {
4252 struct swevent_hlist *swevent_hlist;
4253 struct mutex hlist_mutex;
4254 int hlist_refcount;
4256 /* Recursion avoidance in each contexts */
4257 int recursion[PERF_NR_CONTEXTS];
4260 static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
4263 * We directly increment event->count and keep a second value in
4264 * event->hw.period_left to count intervals. This period event
4265 * is kept in the range [-sample_period, 0] so that we can use the
4266 * sign as trigger.
4269 static u64 perf_swevent_set_period(struct perf_event *event)
4271 struct hw_perf_event *hwc = &event->hw;
4272 u64 period = hwc->last_period;
4273 u64 nr, offset;
4274 s64 old, val;
4276 hwc->last_period = hwc->sample_period;
4278 again:
4279 old = val = local64_read(&hwc->period_left);
4280 if (val < 0)
4281 return 0;
4283 nr = div64_u64(period + val, period);
4284 offset = nr * period;
4285 val -= offset;
4286 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
4287 goto again;
4289 return nr;
4292 static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
4293 int nmi, struct perf_sample_data *data,
4294 struct pt_regs *regs)
4296 struct hw_perf_event *hwc = &event->hw;
4297 int throttle = 0;
4299 data->period = event->hw.last_period;
4300 if (!overflow)
4301 overflow = perf_swevent_set_period(event);
4303 if (hwc->interrupts == MAX_INTERRUPTS)
4304 return;
4306 for (; overflow; overflow--) {
4307 if (__perf_event_overflow(event, nmi, throttle,
4308 data, regs)) {
4310 * We inhibit the overflow from happening when
4311 * hwc->interrupts == MAX_INTERRUPTS.
4313 break;
4315 throttle = 1;
4319 static void perf_swevent_event(struct perf_event *event, u64 nr,
4320 int nmi, struct perf_sample_data *data,
4321 struct pt_regs *regs)
4323 struct hw_perf_event *hwc = &event->hw;
4325 local64_add(nr, &event->count);
4327 if (!regs)
4328 return;
4330 if (!hwc->sample_period)
4331 return;
4333 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
4334 return perf_swevent_overflow(event, 1, nmi, data, regs);
4336 if (local64_add_negative(nr, &hwc->period_left))
4337 return;
4339 perf_swevent_overflow(event, 0, nmi, data, regs);
4342 static int perf_exclude_event(struct perf_event *event,
4343 struct pt_regs *regs)
4345 if (event->hw.state & PERF_HES_STOPPED)
4346 return 0;
4348 if (regs) {
4349 if (event->attr.exclude_user && user_mode(regs))
4350 return 1;
4352 if (event->attr.exclude_kernel && !user_mode(regs))
4353 return 1;
4356 return 0;
4359 static int perf_swevent_match(struct perf_event *event,
4360 enum perf_type_id type,
4361 u32 event_id,
4362 struct perf_sample_data *data,
4363 struct pt_regs *regs)
4365 if (event->attr.type != type)
4366 return 0;
4368 if (event->attr.config != event_id)
4369 return 0;
4371 if (perf_exclude_event(event, regs))
4372 return 0;
4374 return 1;
4377 static inline u64 swevent_hash(u64 type, u32 event_id)
4379 u64 val = event_id | (type << 32);
4381 return hash_64(val, SWEVENT_HLIST_BITS);
4384 static inline struct hlist_head *
4385 __find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
4387 u64 hash = swevent_hash(type, event_id);
4389 return &hlist->heads[hash];
4392 /* For the read side: events when they trigger */
4393 static inline struct hlist_head *
4394 find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
4396 struct swevent_hlist *hlist;
4398 hlist = rcu_dereference(swhash->swevent_hlist);
4399 if (!hlist)
4400 return NULL;
4402 return __find_swevent_head(hlist, type, event_id);
4405 /* For the event head insertion and removal in the hlist */
4406 static inline struct hlist_head *
4407 find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
4409 struct swevent_hlist *hlist;
4410 u32 event_id = event->attr.config;
4411 u64 type = event->attr.type;
4414 * Event scheduling is always serialized against hlist allocation
4415 * and release. Which makes the protected version suitable here.
4416 * The context lock guarantees that.
4418 hlist = rcu_dereference_protected(swhash->swevent_hlist,
4419 lockdep_is_held(&event->ctx->lock));
4420 if (!hlist)
4421 return NULL;
4423 return __find_swevent_head(hlist, type, event_id);
4426 static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
4427 u64 nr, int nmi,
4428 struct perf_sample_data *data,
4429 struct pt_regs *regs)
4431 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
4432 struct perf_event *event;
4433 struct hlist_node *node;
4434 struct hlist_head *head;
4436 rcu_read_lock();
4437 head = find_swevent_head_rcu(swhash, type, event_id);
4438 if (!head)
4439 goto end;
4441 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
4442 if (perf_swevent_match(event, type, event_id, data, regs))
4443 perf_swevent_event(event, nr, nmi, data, regs);
4445 end:
4446 rcu_read_unlock();
4449 int perf_swevent_get_recursion_context(void)
4451 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
4453 return get_recursion_context(swhash->recursion);
4455 EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
4457 void inline perf_swevent_put_recursion_context(int rctx)
4459 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
4461 put_recursion_context(swhash->recursion, rctx);
4464 void __perf_sw_event(u32 event_id, u64 nr, int nmi,
4465 struct pt_regs *regs, u64 addr)
4467 struct perf_sample_data data;
4468 int rctx;
4470 preempt_disable_notrace();
4471 rctx = perf_swevent_get_recursion_context();
4472 if (rctx < 0)
4473 return;
4475 perf_sample_data_init(&data, addr);
4477 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, nmi, &data, regs);
4479 perf_swevent_put_recursion_context(rctx);
4480 preempt_enable_notrace();
4483 static void perf_swevent_read(struct perf_event *event)
4487 static int perf_swevent_add(struct perf_event *event, int flags)
4489 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
4490 struct hw_perf_event *hwc = &event->hw;
4491 struct hlist_head *head;
4493 if (hwc->sample_period) {
4494 hwc->last_period = hwc->sample_period;
4495 perf_swevent_set_period(event);
4498 hwc->state = !(flags & PERF_EF_START);
4500 head = find_swevent_head(swhash, event);
4501 if (WARN_ON_ONCE(!head))
4502 return -EINVAL;
4504 hlist_add_head_rcu(&event->hlist_entry, head);
4506 return 0;
4509 static void perf_swevent_del(struct perf_event *event, int flags)
4511 hlist_del_rcu(&event->hlist_entry);
4514 static void perf_swevent_start(struct perf_event *event, int flags)
4516 event->hw.state = 0;
4519 static void perf_swevent_stop(struct perf_event *event, int flags)
4521 event->hw.state = PERF_HES_STOPPED;
4524 /* Deref the hlist from the update side */
4525 static inline struct swevent_hlist *
4526 swevent_hlist_deref(struct swevent_htable *swhash)
4528 return rcu_dereference_protected(swhash->swevent_hlist,
4529 lockdep_is_held(&swhash->hlist_mutex));
4532 static void swevent_hlist_release_rcu(struct rcu_head *rcu_head)
4534 struct swevent_hlist *hlist;
4536 hlist = container_of(rcu_head, struct swevent_hlist, rcu_head);
4537 kfree(hlist);
4540 static void swevent_hlist_release(struct swevent_htable *swhash)
4542 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
4544 if (!hlist)
4545 return;
4547 rcu_assign_pointer(swhash->swevent_hlist, NULL);
4548 call_rcu(&hlist->rcu_head, swevent_hlist_release_rcu);
4551 static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
4553 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
4555 mutex_lock(&swhash->hlist_mutex);
4557 if (!--swhash->hlist_refcount)
4558 swevent_hlist_release(swhash);
4560 mutex_unlock(&swhash->hlist_mutex);
4563 static void swevent_hlist_put(struct perf_event *event)
4565 int cpu;
4567 if (event->cpu != -1) {
4568 swevent_hlist_put_cpu(event, event->cpu);
4569 return;
4572 for_each_possible_cpu(cpu)
4573 swevent_hlist_put_cpu(event, cpu);
4576 static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
4578 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
4579 int err = 0;
4581 mutex_lock(&swhash->hlist_mutex);
4583 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
4584 struct swevent_hlist *hlist;
4586 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
4587 if (!hlist) {
4588 err = -ENOMEM;
4589 goto exit;
4591 rcu_assign_pointer(swhash->swevent_hlist, hlist);
4593 swhash->hlist_refcount++;
4594 exit:
4595 mutex_unlock(&swhash->hlist_mutex);
4597 return err;
4600 static int swevent_hlist_get(struct perf_event *event)
4602 int err;
4603 int cpu, failed_cpu;
4605 if (event->cpu != -1)
4606 return swevent_hlist_get_cpu(event, event->cpu);
4608 get_online_cpus();
4609 for_each_possible_cpu(cpu) {
4610 err = swevent_hlist_get_cpu(event, cpu);
4611 if (err) {
4612 failed_cpu = cpu;
4613 goto fail;
4616 put_online_cpus();
4618 return 0;
4619 fail:
4620 for_each_possible_cpu(cpu) {
4621 if (cpu == failed_cpu)
4622 break;
4623 swevent_hlist_put_cpu(event, cpu);
4626 put_online_cpus();
4627 return err;
4630 atomic_t perf_swevent_enabled[PERF_COUNT_SW_MAX];
4632 static void sw_perf_event_destroy(struct perf_event *event)
4634 u64 event_id = event->attr.config;
4636 WARN_ON(event->parent);
4638 jump_label_dec(&perf_swevent_enabled[event_id]);
4639 swevent_hlist_put(event);
4642 static int perf_swevent_init(struct perf_event *event)
4644 int event_id = event->attr.config;
4646 if (event->attr.type != PERF_TYPE_SOFTWARE)
4647 return -ENOENT;
4649 switch (event_id) {
4650 case PERF_COUNT_SW_CPU_CLOCK:
4651 case PERF_COUNT_SW_TASK_CLOCK:
4652 return -ENOENT;
4654 default:
4655 break;
4658 if (event_id > PERF_COUNT_SW_MAX)
4659 return -ENOENT;
4661 if (!event->parent) {
4662 int err;
4664 err = swevent_hlist_get(event);
4665 if (err)
4666 return err;
4668 jump_label_inc(&perf_swevent_enabled[event_id]);
4669 event->destroy = sw_perf_event_destroy;
4672 return 0;
4675 static struct pmu perf_swevent = {
4676 .task_ctx_nr = perf_sw_context,
4678 .event_init = perf_swevent_init,
4679 .add = perf_swevent_add,
4680 .del = perf_swevent_del,
4681 .start = perf_swevent_start,
4682 .stop = perf_swevent_stop,
4683 .read = perf_swevent_read,
4686 #ifdef CONFIG_EVENT_TRACING
4688 static int perf_tp_filter_match(struct perf_event *event,
4689 struct perf_sample_data *data)
4691 void *record = data->raw->data;
4693 if (likely(!event->filter) || filter_match_preds(event->filter, record))
4694 return 1;
4695 return 0;
4698 static int perf_tp_event_match(struct perf_event *event,
4699 struct perf_sample_data *data,
4700 struct pt_regs *regs)
4703 * All tracepoints are from kernel-space.
4705 if (event->attr.exclude_kernel)
4706 return 0;
4708 if (!perf_tp_filter_match(event, data))
4709 return 0;
4711 return 1;
4714 void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
4715 struct pt_regs *regs, struct hlist_head *head, int rctx)
4717 struct perf_sample_data data;
4718 struct perf_event *event;
4719 struct hlist_node *node;
4721 struct perf_raw_record raw = {
4722 .size = entry_size,
4723 .data = record,
4726 perf_sample_data_init(&data, addr);
4727 data.raw = &raw;
4729 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
4730 if (perf_tp_event_match(event, &data, regs))
4731 perf_swevent_event(event, count, 1, &data, regs);
4734 perf_swevent_put_recursion_context(rctx);
4736 EXPORT_SYMBOL_GPL(perf_tp_event);
4738 static void tp_perf_event_destroy(struct perf_event *event)
4740 perf_trace_destroy(event);
4743 static int perf_tp_event_init(struct perf_event *event)
4745 int err;
4747 if (event->attr.type != PERF_TYPE_TRACEPOINT)
4748 return -ENOENT;
4751 * Raw tracepoint data is a severe data leak, only allow root to
4752 * have these.
4754 if ((event->attr.sample_type & PERF_SAMPLE_RAW) &&
4755 perf_paranoid_tracepoint_raw() &&
4756 !capable(CAP_SYS_ADMIN))
4757 return -EPERM;
4759 err = perf_trace_init(event);
4760 if (err)
4761 return err;
4763 event->destroy = tp_perf_event_destroy;
4765 return 0;
4768 static struct pmu perf_tracepoint = {
4769 .task_ctx_nr = perf_sw_context,
4771 .event_init = perf_tp_event_init,
4772 .add = perf_trace_add,
4773 .del = perf_trace_del,
4774 .start = perf_swevent_start,
4775 .stop = perf_swevent_stop,
4776 .read = perf_swevent_read,
4779 static inline void perf_tp_register(void)
4781 perf_pmu_register(&perf_tracepoint);
4784 static int perf_event_set_filter(struct perf_event *event, void __user *arg)
4786 char *filter_str;
4787 int ret;
4789 if (event->attr.type != PERF_TYPE_TRACEPOINT)
4790 return -EINVAL;
4792 filter_str = strndup_user(arg, PAGE_SIZE);
4793 if (IS_ERR(filter_str))
4794 return PTR_ERR(filter_str);
4796 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
4798 kfree(filter_str);
4799 return ret;
4802 static void perf_event_free_filter(struct perf_event *event)
4804 ftrace_profile_free_filter(event);
4807 #else
4809 static inline void perf_tp_register(void)
4813 static int perf_event_set_filter(struct perf_event *event, void __user *arg)
4815 return -ENOENT;
4818 static void perf_event_free_filter(struct perf_event *event)
4822 #endif /* CONFIG_EVENT_TRACING */
4824 #ifdef CONFIG_HAVE_HW_BREAKPOINT
4825 void perf_bp_event(struct perf_event *bp, void *data)
4827 struct perf_sample_data sample;
4828 struct pt_regs *regs = data;
4830 perf_sample_data_init(&sample, bp->attr.bp_addr);
4832 if (!bp->hw.state && !perf_exclude_event(bp, regs))
4833 perf_swevent_event(bp, 1, 1, &sample, regs);
4835 #endif
4838 * hrtimer based swevent callback
4841 static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
4843 enum hrtimer_restart ret = HRTIMER_RESTART;
4844 struct perf_sample_data data;
4845 struct pt_regs *regs;
4846 struct perf_event *event;
4847 u64 period;
4849 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
4850 event->pmu->read(event);
4852 perf_sample_data_init(&data, 0);
4853 data.period = event->hw.last_period;
4854 regs = get_irq_regs();
4856 if (regs && !perf_exclude_event(event, regs)) {
4857 if (!(event->attr.exclude_idle && current->pid == 0))
4858 if (perf_event_overflow(event, 0, &data, regs))
4859 ret = HRTIMER_NORESTART;
4862 period = max_t(u64, 10000, event->hw.sample_period);
4863 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
4865 return ret;
4868 static void perf_swevent_start_hrtimer(struct perf_event *event)
4870 struct hw_perf_event *hwc = &event->hw;
4872 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
4873 hwc->hrtimer.function = perf_swevent_hrtimer;
4874 if (hwc->sample_period) {
4875 s64 period = local64_read(&hwc->period_left);
4877 if (period) {
4878 if (period < 0)
4879 period = 10000;
4881 local64_set(&hwc->period_left, 0);
4882 } else {
4883 period = max_t(u64, 10000, hwc->sample_period);
4885 __hrtimer_start_range_ns(&hwc->hrtimer,
4886 ns_to_ktime(period), 0,
4887 HRTIMER_MODE_REL_PINNED, 0);
4891 static void perf_swevent_cancel_hrtimer(struct perf_event *event)
4893 struct hw_perf_event *hwc = &event->hw;
4895 if (hwc->sample_period) {
4896 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
4897 local64_set(&hwc->period_left, ktime_to_ns(remaining));
4899 hrtimer_cancel(&hwc->hrtimer);
4904 * Software event: cpu wall time clock
4907 static void cpu_clock_event_update(struct perf_event *event)
4909 s64 prev;
4910 u64 now;
4912 now = local_clock();
4913 prev = local64_xchg(&event->hw.prev_count, now);
4914 local64_add(now - prev, &event->count);
4917 static void cpu_clock_event_start(struct perf_event *event, int flags)
4919 local64_set(&event->hw.prev_count, local_clock());
4920 perf_swevent_start_hrtimer(event);
4923 static void cpu_clock_event_stop(struct perf_event *event, int flags)
4925 perf_swevent_cancel_hrtimer(event);
4926 cpu_clock_event_update(event);
4929 static int cpu_clock_event_add(struct perf_event *event, int flags)
4931 if (flags & PERF_EF_START)
4932 cpu_clock_event_start(event, flags);
4934 return 0;
4937 static void cpu_clock_event_del(struct perf_event *event, int flags)
4939 cpu_clock_event_stop(event, flags);
4942 static void cpu_clock_event_read(struct perf_event *event)
4944 cpu_clock_event_update(event);
4947 static int cpu_clock_event_init(struct perf_event *event)
4949 if (event->attr.type != PERF_TYPE_SOFTWARE)
4950 return -ENOENT;
4952 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
4953 return -ENOENT;
4955 return 0;
4958 static struct pmu perf_cpu_clock = {
4959 .task_ctx_nr = perf_sw_context,
4961 .event_init = cpu_clock_event_init,
4962 .add = cpu_clock_event_add,
4963 .del = cpu_clock_event_del,
4964 .start = cpu_clock_event_start,
4965 .stop = cpu_clock_event_stop,
4966 .read = cpu_clock_event_read,
4970 * Software event: task time clock
4973 static void task_clock_event_update(struct perf_event *event, u64 now)
4975 u64 prev;
4976 s64 delta;
4978 prev = local64_xchg(&event->hw.prev_count, now);
4979 delta = now - prev;
4980 local64_add(delta, &event->count);
4983 static void task_clock_event_start(struct perf_event *event, int flags)
4985 local64_set(&event->hw.prev_count, event->ctx->time);
4986 perf_swevent_start_hrtimer(event);
4989 static void task_clock_event_stop(struct perf_event *event, int flags)
4991 perf_swevent_cancel_hrtimer(event);
4992 task_clock_event_update(event, event->ctx->time);
4995 static int task_clock_event_add(struct perf_event *event, int flags)
4997 if (flags & PERF_EF_START)
4998 task_clock_event_start(event, flags);
5000 return 0;
5003 static void task_clock_event_del(struct perf_event *event, int flags)
5005 task_clock_event_stop(event, PERF_EF_UPDATE);
5008 static void task_clock_event_read(struct perf_event *event)
5010 u64 time;
5012 if (!in_nmi()) {
5013 update_context_time(event->ctx);
5014 time = event->ctx->time;
5015 } else {
5016 u64 now = perf_clock();
5017 u64 delta = now - event->ctx->timestamp;
5018 time = event->ctx->time + delta;
5021 task_clock_event_update(event, time);
5024 static int task_clock_event_init(struct perf_event *event)
5026 if (event->attr.type != PERF_TYPE_SOFTWARE)
5027 return -ENOENT;
5029 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
5030 return -ENOENT;
5032 return 0;
5035 static struct pmu perf_task_clock = {
5036 .task_ctx_nr = perf_sw_context,
5038 .event_init = task_clock_event_init,
5039 .add = task_clock_event_add,
5040 .del = task_clock_event_del,
5041 .start = task_clock_event_start,
5042 .stop = task_clock_event_stop,
5043 .read = task_clock_event_read,
5046 static void perf_pmu_nop_void(struct pmu *pmu)
5050 static int perf_pmu_nop_int(struct pmu *pmu)
5052 return 0;
5055 static void perf_pmu_start_txn(struct pmu *pmu)
5057 perf_pmu_disable(pmu);
5060 static int perf_pmu_commit_txn(struct pmu *pmu)
5062 perf_pmu_enable(pmu);
5063 return 0;
5066 static void perf_pmu_cancel_txn(struct pmu *pmu)
5068 perf_pmu_enable(pmu);
5072 * Ensures all contexts with the same task_ctx_nr have the same
5073 * pmu_cpu_context too.
5075 static void *find_pmu_context(int ctxn)
5077 struct pmu *pmu;
5079 if (ctxn < 0)
5080 return NULL;
5082 list_for_each_entry(pmu, &pmus, entry) {
5083 if (pmu->task_ctx_nr == ctxn)
5084 return pmu->pmu_cpu_context;
5087 return NULL;
5090 static void free_pmu_context(void * __percpu cpu_context)
5092 struct pmu *pmu;
5094 mutex_lock(&pmus_lock);
5096 * Like a real lame refcount.
5098 list_for_each_entry(pmu, &pmus, entry) {
5099 if (pmu->pmu_cpu_context == cpu_context)
5100 goto out;
5103 free_percpu(cpu_context);
5104 out:
5105 mutex_unlock(&pmus_lock);
5108 int perf_pmu_register(struct pmu *pmu)
5110 int cpu, ret;
5112 mutex_lock(&pmus_lock);
5113 ret = -ENOMEM;
5114 pmu->pmu_disable_count = alloc_percpu(int);
5115 if (!pmu->pmu_disable_count)
5116 goto unlock;
5118 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
5119 if (pmu->pmu_cpu_context)
5120 goto got_cpu_context;
5122 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
5123 if (!pmu->pmu_cpu_context)
5124 goto free_pdc;
5126 for_each_possible_cpu(cpu) {
5127 struct perf_cpu_context *cpuctx;
5129 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
5130 __perf_event_init_context(&cpuctx->ctx);
5131 cpuctx->ctx.type = cpu_context;
5132 cpuctx->ctx.pmu = pmu;
5133 cpuctx->jiffies_interval = 1;
5134 INIT_LIST_HEAD(&cpuctx->rotation_list);
5137 got_cpu_context:
5138 if (!pmu->start_txn) {
5139 if (pmu->pmu_enable) {
5141 * If we have pmu_enable/pmu_disable calls, install
5142 * transaction stubs that use that to try and batch
5143 * hardware accesses.
5145 pmu->start_txn = perf_pmu_start_txn;
5146 pmu->commit_txn = perf_pmu_commit_txn;
5147 pmu->cancel_txn = perf_pmu_cancel_txn;
5148 } else {
5149 pmu->start_txn = perf_pmu_nop_void;
5150 pmu->commit_txn = perf_pmu_nop_int;
5151 pmu->cancel_txn = perf_pmu_nop_void;
5155 if (!pmu->pmu_enable) {
5156 pmu->pmu_enable = perf_pmu_nop_void;
5157 pmu->pmu_disable = perf_pmu_nop_void;
5160 list_add_rcu(&pmu->entry, &pmus);
5161 ret = 0;
5162 unlock:
5163 mutex_unlock(&pmus_lock);
5165 return ret;
5167 free_pdc:
5168 free_percpu(pmu->pmu_disable_count);
5169 goto unlock;
5172 void perf_pmu_unregister(struct pmu *pmu)
5174 mutex_lock(&pmus_lock);
5175 list_del_rcu(&pmu->entry);
5176 mutex_unlock(&pmus_lock);
5179 * We dereference the pmu list under both SRCU and regular RCU, so
5180 * synchronize against both of those.
5182 synchronize_srcu(&pmus_srcu);
5183 synchronize_rcu();
5185 free_percpu(pmu->pmu_disable_count);
5186 free_pmu_context(pmu->pmu_cpu_context);
5189 struct pmu *perf_init_event(struct perf_event *event)
5191 struct pmu *pmu = NULL;
5192 int idx;
5194 idx = srcu_read_lock(&pmus_srcu);
5195 list_for_each_entry_rcu(pmu, &pmus, entry) {
5196 int ret = pmu->event_init(event);
5197 if (!ret)
5198 goto unlock;
5200 if (ret != -ENOENT) {
5201 pmu = ERR_PTR(ret);
5202 goto unlock;
5205 pmu = ERR_PTR(-ENOENT);
5206 unlock:
5207 srcu_read_unlock(&pmus_srcu, idx);
5209 return pmu;
5213 * Allocate and initialize a event structure
5215 static struct perf_event *
5216 perf_event_alloc(struct perf_event_attr *attr, int cpu,
5217 struct task_struct *task,
5218 struct perf_event *group_leader,
5219 struct perf_event *parent_event,
5220 perf_overflow_handler_t overflow_handler)
5222 struct pmu *pmu;
5223 struct perf_event *event;
5224 struct hw_perf_event *hwc;
5225 long err;
5227 event = kzalloc(sizeof(*event), GFP_KERNEL);
5228 if (!event)
5229 return ERR_PTR(-ENOMEM);
5232 * Single events are their own group leaders, with an
5233 * empty sibling list:
5235 if (!group_leader)
5236 group_leader = event;
5238 mutex_init(&event->child_mutex);
5239 INIT_LIST_HEAD(&event->child_list);
5241 INIT_LIST_HEAD(&event->group_entry);
5242 INIT_LIST_HEAD(&event->event_entry);
5243 INIT_LIST_HEAD(&event->sibling_list);
5244 init_waitqueue_head(&event->waitq);
5245 init_irq_work(&event->pending, perf_pending_event);
5247 mutex_init(&event->mmap_mutex);
5249 event->cpu = cpu;
5250 event->attr = *attr;
5251 event->group_leader = group_leader;
5252 event->pmu = NULL;
5253 event->oncpu = -1;
5255 event->parent = parent_event;
5257 event->ns = get_pid_ns(current->nsproxy->pid_ns);
5258 event->id = atomic64_inc_return(&perf_event_id);
5260 event->state = PERF_EVENT_STATE_INACTIVE;
5262 if (task) {
5263 event->attach_state = PERF_ATTACH_TASK;
5264 #ifdef CONFIG_HAVE_HW_BREAKPOINT
5266 * hw_breakpoint is a bit difficult here..
5268 if (attr->type == PERF_TYPE_BREAKPOINT)
5269 event->hw.bp_target = task;
5270 #endif
5273 if (!overflow_handler && parent_event)
5274 overflow_handler = parent_event->overflow_handler;
5276 event->overflow_handler = overflow_handler;
5278 if (attr->disabled)
5279 event->state = PERF_EVENT_STATE_OFF;
5281 pmu = NULL;
5283 hwc = &event->hw;
5284 hwc->sample_period = attr->sample_period;
5285 if (attr->freq && attr->sample_freq)
5286 hwc->sample_period = 1;
5287 hwc->last_period = hwc->sample_period;
5289 local64_set(&hwc->period_left, hwc->sample_period);
5292 * we currently do not support PERF_FORMAT_GROUP on inherited events
5294 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
5295 goto done;
5297 pmu = perf_init_event(event);
5299 done:
5300 err = 0;
5301 if (!pmu)
5302 err = -EINVAL;
5303 else if (IS_ERR(pmu))
5304 err = PTR_ERR(pmu);
5306 if (err) {
5307 if (event->ns)
5308 put_pid_ns(event->ns);
5309 kfree(event);
5310 return ERR_PTR(err);
5313 event->pmu = pmu;
5315 if (!event->parent) {
5316 if (event->attach_state & PERF_ATTACH_TASK)
5317 jump_label_inc(&perf_task_events);
5318 if (event->attr.mmap || event->attr.mmap_data)
5319 atomic_inc(&nr_mmap_events);
5320 if (event->attr.comm)
5321 atomic_inc(&nr_comm_events);
5322 if (event->attr.task)
5323 atomic_inc(&nr_task_events);
5324 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
5325 err = get_callchain_buffers();
5326 if (err) {
5327 free_event(event);
5328 return ERR_PTR(err);
5333 return event;
5336 static int perf_copy_attr(struct perf_event_attr __user *uattr,
5337 struct perf_event_attr *attr)
5339 u32 size;
5340 int ret;
5342 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
5343 return -EFAULT;
5346 * zero the full structure, so that a short copy will be nice.
5348 memset(attr, 0, sizeof(*attr));
5350 ret = get_user(size, &uattr->size);
5351 if (ret)
5352 return ret;
5354 if (size > PAGE_SIZE) /* silly large */
5355 goto err_size;
5357 if (!size) /* abi compat */
5358 size = PERF_ATTR_SIZE_VER0;
5360 if (size < PERF_ATTR_SIZE_VER0)
5361 goto err_size;
5364 * If we're handed a bigger struct than we know of,
5365 * ensure all the unknown bits are 0 - i.e. new
5366 * user-space does not rely on any kernel feature
5367 * extensions we dont know about yet.
5369 if (size > sizeof(*attr)) {
5370 unsigned char __user *addr;
5371 unsigned char __user *end;
5372 unsigned char val;
5374 addr = (void __user *)uattr + sizeof(*attr);
5375 end = (void __user *)uattr + size;
5377 for (; addr < end; addr++) {
5378 ret = get_user(val, addr);
5379 if (ret)
5380 return ret;
5381 if (val)
5382 goto err_size;
5384 size = sizeof(*attr);
5387 ret = copy_from_user(attr, uattr, size);
5388 if (ret)
5389 return -EFAULT;
5392 * If the type exists, the corresponding creation will verify
5393 * the attr->config.
5395 if (attr->type >= PERF_TYPE_MAX)
5396 return -EINVAL;
5398 if (attr->__reserved_1)
5399 return -EINVAL;
5401 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
5402 return -EINVAL;
5404 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
5405 return -EINVAL;
5407 out:
5408 return ret;
5410 err_size:
5411 put_user(sizeof(*attr), &uattr->size);
5412 ret = -E2BIG;
5413 goto out;
5416 static int
5417 perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
5419 struct perf_buffer *buffer = NULL, *old_buffer = NULL;
5420 int ret = -EINVAL;
5422 if (!output_event)
5423 goto set;
5425 /* don't allow circular references */
5426 if (event == output_event)
5427 goto out;
5430 * Don't allow cross-cpu buffers
5432 if (output_event->cpu != event->cpu)
5433 goto out;
5436 * If its not a per-cpu buffer, it must be the same task.
5438 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
5439 goto out;
5441 set:
5442 mutex_lock(&event->mmap_mutex);
5443 /* Can't redirect output if we've got an active mmap() */
5444 if (atomic_read(&event->mmap_count))
5445 goto unlock;
5447 if (output_event) {
5448 /* get the buffer we want to redirect to */
5449 buffer = perf_buffer_get(output_event);
5450 if (!buffer)
5451 goto unlock;
5454 old_buffer = event->buffer;
5455 rcu_assign_pointer(event->buffer, buffer);
5456 ret = 0;
5457 unlock:
5458 mutex_unlock(&event->mmap_mutex);
5460 if (old_buffer)
5461 perf_buffer_put(old_buffer);
5462 out:
5463 return ret;
5467 * sys_perf_event_open - open a performance event, associate it to a task/cpu
5469 * @attr_uptr: event_id type attributes for monitoring/sampling
5470 * @pid: target pid
5471 * @cpu: target cpu
5472 * @group_fd: group leader event fd
5474 SYSCALL_DEFINE5(perf_event_open,
5475 struct perf_event_attr __user *, attr_uptr,
5476 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
5478 struct perf_event *group_leader = NULL, *output_event = NULL;
5479 struct perf_event *event, *sibling;
5480 struct perf_event_attr attr;
5481 struct perf_event_context *ctx;
5482 struct file *event_file = NULL;
5483 struct file *group_file = NULL;
5484 struct task_struct *task = NULL;
5485 struct pmu *pmu;
5486 int event_fd;
5487 int move_group = 0;
5488 int fput_needed = 0;
5489 int err;
5491 /* for future expandability... */
5492 if (flags & ~(PERF_FLAG_FD_NO_GROUP | PERF_FLAG_FD_OUTPUT))
5493 return -EINVAL;
5495 err = perf_copy_attr(attr_uptr, &attr);
5496 if (err)
5497 return err;
5499 if (!attr.exclude_kernel) {
5500 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
5501 return -EACCES;
5504 if (attr.freq) {
5505 if (attr.sample_freq > sysctl_perf_event_sample_rate)
5506 return -EINVAL;
5509 event_fd = get_unused_fd_flags(O_RDWR);
5510 if (event_fd < 0)
5511 return event_fd;
5513 if (group_fd != -1) {
5514 group_leader = perf_fget_light(group_fd, &fput_needed);
5515 if (IS_ERR(group_leader)) {
5516 err = PTR_ERR(group_leader);
5517 goto err_fd;
5519 group_file = group_leader->filp;
5520 if (flags & PERF_FLAG_FD_OUTPUT)
5521 output_event = group_leader;
5522 if (flags & PERF_FLAG_FD_NO_GROUP)
5523 group_leader = NULL;
5526 if (pid != -1) {
5527 task = find_lively_task_by_vpid(pid);
5528 if (IS_ERR(task)) {
5529 err = PTR_ERR(task);
5530 goto err_group_fd;
5534 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL, NULL);
5535 if (IS_ERR(event)) {
5536 err = PTR_ERR(event);
5537 goto err_task;
5541 * Special case software events and allow them to be part of
5542 * any hardware group.
5544 pmu = event->pmu;
5546 if (group_leader &&
5547 (is_software_event(event) != is_software_event(group_leader))) {
5548 if (is_software_event(event)) {
5550 * If event and group_leader are not both a software
5551 * event, and event is, then group leader is not.
5553 * Allow the addition of software events to !software
5554 * groups, this is safe because software events never
5555 * fail to schedule.
5557 pmu = group_leader->pmu;
5558 } else if (is_software_event(group_leader) &&
5559 (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
5561 * In case the group is a pure software group, and we
5562 * try to add a hardware event, move the whole group to
5563 * the hardware context.
5565 move_group = 1;
5570 * Get the target context (task or percpu):
5572 ctx = find_get_context(pmu, task, cpu);
5573 if (IS_ERR(ctx)) {
5574 err = PTR_ERR(ctx);
5575 goto err_alloc;
5579 * Look up the group leader (we will attach this event to it):
5581 if (group_leader) {
5582 err = -EINVAL;
5585 * Do not allow a recursive hierarchy (this new sibling
5586 * becoming part of another group-sibling):
5588 if (group_leader->group_leader != group_leader)
5589 goto err_context;
5591 * Do not allow to attach to a group in a different
5592 * task or CPU context:
5594 if (move_group) {
5595 if (group_leader->ctx->type != ctx->type)
5596 goto err_context;
5597 } else {
5598 if (group_leader->ctx != ctx)
5599 goto err_context;
5603 * Only a group leader can be exclusive or pinned
5605 if (attr.exclusive || attr.pinned)
5606 goto err_context;
5609 if (output_event) {
5610 err = perf_event_set_output(event, output_event);
5611 if (err)
5612 goto err_context;
5615 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, O_RDWR);
5616 if (IS_ERR(event_file)) {
5617 err = PTR_ERR(event_file);
5618 goto err_context;
5621 if (move_group) {
5622 struct perf_event_context *gctx = group_leader->ctx;
5624 mutex_lock(&gctx->mutex);
5625 perf_event_remove_from_context(group_leader);
5626 list_for_each_entry(sibling, &group_leader->sibling_list,
5627 group_entry) {
5628 perf_event_remove_from_context(sibling);
5629 put_ctx(gctx);
5631 mutex_unlock(&gctx->mutex);
5632 put_ctx(gctx);
5635 event->filp = event_file;
5636 WARN_ON_ONCE(ctx->parent_ctx);
5637 mutex_lock(&ctx->mutex);
5639 if (move_group) {
5640 perf_install_in_context(ctx, group_leader, cpu);
5641 get_ctx(ctx);
5642 list_for_each_entry(sibling, &group_leader->sibling_list,
5643 group_entry) {
5644 perf_install_in_context(ctx, sibling, cpu);
5645 get_ctx(ctx);
5649 perf_install_in_context(ctx, event, cpu);
5650 ++ctx->generation;
5651 mutex_unlock(&ctx->mutex);
5653 event->owner = current;
5654 get_task_struct(current);
5655 mutex_lock(&current->perf_event_mutex);
5656 list_add_tail(&event->owner_entry, &current->perf_event_list);
5657 mutex_unlock(&current->perf_event_mutex);
5660 * Drop the reference on the group_event after placing the
5661 * new event on the sibling_list. This ensures destruction
5662 * of the group leader will find the pointer to itself in
5663 * perf_group_detach().
5665 fput_light(group_file, fput_needed);
5666 fd_install(event_fd, event_file);
5667 return event_fd;
5669 err_context:
5670 put_ctx(ctx);
5671 err_alloc:
5672 free_event(event);
5673 err_task:
5674 if (task)
5675 put_task_struct(task);
5676 err_group_fd:
5677 fput_light(group_file, fput_needed);
5678 err_fd:
5679 put_unused_fd(event_fd);
5680 return err;
5684 * perf_event_create_kernel_counter
5686 * @attr: attributes of the counter to create
5687 * @cpu: cpu in which the counter is bound
5688 * @task: task to profile (NULL for percpu)
5690 struct perf_event *
5691 perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
5692 struct task_struct *task,
5693 perf_overflow_handler_t overflow_handler)
5695 struct perf_event_context *ctx;
5696 struct perf_event *event;
5697 int err;
5700 * Get the target context (task or percpu):
5703 event = perf_event_alloc(attr, cpu, task, NULL, NULL, overflow_handler);
5704 if (IS_ERR(event)) {
5705 err = PTR_ERR(event);
5706 goto err;
5709 ctx = find_get_context(event->pmu, task, cpu);
5710 if (IS_ERR(ctx)) {
5711 err = PTR_ERR(ctx);
5712 goto err_free;
5715 event->filp = NULL;
5716 WARN_ON_ONCE(ctx->parent_ctx);
5717 mutex_lock(&ctx->mutex);
5718 perf_install_in_context(ctx, event, cpu);
5719 ++ctx->generation;
5720 mutex_unlock(&ctx->mutex);
5722 event->owner = current;
5723 get_task_struct(current);
5724 mutex_lock(&current->perf_event_mutex);
5725 list_add_tail(&event->owner_entry, &current->perf_event_list);
5726 mutex_unlock(&current->perf_event_mutex);
5728 return event;
5730 err_free:
5731 free_event(event);
5732 err:
5733 return ERR_PTR(err);
5735 EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
5737 static void sync_child_event(struct perf_event *child_event,
5738 struct task_struct *child)
5740 struct perf_event *parent_event = child_event->parent;
5741 u64 child_val;
5743 if (child_event->attr.inherit_stat)
5744 perf_event_read_event(child_event, child);
5746 child_val = perf_event_count(child_event);
5749 * Add back the child's count to the parent's count:
5751 atomic64_add(child_val, &parent_event->child_count);
5752 atomic64_add(child_event->total_time_enabled,
5753 &parent_event->child_total_time_enabled);
5754 atomic64_add(child_event->total_time_running,
5755 &parent_event->child_total_time_running);
5758 * Remove this event from the parent's list
5760 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
5761 mutex_lock(&parent_event->child_mutex);
5762 list_del_init(&child_event->child_list);
5763 mutex_unlock(&parent_event->child_mutex);
5766 * Release the parent event, if this was the last
5767 * reference to it.
5769 fput(parent_event->filp);
5772 static void
5773 __perf_event_exit_task(struct perf_event *child_event,
5774 struct perf_event_context *child_ctx,
5775 struct task_struct *child)
5777 struct perf_event *parent_event;
5779 perf_event_remove_from_context(child_event);
5781 parent_event = child_event->parent;
5783 * It can happen that parent exits first, and has events
5784 * that are still around due to the child reference. These
5785 * events need to be zapped - but otherwise linger.
5787 if (parent_event) {
5788 sync_child_event(child_event, child);
5789 free_event(child_event);
5793 static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
5795 struct perf_event *child_event, *tmp;
5796 struct perf_event_context *child_ctx;
5797 unsigned long flags;
5799 if (likely(!child->perf_event_ctxp[ctxn])) {
5800 perf_event_task(child, NULL, 0);
5801 return;
5804 local_irq_save(flags);
5806 * We can't reschedule here because interrupts are disabled,
5807 * and either child is current or it is a task that can't be
5808 * scheduled, so we are now safe from rescheduling changing
5809 * our context.
5811 child_ctx = child->perf_event_ctxp[ctxn];
5812 task_ctx_sched_out(child_ctx, EVENT_ALL);
5815 * Take the context lock here so that if find_get_context is
5816 * reading child->perf_event_ctxp, we wait until it has
5817 * incremented the context's refcount before we do put_ctx below.
5819 raw_spin_lock(&child_ctx->lock);
5820 child->perf_event_ctxp[ctxn] = NULL;
5822 * If this context is a clone; unclone it so it can't get
5823 * swapped to another process while we're removing all
5824 * the events from it.
5826 unclone_ctx(child_ctx);
5827 update_context_time(child_ctx);
5828 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
5831 * Report the task dead after unscheduling the events so that we
5832 * won't get any samples after PERF_RECORD_EXIT. We can however still
5833 * get a few PERF_RECORD_READ events.
5835 perf_event_task(child, child_ctx, 0);
5838 * We can recurse on the same lock type through:
5840 * __perf_event_exit_task()
5841 * sync_child_event()
5842 * fput(parent_event->filp)
5843 * perf_release()
5844 * mutex_lock(&ctx->mutex)
5846 * But since its the parent context it won't be the same instance.
5848 mutex_lock(&child_ctx->mutex);
5850 again:
5851 list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
5852 group_entry)
5853 __perf_event_exit_task(child_event, child_ctx, child);
5855 list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
5856 group_entry)
5857 __perf_event_exit_task(child_event, child_ctx, child);
5860 * If the last event was a group event, it will have appended all
5861 * its siblings to the list, but we obtained 'tmp' before that which
5862 * will still point to the list head terminating the iteration.
5864 if (!list_empty(&child_ctx->pinned_groups) ||
5865 !list_empty(&child_ctx->flexible_groups))
5866 goto again;
5868 mutex_unlock(&child_ctx->mutex);
5870 put_ctx(child_ctx);
5874 * When a child task exits, feed back event values to parent events.
5876 void perf_event_exit_task(struct task_struct *child)
5878 int ctxn;
5880 for_each_task_context_nr(ctxn)
5881 perf_event_exit_task_context(child, ctxn);
5884 static void perf_free_event(struct perf_event *event,
5885 struct perf_event_context *ctx)
5887 struct perf_event *parent = event->parent;
5889 if (WARN_ON_ONCE(!parent))
5890 return;
5892 mutex_lock(&parent->child_mutex);
5893 list_del_init(&event->child_list);
5894 mutex_unlock(&parent->child_mutex);
5896 fput(parent->filp);
5898 perf_group_detach(event);
5899 list_del_event(event, ctx);
5900 free_event(event);
5904 * free an unexposed, unused context as created by inheritance by
5905 * perf_event_init_task below, used by fork() in case of fail.
5907 void perf_event_free_task(struct task_struct *task)
5909 struct perf_event_context *ctx;
5910 struct perf_event *event, *tmp;
5911 int ctxn;
5913 for_each_task_context_nr(ctxn) {
5914 ctx = task->perf_event_ctxp[ctxn];
5915 if (!ctx)
5916 continue;
5918 mutex_lock(&ctx->mutex);
5919 again:
5920 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
5921 group_entry)
5922 perf_free_event(event, ctx);
5924 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
5925 group_entry)
5926 perf_free_event(event, ctx);
5928 if (!list_empty(&ctx->pinned_groups) ||
5929 !list_empty(&ctx->flexible_groups))
5930 goto again;
5932 mutex_unlock(&ctx->mutex);
5934 put_ctx(ctx);
5938 void perf_event_delayed_put(struct task_struct *task)
5940 int ctxn;
5942 for_each_task_context_nr(ctxn)
5943 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
5947 * inherit a event from parent task to child task:
5949 static struct perf_event *
5950 inherit_event(struct perf_event *parent_event,
5951 struct task_struct *parent,
5952 struct perf_event_context *parent_ctx,
5953 struct task_struct *child,
5954 struct perf_event *group_leader,
5955 struct perf_event_context *child_ctx)
5957 struct perf_event *child_event;
5958 unsigned long flags;
5961 * Instead of creating recursive hierarchies of events,
5962 * we link inherited events back to the original parent,
5963 * which has a filp for sure, which we use as the reference
5964 * count:
5966 if (parent_event->parent)
5967 parent_event = parent_event->parent;
5969 child_event = perf_event_alloc(&parent_event->attr,
5970 parent_event->cpu,
5971 child,
5972 group_leader, parent_event,
5973 NULL);
5974 if (IS_ERR(child_event))
5975 return child_event;
5976 get_ctx(child_ctx);
5979 * Make the child state follow the state of the parent event,
5980 * not its attr.disabled bit. We hold the parent's mutex,
5981 * so we won't race with perf_event_{en, dis}able_family.
5983 if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
5984 child_event->state = PERF_EVENT_STATE_INACTIVE;
5985 else
5986 child_event->state = PERF_EVENT_STATE_OFF;
5988 if (parent_event->attr.freq) {
5989 u64 sample_period = parent_event->hw.sample_period;
5990 struct hw_perf_event *hwc = &child_event->hw;
5992 hwc->sample_period = sample_period;
5993 hwc->last_period = sample_period;
5995 local64_set(&hwc->period_left, sample_period);
5998 child_event->ctx = child_ctx;
5999 child_event->overflow_handler = parent_event->overflow_handler;
6002 * Link it up in the child's context:
6004 raw_spin_lock_irqsave(&child_ctx->lock, flags);
6005 add_event_to_ctx(child_event, child_ctx);
6006 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
6009 * Get a reference to the parent filp - we will fput it
6010 * when the child event exits. This is safe to do because
6011 * we are in the parent and we know that the filp still
6012 * exists and has a nonzero count:
6014 atomic_long_inc(&parent_event->filp->f_count);
6017 * Link this into the parent event's child list
6019 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
6020 mutex_lock(&parent_event->child_mutex);
6021 list_add_tail(&child_event->child_list, &parent_event->child_list);
6022 mutex_unlock(&parent_event->child_mutex);
6024 return child_event;
6027 static int inherit_group(struct perf_event *parent_event,
6028 struct task_struct *parent,
6029 struct perf_event_context *parent_ctx,
6030 struct task_struct *child,
6031 struct perf_event_context *child_ctx)
6033 struct perf_event *leader;
6034 struct perf_event *sub;
6035 struct perf_event *child_ctr;
6037 leader = inherit_event(parent_event, parent, parent_ctx,
6038 child, NULL, child_ctx);
6039 if (IS_ERR(leader))
6040 return PTR_ERR(leader);
6041 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
6042 child_ctr = inherit_event(sub, parent, parent_ctx,
6043 child, leader, child_ctx);
6044 if (IS_ERR(child_ctr))
6045 return PTR_ERR(child_ctr);
6047 return 0;
6050 static int
6051 inherit_task_group(struct perf_event *event, struct task_struct *parent,
6052 struct perf_event_context *parent_ctx,
6053 struct task_struct *child, int ctxn,
6054 int *inherited_all)
6056 int ret;
6057 struct perf_event_context *child_ctx;
6059 if (!event->attr.inherit) {
6060 *inherited_all = 0;
6061 return 0;
6064 child_ctx = child->perf_event_ctxp[ctxn];
6065 if (!child_ctx) {
6067 * This is executed from the parent task context, so
6068 * inherit events that have been marked for cloning.
6069 * First allocate and initialize a context for the
6070 * child.
6073 child_ctx = alloc_perf_context(event->pmu, child);
6074 if (!child_ctx)
6075 return -ENOMEM;
6077 child->perf_event_ctxp[ctxn] = child_ctx;
6080 ret = inherit_group(event, parent, parent_ctx,
6081 child, child_ctx);
6083 if (ret)
6084 *inherited_all = 0;
6086 return ret;
6090 * Initialize the perf_event context in task_struct
6092 int perf_event_init_context(struct task_struct *child, int ctxn)
6094 struct perf_event_context *child_ctx, *parent_ctx;
6095 struct perf_event_context *cloned_ctx;
6096 struct perf_event *event;
6097 struct task_struct *parent = current;
6098 int inherited_all = 1;
6099 int ret = 0;
6101 child->perf_event_ctxp[ctxn] = NULL;
6103 mutex_init(&child->perf_event_mutex);
6104 INIT_LIST_HEAD(&child->perf_event_list);
6106 if (likely(!parent->perf_event_ctxp[ctxn]))
6107 return 0;
6110 * If the parent's context is a clone, pin it so it won't get
6111 * swapped under us.
6113 parent_ctx = perf_pin_task_context(parent, ctxn);
6116 * No need to check if parent_ctx != NULL here; since we saw
6117 * it non-NULL earlier, the only reason for it to become NULL
6118 * is if we exit, and since we're currently in the middle of
6119 * a fork we can't be exiting at the same time.
6123 * Lock the parent list. No need to lock the child - not PID
6124 * hashed yet and not running, so nobody can access it.
6126 mutex_lock(&parent_ctx->mutex);
6129 * We dont have to disable NMIs - we are only looking at
6130 * the list, not manipulating it:
6132 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
6133 ret = inherit_task_group(event, parent, parent_ctx,
6134 child, ctxn, &inherited_all);
6135 if (ret)
6136 break;
6139 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
6140 ret = inherit_task_group(event, parent, parent_ctx,
6141 child, ctxn, &inherited_all);
6142 if (ret)
6143 break;
6146 child_ctx = child->perf_event_ctxp[ctxn];
6148 if (child_ctx && inherited_all) {
6150 * Mark the child context as a clone of the parent
6151 * context, or of whatever the parent is a clone of.
6152 * Note that if the parent is a clone, it could get
6153 * uncloned at any point, but that doesn't matter
6154 * because the list of events and the generation
6155 * count can't have changed since we took the mutex.
6157 cloned_ctx = rcu_dereference(parent_ctx->parent_ctx);
6158 if (cloned_ctx) {
6159 child_ctx->parent_ctx = cloned_ctx;
6160 child_ctx->parent_gen = parent_ctx->parent_gen;
6161 } else {
6162 child_ctx->parent_ctx = parent_ctx;
6163 child_ctx->parent_gen = parent_ctx->generation;
6165 get_ctx(child_ctx->parent_ctx);
6168 mutex_unlock(&parent_ctx->mutex);
6170 perf_unpin_context(parent_ctx);
6172 return ret;
6176 * Initialize the perf_event context in task_struct
6178 int perf_event_init_task(struct task_struct *child)
6180 int ctxn, ret;
6182 for_each_task_context_nr(ctxn) {
6183 ret = perf_event_init_context(child, ctxn);
6184 if (ret)
6185 return ret;
6188 return 0;
6191 static void __init perf_event_init_all_cpus(void)
6193 struct swevent_htable *swhash;
6194 int cpu;
6196 for_each_possible_cpu(cpu) {
6197 swhash = &per_cpu(swevent_htable, cpu);
6198 mutex_init(&swhash->hlist_mutex);
6199 INIT_LIST_HEAD(&per_cpu(rotation_list, cpu));
6203 static void __cpuinit perf_event_init_cpu(int cpu)
6205 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
6207 mutex_lock(&swhash->hlist_mutex);
6208 if (swhash->hlist_refcount > 0) {
6209 struct swevent_hlist *hlist;
6211 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
6212 WARN_ON(!hlist);
6213 rcu_assign_pointer(swhash->swevent_hlist, hlist);
6215 mutex_unlock(&swhash->hlist_mutex);
6218 #ifdef CONFIG_HOTPLUG_CPU
6219 static void perf_pmu_rotate_stop(struct pmu *pmu)
6221 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
6223 WARN_ON(!irqs_disabled());
6225 list_del_init(&cpuctx->rotation_list);
6228 static void __perf_event_exit_context(void *__info)
6230 struct perf_event_context *ctx = __info;
6231 struct perf_event *event, *tmp;
6233 perf_pmu_rotate_stop(ctx->pmu);
6235 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
6236 __perf_event_remove_from_context(event);
6237 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, group_entry)
6238 __perf_event_remove_from_context(event);
6241 static void perf_event_exit_cpu_context(int cpu)
6243 struct perf_event_context *ctx;
6244 struct pmu *pmu;
6245 int idx;
6247 idx = srcu_read_lock(&pmus_srcu);
6248 list_for_each_entry_rcu(pmu, &pmus, entry) {
6249 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
6251 mutex_lock(&ctx->mutex);
6252 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
6253 mutex_unlock(&ctx->mutex);
6255 srcu_read_unlock(&pmus_srcu, idx);
6258 static void perf_event_exit_cpu(int cpu)
6260 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
6262 mutex_lock(&swhash->hlist_mutex);
6263 swevent_hlist_release(swhash);
6264 mutex_unlock(&swhash->hlist_mutex);
6266 perf_event_exit_cpu_context(cpu);
6268 #else
6269 static inline void perf_event_exit_cpu(int cpu) { }
6270 #endif
6272 static int __cpuinit
6273 perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
6275 unsigned int cpu = (long)hcpu;
6277 switch (action & ~CPU_TASKS_FROZEN) {
6279 case CPU_UP_PREPARE:
6280 case CPU_DOWN_FAILED:
6281 perf_event_init_cpu(cpu);
6282 break;
6284 case CPU_UP_CANCELED:
6285 case CPU_DOWN_PREPARE:
6286 perf_event_exit_cpu(cpu);
6287 break;
6289 default:
6290 break;
6293 return NOTIFY_OK;
6296 void __init perf_event_init(void)
6298 perf_event_init_all_cpus();
6299 init_srcu_struct(&pmus_srcu);
6300 perf_pmu_register(&perf_swevent);
6301 perf_pmu_register(&perf_cpu_clock);
6302 perf_pmu_register(&perf_task_clock);
6303 perf_tp_register();
6304 perf_cpu_notifier(perf_cpu_notify);