ccwgroup: move attributes to attribute group
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / kernel / perf_event.c
blob37ebc14c9ce4861e5bffcbb80013c75633bf2e4e
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/sysfs.h>
19 #include <linux/dcache.h>
20 #include <linux/percpu.h>
21 #include <linux/ptrace.h>
22 #include <linux/vmstat.h>
23 #include <linux/vmalloc.h>
24 #include <linux/hardirq.h>
25 #include <linux/rculist.h>
26 #include <linux/uaccess.h>
27 #include <linux/syscalls.h>
28 #include <linux/anon_inodes.h>
29 #include <linux/kernel_stat.h>
30 #include <linux/perf_event.h>
32 #include <asm/irq_regs.h>
35 * Each CPU has a list of per CPU events:
37 DEFINE_PER_CPU(struct perf_cpu_context, perf_cpu_context);
39 int perf_max_events __read_mostly = 1;
40 static int perf_reserved_percpu __read_mostly;
41 static int perf_overcommit __read_mostly = 1;
43 static atomic_t nr_events __read_mostly;
44 static atomic_t nr_mmap_events __read_mostly;
45 static atomic_t nr_comm_events __read_mostly;
46 static atomic_t nr_task_events __read_mostly;
49 * perf event paranoia level:
50 * -1 - not paranoid at all
51 * 0 - disallow raw tracepoint access for unpriv
52 * 1 - disallow cpu events for unpriv
53 * 2 - disallow kernel profiling for unpriv
55 int sysctl_perf_event_paranoid __read_mostly = 1;
57 static inline bool perf_paranoid_tracepoint_raw(void)
59 return sysctl_perf_event_paranoid > -1;
62 static inline bool perf_paranoid_cpu(void)
64 return sysctl_perf_event_paranoid > 0;
67 static inline bool perf_paranoid_kernel(void)
69 return sysctl_perf_event_paranoid > 1;
72 /* Minimum for 128 pages + 1 for the user control page */
73 int sysctl_perf_event_mlock __read_mostly = 516; /* 'free' kb per user */
76 * max perf event sample rate
78 int sysctl_perf_event_sample_rate __read_mostly = 100000;
80 static atomic64_t perf_event_id;
83 * Lock for (sysadmin-configurable) event reservations:
85 static DEFINE_SPINLOCK(perf_resource_lock);
88 * Architecture provided APIs - weak aliases:
90 extern __weak const struct pmu *hw_perf_event_init(struct perf_event *event)
92 return NULL;
95 void __weak hw_perf_disable(void) { barrier(); }
96 void __weak hw_perf_enable(void) { barrier(); }
98 void __weak hw_perf_event_setup(int cpu) { barrier(); }
99 void __weak hw_perf_event_setup_online(int cpu) { barrier(); }
101 int __weak
102 hw_perf_group_sched_in(struct perf_event *group_leader,
103 struct perf_cpu_context *cpuctx,
104 struct perf_event_context *ctx, int cpu)
106 return 0;
109 void __weak perf_event_print_debug(void) { }
111 static DEFINE_PER_CPU(int, perf_disable_count);
113 void __perf_disable(void)
115 __get_cpu_var(perf_disable_count)++;
118 bool __perf_enable(void)
120 return !--__get_cpu_var(perf_disable_count);
123 void perf_disable(void)
125 __perf_disable();
126 hw_perf_disable();
129 void perf_enable(void)
131 if (__perf_enable())
132 hw_perf_enable();
135 static void get_ctx(struct perf_event_context *ctx)
137 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
140 static void free_ctx(struct rcu_head *head)
142 struct perf_event_context *ctx;
144 ctx = container_of(head, struct perf_event_context, rcu_head);
145 kfree(ctx);
148 static void put_ctx(struct perf_event_context *ctx)
150 if (atomic_dec_and_test(&ctx->refcount)) {
151 if (ctx->parent_ctx)
152 put_ctx(ctx->parent_ctx);
153 if (ctx->task)
154 put_task_struct(ctx->task);
155 call_rcu(&ctx->rcu_head, free_ctx);
159 static void unclone_ctx(struct perf_event_context *ctx)
161 if (ctx->parent_ctx) {
162 put_ctx(ctx->parent_ctx);
163 ctx->parent_ctx = NULL;
168 * If we inherit events we want to return the parent event id
169 * to userspace.
171 static u64 primary_event_id(struct perf_event *event)
173 u64 id = event->id;
175 if (event->parent)
176 id = event->parent->id;
178 return id;
182 * Get the perf_event_context for a task and lock it.
183 * This has to cope with with the fact that until it is locked,
184 * the context could get moved to another task.
186 static struct perf_event_context *
187 perf_lock_task_context(struct task_struct *task, unsigned long *flags)
189 struct perf_event_context *ctx;
191 rcu_read_lock();
192 retry:
193 ctx = rcu_dereference(task->perf_event_ctxp);
194 if (ctx) {
196 * If this context is a clone of another, it might
197 * get swapped for another underneath us by
198 * perf_event_task_sched_out, though the
199 * rcu_read_lock() protects us from any context
200 * getting freed. Lock the context and check if it
201 * got swapped before we could get the lock, and retry
202 * if so. If we locked the right context, then it
203 * can't get swapped on us any more.
205 spin_lock_irqsave(&ctx->lock, *flags);
206 if (ctx != rcu_dereference(task->perf_event_ctxp)) {
207 spin_unlock_irqrestore(&ctx->lock, *flags);
208 goto retry;
211 if (!atomic_inc_not_zero(&ctx->refcount)) {
212 spin_unlock_irqrestore(&ctx->lock, *flags);
213 ctx = NULL;
216 rcu_read_unlock();
217 return ctx;
221 * Get the context for a task and increment its pin_count so it
222 * can't get swapped to another task. This also increments its
223 * reference count so that the context can't get freed.
225 static struct perf_event_context *perf_pin_task_context(struct task_struct *task)
227 struct perf_event_context *ctx;
228 unsigned long flags;
230 ctx = perf_lock_task_context(task, &flags);
231 if (ctx) {
232 ++ctx->pin_count;
233 spin_unlock_irqrestore(&ctx->lock, flags);
235 return ctx;
238 static void perf_unpin_context(struct perf_event_context *ctx)
240 unsigned long flags;
242 spin_lock_irqsave(&ctx->lock, flags);
243 --ctx->pin_count;
244 spin_unlock_irqrestore(&ctx->lock, flags);
245 put_ctx(ctx);
249 * Add a event from the lists for its context.
250 * Must be called with ctx->mutex and ctx->lock held.
252 static void
253 list_add_event(struct perf_event *event, struct perf_event_context *ctx)
255 struct perf_event *group_leader = event->group_leader;
258 * Depending on whether it is a standalone or sibling event,
259 * add it straight to the context's event list, or to the group
260 * leader's sibling list:
262 if (group_leader == event)
263 list_add_tail(&event->group_entry, &ctx->group_list);
264 else {
265 list_add_tail(&event->group_entry, &group_leader->sibling_list);
266 group_leader->nr_siblings++;
269 list_add_rcu(&event->event_entry, &ctx->event_list);
270 ctx->nr_events++;
271 if (event->attr.inherit_stat)
272 ctx->nr_stat++;
276 * Remove a event from the lists for its context.
277 * Must be called with ctx->mutex and ctx->lock held.
279 static void
280 list_del_event(struct perf_event *event, struct perf_event_context *ctx)
282 struct perf_event *sibling, *tmp;
284 if (list_empty(&event->group_entry))
285 return;
286 ctx->nr_events--;
287 if (event->attr.inherit_stat)
288 ctx->nr_stat--;
290 list_del_init(&event->group_entry);
291 list_del_rcu(&event->event_entry);
293 if (event->group_leader != event)
294 event->group_leader->nr_siblings--;
297 * If this was a group event with sibling events then
298 * upgrade the siblings to singleton events by adding them
299 * to the context list directly:
301 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
303 list_move_tail(&sibling->group_entry, &ctx->group_list);
304 sibling->group_leader = sibling;
308 static void
309 event_sched_out(struct perf_event *event,
310 struct perf_cpu_context *cpuctx,
311 struct perf_event_context *ctx)
313 if (event->state != PERF_EVENT_STATE_ACTIVE)
314 return;
316 event->state = PERF_EVENT_STATE_INACTIVE;
317 if (event->pending_disable) {
318 event->pending_disable = 0;
319 event->state = PERF_EVENT_STATE_OFF;
321 event->tstamp_stopped = ctx->time;
322 event->pmu->disable(event);
323 event->oncpu = -1;
325 if (!is_software_event(event))
326 cpuctx->active_oncpu--;
327 ctx->nr_active--;
328 if (event->attr.exclusive || !cpuctx->active_oncpu)
329 cpuctx->exclusive = 0;
332 static void
333 group_sched_out(struct perf_event *group_event,
334 struct perf_cpu_context *cpuctx,
335 struct perf_event_context *ctx)
337 struct perf_event *event;
339 if (group_event->state != PERF_EVENT_STATE_ACTIVE)
340 return;
342 event_sched_out(group_event, cpuctx, ctx);
345 * Schedule out siblings (if any):
347 list_for_each_entry(event, &group_event->sibling_list, group_entry)
348 event_sched_out(event, cpuctx, ctx);
350 if (group_event->attr.exclusive)
351 cpuctx->exclusive = 0;
355 * Cross CPU call to remove a performance event
357 * We disable the event on the hardware level first. After that we
358 * remove it from the context list.
360 static void __perf_event_remove_from_context(void *info)
362 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
363 struct perf_event *event = info;
364 struct perf_event_context *ctx = event->ctx;
367 * If this is a task context, we need to check whether it is
368 * the current task context of this cpu. If not it has been
369 * scheduled out before the smp call arrived.
371 if (ctx->task && cpuctx->task_ctx != ctx)
372 return;
374 spin_lock(&ctx->lock);
376 * Protect the list operation against NMI by disabling the
377 * events on a global level.
379 perf_disable();
381 event_sched_out(event, cpuctx, ctx);
383 list_del_event(event, ctx);
385 if (!ctx->task) {
387 * Allow more per task events with respect to the
388 * reservation:
390 cpuctx->max_pertask =
391 min(perf_max_events - ctx->nr_events,
392 perf_max_events - perf_reserved_percpu);
395 perf_enable();
396 spin_unlock(&ctx->lock);
401 * Remove the event from a task's (or a CPU's) list of events.
403 * Must be called with ctx->mutex held.
405 * CPU events are removed with a smp call. For task events we only
406 * call when the task is on a CPU.
408 * If event->ctx is a cloned context, callers must make sure that
409 * every task struct that event->ctx->task could possibly point to
410 * remains valid. This is OK when called from perf_release since
411 * that only calls us on the top-level context, which can't be a clone.
412 * When called from perf_event_exit_task, it's OK because the
413 * context has been detached from its task.
415 static void perf_event_remove_from_context(struct perf_event *event)
417 struct perf_event_context *ctx = event->ctx;
418 struct task_struct *task = ctx->task;
420 if (!task) {
422 * Per cpu events are removed via an smp call and
423 * the removal is always sucessful.
425 smp_call_function_single(event->cpu,
426 __perf_event_remove_from_context,
427 event, 1);
428 return;
431 retry:
432 task_oncpu_function_call(task, __perf_event_remove_from_context,
433 event);
435 spin_lock_irq(&ctx->lock);
437 * If the context is active we need to retry the smp call.
439 if (ctx->nr_active && !list_empty(&event->group_entry)) {
440 spin_unlock_irq(&ctx->lock);
441 goto retry;
445 * The lock prevents that this context is scheduled in so we
446 * can remove the event safely, if the call above did not
447 * succeed.
449 if (!list_empty(&event->group_entry)) {
450 list_del_event(event, ctx);
452 spin_unlock_irq(&ctx->lock);
455 static inline u64 perf_clock(void)
457 return cpu_clock(smp_processor_id());
461 * Update the record of the current time in a context.
463 static void update_context_time(struct perf_event_context *ctx)
465 u64 now = perf_clock();
467 ctx->time += now - ctx->timestamp;
468 ctx->timestamp = now;
472 * Update the total_time_enabled and total_time_running fields for a event.
474 static void update_event_times(struct perf_event *event)
476 struct perf_event_context *ctx = event->ctx;
477 u64 run_end;
479 if (event->state < PERF_EVENT_STATE_INACTIVE ||
480 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
481 return;
483 event->total_time_enabled = ctx->time - event->tstamp_enabled;
485 if (event->state == PERF_EVENT_STATE_INACTIVE)
486 run_end = event->tstamp_stopped;
487 else
488 run_end = ctx->time;
490 event->total_time_running = run_end - event->tstamp_running;
494 * Update total_time_enabled and total_time_running for all events in a group.
496 static void update_group_times(struct perf_event *leader)
498 struct perf_event *event;
500 update_event_times(leader);
501 list_for_each_entry(event, &leader->sibling_list, group_entry)
502 update_event_times(event);
506 * Cross CPU call to disable a performance event
508 static void __perf_event_disable(void *info)
510 struct perf_event *event = info;
511 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
512 struct perf_event_context *ctx = event->ctx;
515 * If this is a per-task event, need to check whether this
516 * event's task is the current task on this cpu.
518 if (ctx->task && cpuctx->task_ctx != ctx)
519 return;
521 spin_lock(&ctx->lock);
524 * If the event is on, turn it off.
525 * If it is in error state, leave it in error state.
527 if (event->state >= PERF_EVENT_STATE_INACTIVE) {
528 update_context_time(ctx);
529 update_group_times(event);
530 if (event == event->group_leader)
531 group_sched_out(event, cpuctx, ctx);
532 else
533 event_sched_out(event, cpuctx, ctx);
534 event->state = PERF_EVENT_STATE_OFF;
537 spin_unlock(&ctx->lock);
541 * Disable a event.
543 * If event->ctx is a cloned context, callers must make sure that
544 * every task struct that event->ctx->task could possibly point to
545 * remains valid. This condition is satisifed when called through
546 * perf_event_for_each_child or perf_event_for_each because they
547 * hold the top-level event's child_mutex, so any descendant that
548 * goes to exit will block in sync_child_event.
549 * When called from perf_pending_event it's OK because event->ctx
550 * is the current context on this CPU and preemption is disabled,
551 * hence we can't get into perf_event_task_sched_out for this context.
553 static void perf_event_disable(struct perf_event *event)
555 struct perf_event_context *ctx = event->ctx;
556 struct task_struct *task = ctx->task;
558 if (!task) {
560 * Disable the event on the cpu that it's on
562 smp_call_function_single(event->cpu, __perf_event_disable,
563 event, 1);
564 return;
567 retry:
568 task_oncpu_function_call(task, __perf_event_disable, event);
570 spin_lock_irq(&ctx->lock);
572 * If the event is still active, we need to retry the cross-call.
574 if (event->state == PERF_EVENT_STATE_ACTIVE) {
575 spin_unlock_irq(&ctx->lock);
576 goto retry;
580 * Since we have the lock this context can't be scheduled
581 * in, so we can change the state safely.
583 if (event->state == PERF_EVENT_STATE_INACTIVE) {
584 update_group_times(event);
585 event->state = PERF_EVENT_STATE_OFF;
588 spin_unlock_irq(&ctx->lock);
591 static int
592 event_sched_in(struct perf_event *event,
593 struct perf_cpu_context *cpuctx,
594 struct perf_event_context *ctx,
595 int cpu)
597 if (event->state <= PERF_EVENT_STATE_OFF)
598 return 0;
600 event->state = PERF_EVENT_STATE_ACTIVE;
601 event->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
603 * The new state must be visible before we turn it on in the hardware:
605 smp_wmb();
607 if (event->pmu->enable(event)) {
608 event->state = PERF_EVENT_STATE_INACTIVE;
609 event->oncpu = -1;
610 return -EAGAIN;
613 event->tstamp_running += ctx->time - event->tstamp_stopped;
615 if (!is_software_event(event))
616 cpuctx->active_oncpu++;
617 ctx->nr_active++;
619 if (event->attr.exclusive)
620 cpuctx->exclusive = 1;
622 return 0;
625 static int
626 group_sched_in(struct perf_event *group_event,
627 struct perf_cpu_context *cpuctx,
628 struct perf_event_context *ctx,
629 int cpu)
631 struct perf_event *event, *partial_group;
632 int ret;
634 if (group_event->state == PERF_EVENT_STATE_OFF)
635 return 0;
637 ret = hw_perf_group_sched_in(group_event, cpuctx, ctx, cpu);
638 if (ret)
639 return ret < 0 ? ret : 0;
641 if (event_sched_in(group_event, cpuctx, ctx, cpu))
642 return -EAGAIN;
645 * Schedule in siblings as one group (if any):
647 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
648 if (event_sched_in(event, cpuctx, ctx, cpu)) {
649 partial_group = event;
650 goto group_error;
654 return 0;
656 group_error:
658 * Groups can be scheduled in as one unit only, so undo any
659 * partial group before returning:
661 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
662 if (event == partial_group)
663 break;
664 event_sched_out(event, cpuctx, ctx);
666 event_sched_out(group_event, cpuctx, ctx);
668 return -EAGAIN;
672 * Return 1 for a group consisting entirely of software events,
673 * 0 if the group contains any hardware events.
675 static int is_software_only_group(struct perf_event *leader)
677 struct perf_event *event;
679 if (!is_software_event(leader))
680 return 0;
682 list_for_each_entry(event, &leader->sibling_list, group_entry)
683 if (!is_software_event(event))
684 return 0;
686 return 1;
690 * Work out whether we can put this event group on the CPU now.
692 static int group_can_go_on(struct perf_event *event,
693 struct perf_cpu_context *cpuctx,
694 int can_add_hw)
697 * Groups consisting entirely of software events can always go on.
699 if (is_software_only_group(event))
700 return 1;
702 * If an exclusive group is already on, no other hardware
703 * events can go on.
705 if (cpuctx->exclusive)
706 return 0;
708 * If this group is exclusive and there are already
709 * events on the CPU, it can't go on.
711 if (event->attr.exclusive && cpuctx->active_oncpu)
712 return 0;
714 * Otherwise, try to add it if all previous groups were able
715 * to go on.
717 return can_add_hw;
720 static void add_event_to_ctx(struct perf_event *event,
721 struct perf_event_context *ctx)
723 list_add_event(event, ctx);
724 event->tstamp_enabled = ctx->time;
725 event->tstamp_running = ctx->time;
726 event->tstamp_stopped = ctx->time;
730 * Cross CPU call to install and enable a performance event
732 * Must be called with ctx->mutex held
734 static void __perf_install_in_context(void *info)
736 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
737 struct perf_event *event = info;
738 struct perf_event_context *ctx = event->ctx;
739 struct perf_event *leader = event->group_leader;
740 int cpu = smp_processor_id();
741 int err;
744 * If this is a task context, we need to check whether it is
745 * the current task context of this cpu. If not it has been
746 * scheduled out before the smp call arrived.
747 * Or possibly this is the right context but it isn't
748 * on this cpu because it had no events.
750 if (ctx->task && cpuctx->task_ctx != ctx) {
751 if (cpuctx->task_ctx || ctx->task != current)
752 return;
753 cpuctx->task_ctx = ctx;
756 spin_lock(&ctx->lock);
757 ctx->is_active = 1;
758 update_context_time(ctx);
761 * Protect the list operation against NMI by disabling the
762 * events on a global level. NOP for non NMI based events.
764 perf_disable();
766 add_event_to_ctx(event, ctx);
769 * Don't put the event on if it is disabled or if
770 * it is in a group and the group isn't on.
772 if (event->state != PERF_EVENT_STATE_INACTIVE ||
773 (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE))
774 goto unlock;
777 * An exclusive event can't go on if there are already active
778 * hardware events, and no hardware event can go on if there
779 * is already an exclusive event on.
781 if (!group_can_go_on(event, cpuctx, 1))
782 err = -EEXIST;
783 else
784 err = event_sched_in(event, cpuctx, ctx, cpu);
786 if (err) {
788 * This event couldn't go on. If it is in a group
789 * then we have to pull the whole group off.
790 * If the event group is pinned then put it in error state.
792 if (leader != event)
793 group_sched_out(leader, cpuctx, ctx);
794 if (leader->attr.pinned) {
795 update_group_times(leader);
796 leader->state = PERF_EVENT_STATE_ERROR;
800 if (!err && !ctx->task && cpuctx->max_pertask)
801 cpuctx->max_pertask--;
803 unlock:
804 perf_enable();
806 spin_unlock(&ctx->lock);
810 * Attach a performance event to a context
812 * First we add the event to the list with the hardware enable bit
813 * in event->hw_config cleared.
815 * If the event is attached to a task which is on a CPU we use a smp
816 * call to enable it in the task context. The task might have been
817 * scheduled away, but we check this in the smp call again.
819 * Must be called with ctx->mutex held.
821 static void
822 perf_install_in_context(struct perf_event_context *ctx,
823 struct perf_event *event,
824 int cpu)
826 struct task_struct *task = ctx->task;
828 if (!task) {
830 * Per cpu events are installed via an smp call and
831 * the install is always sucessful.
833 smp_call_function_single(cpu, __perf_install_in_context,
834 event, 1);
835 return;
838 retry:
839 task_oncpu_function_call(task, __perf_install_in_context,
840 event);
842 spin_lock_irq(&ctx->lock);
844 * we need to retry the smp call.
846 if (ctx->is_active && list_empty(&event->group_entry)) {
847 spin_unlock_irq(&ctx->lock);
848 goto retry;
852 * The lock prevents that this context is scheduled in so we
853 * can add the event safely, if it the call above did not
854 * succeed.
856 if (list_empty(&event->group_entry))
857 add_event_to_ctx(event, ctx);
858 spin_unlock_irq(&ctx->lock);
862 * Put a event into inactive state and update time fields.
863 * Enabling the leader of a group effectively enables all
864 * the group members that aren't explicitly disabled, so we
865 * have to update their ->tstamp_enabled also.
866 * Note: this works for group members as well as group leaders
867 * since the non-leader members' sibling_lists will be empty.
869 static void __perf_event_mark_enabled(struct perf_event *event,
870 struct perf_event_context *ctx)
872 struct perf_event *sub;
874 event->state = PERF_EVENT_STATE_INACTIVE;
875 event->tstamp_enabled = ctx->time - event->total_time_enabled;
876 list_for_each_entry(sub, &event->sibling_list, group_entry)
877 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
878 sub->tstamp_enabled =
879 ctx->time - sub->total_time_enabled;
883 * Cross CPU call to enable a performance event
885 static void __perf_event_enable(void *info)
887 struct perf_event *event = info;
888 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
889 struct perf_event_context *ctx = event->ctx;
890 struct perf_event *leader = event->group_leader;
891 int err;
894 * If this is a per-task event, need to check whether this
895 * event's task is the current task on this cpu.
897 if (ctx->task && cpuctx->task_ctx != ctx) {
898 if (cpuctx->task_ctx || ctx->task != current)
899 return;
900 cpuctx->task_ctx = ctx;
903 spin_lock(&ctx->lock);
904 ctx->is_active = 1;
905 update_context_time(ctx);
907 if (event->state >= PERF_EVENT_STATE_INACTIVE)
908 goto unlock;
909 __perf_event_mark_enabled(event, ctx);
912 * If the event is in a group and isn't the group leader,
913 * then don't put it on unless the group is on.
915 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
916 goto unlock;
918 if (!group_can_go_on(event, cpuctx, 1)) {
919 err = -EEXIST;
920 } else {
921 perf_disable();
922 if (event == leader)
923 err = group_sched_in(event, cpuctx, ctx,
924 smp_processor_id());
925 else
926 err = event_sched_in(event, cpuctx, ctx,
927 smp_processor_id());
928 perf_enable();
931 if (err) {
933 * If this event can't go on and it's part of a
934 * group, then the whole group has to come off.
936 if (leader != event)
937 group_sched_out(leader, cpuctx, ctx);
938 if (leader->attr.pinned) {
939 update_group_times(leader);
940 leader->state = PERF_EVENT_STATE_ERROR;
944 unlock:
945 spin_unlock(&ctx->lock);
949 * Enable a event.
951 * If event->ctx is a cloned context, callers must make sure that
952 * every task struct that event->ctx->task could possibly point to
953 * remains valid. This condition is satisfied when called through
954 * perf_event_for_each_child or perf_event_for_each as described
955 * for perf_event_disable.
957 static void perf_event_enable(struct perf_event *event)
959 struct perf_event_context *ctx = event->ctx;
960 struct task_struct *task = ctx->task;
962 if (!task) {
964 * Enable the event on the cpu that it's on
966 smp_call_function_single(event->cpu, __perf_event_enable,
967 event, 1);
968 return;
971 spin_lock_irq(&ctx->lock);
972 if (event->state >= PERF_EVENT_STATE_INACTIVE)
973 goto out;
976 * If the event is in error state, clear that first.
977 * That way, if we see the event in error state below, we
978 * know that it has gone back into error state, as distinct
979 * from the task having been scheduled away before the
980 * cross-call arrived.
982 if (event->state == PERF_EVENT_STATE_ERROR)
983 event->state = PERF_EVENT_STATE_OFF;
985 retry:
986 spin_unlock_irq(&ctx->lock);
987 task_oncpu_function_call(task, __perf_event_enable, event);
989 spin_lock_irq(&ctx->lock);
992 * If the context is active and the event is still off,
993 * we need to retry the cross-call.
995 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF)
996 goto retry;
999 * Since we have the lock this context can't be scheduled
1000 * in, so we can change the state safely.
1002 if (event->state == PERF_EVENT_STATE_OFF)
1003 __perf_event_mark_enabled(event, ctx);
1005 out:
1006 spin_unlock_irq(&ctx->lock);
1009 static int perf_event_refresh(struct perf_event *event, int refresh)
1012 * not supported on inherited events
1014 if (event->attr.inherit)
1015 return -EINVAL;
1017 atomic_add(refresh, &event->event_limit);
1018 perf_event_enable(event);
1020 return 0;
1023 void __perf_event_sched_out(struct perf_event_context *ctx,
1024 struct perf_cpu_context *cpuctx)
1026 struct perf_event *event;
1028 spin_lock(&ctx->lock);
1029 ctx->is_active = 0;
1030 if (likely(!ctx->nr_events))
1031 goto out;
1032 update_context_time(ctx);
1034 perf_disable();
1035 if (ctx->nr_active)
1036 list_for_each_entry(event, &ctx->group_list, group_entry)
1037 group_sched_out(event, cpuctx, ctx);
1039 perf_enable();
1040 out:
1041 spin_unlock(&ctx->lock);
1045 * Test whether two contexts are equivalent, i.e. whether they
1046 * have both been cloned from the same version of the same context
1047 * and they both have the same number of enabled events.
1048 * If the number of enabled events is the same, then the set
1049 * of enabled events should be the same, because these are both
1050 * inherited contexts, therefore we can't access individual events
1051 * in them directly with an fd; we can only enable/disable all
1052 * events via prctl, or enable/disable all events in a family
1053 * via ioctl, which will have the same effect on both contexts.
1055 static int context_equiv(struct perf_event_context *ctx1,
1056 struct perf_event_context *ctx2)
1058 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
1059 && ctx1->parent_gen == ctx2->parent_gen
1060 && !ctx1->pin_count && !ctx2->pin_count;
1063 static void __perf_event_read(void *event);
1065 static void __perf_event_sync_stat(struct perf_event *event,
1066 struct perf_event *next_event)
1068 u64 value;
1070 if (!event->attr.inherit_stat)
1071 return;
1074 * Update the event value, we cannot use perf_event_read()
1075 * because we're in the middle of a context switch and have IRQs
1076 * disabled, which upsets smp_call_function_single(), however
1077 * we know the event must be on the current CPU, therefore we
1078 * don't need to use it.
1080 switch (event->state) {
1081 case PERF_EVENT_STATE_ACTIVE:
1082 __perf_event_read(event);
1083 break;
1085 case PERF_EVENT_STATE_INACTIVE:
1086 update_event_times(event);
1087 break;
1089 default:
1090 break;
1094 * In order to keep per-task stats reliable we need to flip the event
1095 * values when we flip the contexts.
1097 value = atomic64_read(&next_event->count);
1098 value = atomic64_xchg(&event->count, value);
1099 atomic64_set(&next_event->count, value);
1101 swap(event->total_time_enabled, next_event->total_time_enabled);
1102 swap(event->total_time_running, next_event->total_time_running);
1105 * Since we swizzled the values, update the user visible data too.
1107 perf_event_update_userpage(event);
1108 perf_event_update_userpage(next_event);
1111 #define list_next_entry(pos, member) \
1112 list_entry(pos->member.next, typeof(*pos), member)
1114 static void perf_event_sync_stat(struct perf_event_context *ctx,
1115 struct perf_event_context *next_ctx)
1117 struct perf_event *event, *next_event;
1119 if (!ctx->nr_stat)
1120 return;
1122 event = list_first_entry(&ctx->event_list,
1123 struct perf_event, event_entry);
1125 next_event = list_first_entry(&next_ctx->event_list,
1126 struct perf_event, event_entry);
1128 while (&event->event_entry != &ctx->event_list &&
1129 &next_event->event_entry != &next_ctx->event_list) {
1131 __perf_event_sync_stat(event, next_event);
1133 event = list_next_entry(event, event_entry);
1134 next_event = list_next_entry(next_event, event_entry);
1139 * Called from scheduler to remove the events of the current task,
1140 * with interrupts disabled.
1142 * We stop each event and update the event value in event->count.
1144 * This does not protect us against NMI, but disable()
1145 * sets the disabled bit in the control field of event _before_
1146 * accessing the event control register. If a NMI hits, then it will
1147 * not restart the event.
1149 void perf_event_task_sched_out(struct task_struct *task,
1150 struct task_struct *next, int cpu)
1152 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
1153 struct perf_event_context *ctx = task->perf_event_ctxp;
1154 struct perf_event_context *next_ctx;
1155 struct perf_event_context *parent;
1156 struct pt_regs *regs;
1157 int do_switch = 1;
1159 regs = task_pt_regs(task);
1160 perf_sw_event(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 1, regs, 0);
1162 if (likely(!ctx || !cpuctx->task_ctx))
1163 return;
1165 update_context_time(ctx);
1167 rcu_read_lock();
1168 parent = rcu_dereference(ctx->parent_ctx);
1169 next_ctx = next->perf_event_ctxp;
1170 if (parent && next_ctx &&
1171 rcu_dereference(next_ctx->parent_ctx) == parent) {
1173 * Looks like the two contexts are clones, so we might be
1174 * able to optimize the context switch. We lock both
1175 * contexts and check that they are clones under the
1176 * lock (including re-checking that neither has been
1177 * uncloned in the meantime). It doesn't matter which
1178 * order we take the locks because no other cpu could
1179 * be trying to lock both of these tasks.
1181 spin_lock(&ctx->lock);
1182 spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
1183 if (context_equiv(ctx, next_ctx)) {
1185 * XXX do we need a memory barrier of sorts
1186 * wrt to rcu_dereference() of perf_event_ctxp
1188 task->perf_event_ctxp = next_ctx;
1189 next->perf_event_ctxp = ctx;
1190 ctx->task = next;
1191 next_ctx->task = task;
1192 do_switch = 0;
1194 perf_event_sync_stat(ctx, next_ctx);
1196 spin_unlock(&next_ctx->lock);
1197 spin_unlock(&ctx->lock);
1199 rcu_read_unlock();
1201 if (do_switch) {
1202 __perf_event_sched_out(ctx, cpuctx);
1203 cpuctx->task_ctx = NULL;
1208 * Called with IRQs disabled
1210 static void __perf_event_task_sched_out(struct perf_event_context *ctx)
1212 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1214 if (!cpuctx->task_ctx)
1215 return;
1217 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
1218 return;
1220 __perf_event_sched_out(ctx, cpuctx);
1221 cpuctx->task_ctx = NULL;
1225 * Called with IRQs disabled
1227 static void perf_event_cpu_sched_out(struct perf_cpu_context *cpuctx)
1229 __perf_event_sched_out(&cpuctx->ctx, cpuctx);
1232 static void
1233 __perf_event_sched_in(struct perf_event_context *ctx,
1234 struct perf_cpu_context *cpuctx, int cpu)
1236 struct perf_event *event;
1237 int can_add_hw = 1;
1239 spin_lock(&ctx->lock);
1240 ctx->is_active = 1;
1241 if (likely(!ctx->nr_events))
1242 goto out;
1244 ctx->timestamp = perf_clock();
1246 perf_disable();
1249 * First go through the list and put on any pinned groups
1250 * in order to give them the best chance of going on.
1252 list_for_each_entry(event, &ctx->group_list, group_entry) {
1253 if (event->state <= PERF_EVENT_STATE_OFF ||
1254 !event->attr.pinned)
1255 continue;
1256 if (event->cpu != -1 && event->cpu != cpu)
1257 continue;
1259 if (group_can_go_on(event, cpuctx, 1))
1260 group_sched_in(event, cpuctx, ctx, cpu);
1263 * If this pinned group hasn't been scheduled,
1264 * put it in error state.
1266 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1267 update_group_times(event);
1268 event->state = PERF_EVENT_STATE_ERROR;
1272 list_for_each_entry(event, &ctx->group_list, group_entry) {
1274 * Ignore events in OFF or ERROR state, and
1275 * ignore pinned events since we did them already.
1277 if (event->state <= PERF_EVENT_STATE_OFF ||
1278 event->attr.pinned)
1279 continue;
1282 * Listen to the 'cpu' scheduling filter constraint
1283 * of events:
1285 if (event->cpu != -1 && event->cpu != cpu)
1286 continue;
1288 if (group_can_go_on(event, cpuctx, can_add_hw))
1289 if (group_sched_in(event, cpuctx, ctx, cpu))
1290 can_add_hw = 0;
1292 perf_enable();
1293 out:
1294 spin_unlock(&ctx->lock);
1298 * Called from scheduler to add the events of the current task
1299 * with interrupts disabled.
1301 * We restore the event value and then enable it.
1303 * This does not protect us against NMI, but enable()
1304 * sets the enabled bit in the control field of event _before_
1305 * accessing the event control register. If a NMI hits, then it will
1306 * keep the event running.
1308 void perf_event_task_sched_in(struct task_struct *task, int cpu)
1310 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
1311 struct perf_event_context *ctx = task->perf_event_ctxp;
1313 if (likely(!ctx))
1314 return;
1315 if (cpuctx->task_ctx == ctx)
1316 return;
1317 __perf_event_sched_in(ctx, cpuctx, cpu);
1318 cpuctx->task_ctx = ctx;
1321 static void perf_event_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu)
1323 struct perf_event_context *ctx = &cpuctx->ctx;
1325 __perf_event_sched_in(ctx, cpuctx, cpu);
1328 #define MAX_INTERRUPTS (~0ULL)
1330 static void perf_log_throttle(struct perf_event *event, int enable);
1332 static void perf_adjust_period(struct perf_event *event, u64 events)
1334 struct hw_perf_event *hwc = &event->hw;
1335 u64 period, sample_period;
1336 s64 delta;
1338 events *= hwc->sample_period;
1339 period = div64_u64(events, event->attr.sample_freq);
1341 delta = (s64)(period - hwc->sample_period);
1342 delta = (delta + 7) / 8; /* low pass filter */
1344 sample_period = hwc->sample_period + delta;
1346 if (!sample_period)
1347 sample_period = 1;
1349 hwc->sample_period = sample_period;
1352 static void perf_ctx_adjust_freq(struct perf_event_context *ctx)
1354 struct perf_event *event;
1355 struct hw_perf_event *hwc;
1356 u64 interrupts, freq;
1358 spin_lock(&ctx->lock);
1359 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
1360 if (event->state != PERF_EVENT_STATE_ACTIVE)
1361 continue;
1363 if (event->cpu != -1 && event->cpu != smp_processor_id())
1364 continue;
1366 hwc = &event->hw;
1368 interrupts = hwc->interrupts;
1369 hwc->interrupts = 0;
1372 * unthrottle events on the tick
1374 if (interrupts == MAX_INTERRUPTS) {
1375 perf_log_throttle(event, 1);
1376 event->pmu->unthrottle(event);
1377 interrupts = 2*sysctl_perf_event_sample_rate/HZ;
1380 if (!event->attr.freq || !event->attr.sample_freq)
1381 continue;
1384 * if the specified freq < HZ then we need to skip ticks
1386 if (event->attr.sample_freq < HZ) {
1387 freq = event->attr.sample_freq;
1389 hwc->freq_count += freq;
1390 hwc->freq_interrupts += interrupts;
1392 if (hwc->freq_count < HZ)
1393 continue;
1395 interrupts = hwc->freq_interrupts;
1396 hwc->freq_interrupts = 0;
1397 hwc->freq_count -= HZ;
1398 } else
1399 freq = HZ;
1401 perf_adjust_period(event, freq * interrupts);
1404 * In order to avoid being stalled by an (accidental) huge
1405 * sample period, force reset the sample period if we didn't
1406 * get any events in this freq period.
1408 if (!interrupts) {
1409 perf_disable();
1410 event->pmu->disable(event);
1411 atomic64_set(&hwc->period_left, 0);
1412 event->pmu->enable(event);
1413 perf_enable();
1416 spin_unlock(&ctx->lock);
1420 * Round-robin a context's events:
1422 static void rotate_ctx(struct perf_event_context *ctx)
1424 struct perf_event *event;
1426 if (!ctx->nr_events)
1427 return;
1429 spin_lock(&ctx->lock);
1431 * Rotate the first entry last (works just fine for group events too):
1433 perf_disable();
1434 list_for_each_entry(event, &ctx->group_list, group_entry) {
1435 list_move_tail(&event->group_entry, &ctx->group_list);
1436 break;
1438 perf_enable();
1440 spin_unlock(&ctx->lock);
1443 void perf_event_task_tick(struct task_struct *curr, int cpu)
1445 struct perf_cpu_context *cpuctx;
1446 struct perf_event_context *ctx;
1448 if (!atomic_read(&nr_events))
1449 return;
1451 cpuctx = &per_cpu(perf_cpu_context, cpu);
1452 ctx = curr->perf_event_ctxp;
1454 perf_ctx_adjust_freq(&cpuctx->ctx);
1455 if (ctx)
1456 perf_ctx_adjust_freq(ctx);
1458 perf_event_cpu_sched_out(cpuctx);
1459 if (ctx)
1460 __perf_event_task_sched_out(ctx);
1462 rotate_ctx(&cpuctx->ctx);
1463 if (ctx)
1464 rotate_ctx(ctx);
1466 perf_event_cpu_sched_in(cpuctx, cpu);
1467 if (ctx)
1468 perf_event_task_sched_in(curr, cpu);
1472 * Enable all of a task's events that have been marked enable-on-exec.
1473 * This expects task == current.
1475 static void perf_event_enable_on_exec(struct task_struct *task)
1477 struct perf_event_context *ctx;
1478 struct perf_event *event;
1479 unsigned long flags;
1480 int enabled = 0;
1482 local_irq_save(flags);
1483 ctx = task->perf_event_ctxp;
1484 if (!ctx || !ctx->nr_events)
1485 goto out;
1487 __perf_event_task_sched_out(ctx);
1489 spin_lock(&ctx->lock);
1491 list_for_each_entry(event, &ctx->group_list, group_entry) {
1492 if (!event->attr.enable_on_exec)
1493 continue;
1494 event->attr.enable_on_exec = 0;
1495 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1496 continue;
1497 __perf_event_mark_enabled(event, ctx);
1498 enabled = 1;
1502 * Unclone this context if we enabled any event.
1504 if (enabled)
1505 unclone_ctx(ctx);
1507 spin_unlock(&ctx->lock);
1509 perf_event_task_sched_in(task, smp_processor_id());
1510 out:
1511 local_irq_restore(flags);
1515 * Cross CPU call to read the hardware event
1517 static void __perf_event_read(void *info)
1519 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1520 struct perf_event *event = info;
1521 struct perf_event_context *ctx = event->ctx;
1522 unsigned long flags;
1525 * If this is a task context, we need to check whether it is
1526 * the current task context of this cpu. If not it has been
1527 * scheduled out before the smp call arrived. In that case
1528 * event->count would have been updated to a recent sample
1529 * when the event was scheduled out.
1531 if (ctx->task && cpuctx->task_ctx != ctx)
1532 return;
1534 local_irq_save(flags);
1535 if (ctx->is_active)
1536 update_context_time(ctx);
1537 event->pmu->read(event);
1538 update_event_times(event);
1539 local_irq_restore(flags);
1542 static u64 perf_event_read(struct perf_event *event)
1545 * If event is enabled and currently active on a CPU, update the
1546 * value in the event structure:
1548 if (event->state == PERF_EVENT_STATE_ACTIVE) {
1549 smp_call_function_single(event->oncpu,
1550 __perf_event_read, event, 1);
1551 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
1552 update_event_times(event);
1555 return atomic64_read(&event->count);
1559 * Initialize the perf_event context in a task_struct:
1561 static void
1562 __perf_event_init_context(struct perf_event_context *ctx,
1563 struct task_struct *task)
1565 memset(ctx, 0, sizeof(*ctx));
1566 spin_lock_init(&ctx->lock);
1567 mutex_init(&ctx->mutex);
1568 INIT_LIST_HEAD(&ctx->group_list);
1569 INIT_LIST_HEAD(&ctx->event_list);
1570 atomic_set(&ctx->refcount, 1);
1571 ctx->task = task;
1574 static struct perf_event_context *find_get_context(pid_t pid, int cpu)
1576 struct perf_event_context *ctx;
1577 struct perf_cpu_context *cpuctx;
1578 struct task_struct *task;
1579 unsigned long flags;
1580 int err;
1583 * If cpu is not a wildcard then this is a percpu event:
1585 if (cpu != -1) {
1586 /* Must be root to operate on a CPU event: */
1587 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
1588 return ERR_PTR(-EACCES);
1590 if (cpu < 0 || cpu >= nr_cpumask_bits)
1591 return ERR_PTR(-EINVAL);
1594 * We could be clever and allow to attach a event to an
1595 * offline CPU and activate it when the CPU comes up, but
1596 * that's for later.
1598 if (!cpu_isset(cpu, cpu_online_map))
1599 return ERR_PTR(-ENODEV);
1601 cpuctx = &per_cpu(perf_cpu_context, cpu);
1602 ctx = &cpuctx->ctx;
1603 get_ctx(ctx);
1605 return ctx;
1608 rcu_read_lock();
1609 if (!pid)
1610 task = current;
1611 else
1612 task = find_task_by_vpid(pid);
1613 if (task)
1614 get_task_struct(task);
1615 rcu_read_unlock();
1617 if (!task)
1618 return ERR_PTR(-ESRCH);
1621 * Can't attach events to a dying task.
1623 err = -ESRCH;
1624 if (task->flags & PF_EXITING)
1625 goto errout;
1627 /* Reuse ptrace permission checks for now. */
1628 err = -EACCES;
1629 if (!ptrace_may_access(task, PTRACE_MODE_READ))
1630 goto errout;
1632 retry:
1633 ctx = perf_lock_task_context(task, &flags);
1634 if (ctx) {
1635 unclone_ctx(ctx);
1636 spin_unlock_irqrestore(&ctx->lock, flags);
1639 if (!ctx) {
1640 ctx = kmalloc(sizeof(struct perf_event_context), GFP_KERNEL);
1641 err = -ENOMEM;
1642 if (!ctx)
1643 goto errout;
1644 __perf_event_init_context(ctx, task);
1645 get_ctx(ctx);
1646 if (cmpxchg(&task->perf_event_ctxp, NULL, ctx)) {
1648 * We raced with some other task; use
1649 * the context they set.
1651 kfree(ctx);
1652 goto retry;
1654 get_task_struct(task);
1657 put_task_struct(task);
1658 return ctx;
1660 errout:
1661 put_task_struct(task);
1662 return ERR_PTR(err);
1665 static void free_event_rcu(struct rcu_head *head)
1667 struct perf_event *event;
1669 event = container_of(head, struct perf_event, rcu_head);
1670 if (event->ns)
1671 put_pid_ns(event->ns);
1672 kfree(event);
1675 static void perf_pending_sync(struct perf_event *event);
1677 static void free_event(struct perf_event *event)
1679 perf_pending_sync(event);
1681 if (!event->parent) {
1682 atomic_dec(&nr_events);
1683 if (event->attr.mmap)
1684 atomic_dec(&nr_mmap_events);
1685 if (event->attr.comm)
1686 atomic_dec(&nr_comm_events);
1687 if (event->attr.task)
1688 atomic_dec(&nr_task_events);
1691 if (event->output) {
1692 fput(event->output->filp);
1693 event->output = NULL;
1696 if (event->destroy)
1697 event->destroy(event);
1699 put_ctx(event->ctx);
1700 call_rcu(&event->rcu_head, free_event_rcu);
1704 * Called when the last reference to the file is gone.
1706 static int perf_release(struct inode *inode, struct file *file)
1708 struct perf_event *event = file->private_data;
1709 struct perf_event_context *ctx = event->ctx;
1711 file->private_data = NULL;
1713 WARN_ON_ONCE(ctx->parent_ctx);
1714 mutex_lock(&ctx->mutex);
1715 perf_event_remove_from_context(event);
1716 mutex_unlock(&ctx->mutex);
1718 mutex_lock(&event->owner->perf_event_mutex);
1719 list_del_init(&event->owner_entry);
1720 mutex_unlock(&event->owner->perf_event_mutex);
1721 put_task_struct(event->owner);
1723 free_event(event);
1725 return 0;
1728 static int perf_event_read_size(struct perf_event *event)
1730 int entry = sizeof(u64); /* value */
1731 int size = 0;
1732 int nr = 1;
1734 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1735 size += sizeof(u64);
1737 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1738 size += sizeof(u64);
1740 if (event->attr.read_format & PERF_FORMAT_ID)
1741 entry += sizeof(u64);
1743 if (event->attr.read_format & PERF_FORMAT_GROUP) {
1744 nr += event->group_leader->nr_siblings;
1745 size += sizeof(u64);
1748 size += entry * nr;
1750 return size;
1753 static u64 perf_event_read_value(struct perf_event *event)
1755 struct perf_event *child;
1756 u64 total = 0;
1758 total += perf_event_read(event);
1759 list_for_each_entry(child, &event->child_list, child_list)
1760 total += perf_event_read(child);
1762 return total;
1765 static int perf_event_read_entry(struct perf_event *event,
1766 u64 read_format, char __user *buf)
1768 int n = 0, count = 0;
1769 u64 values[2];
1771 values[n++] = perf_event_read_value(event);
1772 if (read_format & PERF_FORMAT_ID)
1773 values[n++] = primary_event_id(event);
1775 count = n * sizeof(u64);
1777 if (copy_to_user(buf, values, count))
1778 return -EFAULT;
1780 return count;
1783 static int perf_event_read_group(struct perf_event *event,
1784 u64 read_format, char __user *buf)
1786 struct perf_event *leader = event->group_leader, *sub;
1787 int n = 0, size = 0, err = -EFAULT;
1788 u64 values[3];
1790 values[n++] = 1 + leader->nr_siblings;
1791 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
1792 values[n++] = leader->total_time_enabled +
1793 atomic64_read(&leader->child_total_time_enabled);
1795 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
1796 values[n++] = leader->total_time_running +
1797 atomic64_read(&leader->child_total_time_running);
1800 size = n * sizeof(u64);
1802 if (copy_to_user(buf, values, size))
1803 return -EFAULT;
1805 err = perf_event_read_entry(leader, read_format, buf + size);
1806 if (err < 0)
1807 return err;
1809 size += err;
1811 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
1812 err = perf_event_read_entry(sub, read_format,
1813 buf + size);
1814 if (err < 0)
1815 return err;
1817 size += err;
1820 return size;
1823 static int perf_event_read_one(struct perf_event *event,
1824 u64 read_format, char __user *buf)
1826 u64 values[4];
1827 int n = 0;
1829 values[n++] = perf_event_read_value(event);
1830 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
1831 values[n++] = event->total_time_enabled +
1832 atomic64_read(&event->child_total_time_enabled);
1834 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
1835 values[n++] = event->total_time_running +
1836 atomic64_read(&event->child_total_time_running);
1838 if (read_format & PERF_FORMAT_ID)
1839 values[n++] = primary_event_id(event);
1841 if (copy_to_user(buf, values, n * sizeof(u64)))
1842 return -EFAULT;
1844 return n * sizeof(u64);
1848 * Read the performance event - simple non blocking version for now
1850 static ssize_t
1851 perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
1853 u64 read_format = event->attr.read_format;
1854 int ret;
1857 * Return end-of-file for a read on a event that is in
1858 * error state (i.e. because it was pinned but it couldn't be
1859 * scheduled on to the CPU at some point).
1861 if (event->state == PERF_EVENT_STATE_ERROR)
1862 return 0;
1864 if (count < perf_event_read_size(event))
1865 return -ENOSPC;
1867 WARN_ON_ONCE(event->ctx->parent_ctx);
1868 mutex_lock(&event->child_mutex);
1869 if (read_format & PERF_FORMAT_GROUP)
1870 ret = perf_event_read_group(event, read_format, buf);
1871 else
1872 ret = perf_event_read_one(event, read_format, buf);
1873 mutex_unlock(&event->child_mutex);
1875 return ret;
1878 static ssize_t
1879 perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
1881 struct perf_event *event = file->private_data;
1883 return perf_read_hw(event, buf, count);
1886 static unsigned int perf_poll(struct file *file, poll_table *wait)
1888 struct perf_event *event = file->private_data;
1889 struct perf_mmap_data *data;
1890 unsigned int events = POLL_HUP;
1892 rcu_read_lock();
1893 data = rcu_dereference(event->data);
1894 if (data)
1895 events = atomic_xchg(&data->poll, 0);
1896 rcu_read_unlock();
1898 poll_wait(file, &event->waitq, wait);
1900 return events;
1903 static void perf_event_reset(struct perf_event *event)
1905 (void)perf_event_read(event);
1906 atomic64_set(&event->count, 0);
1907 perf_event_update_userpage(event);
1911 * Holding the top-level event's child_mutex means that any
1912 * descendant process that has inherited this event will block
1913 * in sync_child_event if it goes to exit, thus satisfying the
1914 * task existence requirements of perf_event_enable/disable.
1916 static void perf_event_for_each_child(struct perf_event *event,
1917 void (*func)(struct perf_event *))
1919 struct perf_event *child;
1921 WARN_ON_ONCE(event->ctx->parent_ctx);
1922 mutex_lock(&event->child_mutex);
1923 func(event);
1924 list_for_each_entry(child, &event->child_list, child_list)
1925 func(child);
1926 mutex_unlock(&event->child_mutex);
1929 static void perf_event_for_each(struct perf_event *event,
1930 void (*func)(struct perf_event *))
1932 struct perf_event_context *ctx = event->ctx;
1933 struct perf_event *sibling;
1935 WARN_ON_ONCE(ctx->parent_ctx);
1936 mutex_lock(&ctx->mutex);
1937 event = event->group_leader;
1939 perf_event_for_each_child(event, func);
1940 func(event);
1941 list_for_each_entry(sibling, &event->sibling_list, group_entry)
1942 perf_event_for_each_child(event, func);
1943 mutex_unlock(&ctx->mutex);
1946 static int perf_event_period(struct perf_event *event, u64 __user *arg)
1948 struct perf_event_context *ctx = event->ctx;
1949 unsigned long size;
1950 int ret = 0;
1951 u64 value;
1953 if (!event->attr.sample_period)
1954 return -EINVAL;
1956 size = copy_from_user(&value, arg, sizeof(value));
1957 if (size != sizeof(value))
1958 return -EFAULT;
1960 if (!value)
1961 return -EINVAL;
1963 spin_lock_irq(&ctx->lock);
1964 if (event->attr.freq) {
1965 if (value > sysctl_perf_event_sample_rate) {
1966 ret = -EINVAL;
1967 goto unlock;
1970 event->attr.sample_freq = value;
1971 } else {
1972 event->attr.sample_period = value;
1973 event->hw.sample_period = value;
1975 unlock:
1976 spin_unlock_irq(&ctx->lock);
1978 return ret;
1981 int perf_event_set_output(struct perf_event *event, int output_fd);
1983 static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1985 struct perf_event *event = file->private_data;
1986 void (*func)(struct perf_event *);
1987 u32 flags = arg;
1989 switch (cmd) {
1990 case PERF_EVENT_IOC_ENABLE:
1991 func = perf_event_enable;
1992 break;
1993 case PERF_EVENT_IOC_DISABLE:
1994 func = perf_event_disable;
1995 break;
1996 case PERF_EVENT_IOC_RESET:
1997 func = perf_event_reset;
1998 break;
2000 case PERF_EVENT_IOC_REFRESH:
2001 return perf_event_refresh(event, arg);
2003 case PERF_EVENT_IOC_PERIOD:
2004 return perf_event_period(event, (u64 __user *)arg);
2006 case PERF_EVENT_IOC_SET_OUTPUT:
2007 return perf_event_set_output(event, arg);
2009 default:
2010 return -ENOTTY;
2013 if (flags & PERF_IOC_FLAG_GROUP)
2014 perf_event_for_each(event, func);
2015 else
2016 perf_event_for_each_child(event, func);
2018 return 0;
2021 int perf_event_task_enable(void)
2023 struct perf_event *event;
2025 mutex_lock(&current->perf_event_mutex);
2026 list_for_each_entry(event, &current->perf_event_list, owner_entry)
2027 perf_event_for_each_child(event, perf_event_enable);
2028 mutex_unlock(&current->perf_event_mutex);
2030 return 0;
2033 int perf_event_task_disable(void)
2035 struct perf_event *event;
2037 mutex_lock(&current->perf_event_mutex);
2038 list_for_each_entry(event, &current->perf_event_list, owner_entry)
2039 perf_event_for_each_child(event, perf_event_disable);
2040 mutex_unlock(&current->perf_event_mutex);
2042 return 0;
2045 #ifndef PERF_EVENT_INDEX_OFFSET
2046 # define PERF_EVENT_INDEX_OFFSET 0
2047 #endif
2049 static int perf_event_index(struct perf_event *event)
2051 if (event->state != PERF_EVENT_STATE_ACTIVE)
2052 return 0;
2054 return event->hw.idx + 1 - PERF_EVENT_INDEX_OFFSET;
2058 * Callers need to ensure there can be no nesting of this function, otherwise
2059 * the seqlock logic goes bad. We can not serialize this because the arch
2060 * code calls this from NMI context.
2062 void perf_event_update_userpage(struct perf_event *event)
2064 struct perf_event_mmap_page *userpg;
2065 struct perf_mmap_data *data;
2067 rcu_read_lock();
2068 data = rcu_dereference(event->data);
2069 if (!data)
2070 goto unlock;
2072 userpg = data->user_page;
2075 * Disable preemption so as to not let the corresponding user-space
2076 * spin too long if we get preempted.
2078 preempt_disable();
2079 ++userpg->lock;
2080 barrier();
2081 userpg->index = perf_event_index(event);
2082 userpg->offset = atomic64_read(&event->count);
2083 if (event->state == PERF_EVENT_STATE_ACTIVE)
2084 userpg->offset -= atomic64_read(&event->hw.prev_count);
2086 userpg->time_enabled = event->total_time_enabled +
2087 atomic64_read(&event->child_total_time_enabled);
2089 userpg->time_running = event->total_time_running +
2090 atomic64_read(&event->child_total_time_running);
2092 barrier();
2093 ++userpg->lock;
2094 preempt_enable();
2095 unlock:
2096 rcu_read_unlock();
2099 static unsigned long perf_data_size(struct perf_mmap_data *data)
2101 return data->nr_pages << (PAGE_SHIFT + data->data_order);
2104 #ifndef CONFIG_PERF_USE_VMALLOC
2107 * Back perf_mmap() with regular GFP_KERNEL-0 pages.
2110 static struct page *
2111 perf_mmap_to_page(struct perf_mmap_data *data, unsigned long pgoff)
2113 if (pgoff > data->nr_pages)
2114 return NULL;
2116 if (pgoff == 0)
2117 return virt_to_page(data->user_page);
2119 return virt_to_page(data->data_pages[pgoff - 1]);
2122 static struct perf_mmap_data *
2123 perf_mmap_data_alloc(struct perf_event *event, int nr_pages)
2125 struct perf_mmap_data *data;
2126 unsigned long size;
2127 int i;
2129 WARN_ON(atomic_read(&event->mmap_count));
2131 size = sizeof(struct perf_mmap_data);
2132 size += nr_pages * sizeof(void *);
2134 data = kzalloc(size, GFP_KERNEL);
2135 if (!data)
2136 goto fail;
2138 data->user_page = (void *)get_zeroed_page(GFP_KERNEL);
2139 if (!data->user_page)
2140 goto fail_user_page;
2142 for (i = 0; i < nr_pages; i++) {
2143 data->data_pages[i] = (void *)get_zeroed_page(GFP_KERNEL);
2144 if (!data->data_pages[i])
2145 goto fail_data_pages;
2148 data->data_order = 0;
2149 data->nr_pages = nr_pages;
2151 return data;
2153 fail_data_pages:
2154 for (i--; i >= 0; i--)
2155 free_page((unsigned long)data->data_pages[i]);
2157 free_page((unsigned long)data->user_page);
2159 fail_user_page:
2160 kfree(data);
2162 fail:
2163 return NULL;
2166 static void perf_mmap_free_page(unsigned long addr)
2168 struct page *page = virt_to_page((void *)addr);
2170 page->mapping = NULL;
2171 __free_page(page);
2174 static void perf_mmap_data_free(struct perf_mmap_data *data)
2176 int i;
2178 perf_mmap_free_page((unsigned long)data->user_page);
2179 for (i = 0; i < data->nr_pages; i++)
2180 perf_mmap_free_page((unsigned long)data->data_pages[i]);
2181 kfree(data);
2184 #else
2187 * Back perf_mmap() with vmalloc memory.
2189 * Required for architectures that have d-cache aliasing issues.
2192 static struct page *
2193 perf_mmap_to_page(struct perf_mmap_data *data, unsigned long pgoff)
2195 if (pgoff > (1UL << data->data_order))
2196 return NULL;
2198 return vmalloc_to_page((void *)data->user_page + pgoff * PAGE_SIZE);
2201 static void perf_mmap_unmark_page(void *addr)
2203 struct page *page = vmalloc_to_page(addr);
2205 page->mapping = NULL;
2208 static void perf_mmap_data_free_work(struct work_struct *work)
2210 struct perf_mmap_data *data;
2211 void *base;
2212 int i, nr;
2214 data = container_of(work, struct perf_mmap_data, work);
2215 nr = 1 << data->data_order;
2217 base = data->user_page;
2218 for (i = 0; i < nr + 1; i++)
2219 perf_mmap_unmark_page(base + (i * PAGE_SIZE));
2221 vfree(base);
2222 kfree(data);
2225 static void perf_mmap_data_free(struct perf_mmap_data *data)
2227 schedule_work(&data->work);
2230 static struct perf_mmap_data *
2231 perf_mmap_data_alloc(struct perf_event *event, int nr_pages)
2233 struct perf_mmap_data *data;
2234 unsigned long size;
2235 void *all_buf;
2237 WARN_ON(atomic_read(&event->mmap_count));
2239 size = sizeof(struct perf_mmap_data);
2240 size += sizeof(void *);
2242 data = kzalloc(size, GFP_KERNEL);
2243 if (!data)
2244 goto fail;
2246 INIT_WORK(&data->work, perf_mmap_data_free_work);
2248 all_buf = vmalloc_user((nr_pages + 1) * PAGE_SIZE);
2249 if (!all_buf)
2250 goto fail_all_buf;
2252 data->user_page = all_buf;
2253 data->data_pages[0] = all_buf + PAGE_SIZE;
2254 data->data_order = ilog2(nr_pages);
2255 data->nr_pages = 1;
2257 return data;
2259 fail_all_buf:
2260 kfree(data);
2262 fail:
2263 return NULL;
2266 #endif
2268 static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
2270 struct perf_event *event = vma->vm_file->private_data;
2271 struct perf_mmap_data *data;
2272 int ret = VM_FAULT_SIGBUS;
2274 if (vmf->flags & FAULT_FLAG_MKWRITE) {
2275 if (vmf->pgoff == 0)
2276 ret = 0;
2277 return ret;
2280 rcu_read_lock();
2281 data = rcu_dereference(event->data);
2282 if (!data)
2283 goto unlock;
2285 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
2286 goto unlock;
2288 vmf->page = perf_mmap_to_page(data, vmf->pgoff);
2289 if (!vmf->page)
2290 goto unlock;
2292 get_page(vmf->page);
2293 vmf->page->mapping = vma->vm_file->f_mapping;
2294 vmf->page->index = vmf->pgoff;
2296 ret = 0;
2297 unlock:
2298 rcu_read_unlock();
2300 return ret;
2303 static void
2304 perf_mmap_data_init(struct perf_event *event, struct perf_mmap_data *data)
2306 long max_size = perf_data_size(data);
2308 atomic_set(&data->lock, -1);
2310 if (event->attr.watermark) {
2311 data->watermark = min_t(long, max_size,
2312 event->attr.wakeup_watermark);
2315 if (!data->watermark)
2316 data->watermark = max_t(long, PAGE_SIZE, max_size / 2);
2319 rcu_assign_pointer(event->data, data);
2322 static void perf_mmap_data_free_rcu(struct rcu_head *rcu_head)
2324 struct perf_mmap_data *data;
2326 data = container_of(rcu_head, struct perf_mmap_data, rcu_head);
2327 perf_mmap_data_free(data);
2330 static void perf_mmap_data_release(struct perf_event *event)
2332 struct perf_mmap_data *data = event->data;
2334 WARN_ON(atomic_read(&event->mmap_count));
2336 rcu_assign_pointer(event->data, NULL);
2337 call_rcu(&data->rcu_head, perf_mmap_data_free_rcu);
2340 static void perf_mmap_open(struct vm_area_struct *vma)
2342 struct perf_event *event = vma->vm_file->private_data;
2344 atomic_inc(&event->mmap_count);
2347 static void perf_mmap_close(struct vm_area_struct *vma)
2349 struct perf_event *event = vma->vm_file->private_data;
2351 WARN_ON_ONCE(event->ctx->parent_ctx);
2352 if (atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) {
2353 unsigned long size = perf_data_size(event->data);
2354 struct user_struct *user = current_user();
2356 atomic_long_sub((size >> PAGE_SHIFT) + 1, &user->locked_vm);
2357 vma->vm_mm->locked_vm -= event->data->nr_locked;
2358 perf_mmap_data_release(event);
2359 mutex_unlock(&event->mmap_mutex);
2363 static const struct vm_operations_struct perf_mmap_vmops = {
2364 .open = perf_mmap_open,
2365 .close = perf_mmap_close,
2366 .fault = perf_mmap_fault,
2367 .page_mkwrite = perf_mmap_fault,
2370 static int perf_mmap(struct file *file, struct vm_area_struct *vma)
2372 struct perf_event *event = file->private_data;
2373 unsigned long user_locked, user_lock_limit;
2374 struct user_struct *user = current_user();
2375 unsigned long locked, lock_limit;
2376 struct perf_mmap_data *data;
2377 unsigned long vma_size;
2378 unsigned long nr_pages;
2379 long user_extra, extra;
2380 int ret = 0;
2382 if (!(vma->vm_flags & VM_SHARED))
2383 return -EINVAL;
2385 vma_size = vma->vm_end - vma->vm_start;
2386 nr_pages = (vma_size / PAGE_SIZE) - 1;
2389 * If we have data pages ensure they're a power-of-two number, so we
2390 * can do bitmasks instead of modulo.
2392 if (nr_pages != 0 && !is_power_of_2(nr_pages))
2393 return -EINVAL;
2395 if (vma_size != PAGE_SIZE * (1 + nr_pages))
2396 return -EINVAL;
2398 if (vma->vm_pgoff != 0)
2399 return -EINVAL;
2401 WARN_ON_ONCE(event->ctx->parent_ctx);
2402 mutex_lock(&event->mmap_mutex);
2403 if (event->output) {
2404 ret = -EINVAL;
2405 goto unlock;
2408 if (atomic_inc_not_zero(&event->mmap_count)) {
2409 if (nr_pages != event->data->nr_pages)
2410 ret = -EINVAL;
2411 goto unlock;
2414 user_extra = nr_pages + 1;
2415 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
2418 * Increase the limit linearly with more CPUs:
2420 user_lock_limit *= num_online_cpus();
2422 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
2424 extra = 0;
2425 if (user_locked > user_lock_limit)
2426 extra = user_locked - user_lock_limit;
2428 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
2429 lock_limit >>= PAGE_SHIFT;
2430 locked = vma->vm_mm->locked_vm + extra;
2432 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
2433 !capable(CAP_IPC_LOCK)) {
2434 ret = -EPERM;
2435 goto unlock;
2438 WARN_ON(event->data);
2440 data = perf_mmap_data_alloc(event, nr_pages);
2441 ret = -ENOMEM;
2442 if (!data)
2443 goto unlock;
2445 ret = 0;
2446 perf_mmap_data_init(event, data);
2448 atomic_set(&event->mmap_count, 1);
2449 atomic_long_add(user_extra, &user->locked_vm);
2450 vma->vm_mm->locked_vm += extra;
2451 event->data->nr_locked = extra;
2452 if (vma->vm_flags & VM_WRITE)
2453 event->data->writable = 1;
2455 unlock:
2456 mutex_unlock(&event->mmap_mutex);
2458 vma->vm_flags |= VM_RESERVED;
2459 vma->vm_ops = &perf_mmap_vmops;
2461 return ret;
2464 static int perf_fasync(int fd, struct file *filp, int on)
2466 struct inode *inode = filp->f_path.dentry->d_inode;
2467 struct perf_event *event = filp->private_data;
2468 int retval;
2470 mutex_lock(&inode->i_mutex);
2471 retval = fasync_helper(fd, filp, on, &event->fasync);
2472 mutex_unlock(&inode->i_mutex);
2474 if (retval < 0)
2475 return retval;
2477 return 0;
2480 static const struct file_operations perf_fops = {
2481 .release = perf_release,
2482 .read = perf_read,
2483 .poll = perf_poll,
2484 .unlocked_ioctl = perf_ioctl,
2485 .compat_ioctl = perf_ioctl,
2486 .mmap = perf_mmap,
2487 .fasync = perf_fasync,
2491 * Perf event wakeup
2493 * If there's data, ensure we set the poll() state and publish everything
2494 * to user-space before waking everybody up.
2497 void perf_event_wakeup(struct perf_event *event)
2499 wake_up_all(&event->waitq);
2501 if (event->pending_kill) {
2502 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
2503 event->pending_kill = 0;
2508 * Pending wakeups
2510 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
2512 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
2513 * single linked list and use cmpxchg() to add entries lockless.
2516 static void perf_pending_event(struct perf_pending_entry *entry)
2518 struct perf_event *event = container_of(entry,
2519 struct perf_event, pending);
2521 if (event->pending_disable) {
2522 event->pending_disable = 0;
2523 __perf_event_disable(event);
2526 if (event->pending_wakeup) {
2527 event->pending_wakeup = 0;
2528 perf_event_wakeup(event);
2532 #define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
2534 static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
2535 PENDING_TAIL,
2538 static void perf_pending_queue(struct perf_pending_entry *entry,
2539 void (*func)(struct perf_pending_entry *))
2541 struct perf_pending_entry **head;
2543 if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
2544 return;
2546 entry->func = func;
2548 head = &get_cpu_var(perf_pending_head);
2550 do {
2551 entry->next = *head;
2552 } while (cmpxchg(head, entry->next, entry) != entry->next);
2554 set_perf_event_pending();
2556 put_cpu_var(perf_pending_head);
2559 static int __perf_pending_run(void)
2561 struct perf_pending_entry *list;
2562 int nr = 0;
2564 list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
2565 while (list != PENDING_TAIL) {
2566 void (*func)(struct perf_pending_entry *);
2567 struct perf_pending_entry *entry = list;
2569 list = list->next;
2571 func = entry->func;
2572 entry->next = NULL;
2574 * Ensure we observe the unqueue before we issue the wakeup,
2575 * so that we won't be waiting forever.
2576 * -- see perf_not_pending().
2578 smp_wmb();
2580 func(entry);
2581 nr++;
2584 return nr;
2587 static inline int perf_not_pending(struct perf_event *event)
2590 * If we flush on whatever cpu we run, there is a chance we don't
2591 * need to wait.
2593 get_cpu();
2594 __perf_pending_run();
2595 put_cpu();
2598 * Ensure we see the proper queue state before going to sleep
2599 * so that we do not miss the wakeup. -- see perf_pending_handle()
2601 smp_rmb();
2602 return event->pending.next == NULL;
2605 static void perf_pending_sync(struct perf_event *event)
2607 wait_event(event->waitq, perf_not_pending(event));
2610 void perf_event_do_pending(void)
2612 __perf_pending_run();
2616 * Callchain support -- arch specific
2619 __weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
2621 return NULL;
2625 * Output
2627 static bool perf_output_space(struct perf_mmap_data *data, unsigned long tail,
2628 unsigned long offset, unsigned long head)
2630 unsigned long mask;
2632 if (!data->writable)
2633 return true;
2635 mask = perf_data_size(data) - 1;
2637 offset = (offset - tail) & mask;
2638 head = (head - tail) & mask;
2640 if ((int)(head - offset) < 0)
2641 return false;
2643 return true;
2646 static void perf_output_wakeup(struct perf_output_handle *handle)
2648 atomic_set(&handle->data->poll, POLL_IN);
2650 if (handle->nmi) {
2651 handle->event->pending_wakeup = 1;
2652 perf_pending_queue(&handle->event->pending,
2653 perf_pending_event);
2654 } else
2655 perf_event_wakeup(handle->event);
2659 * Curious locking construct.
2661 * We need to ensure a later event_id doesn't publish a head when a former
2662 * event_id isn't done writing. However since we need to deal with NMIs we
2663 * cannot fully serialize things.
2665 * What we do is serialize between CPUs so we only have to deal with NMI
2666 * nesting on a single CPU.
2668 * We only publish the head (and generate a wakeup) when the outer-most
2669 * event_id completes.
2671 static void perf_output_lock(struct perf_output_handle *handle)
2673 struct perf_mmap_data *data = handle->data;
2674 int cpu;
2676 handle->locked = 0;
2678 local_irq_save(handle->flags);
2679 cpu = smp_processor_id();
2681 if (in_nmi() && atomic_read(&data->lock) == cpu)
2682 return;
2684 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
2685 cpu_relax();
2687 handle->locked = 1;
2690 static void perf_output_unlock(struct perf_output_handle *handle)
2692 struct perf_mmap_data *data = handle->data;
2693 unsigned long head;
2694 int cpu;
2696 data->done_head = data->head;
2698 if (!handle->locked)
2699 goto out;
2701 again:
2703 * The xchg implies a full barrier that ensures all writes are done
2704 * before we publish the new head, matched by a rmb() in userspace when
2705 * reading this position.
2707 while ((head = atomic_long_xchg(&data->done_head, 0)))
2708 data->user_page->data_head = head;
2711 * NMI can happen here, which means we can miss a done_head update.
2714 cpu = atomic_xchg(&data->lock, -1);
2715 WARN_ON_ONCE(cpu != smp_processor_id());
2718 * Therefore we have to validate we did not indeed do so.
2720 if (unlikely(atomic_long_read(&data->done_head))) {
2722 * Since we had it locked, we can lock it again.
2724 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
2725 cpu_relax();
2727 goto again;
2730 if (atomic_xchg(&data->wakeup, 0))
2731 perf_output_wakeup(handle);
2732 out:
2733 local_irq_restore(handle->flags);
2736 void perf_output_copy(struct perf_output_handle *handle,
2737 const void *buf, unsigned int len)
2739 unsigned int pages_mask;
2740 unsigned long offset;
2741 unsigned int size;
2742 void **pages;
2744 offset = handle->offset;
2745 pages_mask = handle->data->nr_pages - 1;
2746 pages = handle->data->data_pages;
2748 do {
2749 unsigned long page_offset;
2750 unsigned long page_size;
2751 int nr;
2753 nr = (offset >> PAGE_SHIFT) & pages_mask;
2754 page_size = 1UL << (handle->data->data_order + PAGE_SHIFT);
2755 page_offset = offset & (page_size - 1);
2756 size = min_t(unsigned int, page_size - page_offset, len);
2758 memcpy(pages[nr] + page_offset, buf, size);
2760 len -= size;
2761 buf += size;
2762 offset += size;
2763 } while (len);
2765 handle->offset = offset;
2768 * Check we didn't copy past our reservation window, taking the
2769 * possible unsigned int wrap into account.
2771 WARN_ON_ONCE(((long)(handle->head - handle->offset)) < 0);
2774 int perf_output_begin(struct perf_output_handle *handle,
2775 struct perf_event *event, unsigned int size,
2776 int nmi, int sample)
2778 struct perf_event *output_event;
2779 struct perf_mmap_data *data;
2780 unsigned long tail, offset, head;
2781 int have_lost;
2782 struct {
2783 struct perf_event_header header;
2784 u64 id;
2785 u64 lost;
2786 } lost_event;
2788 rcu_read_lock();
2790 * For inherited events we send all the output towards the parent.
2792 if (event->parent)
2793 event = event->parent;
2795 output_event = rcu_dereference(event->output);
2796 if (output_event)
2797 event = output_event;
2799 data = rcu_dereference(event->data);
2800 if (!data)
2801 goto out;
2803 handle->data = data;
2804 handle->event = event;
2805 handle->nmi = nmi;
2806 handle->sample = sample;
2808 if (!data->nr_pages)
2809 goto fail;
2811 have_lost = atomic_read(&data->lost);
2812 if (have_lost)
2813 size += sizeof(lost_event);
2815 perf_output_lock(handle);
2817 do {
2819 * Userspace could choose to issue a mb() before updating the
2820 * tail pointer. So that all reads will be completed before the
2821 * write is issued.
2823 tail = ACCESS_ONCE(data->user_page->data_tail);
2824 smp_rmb();
2825 offset = head = atomic_long_read(&data->head);
2826 head += size;
2827 if (unlikely(!perf_output_space(data, tail, offset, head)))
2828 goto fail;
2829 } while (atomic_long_cmpxchg(&data->head, offset, head) != offset);
2831 handle->offset = offset;
2832 handle->head = head;
2834 if (head - tail > data->watermark)
2835 atomic_set(&data->wakeup, 1);
2837 if (have_lost) {
2838 lost_event.header.type = PERF_RECORD_LOST;
2839 lost_event.header.misc = 0;
2840 lost_event.header.size = sizeof(lost_event);
2841 lost_event.id = event->id;
2842 lost_event.lost = atomic_xchg(&data->lost, 0);
2844 perf_output_put(handle, lost_event);
2847 return 0;
2849 fail:
2850 atomic_inc(&data->lost);
2851 perf_output_unlock(handle);
2852 out:
2853 rcu_read_unlock();
2855 return -ENOSPC;
2858 void perf_output_end(struct perf_output_handle *handle)
2860 struct perf_event *event = handle->event;
2861 struct perf_mmap_data *data = handle->data;
2863 int wakeup_events = event->attr.wakeup_events;
2865 if (handle->sample && wakeup_events) {
2866 int events = atomic_inc_return(&data->events);
2867 if (events >= wakeup_events) {
2868 atomic_sub(wakeup_events, &data->events);
2869 atomic_set(&data->wakeup, 1);
2873 perf_output_unlock(handle);
2874 rcu_read_unlock();
2877 static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
2880 * only top level events have the pid namespace they were created in
2882 if (event->parent)
2883 event = event->parent;
2885 return task_tgid_nr_ns(p, event->ns);
2888 static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
2891 * only top level events have the pid namespace they were created in
2893 if (event->parent)
2894 event = event->parent;
2896 return task_pid_nr_ns(p, event->ns);
2899 static void perf_output_read_one(struct perf_output_handle *handle,
2900 struct perf_event *event)
2902 u64 read_format = event->attr.read_format;
2903 u64 values[4];
2904 int n = 0;
2906 values[n++] = atomic64_read(&event->count);
2907 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
2908 values[n++] = event->total_time_enabled +
2909 atomic64_read(&event->child_total_time_enabled);
2911 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
2912 values[n++] = event->total_time_running +
2913 atomic64_read(&event->child_total_time_running);
2915 if (read_format & PERF_FORMAT_ID)
2916 values[n++] = primary_event_id(event);
2918 perf_output_copy(handle, values, n * sizeof(u64));
2922 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
2924 static void perf_output_read_group(struct perf_output_handle *handle,
2925 struct perf_event *event)
2927 struct perf_event *leader = event->group_leader, *sub;
2928 u64 read_format = event->attr.read_format;
2929 u64 values[5];
2930 int n = 0;
2932 values[n++] = 1 + leader->nr_siblings;
2934 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2935 values[n++] = leader->total_time_enabled;
2937 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2938 values[n++] = leader->total_time_running;
2940 if (leader != event)
2941 leader->pmu->read(leader);
2943 values[n++] = atomic64_read(&leader->count);
2944 if (read_format & PERF_FORMAT_ID)
2945 values[n++] = primary_event_id(leader);
2947 perf_output_copy(handle, values, n * sizeof(u64));
2949 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
2950 n = 0;
2952 if (sub != event)
2953 sub->pmu->read(sub);
2955 values[n++] = atomic64_read(&sub->count);
2956 if (read_format & PERF_FORMAT_ID)
2957 values[n++] = primary_event_id(sub);
2959 perf_output_copy(handle, values, n * sizeof(u64));
2963 static void perf_output_read(struct perf_output_handle *handle,
2964 struct perf_event *event)
2966 if (event->attr.read_format & PERF_FORMAT_GROUP)
2967 perf_output_read_group(handle, event);
2968 else
2969 perf_output_read_one(handle, event);
2972 void perf_output_sample(struct perf_output_handle *handle,
2973 struct perf_event_header *header,
2974 struct perf_sample_data *data,
2975 struct perf_event *event)
2977 u64 sample_type = data->type;
2979 perf_output_put(handle, *header);
2981 if (sample_type & PERF_SAMPLE_IP)
2982 perf_output_put(handle, data->ip);
2984 if (sample_type & PERF_SAMPLE_TID)
2985 perf_output_put(handle, data->tid_entry);
2987 if (sample_type & PERF_SAMPLE_TIME)
2988 perf_output_put(handle, data->time);
2990 if (sample_type & PERF_SAMPLE_ADDR)
2991 perf_output_put(handle, data->addr);
2993 if (sample_type & PERF_SAMPLE_ID)
2994 perf_output_put(handle, data->id);
2996 if (sample_type & PERF_SAMPLE_STREAM_ID)
2997 perf_output_put(handle, data->stream_id);
2999 if (sample_type & PERF_SAMPLE_CPU)
3000 perf_output_put(handle, data->cpu_entry);
3002 if (sample_type & PERF_SAMPLE_PERIOD)
3003 perf_output_put(handle, data->period);
3005 if (sample_type & PERF_SAMPLE_READ)
3006 perf_output_read(handle, event);
3008 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3009 if (data->callchain) {
3010 int size = 1;
3012 if (data->callchain)
3013 size += data->callchain->nr;
3015 size *= sizeof(u64);
3017 perf_output_copy(handle, data->callchain, size);
3018 } else {
3019 u64 nr = 0;
3020 perf_output_put(handle, nr);
3024 if (sample_type & PERF_SAMPLE_RAW) {
3025 if (data->raw) {
3026 perf_output_put(handle, data->raw->size);
3027 perf_output_copy(handle, data->raw->data,
3028 data->raw->size);
3029 } else {
3030 struct {
3031 u32 size;
3032 u32 data;
3033 } raw = {
3034 .size = sizeof(u32),
3035 .data = 0,
3037 perf_output_put(handle, raw);
3042 void perf_prepare_sample(struct perf_event_header *header,
3043 struct perf_sample_data *data,
3044 struct perf_event *event,
3045 struct pt_regs *regs)
3047 u64 sample_type = event->attr.sample_type;
3049 data->type = sample_type;
3051 header->type = PERF_RECORD_SAMPLE;
3052 header->size = sizeof(*header);
3054 header->misc = 0;
3055 header->misc |= perf_misc_flags(regs);
3057 if (sample_type & PERF_SAMPLE_IP) {
3058 data->ip = perf_instruction_pointer(regs);
3060 header->size += sizeof(data->ip);
3063 if (sample_type & PERF_SAMPLE_TID) {
3064 /* namespace issues */
3065 data->tid_entry.pid = perf_event_pid(event, current);
3066 data->tid_entry.tid = perf_event_tid(event, current);
3068 header->size += sizeof(data->tid_entry);
3071 if (sample_type & PERF_SAMPLE_TIME) {
3072 data->time = perf_clock();
3074 header->size += sizeof(data->time);
3077 if (sample_type & PERF_SAMPLE_ADDR)
3078 header->size += sizeof(data->addr);
3080 if (sample_type & PERF_SAMPLE_ID) {
3081 data->id = primary_event_id(event);
3083 header->size += sizeof(data->id);
3086 if (sample_type & PERF_SAMPLE_STREAM_ID) {
3087 data->stream_id = event->id;
3089 header->size += sizeof(data->stream_id);
3092 if (sample_type & PERF_SAMPLE_CPU) {
3093 data->cpu_entry.cpu = raw_smp_processor_id();
3094 data->cpu_entry.reserved = 0;
3096 header->size += sizeof(data->cpu_entry);
3099 if (sample_type & PERF_SAMPLE_PERIOD)
3100 header->size += sizeof(data->period);
3102 if (sample_type & PERF_SAMPLE_READ)
3103 header->size += perf_event_read_size(event);
3105 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3106 int size = 1;
3108 data->callchain = perf_callchain(regs);
3110 if (data->callchain)
3111 size += data->callchain->nr;
3113 header->size += size * sizeof(u64);
3116 if (sample_type & PERF_SAMPLE_RAW) {
3117 int size = sizeof(u32);
3119 if (data->raw)
3120 size += data->raw->size;
3121 else
3122 size += sizeof(u32);
3124 WARN_ON_ONCE(size & (sizeof(u64)-1));
3125 header->size += size;
3129 static void perf_event_output(struct perf_event *event, int nmi,
3130 struct perf_sample_data *data,
3131 struct pt_regs *regs)
3133 struct perf_output_handle handle;
3134 struct perf_event_header header;
3136 perf_prepare_sample(&header, data, event, regs);
3138 if (perf_output_begin(&handle, event, header.size, nmi, 1))
3139 return;
3141 perf_output_sample(&handle, &header, data, event);
3143 perf_output_end(&handle);
3147 * read event_id
3150 struct perf_read_event {
3151 struct perf_event_header header;
3153 u32 pid;
3154 u32 tid;
3157 static void
3158 perf_event_read_event(struct perf_event *event,
3159 struct task_struct *task)
3161 struct perf_output_handle handle;
3162 struct perf_read_event read_event = {
3163 .header = {
3164 .type = PERF_RECORD_READ,
3165 .misc = 0,
3166 .size = sizeof(read_event) + perf_event_read_size(event),
3168 .pid = perf_event_pid(event, task),
3169 .tid = perf_event_tid(event, task),
3171 int ret;
3173 ret = perf_output_begin(&handle, event, read_event.header.size, 0, 0);
3174 if (ret)
3175 return;
3177 perf_output_put(&handle, read_event);
3178 perf_output_read(&handle, event);
3180 perf_output_end(&handle);
3184 * task tracking -- fork/exit
3186 * enabled by: attr.comm | attr.mmap | attr.task
3189 struct perf_task_event {
3190 struct task_struct *task;
3191 struct perf_event_context *task_ctx;
3193 struct {
3194 struct perf_event_header header;
3196 u32 pid;
3197 u32 ppid;
3198 u32 tid;
3199 u32 ptid;
3200 u64 time;
3201 } event_id;
3204 static void perf_event_task_output(struct perf_event *event,
3205 struct perf_task_event *task_event)
3207 struct perf_output_handle handle;
3208 int size;
3209 struct task_struct *task = task_event->task;
3210 int ret;
3212 size = task_event->event_id.header.size;
3213 ret = perf_output_begin(&handle, event, size, 0, 0);
3215 if (ret)
3216 return;
3218 task_event->event_id.pid = perf_event_pid(event, task);
3219 task_event->event_id.ppid = perf_event_pid(event, current);
3221 task_event->event_id.tid = perf_event_tid(event, task);
3222 task_event->event_id.ptid = perf_event_tid(event, current);
3224 task_event->event_id.time = perf_clock();
3226 perf_output_put(&handle, task_event->event_id);
3228 perf_output_end(&handle);
3231 static int perf_event_task_match(struct perf_event *event)
3233 if (event->state != PERF_EVENT_STATE_ACTIVE)
3234 return 0;
3236 if (event->cpu != -1 && event->cpu != smp_processor_id())
3237 return 0;
3239 if (event->attr.comm || event->attr.mmap || event->attr.task)
3240 return 1;
3242 return 0;
3245 static void perf_event_task_ctx(struct perf_event_context *ctx,
3246 struct perf_task_event *task_event)
3248 struct perf_event *event;
3250 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
3251 return;
3253 rcu_read_lock();
3254 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3255 if (perf_event_task_match(event))
3256 perf_event_task_output(event, task_event);
3258 rcu_read_unlock();
3261 static void perf_event_task_event(struct perf_task_event *task_event)
3263 struct perf_cpu_context *cpuctx;
3264 struct perf_event_context *ctx = task_event->task_ctx;
3266 cpuctx = &get_cpu_var(perf_cpu_context);
3267 perf_event_task_ctx(&cpuctx->ctx, task_event);
3269 rcu_read_lock();
3270 if (!ctx)
3271 ctx = rcu_dereference(task_event->task->perf_event_ctxp);
3272 if (ctx)
3273 perf_event_task_ctx(ctx, task_event);
3274 put_cpu_var(perf_cpu_context);
3275 rcu_read_unlock();
3278 static void perf_event_task(struct task_struct *task,
3279 struct perf_event_context *task_ctx,
3280 int new)
3282 struct perf_task_event task_event;
3284 if (!atomic_read(&nr_comm_events) &&
3285 !atomic_read(&nr_mmap_events) &&
3286 !atomic_read(&nr_task_events))
3287 return;
3289 task_event = (struct perf_task_event){
3290 .task = task,
3291 .task_ctx = task_ctx,
3292 .event_id = {
3293 .header = {
3294 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
3295 .misc = 0,
3296 .size = sizeof(task_event.event_id),
3298 /* .pid */
3299 /* .ppid */
3300 /* .tid */
3301 /* .ptid */
3305 perf_event_task_event(&task_event);
3308 void perf_event_fork(struct task_struct *task)
3310 perf_event_task(task, NULL, 1);
3314 * comm tracking
3317 struct perf_comm_event {
3318 struct task_struct *task;
3319 char *comm;
3320 int comm_size;
3322 struct {
3323 struct perf_event_header header;
3325 u32 pid;
3326 u32 tid;
3327 } event_id;
3330 static void perf_event_comm_output(struct perf_event *event,
3331 struct perf_comm_event *comm_event)
3333 struct perf_output_handle handle;
3334 int size = comm_event->event_id.header.size;
3335 int ret = perf_output_begin(&handle, event, size, 0, 0);
3337 if (ret)
3338 return;
3340 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
3341 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
3343 perf_output_put(&handle, comm_event->event_id);
3344 perf_output_copy(&handle, comm_event->comm,
3345 comm_event->comm_size);
3346 perf_output_end(&handle);
3349 static int perf_event_comm_match(struct perf_event *event)
3351 if (event->state != PERF_EVENT_STATE_ACTIVE)
3352 return 0;
3354 if (event->cpu != -1 && event->cpu != smp_processor_id())
3355 return 0;
3357 if (event->attr.comm)
3358 return 1;
3360 return 0;
3363 static void perf_event_comm_ctx(struct perf_event_context *ctx,
3364 struct perf_comm_event *comm_event)
3366 struct perf_event *event;
3368 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
3369 return;
3371 rcu_read_lock();
3372 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3373 if (perf_event_comm_match(event))
3374 perf_event_comm_output(event, comm_event);
3376 rcu_read_unlock();
3379 static void perf_event_comm_event(struct perf_comm_event *comm_event)
3381 struct perf_cpu_context *cpuctx;
3382 struct perf_event_context *ctx;
3383 unsigned int size;
3384 char comm[TASK_COMM_LEN];
3386 memset(comm, 0, sizeof(comm));
3387 strncpy(comm, comm_event->task->comm, sizeof(comm));
3388 size = ALIGN(strlen(comm)+1, sizeof(u64));
3390 comm_event->comm = comm;
3391 comm_event->comm_size = size;
3393 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
3395 cpuctx = &get_cpu_var(perf_cpu_context);
3396 perf_event_comm_ctx(&cpuctx->ctx, comm_event);
3398 rcu_read_lock();
3400 * doesn't really matter which of the child contexts the
3401 * events ends up in.
3403 ctx = rcu_dereference(current->perf_event_ctxp);
3404 if (ctx)
3405 perf_event_comm_ctx(ctx, comm_event);
3406 put_cpu_var(perf_cpu_context);
3407 rcu_read_unlock();
3410 void perf_event_comm(struct task_struct *task)
3412 struct perf_comm_event comm_event;
3414 if (task->perf_event_ctxp)
3415 perf_event_enable_on_exec(task);
3417 if (!atomic_read(&nr_comm_events))
3418 return;
3420 comm_event = (struct perf_comm_event){
3421 .task = task,
3422 /* .comm */
3423 /* .comm_size */
3424 .event_id = {
3425 .header = {
3426 .type = PERF_RECORD_COMM,
3427 .misc = 0,
3428 /* .size */
3430 /* .pid */
3431 /* .tid */
3435 perf_event_comm_event(&comm_event);
3439 * mmap tracking
3442 struct perf_mmap_event {
3443 struct vm_area_struct *vma;
3445 const char *file_name;
3446 int file_size;
3448 struct {
3449 struct perf_event_header header;
3451 u32 pid;
3452 u32 tid;
3453 u64 start;
3454 u64 len;
3455 u64 pgoff;
3456 } event_id;
3459 static void perf_event_mmap_output(struct perf_event *event,
3460 struct perf_mmap_event *mmap_event)
3462 struct perf_output_handle handle;
3463 int size = mmap_event->event_id.header.size;
3464 int ret = perf_output_begin(&handle, event, size, 0, 0);
3466 if (ret)
3467 return;
3469 mmap_event->event_id.pid = perf_event_pid(event, current);
3470 mmap_event->event_id.tid = perf_event_tid(event, current);
3472 perf_output_put(&handle, mmap_event->event_id);
3473 perf_output_copy(&handle, mmap_event->file_name,
3474 mmap_event->file_size);
3475 perf_output_end(&handle);
3478 static int perf_event_mmap_match(struct perf_event *event,
3479 struct perf_mmap_event *mmap_event)
3481 if (event->state != PERF_EVENT_STATE_ACTIVE)
3482 return 0;
3484 if (event->cpu != -1 && event->cpu != smp_processor_id())
3485 return 0;
3487 if (event->attr.mmap)
3488 return 1;
3490 return 0;
3493 static void perf_event_mmap_ctx(struct perf_event_context *ctx,
3494 struct perf_mmap_event *mmap_event)
3496 struct perf_event *event;
3498 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
3499 return;
3501 rcu_read_lock();
3502 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3503 if (perf_event_mmap_match(event, mmap_event))
3504 perf_event_mmap_output(event, mmap_event);
3506 rcu_read_unlock();
3509 static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
3511 struct perf_cpu_context *cpuctx;
3512 struct perf_event_context *ctx;
3513 struct vm_area_struct *vma = mmap_event->vma;
3514 struct file *file = vma->vm_file;
3515 unsigned int size;
3516 char tmp[16];
3517 char *buf = NULL;
3518 const char *name;
3520 memset(tmp, 0, sizeof(tmp));
3522 if (file) {
3524 * d_path works from the end of the buffer backwards, so we
3525 * need to add enough zero bytes after the string to handle
3526 * the 64bit alignment we do later.
3528 buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
3529 if (!buf) {
3530 name = strncpy(tmp, "//enomem", sizeof(tmp));
3531 goto got_name;
3533 name = d_path(&file->f_path, buf, PATH_MAX);
3534 if (IS_ERR(name)) {
3535 name = strncpy(tmp, "//toolong", sizeof(tmp));
3536 goto got_name;
3538 } else {
3539 if (arch_vma_name(mmap_event->vma)) {
3540 name = strncpy(tmp, arch_vma_name(mmap_event->vma),
3541 sizeof(tmp));
3542 goto got_name;
3545 if (!vma->vm_mm) {
3546 name = strncpy(tmp, "[vdso]", sizeof(tmp));
3547 goto got_name;
3550 name = strncpy(tmp, "//anon", sizeof(tmp));
3551 goto got_name;
3554 got_name:
3555 size = ALIGN(strlen(name)+1, sizeof(u64));
3557 mmap_event->file_name = name;
3558 mmap_event->file_size = size;
3560 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
3562 cpuctx = &get_cpu_var(perf_cpu_context);
3563 perf_event_mmap_ctx(&cpuctx->ctx, mmap_event);
3565 rcu_read_lock();
3567 * doesn't really matter which of the child contexts the
3568 * events ends up in.
3570 ctx = rcu_dereference(current->perf_event_ctxp);
3571 if (ctx)
3572 perf_event_mmap_ctx(ctx, mmap_event);
3573 put_cpu_var(perf_cpu_context);
3574 rcu_read_unlock();
3576 kfree(buf);
3579 void __perf_event_mmap(struct vm_area_struct *vma)
3581 struct perf_mmap_event mmap_event;
3583 if (!atomic_read(&nr_mmap_events))
3584 return;
3586 mmap_event = (struct perf_mmap_event){
3587 .vma = vma,
3588 /* .file_name */
3589 /* .file_size */
3590 .event_id = {
3591 .header = {
3592 .type = PERF_RECORD_MMAP,
3593 .misc = 0,
3594 /* .size */
3596 /* .pid */
3597 /* .tid */
3598 .start = vma->vm_start,
3599 .len = vma->vm_end - vma->vm_start,
3600 .pgoff = vma->vm_pgoff,
3604 perf_event_mmap_event(&mmap_event);
3608 * IRQ throttle logging
3611 static void perf_log_throttle(struct perf_event *event, int enable)
3613 struct perf_output_handle handle;
3614 int ret;
3616 struct {
3617 struct perf_event_header header;
3618 u64 time;
3619 u64 id;
3620 u64 stream_id;
3621 } throttle_event = {
3622 .header = {
3623 .type = PERF_RECORD_THROTTLE,
3624 .misc = 0,
3625 .size = sizeof(throttle_event),
3627 .time = perf_clock(),
3628 .id = primary_event_id(event),
3629 .stream_id = event->id,
3632 if (enable)
3633 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
3635 ret = perf_output_begin(&handle, event, sizeof(throttle_event), 1, 0);
3636 if (ret)
3637 return;
3639 perf_output_put(&handle, throttle_event);
3640 perf_output_end(&handle);
3644 * Generic event overflow handling, sampling.
3647 static int __perf_event_overflow(struct perf_event *event, int nmi,
3648 int throttle, struct perf_sample_data *data,
3649 struct pt_regs *regs)
3651 int events = atomic_read(&event->event_limit);
3652 struct hw_perf_event *hwc = &event->hw;
3653 int ret = 0;
3655 throttle = (throttle && event->pmu->unthrottle != NULL);
3657 if (!throttle) {
3658 hwc->interrupts++;
3659 } else {
3660 if (hwc->interrupts != MAX_INTERRUPTS) {
3661 hwc->interrupts++;
3662 if (HZ * hwc->interrupts >
3663 (u64)sysctl_perf_event_sample_rate) {
3664 hwc->interrupts = MAX_INTERRUPTS;
3665 perf_log_throttle(event, 0);
3666 ret = 1;
3668 } else {
3670 * Keep re-disabling events even though on the previous
3671 * pass we disabled it - just in case we raced with a
3672 * sched-in and the event got enabled again:
3674 ret = 1;
3678 if (event->attr.freq) {
3679 u64 now = perf_clock();
3680 s64 delta = now - hwc->freq_stamp;
3682 hwc->freq_stamp = now;
3684 if (delta > 0 && delta < TICK_NSEC)
3685 perf_adjust_period(event, NSEC_PER_SEC / (int)delta);
3689 * XXX event_limit might not quite work as expected on inherited
3690 * events
3693 event->pending_kill = POLL_IN;
3694 if (events && atomic_dec_and_test(&event->event_limit)) {
3695 ret = 1;
3696 event->pending_kill = POLL_HUP;
3697 event->pending_disable = 1;
3698 perf_pending_queue(&event->pending, perf_pending_event);
3701 perf_event_output(event, nmi, data, regs);
3702 return ret;
3705 int perf_event_overflow(struct perf_event *event, int nmi,
3706 struct perf_sample_data *data,
3707 struct pt_regs *regs)
3709 return __perf_event_overflow(event, nmi, 1, data, regs);
3713 * Generic software event infrastructure
3717 * We directly increment event->count and keep a second value in
3718 * event->hw.period_left to count intervals. This period event
3719 * is kept in the range [-sample_period, 0] so that we can use the
3720 * sign as trigger.
3723 static u64 perf_swevent_set_period(struct perf_event *event)
3725 struct hw_perf_event *hwc = &event->hw;
3726 u64 period = hwc->last_period;
3727 u64 nr, offset;
3728 s64 old, val;
3730 hwc->last_period = hwc->sample_period;
3732 again:
3733 old = val = atomic64_read(&hwc->period_left);
3734 if (val < 0)
3735 return 0;
3737 nr = div64_u64(period + val, period);
3738 offset = nr * period;
3739 val -= offset;
3740 if (atomic64_cmpxchg(&hwc->period_left, old, val) != old)
3741 goto again;
3743 return nr;
3746 static void perf_swevent_overflow(struct perf_event *event,
3747 int nmi, struct perf_sample_data *data,
3748 struct pt_regs *regs)
3750 struct hw_perf_event *hwc = &event->hw;
3751 int throttle = 0;
3752 u64 overflow;
3754 data->period = event->hw.last_period;
3755 overflow = perf_swevent_set_period(event);
3757 if (hwc->interrupts == MAX_INTERRUPTS)
3758 return;
3760 for (; overflow; overflow--) {
3761 if (__perf_event_overflow(event, nmi, throttle,
3762 data, regs)) {
3764 * We inhibit the overflow from happening when
3765 * hwc->interrupts == MAX_INTERRUPTS.
3767 break;
3769 throttle = 1;
3773 static void perf_swevent_unthrottle(struct perf_event *event)
3776 * Nothing to do, we already reset hwc->interrupts.
3780 static void perf_swevent_add(struct perf_event *event, u64 nr,
3781 int nmi, struct perf_sample_data *data,
3782 struct pt_regs *regs)
3784 struct hw_perf_event *hwc = &event->hw;
3786 atomic64_add(nr, &event->count);
3788 if (!hwc->sample_period)
3789 return;
3791 if (!regs)
3792 return;
3794 if (!atomic64_add_negative(nr, &hwc->period_left))
3795 perf_swevent_overflow(event, nmi, data, regs);
3798 static int perf_swevent_is_counting(struct perf_event *event)
3801 * The event is active, we're good!
3803 if (event->state == PERF_EVENT_STATE_ACTIVE)
3804 return 1;
3807 * The event is off/error, not counting.
3809 if (event->state != PERF_EVENT_STATE_INACTIVE)
3810 return 0;
3813 * The event is inactive, if the context is active
3814 * we're part of a group that didn't make it on the 'pmu',
3815 * not counting.
3817 if (event->ctx->is_active)
3818 return 0;
3821 * We're inactive and the context is too, this means the
3822 * task is scheduled out, we're counting events that happen
3823 * to us, like migration events.
3825 return 1;
3828 static int perf_swevent_match(struct perf_event *event,
3829 enum perf_type_id type,
3830 u32 event_id, struct pt_regs *regs)
3832 if (event->cpu != -1 && event->cpu != smp_processor_id())
3833 return 0;
3835 if (!perf_swevent_is_counting(event))
3836 return 0;
3838 if (event->attr.type != type)
3839 return 0;
3840 if (event->attr.config != event_id)
3841 return 0;
3843 if (regs) {
3844 if (event->attr.exclude_user && user_mode(regs))
3845 return 0;
3847 if (event->attr.exclude_kernel && !user_mode(regs))
3848 return 0;
3851 return 1;
3854 static void perf_swevent_ctx_event(struct perf_event_context *ctx,
3855 enum perf_type_id type,
3856 u32 event_id, u64 nr, int nmi,
3857 struct perf_sample_data *data,
3858 struct pt_regs *regs)
3860 struct perf_event *event;
3862 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
3863 return;
3865 rcu_read_lock();
3866 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3867 if (perf_swevent_match(event, type, event_id, regs))
3868 perf_swevent_add(event, nr, nmi, data, regs);
3870 rcu_read_unlock();
3873 static int *perf_swevent_recursion_context(struct perf_cpu_context *cpuctx)
3875 if (in_nmi())
3876 return &cpuctx->recursion[3];
3878 if (in_irq())
3879 return &cpuctx->recursion[2];
3881 if (in_softirq())
3882 return &cpuctx->recursion[1];
3884 return &cpuctx->recursion[0];
3887 static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
3888 u64 nr, int nmi,
3889 struct perf_sample_data *data,
3890 struct pt_regs *regs)
3892 struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
3893 int *recursion = perf_swevent_recursion_context(cpuctx);
3894 struct perf_event_context *ctx;
3896 if (*recursion)
3897 goto out;
3899 (*recursion)++;
3900 barrier();
3902 perf_swevent_ctx_event(&cpuctx->ctx, type, event_id,
3903 nr, nmi, data, regs);
3904 rcu_read_lock();
3906 * doesn't really matter which of the child contexts the
3907 * events ends up in.
3909 ctx = rcu_dereference(current->perf_event_ctxp);
3910 if (ctx)
3911 perf_swevent_ctx_event(ctx, type, event_id, nr, nmi, data, regs);
3912 rcu_read_unlock();
3914 barrier();
3915 (*recursion)--;
3917 out:
3918 put_cpu_var(perf_cpu_context);
3921 void __perf_sw_event(u32 event_id, u64 nr, int nmi,
3922 struct pt_regs *regs, u64 addr)
3924 struct perf_sample_data data = {
3925 .addr = addr,
3928 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, nmi,
3929 &data, regs);
3932 static void perf_swevent_read(struct perf_event *event)
3936 static int perf_swevent_enable(struct perf_event *event)
3938 struct hw_perf_event *hwc = &event->hw;
3940 if (hwc->sample_period) {
3941 hwc->last_period = hwc->sample_period;
3942 perf_swevent_set_period(event);
3944 return 0;
3947 static void perf_swevent_disable(struct perf_event *event)
3951 static const struct pmu perf_ops_generic = {
3952 .enable = perf_swevent_enable,
3953 .disable = perf_swevent_disable,
3954 .read = perf_swevent_read,
3955 .unthrottle = perf_swevent_unthrottle,
3959 * hrtimer based swevent callback
3962 static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
3964 enum hrtimer_restart ret = HRTIMER_RESTART;
3965 struct perf_sample_data data;
3966 struct pt_regs *regs;
3967 struct perf_event *event;
3968 u64 period;
3970 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
3971 event->pmu->read(event);
3973 data.addr = 0;
3974 data.period = event->hw.last_period;
3975 regs = get_irq_regs();
3977 * In case we exclude kernel IPs or are somehow not in interrupt
3978 * context, provide the next best thing, the user IP.
3980 if ((event->attr.exclude_kernel || !regs) &&
3981 !event->attr.exclude_user)
3982 regs = task_pt_regs(current);
3984 if (regs) {
3985 if (!(event->attr.exclude_idle && current->pid == 0))
3986 if (perf_event_overflow(event, 0, &data, regs))
3987 ret = HRTIMER_NORESTART;
3990 period = max_t(u64, 10000, event->hw.sample_period);
3991 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
3993 return ret;
3996 static void perf_swevent_start_hrtimer(struct perf_event *event)
3998 struct hw_perf_event *hwc = &event->hw;
4000 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
4001 hwc->hrtimer.function = perf_swevent_hrtimer;
4002 if (hwc->sample_period) {
4003 u64 period;
4005 if (hwc->remaining) {
4006 if (hwc->remaining < 0)
4007 period = 10000;
4008 else
4009 period = hwc->remaining;
4010 hwc->remaining = 0;
4011 } else {
4012 period = max_t(u64, 10000, hwc->sample_period);
4014 __hrtimer_start_range_ns(&hwc->hrtimer,
4015 ns_to_ktime(period), 0,
4016 HRTIMER_MODE_REL, 0);
4020 static void perf_swevent_cancel_hrtimer(struct perf_event *event)
4022 struct hw_perf_event *hwc = &event->hw;
4024 if (hwc->sample_period) {
4025 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
4026 hwc->remaining = ktime_to_ns(remaining);
4028 hrtimer_cancel(&hwc->hrtimer);
4033 * Software event: cpu wall time clock
4036 static void cpu_clock_perf_event_update(struct perf_event *event)
4038 int cpu = raw_smp_processor_id();
4039 s64 prev;
4040 u64 now;
4042 now = cpu_clock(cpu);
4043 prev = atomic64_read(&event->hw.prev_count);
4044 atomic64_set(&event->hw.prev_count, now);
4045 atomic64_add(now - prev, &event->count);
4048 static int cpu_clock_perf_event_enable(struct perf_event *event)
4050 struct hw_perf_event *hwc = &event->hw;
4051 int cpu = raw_smp_processor_id();
4053 atomic64_set(&hwc->prev_count, cpu_clock(cpu));
4054 perf_swevent_start_hrtimer(event);
4056 return 0;
4059 static void cpu_clock_perf_event_disable(struct perf_event *event)
4061 perf_swevent_cancel_hrtimer(event);
4062 cpu_clock_perf_event_update(event);
4065 static void cpu_clock_perf_event_read(struct perf_event *event)
4067 cpu_clock_perf_event_update(event);
4070 static const struct pmu perf_ops_cpu_clock = {
4071 .enable = cpu_clock_perf_event_enable,
4072 .disable = cpu_clock_perf_event_disable,
4073 .read = cpu_clock_perf_event_read,
4077 * Software event: task time clock
4080 static void task_clock_perf_event_update(struct perf_event *event, u64 now)
4082 u64 prev;
4083 s64 delta;
4085 prev = atomic64_xchg(&event->hw.prev_count, now);
4086 delta = now - prev;
4087 atomic64_add(delta, &event->count);
4090 static int task_clock_perf_event_enable(struct perf_event *event)
4092 struct hw_perf_event *hwc = &event->hw;
4093 u64 now;
4095 now = event->ctx->time;
4097 atomic64_set(&hwc->prev_count, now);
4099 perf_swevent_start_hrtimer(event);
4101 return 0;
4104 static void task_clock_perf_event_disable(struct perf_event *event)
4106 perf_swevent_cancel_hrtimer(event);
4107 task_clock_perf_event_update(event, event->ctx->time);
4111 static void task_clock_perf_event_read(struct perf_event *event)
4113 u64 time;
4115 if (!in_nmi()) {
4116 update_context_time(event->ctx);
4117 time = event->ctx->time;
4118 } else {
4119 u64 now = perf_clock();
4120 u64 delta = now - event->ctx->timestamp;
4121 time = event->ctx->time + delta;
4124 task_clock_perf_event_update(event, time);
4127 static const struct pmu perf_ops_task_clock = {
4128 .enable = task_clock_perf_event_enable,
4129 .disable = task_clock_perf_event_disable,
4130 .read = task_clock_perf_event_read,
4133 #ifdef CONFIG_EVENT_PROFILE
4134 void perf_tp_event(int event_id, u64 addr, u64 count, void *record,
4135 int entry_size)
4137 struct perf_raw_record raw = {
4138 .size = entry_size,
4139 .data = record,
4142 struct perf_sample_data data = {
4143 .addr = addr,
4144 .raw = &raw,
4147 struct pt_regs *regs = get_irq_regs();
4149 if (!regs)
4150 regs = task_pt_regs(current);
4152 do_perf_sw_event(PERF_TYPE_TRACEPOINT, event_id, count, 1,
4153 &data, regs);
4155 EXPORT_SYMBOL_GPL(perf_tp_event);
4157 extern int ftrace_profile_enable(int);
4158 extern void ftrace_profile_disable(int);
4160 static void tp_perf_event_destroy(struct perf_event *event)
4162 ftrace_profile_disable(event->attr.config);
4165 static const struct pmu *tp_perf_event_init(struct perf_event *event)
4168 * Raw tracepoint data is a severe data leak, only allow root to
4169 * have these.
4171 if ((event->attr.sample_type & PERF_SAMPLE_RAW) &&
4172 perf_paranoid_tracepoint_raw() &&
4173 !capable(CAP_SYS_ADMIN))
4174 return ERR_PTR(-EPERM);
4176 if (ftrace_profile_enable(event->attr.config))
4177 return NULL;
4179 event->destroy = tp_perf_event_destroy;
4181 return &perf_ops_generic;
4183 #else
4184 static const struct pmu *tp_perf_event_init(struct perf_event *event)
4186 return NULL;
4188 #endif
4190 atomic_t perf_swevent_enabled[PERF_COUNT_SW_MAX];
4192 static void sw_perf_event_destroy(struct perf_event *event)
4194 u64 event_id = event->attr.config;
4196 WARN_ON(event->parent);
4198 atomic_dec(&perf_swevent_enabled[event_id]);
4201 static const struct pmu *sw_perf_event_init(struct perf_event *event)
4203 const struct pmu *pmu = NULL;
4204 u64 event_id = event->attr.config;
4207 * Software events (currently) can't in general distinguish
4208 * between user, kernel and hypervisor events.
4209 * However, context switches and cpu migrations are considered
4210 * to be kernel events, and page faults are never hypervisor
4211 * events.
4213 switch (event_id) {
4214 case PERF_COUNT_SW_CPU_CLOCK:
4215 pmu = &perf_ops_cpu_clock;
4217 break;
4218 case PERF_COUNT_SW_TASK_CLOCK:
4220 * If the user instantiates this as a per-cpu event,
4221 * use the cpu_clock event instead.
4223 if (event->ctx->task)
4224 pmu = &perf_ops_task_clock;
4225 else
4226 pmu = &perf_ops_cpu_clock;
4228 break;
4229 case PERF_COUNT_SW_PAGE_FAULTS:
4230 case PERF_COUNT_SW_PAGE_FAULTS_MIN:
4231 case PERF_COUNT_SW_PAGE_FAULTS_MAJ:
4232 case PERF_COUNT_SW_CONTEXT_SWITCHES:
4233 case PERF_COUNT_SW_CPU_MIGRATIONS:
4234 if (!event->parent) {
4235 atomic_inc(&perf_swevent_enabled[event_id]);
4236 event->destroy = sw_perf_event_destroy;
4238 pmu = &perf_ops_generic;
4239 break;
4242 return pmu;
4246 * Allocate and initialize a event structure
4248 static struct perf_event *
4249 perf_event_alloc(struct perf_event_attr *attr,
4250 int cpu,
4251 struct perf_event_context *ctx,
4252 struct perf_event *group_leader,
4253 struct perf_event *parent_event,
4254 gfp_t gfpflags)
4256 const struct pmu *pmu;
4257 struct perf_event *event;
4258 struct hw_perf_event *hwc;
4259 long err;
4261 event = kzalloc(sizeof(*event), gfpflags);
4262 if (!event)
4263 return ERR_PTR(-ENOMEM);
4266 * Single events are their own group leaders, with an
4267 * empty sibling list:
4269 if (!group_leader)
4270 group_leader = event;
4272 mutex_init(&event->child_mutex);
4273 INIT_LIST_HEAD(&event->child_list);
4275 INIT_LIST_HEAD(&event->group_entry);
4276 INIT_LIST_HEAD(&event->event_entry);
4277 INIT_LIST_HEAD(&event->sibling_list);
4278 init_waitqueue_head(&event->waitq);
4280 mutex_init(&event->mmap_mutex);
4282 event->cpu = cpu;
4283 event->attr = *attr;
4284 event->group_leader = group_leader;
4285 event->pmu = NULL;
4286 event->ctx = ctx;
4287 event->oncpu = -1;
4289 event->parent = parent_event;
4291 event->ns = get_pid_ns(current->nsproxy->pid_ns);
4292 event->id = atomic64_inc_return(&perf_event_id);
4294 event->state = PERF_EVENT_STATE_INACTIVE;
4296 if (attr->disabled)
4297 event->state = PERF_EVENT_STATE_OFF;
4299 pmu = NULL;
4301 hwc = &event->hw;
4302 hwc->sample_period = attr->sample_period;
4303 if (attr->freq && attr->sample_freq)
4304 hwc->sample_period = 1;
4305 hwc->last_period = hwc->sample_period;
4307 atomic64_set(&hwc->period_left, hwc->sample_period);
4310 * we currently do not support PERF_FORMAT_GROUP on inherited events
4312 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
4313 goto done;
4315 switch (attr->type) {
4316 case PERF_TYPE_RAW:
4317 case PERF_TYPE_HARDWARE:
4318 case PERF_TYPE_HW_CACHE:
4319 pmu = hw_perf_event_init(event);
4320 break;
4322 case PERF_TYPE_SOFTWARE:
4323 pmu = sw_perf_event_init(event);
4324 break;
4326 case PERF_TYPE_TRACEPOINT:
4327 pmu = tp_perf_event_init(event);
4328 break;
4330 default:
4331 break;
4333 done:
4334 err = 0;
4335 if (!pmu)
4336 err = -EINVAL;
4337 else if (IS_ERR(pmu))
4338 err = PTR_ERR(pmu);
4340 if (err) {
4341 if (event->ns)
4342 put_pid_ns(event->ns);
4343 kfree(event);
4344 return ERR_PTR(err);
4347 event->pmu = pmu;
4349 if (!event->parent) {
4350 atomic_inc(&nr_events);
4351 if (event->attr.mmap)
4352 atomic_inc(&nr_mmap_events);
4353 if (event->attr.comm)
4354 atomic_inc(&nr_comm_events);
4355 if (event->attr.task)
4356 atomic_inc(&nr_task_events);
4359 return event;
4362 static int perf_copy_attr(struct perf_event_attr __user *uattr,
4363 struct perf_event_attr *attr)
4365 u32 size;
4366 int ret;
4368 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
4369 return -EFAULT;
4372 * zero the full structure, so that a short copy will be nice.
4374 memset(attr, 0, sizeof(*attr));
4376 ret = get_user(size, &uattr->size);
4377 if (ret)
4378 return ret;
4380 if (size > PAGE_SIZE) /* silly large */
4381 goto err_size;
4383 if (!size) /* abi compat */
4384 size = PERF_ATTR_SIZE_VER0;
4386 if (size < PERF_ATTR_SIZE_VER0)
4387 goto err_size;
4390 * If we're handed a bigger struct than we know of,
4391 * ensure all the unknown bits are 0 - i.e. new
4392 * user-space does not rely on any kernel feature
4393 * extensions we dont know about yet.
4395 if (size > sizeof(*attr)) {
4396 unsigned char __user *addr;
4397 unsigned char __user *end;
4398 unsigned char val;
4400 addr = (void __user *)uattr + sizeof(*attr);
4401 end = (void __user *)uattr + size;
4403 for (; addr < end; addr++) {
4404 ret = get_user(val, addr);
4405 if (ret)
4406 return ret;
4407 if (val)
4408 goto err_size;
4410 size = sizeof(*attr);
4413 ret = copy_from_user(attr, uattr, size);
4414 if (ret)
4415 return -EFAULT;
4418 * If the type exists, the corresponding creation will verify
4419 * the attr->config.
4421 if (attr->type >= PERF_TYPE_MAX)
4422 return -EINVAL;
4424 if (attr->__reserved_1 || attr->__reserved_2 || attr->__reserved_3)
4425 return -EINVAL;
4427 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
4428 return -EINVAL;
4430 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
4431 return -EINVAL;
4433 out:
4434 return ret;
4436 err_size:
4437 put_user(sizeof(*attr), &uattr->size);
4438 ret = -E2BIG;
4439 goto out;
4442 int perf_event_set_output(struct perf_event *event, int output_fd)
4444 struct perf_event *output_event = NULL;
4445 struct file *output_file = NULL;
4446 struct perf_event *old_output;
4447 int fput_needed = 0;
4448 int ret = -EINVAL;
4450 if (!output_fd)
4451 goto set;
4453 output_file = fget_light(output_fd, &fput_needed);
4454 if (!output_file)
4455 return -EBADF;
4457 if (output_file->f_op != &perf_fops)
4458 goto out;
4460 output_event = output_file->private_data;
4462 /* Don't chain output fds */
4463 if (output_event->output)
4464 goto out;
4466 /* Don't set an output fd when we already have an output channel */
4467 if (event->data)
4468 goto out;
4470 atomic_long_inc(&output_file->f_count);
4472 set:
4473 mutex_lock(&event->mmap_mutex);
4474 old_output = event->output;
4475 rcu_assign_pointer(event->output, output_event);
4476 mutex_unlock(&event->mmap_mutex);
4478 if (old_output) {
4480 * we need to make sure no existing perf_output_*()
4481 * is still referencing this event.
4483 synchronize_rcu();
4484 fput(old_output->filp);
4487 ret = 0;
4488 out:
4489 fput_light(output_file, fput_needed);
4490 return ret;
4494 * sys_perf_event_open - open a performance event, associate it to a task/cpu
4496 * @attr_uptr: event_id type attributes for monitoring/sampling
4497 * @pid: target pid
4498 * @cpu: target cpu
4499 * @group_fd: group leader event fd
4501 SYSCALL_DEFINE5(perf_event_open,
4502 struct perf_event_attr __user *, attr_uptr,
4503 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
4505 struct perf_event *event, *group_leader;
4506 struct perf_event_attr attr;
4507 struct perf_event_context *ctx;
4508 struct file *event_file = NULL;
4509 struct file *group_file = NULL;
4510 int event_fd;
4511 int fput_needed = 0;
4512 int err;
4514 /* for future expandability... */
4515 if (flags & ~(PERF_FLAG_FD_NO_GROUP | PERF_FLAG_FD_OUTPUT))
4516 return -EINVAL;
4518 err = perf_copy_attr(attr_uptr, &attr);
4519 if (err)
4520 return err;
4522 if (!attr.exclude_kernel) {
4523 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
4524 return -EACCES;
4527 if (attr.freq) {
4528 if (attr.sample_freq > sysctl_perf_event_sample_rate)
4529 return -EINVAL;
4532 event_fd = get_unused_fd_flags(O_RDWR);
4533 if (event_fd < 0)
4534 return event_fd;
4537 * Get the target context (task or percpu):
4539 ctx = find_get_context(pid, cpu);
4540 if (IS_ERR(ctx)) {
4541 err = PTR_ERR(ctx);
4542 goto err_fd;
4546 * Look up the group leader (we will attach this event to it):
4548 group_leader = NULL;
4549 if (group_fd != -1 && !(flags & PERF_FLAG_FD_NO_GROUP)) {
4550 err = -EINVAL;
4551 group_file = fget_light(group_fd, &fput_needed);
4552 if (!group_file)
4553 goto err_put_context;
4554 if (group_file->f_op != &perf_fops)
4555 goto err_put_context;
4557 group_leader = group_file->private_data;
4559 * Do not allow a recursive hierarchy (this new sibling
4560 * becoming part of another group-sibling):
4562 if (group_leader->group_leader != group_leader)
4563 goto err_put_context;
4565 * Do not allow to attach to a group in a different
4566 * task or CPU context:
4568 if (group_leader->ctx != ctx)
4569 goto err_put_context;
4571 * Only a group leader can be exclusive or pinned
4573 if (attr.exclusive || attr.pinned)
4574 goto err_put_context;
4577 event = perf_event_alloc(&attr, cpu, ctx, group_leader,
4578 NULL, GFP_KERNEL);
4579 err = PTR_ERR(event);
4580 if (IS_ERR(event))
4581 goto err_put_context;
4583 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, O_RDWR);
4584 if (IS_ERR(event_file)) {
4585 err = PTR_ERR(event_file);
4586 goto err_free_put_context;
4589 if (flags & PERF_FLAG_FD_OUTPUT) {
4590 err = perf_event_set_output(event, group_fd);
4591 if (err)
4592 goto err_fput_free_put_context;
4595 event->filp = event_file;
4596 WARN_ON_ONCE(ctx->parent_ctx);
4597 mutex_lock(&ctx->mutex);
4598 perf_install_in_context(ctx, event, cpu);
4599 ++ctx->generation;
4600 mutex_unlock(&ctx->mutex);
4602 event->owner = current;
4603 get_task_struct(current);
4604 mutex_lock(&current->perf_event_mutex);
4605 list_add_tail(&event->owner_entry, &current->perf_event_list);
4606 mutex_unlock(&current->perf_event_mutex);
4608 fput_light(group_file, fput_needed);
4609 fd_install(event_fd, event_file);
4610 return event_fd;
4612 err_fput_free_put_context:
4613 fput(event_file);
4614 err_free_put_context:
4615 free_event(event);
4616 err_put_context:
4617 fput_light(group_file, fput_needed);
4618 put_ctx(ctx);
4619 err_fd:
4620 put_unused_fd(event_fd);
4621 return err;
4625 * inherit a event from parent task to child task:
4627 static struct perf_event *
4628 inherit_event(struct perf_event *parent_event,
4629 struct task_struct *parent,
4630 struct perf_event_context *parent_ctx,
4631 struct task_struct *child,
4632 struct perf_event *group_leader,
4633 struct perf_event_context *child_ctx)
4635 struct perf_event *child_event;
4638 * Instead of creating recursive hierarchies of events,
4639 * we link inherited events back to the original parent,
4640 * which has a filp for sure, which we use as the reference
4641 * count:
4643 if (parent_event->parent)
4644 parent_event = parent_event->parent;
4646 child_event = perf_event_alloc(&parent_event->attr,
4647 parent_event->cpu, child_ctx,
4648 group_leader, parent_event,
4649 GFP_KERNEL);
4650 if (IS_ERR(child_event))
4651 return child_event;
4652 get_ctx(child_ctx);
4655 * Make the child state follow the state of the parent event,
4656 * not its attr.disabled bit. We hold the parent's mutex,
4657 * so we won't race with perf_event_{en, dis}able_family.
4659 if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
4660 child_event->state = PERF_EVENT_STATE_INACTIVE;
4661 else
4662 child_event->state = PERF_EVENT_STATE_OFF;
4664 if (parent_event->attr.freq)
4665 child_event->hw.sample_period = parent_event->hw.sample_period;
4668 * Link it up in the child's context:
4670 add_event_to_ctx(child_event, child_ctx);
4673 * Get a reference to the parent filp - we will fput it
4674 * when the child event exits. This is safe to do because
4675 * we are in the parent and we know that the filp still
4676 * exists and has a nonzero count:
4678 atomic_long_inc(&parent_event->filp->f_count);
4681 * Link this into the parent event's child list
4683 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
4684 mutex_lock(&parent_event->child_mutex);
4685 list_add_tail(&child_event->child_list, &parent_event->child_list);
4686 mutex_unlock(&parent_event->child_mutex);
4688 return child_event;
4691 static int inherit_group(struct perf_event *parent_event,
4692 struct task_struct *parent,
4693 struct perf_event_context *parent_ctx,
4694 struct task_struct *child,
4695 struct perf_event_context *child_ctx)
4697 struct perf_event *leader;
4698 struct perf_event *sub;
4699 struct perf_event *child_ctr;
4701 leader = inherit_event(parent_event, parent, parent_ctx,
4702 child, NULL, child_ctx);
4703 if (IS_ERR(leader))
4704 return PTR_ERR(leader);
4705 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
4706 child_ctr = inherit_event(sub, parent, parent_ctx,
4707 child, leader, child_ctx);
4708 if (IS_ERR(child_ctr))
4709 return PTR_ERR(child_ctr);
4711 return 0;
4714 static void sync_child_event(struct perf_event *child_event,
4715 struct task_struct *child)
4717 struct perf_event *parent_event = child_event->parent;
4718 u64 child_val;
4720 if (child_event->attr.inherit_stat)
4721 perf_event_read_event(child_event, child);
4723 child_val = atomic64_read(&child_event->count);
4726 * Add back the child's count to the parent's count:
4728 atomic64_add(child_val, &parent_event->count);
4729 atomic64_add(child_event->total_time_enabled,
4730 &parent_event->child_total_time_enabled);
4731 atomic64_add(child_event->total_time_running,
4732 &parent_event->child_total_time_running);
4735 * Remove this event from the parent's list
4737 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
4738 mutex_lock(&parent_event->child_mutex);
4739 list_del_init(&child_event->child_list);
4740 mutex_unlock(&parent_event->child_mutex);
4743 * Release the parent event, if this was the last
4744 * reference to it.
4746 fput(parent_event->filp);
4749 static void
4750 __perf_event_exit_task(struct perf_event *child_event,
4751 struct perf_event_context *child_ctx,
4752 struct task_struct *child)
4754 struct perf_event *parent_event;
4756 update_event_times(child_event);
4757 perf_event_remove_from_context(child_event);
4759 parent_event = child_event->parent;
4761 * It can happen that parent exits first, and has events
4762 * that are still around due to the child reference. These
4763 * events need to be zapped - but otherwise linger.
4765 if (parent_event) {
4766 sync_child_event(child_event, child);
4767 free_event(child_event);
4772 * When a child task exits, feed back event values to parent events.
4774 void perf_event_exit_task(struct task_struct *child)
4776 struct perf_event *child_event, *tmp;
4777 struct perf_event_context *child_ctx;
4778 unsigned long flags;
4780 if (likely(!child->perf_event_ctxp)) {
4781 perf_event_task(child, NULL, 0);
4782 return;
4785 local_irq_save(flags);
4787 * We can't reschedule here because interrupts are disabled,
4788 * and either child is current or it is a task that can't be
4789 * scheduled, so we are now safe from rescheduling changing
4790 * our context.
4792 child_ctx = child->perf_event_ctxp;
4793 __perf_event_task_sched_out(child_ctx);
4796 * Take the context lock here so that if find_get_context is
4797 * reading child->perf_event_ctxp, we wait until it has
4798 * incremented the context's refcount before we do put_ctx below.
4800 spin_lock(&child_ctx->lock);
4801 child->perf_event_ctxp = NULL;
4803 * If this context is a clone; unclone it so it can't get
4804 * swapped to another process while we're removing all
4805 * the events from it.
4807 unclone_ctx(child_ctx);
4808 spin_unlock_irqrestore(&child_ctx->lock, flags);
4811 * Report the task dead after unscheduling the events so that we
4812 * won't get any samples after PERF_RECORD_EXIT. We can however still
4813 * get a few PERF_RECORD_READ events.
4815 perf_event_task(child, child_ctx, 0);
4818 * We can recurse on the same lock type through:
4820 * __perf_event_exit_task()
4821 * sync_child_event()
4822 * fput(parent_event->filp)
4823 * perf_release()
4824 * mutex_lock(&ctx->mutex)
4826 * But since its the parent context it won't be the same instance.
4828 mutex_lock_nested(&child_ctx->mutex, SINGLE_DEPTH_NESTING);
4830 again:
4831 list_for_each_entry_safe(child_event, tmp, &child_ctx->group_list,
4832 group_entry)
4833 __perf_event_exit_task(child_event, child_ctx, child);
4836 * If the last event was a group event, it will have appended all
4837 * its siblings to the list, but we obtained 'tmp' before that which
4838 * will still point to the list head terminating the iteration.
4840 if (!list_empty(&child_ctx->group_list))
4841 goto again;
4843 mutex_unlock(&child_ctx->mutex);
4845 put_ctx(child_ctx);
4849 * free an unexposed, unused context as created by inheritance by
4850 * init_task below, used by fork() in case of fail.
4852 void perf_event_free_task(struct task_struct *task)
4854 struct perf_event_context *ctx = task->perf_event_ctxp;
4855 struct perf_event *event, *tmp;
4857 if (!ctx)
4858 return;
4860 mutex_lock(&ctx->mutex);
4861 again:
4862 list_for_each_entry_safe(event, tmp, &ctx->group_list, group_entry) {
4863 struct perf_event *parent = event->parent;
4865 if (WARN_ON_ONCE(!parent))
4866 continue;
4868 mutex_lock(&parent->child_mutex);
4869 list_del_init(&event->child_list);
4870 mutex_unlock(&parent->child_mutex);
4872 fput(parent->filp);
4874 list_del_event(event, ctx);
4875 free_event(event);
4878 if (!list_empty(&ctx->group_list))
4879 goto again;
4881 mutex_unlock(&ctx->mutex);
4883 put_ctx(ctx);
4887 * Initialize the perf_event context in task_struct
4889 int perf_event_init_task(struct task_struct *child)
4891 struct perf_event_context *child_ctx, *parent_ctx;
4892 struct perf_event_context *cloned_ctx;
4893 struct perf_event *event;
4894 struct task_struct *parent = current;
4895 int inherited_all = 1;
4896 int ret = 0;
4898 child->perf_event_ctxp = NULL;
4900 mutex_init(&child->perf_event_mutex);
4901 INIT_LIST_HEAD(&child->perf_event_list);
4903 if (likely(!parent->perf_event_ctxp))
4904 return 0;
4907 * This is executed from the parent task context, so inherit
4908 * events that have been marked for cloning.
4909 * First allocate and initialize a context for the child.
4912 child_ctx = kmalloc(sizeof(struct perf_event_context), GFP_KERNEL);
4913 if (!child_ctx)
4914 return -ENOMEM;
4916 __perf_event_init_context(child_ctx, child);
4917 child->perf_event_ctxp = child_ctx;
4918 get_task_struct(child);
4921 * If the parent's context is a clone, pin it so it won't get
4922 * swapped under us.
4924 parent_ctx = perf_pin_task_context(parent);
4927 * No need to check if parent_ctx != NULL here; since we saw
4928 * it non-NULL earlier, the only reason for it to become NULL
4929 * is if we exit, and since we're currently in the middle of
4930 * a fork we can't be exiting at the same time.
4934 * Lock the parent list. No need to lock the child - not PID
4935 * hashed yet and not running, so nobody can access it.
4937 mutex_lock(&parent_ctx->mutex);
4940 * We dont have to disable NMIs - we are only looking at
4941 * the list, not manipulating it:
4943 list_for_each_entry(event, &parent_ctx->group_list, group_entry) {
4945 if (!event->attr.inherit) {
4946 inherited_all = 0;
4947 continue;
4950 ret = inherit_group(event, parent, parent_ctx,
4951 child, child_ctx);
4952 if (ret) {
4953 inherited_all = 0;
4954 break;
4958 if (inherited_all) {
4960 * Mark the child context as a clone of the parent
4961 * context, or of whatever the parent is a clone of.
4962 * Note that if the parent is a clone, it could get
4963 * uncloned at any point, but that doesn't matter
4964 * because the list of events and the generation
4965 * count can't have changed since we took the mutex.
4967 cloned_ctx = rcu_dereference(parent_ctx->parent_ctx);
4968 if (cloned_ctx) {
4969 child_ctx->parent_ctx = cloned_ctx;
4970 child_ctx->parent_gen = parent_ctx->parent_gen;
4971 } else {
4972 child_ctx->parent_ctx = parent_ctx;
4973 child_ctx->parent_gen = parent_ctx->generation;
4975 get_ctx(child_ctx->parent_ctx);
4978 mutex_unlock(&parent_ctx->mutex);
4980 perf_unpin_context(parent_ctx);
4982 return ret;
4985 static void __init perf_event_init_all_cpus(void)
4987 int cpu;
4988 struct perf_cpu_context *cpuctx;
4990 for_each_possible_cpu(cpu) {
4991 cpuctx = &per_cpu(perf_cpu_context, cpu);
4992 __perf_event_init_context(&cpuctx->ctx, NULL);
4996 static void __cpuinit perf_event_init_cpu(int cpu)
4998 struct perf_cpu_context *cpuctx;
5000 cpuctx = &per_cpu(perf_cpu_context, cpu);
5002 spin_lock(&perf_resource_lock);
5003 cpuctx->max_pertask = perf_max_events - perf_reserved_percpu;
5004 spin_unlock(&perf_resource_lock);
5006 hw_perf_event_setup(cpu);
5009 #ifdef CONFIG_HOTPLUG_CPU
5010 static void __perf_event_exit_cpu(void *info)
5012 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
5013 struct perf_event_context *ctx = &cpuctx->ctx;
5014 struct perf_event *event, *tmp;
5016 list_for_each_entry_safe(event, tmp, &ctx->group_list, group_entry)
5017 __perf_event_remove_from_context(event);
5019 static void perf_event_exit_cpu(int cpu)
5021 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
5022 struct perf_event_context *ctx = &cpuctx->ctx;
5024 mutex_lock(&ctx->mutex);
5025 smp_call_function_single(cpu, __perf_event_exit_cpu, NULL, 1);
5026 mutex_unlock(&ctx->mutex);
5028 #else
5029 static inline void perf_event_exit_cpu(int cpu) { }
5030 #endif
5032 static int __cpuinit
5033 perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
5035 unsigned int cpu = (long)hcpu;
5037 switch (action) {
5039 case CPU_UP_PREPARE:
5040 case CPU_UP_PREPARE_FROZEN:
5041 perf_event_init_cpu(cpu);
5042 break;
5044 case CPU_ONLINE:
5045 case CPU_ONLINE_FROZEN:
5046 hw_perf_event_setup_online(cpu);
5047 break;
5049 case CPU_DOWN_PREPARE:
5050 case CPU_DOWN_PREPARE_FROZEN:
5051 perf_event_exit_cpu(cpu);
5052 break;
5054 default:
5055 break;
5058 return NOTIFY_OK;
5062 * This has to have a higher priority than migration_notifier in sched.c.
5064 static struct notifier_block __cpuinitdata perf_cpu_nb = {
5065 .notifier_call = perf_cpu_notify,
5066 .priority = 20,
5069 void __init perf_event_init(void)
5071 perf_event_init_all_cpus();
5072 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
5073 (void *)(long)smp_processor_id());
5074 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_ONLINE,
5075 (void *)(long)smp_processor_id());
5076 register_cpu_notifier(&perf_cpu_nb);
5079 static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
5081 return sprintf(buf, "%d\n", perf_reserved_percpu);
5084 static ssize_t
5085 perf_set_reserve_percpu(struct sysdev_class *class,
5086 const char *buf,
5087 size_t count)
5089 struct perf_cpu_context *cpuctx;
5090 unsigned long val;
5091 int err, cpu, mpt;
5093 err = strict_strtoul(buf, 10, &val);
5094 if (err)
5095 return err;
5096 if (val > perf_max_events)
5097 return -EINVAL;
5099 spin_lock(&perf_resource_lock);
5100 perf_reserved_percpu = val;
5101 for_each_online_cpu(cpu) {
5102 cpuctx = &per_cpu(perf_cpu_context, cpu);
5103 spin_lock_irq(&cpuctx->ctx.lock);
5104 mpt = min(perf_max_events - cpuctx->ctx.nr_events,
5105 perf_max_events - perf_reserved_percpu);
5106 cpuctx->max_pertask = mpt;
5107 spin_unlock_irq(&cpuctx->ctx.lock);
5109 spin_unlock(&perf_resource_lock);
5111 return count;
5114 static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
5116 return sprintf(buf, "%d\n", perf_overcommit);
5119 static ssize_t
5120 perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
5122 unsigned long val;
5123 int err;
5125 err = strict_strtoul(buf, 10, &val);
5126 if (err)
5127 return err;
5128 if (val > 1)
5129 return -EINVAL;
5131 spin_lock(&perf_resource_lock);
5132 perf_overcommit = val;
5133 spin_unlock(&perf_resource_lock);
5135 return count;
5138 static SYSDEV_CLASS_ATTR(
5139 reserve_percpu,
5140 0644,
5141 perf_show_reserve_percpu,
5142 perf_set_reserve_percpu
5145 static SYSDEV_CLASS_ATTR(
5146 overcommit,
5147 0644,
5148 perf_show_overcommit,
5149 perf_set_overcommit
5152 static struct attribute *perfclass_attrs[] = {
5153 &attr_reserve_percpu.attr,
5154 &attr_overcommit.attr,
5155 NULL
5158 static struct attribute_group perfclass_attr_group = {
5159 .attrs = perfclass_attrs,
5160 .name = "perf_events",
5163 static int __init perf_event_sysfs_init(void)
5165 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
5166 &perfclass_attr_group);
5168 device_initcall(perf_event_sysfs_init);