Merge branch 'perf/urgent' into perf/core
[linux-2.6/x86.git] / kernel / perf_event.c
blob27f69a04541dbd1ec3963b831da76ffb244833ad
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>
31 #include <linux/ftrace_event.h>
32 #include <linux/hw_breakpoint.h>
34 #include <asm/irq_regs.h>
37 * Each CPU has a list of per CPU events:
39 static DEFINE_PER_CPU(struct perf_cpu_context, perf_cpu_context);
41 int perf_max_events __read_mostly = 1;
42 static int perf_reserved_percpu __read_mostly;
43 static int perf_overcommit __read_mostly = 1;
45 static atomic_t nr_events __read_mostly;
46 static atomic_t nr_mmap_events __read_mostly;
47 static atomic_t nr_comm_events __read_mostly;
48 static atomic_t nr_task_events __read_mostly;
51 * perf event paranoia level:
52 * -1 - not paranoid at all
53 * 0 - disallow raw tracepoint access for unpriv
54 * 1 - disallow cpu events for unpriv
55 * 2 - disallow kernel profiling for unpriv
57 int sysctl_perf_event_paranoid __read_mostly = 1;
59 static inline bool perf_paranoid_tracepoint_raw(void)
61 return sysctl_perf_event_paranoid > -1;
64 static inline bool perf_paranoid_cpu(void)
66 return sysctl_perf_event_paranoid > 0;
69 static inline bool perf_paranoid_kernel(void)
71 return sysctl_perf_event_paranoid > 1;
74 int sysctl_perf_event_mlock __read_mostly = 512; /* 'free' kb per user */
77 * max perf event sample rate
79 int sysctl_perf_event_sample_rate __read_mostly = 100000;
81 static atomic64_t perf_event_id;
84 * Lock for (sysadmin-configurable) event reservations:
86 static DEFINE_SPINLOCK(perf_resource_lock);
89 * Architecture provided APIs - weak aliases:
91 extern __weak const struct pmu *hw_perf_event_init(struct perf_event *event)
93 return NULL;
96 void __weak hw_perf_disable(void) { barrier(); }
97 void __weak hw_perf_enable(void) { barrier(); }
99 void __weak hw_perf_event_setup(int cpu) { barrier(); }
100 void __weak hw_perf_event_setup_online(int cpu) { barrier(); }
102 int __weak
103 hw_perf_group_sched_in(struct perf_event *group_leader,
104 struct perf_cpu_context *cpuctx,
105 struct perf_event_context *ctx, int cpu)
107 return 0;
110 void __weak perf_event_print_debug(void) { }
112 static DEFINE_PER_CPU(int, perf_disable_count);
114 void __perf_disable(void)
116 __get_cpu_var(perf_disable_count)++;
119 bool __perf_enable(void)
121 return !--__get_cpu_var(perf_disable_count);
124 void perf_disable(void)
126 __perf_disable();
127 hw_perf_disable();
130 void perf_enable(void)
132 if (__perf_enable())
133 hw_perf_enable();
136 static void get_ctx(struct perf_event_context *ctx)
138 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
141 static void free_ctx(struct rcu_head *head)
143 struct perf_event_context *ctx;
145 ctx = container_of(head, struct perf_event_context, rcu_head);
146 kfree(ctx);
149 static void put_ctx(struct perf_event_context *ctx)
151 if (atomic_dec_and_test(&ctx->refcount)) {
152 if (ctx->parent_ctx)
153 put_ctx(ctx->parent_ctx);
154 if (ctx->task)
155 put_task_struct(ctx->task);
156 call_rcu(&ctx->rcu_head, free_ctx);
160 static void unclone_ctx(struct perf_event_context *ctx)
162 if (ctx->parent_ctx) {
163 put_ctx(ctx->parent_ctx);
164 ctx->parent_ctx = NULL;
169 * If we inherit events we want to return the parent event id
170 * to userspace.
172 static u64 primary_event_id(struct perf_event *event)
174 u64 id = event->id;
176 if (event->parent)
177 id = event->parent->id;
179 return id;
183 * Get the perf_event_context for a task and lock it.
184 * This has to cope with with the fact that until it is locked,
185 * the context could get moved to another task.
187 static struct perf_event_context *
188 perf_lock_task_context(struct task_struct *task, unsigned long *flags)
190 struct perf_event_context *ctx;
192 rcu_read_lock();
193 retry:
194 ctx = rcu_dereference(task->perf_event_ctxp);
195 if (ctx) {
197 * If this context is a clone of another, it might
198 * get swapped for another underneath us by
199 * perf_event_task_sched_out, though the
200 * rcu_read_lock() protects us from any context
201 * getting freed. Lock the context and check if it
202 * got swapped before we could get the lock, and retry
203 * if so. If we locked the right context, then it
204 * can't get swapped on us any more.
206 raw_spin_lock_irqsave(&ctx->lock, *flags);
207 if (ctx != rcu_dereference(task->perf_event_ctxp)) {
208 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
209 goto retry;
212 if (!atomic_inc_not_zero(&ctx->refcount)) {
213 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
214 ctx = NULL;
217 rcu_read_unlock();
218 return ctx;
222 * Get the context for a task and increment its pin_count so it
223 * can't get swapped to another task. This also increments its
224 * reference count so that the context can't get freed.
226 static struct perf_event_context *perf_pin_task_context(struct task_struct *task)
228 struct perf_event_context *ctx;
229 unsigned long flags;
231 ctx = perf_lock_task_context(task, &flags);
232 if (ctx) {
233 ++ctx->pin_count;
234 raw_spin_unlock_irqrestore(&ctx->lock, flags);
236 return ctx;
239 static void perf_unpin_context(struct perf_event_context *ctx)
241 unsigned long flags;
243 raw_spin_lock_irqsave(&ctx->lock, flags);
244 --ctx->pin_count;
245 raw_spin_unlock_irqrestore(&ctx->lock, flags);
246 put_ctx(ctx);
249 static inline u64 perf_clock(void)
251 return cpu_clock(smp_processor_id());
255 * Update the record of the current time in a context.
257 static void update_context_time(struct perf_event_context *ctx)
259 u64 now = perf_clock();
261 ctx->time += now - ctx->timestamp;
262 ctx->timestamp = now;
266 * Update the total_time_enabled and total_time_running fields for a event.
268 static void update_event_times(struct perf_event *event)
270 struct perf_event_context *ctx = event->ctx;
271 u64 run_end;
273 if (event->state < PERF_EVENT_STATE_INACTIVE ||
274 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
275 return;
277 if (ctx->is_active)
278 run_end = ctx->time;
279 else
280 run_end = event->tstamp_stopped;
282 event->total_time_enabled = run_end - event->tstamp_enabled;
284 if (event->state == PERF_EVENT_STATE_INACTIVE)
285 run_end = event->tstamp_stopped;
286 else
287 run_end = ctx->time;
289 event->total_time_running = run_end - event->tstamp_running;
293 * Add a event from the lists for its context.
294 * Must be called with ctx->mutex and ctx->lock held.
296 static void
297 list_add_event(struct perf_event *event, struct perf_event_context *ctx)
299 struct perf_event *group_leader = event->group_leader;
302 * Depending on whether it is a standalone or sibling event,
303 * add it straight to the context's event list, or to the group
304 * leader's sibling list:
306 if (group_leader == event)
307 list_add_tail(&event->group_entry, &ctx->group_list);
308 else {
309 list_add_tail(&event->group_entry, &group_leader->sibling_list);
310 group_leader->nr_siblings++;
313 list_add_rcu(&event->event_entry, &ctx->event_list);
314 ctx->nr_events++;
315 if (event->attr.inherit_stat)
316 ctx->nr_stat++;
320 * Remove a event from the lists for its context.
321 * Must be called with ctx->mutex and ctx->lock held.
323 static void
324 list_del_event(struct perf_event *event, struct perf_event_context *ctx)
326 struct perf_event *sibling, *tmp;
328 if (list_empty(&event->group_entry))
329 return;
330 ctx->nr_events--;
331 if (event->attr.inherit_stat)
332 ctx->nr_stat--;
334 list_del_init(&event->group_entry);
335 list_del_rcu(&event->event_entry);
337 if (event->group_leader != event)
338 event->group_leader->nr_siblings--;
340 update_event_times(event);
343 * If event was in error state, then keep it
344 * that way, otherwise bogus counts will be
345 * returned on read(). The only way to get out
346 * of error state is by explicit re-enabling
347 * of the event
349 if (event->state > PERF_EVENT_STATE_OFF)
350 event->state = PERF_EVENT_STATE_OFF;
353 * If this was a group event with sibling events then
354 * upgrade the siblings to singleton events by adding them
355 * to the context list directly:
357 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
359 list_move_tail(&sibling->group_entry, &ctx->group_list);
360 sibling->group_leader = sibling;
364 static void
365 event_sched_out(struct perf_event *event,
366 struct perf_cpu_context *cpuctx,
367 struct perf_event_context *ctx)
369 if (event->state != PERF_EVENT_STATE_ACTIVE)
370 return;
372 event->state = PERF_EVENT_STATE_INACTIVE;
373 if (event->pending_disable) {
374 event->pending_disable = 0;
375 event->state = PERF_EVENT_STATE_OFF;
377 event->tstamp_stopped = ctx->time;
378 event->pmu->disable(event);
379 event->oncpu = -1;
381 if (!is_software_event(event))
382 cpuctx->active_oncpu--;
383 ctx->nr_active--;
384 if (event->attr.exclusive || !cpuctx->active_oncpu)
385 cpuctx->exclusive = 0;
388 static void
389 group_sched_out(struct perf_event *group_event,
390 struct perf_cpu_context *cpuctx,
391 struct perf_event_context *ctx)
393 struct perf_event *event;
395 if (group_event->state != PERF_EVENT_STATE_ACTIVE)
396 return;
398 event_sched_out(group_event, cpuctx, ctx);
401 * Schedule out siblings (if any):
403 list_for_each_entry(event, &group_event->sibling_list, group_entry)
404 event_sched_out(event, cpuctx, ctx);
406 if (group_event->attr.exclusive)
407 cpuctx->exclusive = 0;
411 * Cross CPU call to remove a performance event
413 * We disable the event on the hardware level first. After that we
414 * remove it from the context list.
416 static void __perf_event_remove_from_context(void *info)
418 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
419 struct perf_event *event = info;
420 struct perf_event_context *ctx = event->ctx;
423 * If this is a task context, we need to check whether it is
424 * the current task context of this cpu. If not it has been
425 * scheduled out before the smp call arrived.
427 if (ctx->task && cpuctx->task_ctx != ctx)
428 return;
430 raw_spin_lock(&ctx->lock);
432 * Protect the list operation against NMI by disabling the
433 * events on a global level.
435 perf_disable();
437 event_sched_out(event, cpuctx, ctx);
439 list_del_event(event, ctx);
441 if (!ctx->task) {
443 * Allow more per task events with respect to the
444 * reservation:
446 cpuctx->max_pertask =
447 min(perf_max_events - ctx->nr_events,
448 perf_max_events - perf_reserved_percpu);
451 perf_enable();
452 raw_spin_unlock(&ctx->lock);
457 * Remove the event from a task's (or a CPU's) list of events.
459 * Must be called with ctx->mutex held.
461 * CPU events are removed with a smp call. For task events we only
462 * call when the task is on a CPU.
464 * If event->ctx is a cloned context, callers must make sure that
465 * every task struct that event->ctx->task could possibly point to
466 * remains valid. This is OK when called from perf_release since
467 * that only calls us on the top-level context, which can't be a clone.
468 * When called from perf_event_exit_task, it's OK because the
469 * context has been detached from its task.
471 static void perf_event_remove_from_context(struct perf_event *event)
473 struct perf_event_context *ctx = event->ctx;
474 struct task_struct *task = ctx->task;
476 if (!task) {
478 * Per cpu events are removed via an smp call and
479 * the removal is always successful.
481 smp_call_function_single(event->cpu,
482 __perf_event_remove_from_context,
483 event, 1);
484 return;
487 retry:
488 task_oncpu_function_call(task, __perf_event_remove_from_context,
489 event);
491 raw_spin_lock_irq(&ctx->lock);
493 * If the context is active we need to retry the smp call.
495 if (ctx->nr_active && !list_empty(&event->group_entry)) {
496 raw_spin_unlock_irq(&ctx->lock);
497 goto retry;
501 * The lock prevents that this context is scheduled in so we
502 * can remove the event safely, if the call above did not
503 * succeed.
505 if (!list_empty(&event->group_entry))
506 list_del_event(event, ctx);
507 raw_spin_unlock_irq(&ctx->lock);
511 * Update total_time_enabled and total_time_running for all events in a group.
513 static void update_group_times(struct perf_event *leader)
515 struct perf_event *event;
517 update_event_times(leader);
518 list_for_each_entry(event, &leader->sibling_list, group_entry)
519 update_event_times(event);
523 * Cross CPU call to disable a performance event
525 static void __perf_event_disable(void *info)
527 struct perf_event *event = info;
528 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
529 struct perf_event_context *ctx = event->ctx;
532 * If this is a per-task event, need to check whether this
533 * event's task is the current task on this cpu.
535 if (ctx->task && cpuctx->task_ctx != ctx)
536 return;
538 raw_spin_lock(&ctx->lock);
541 * If the event is on, turn it off.
542 * If it is in error state, leave it in error state.
544 if (event->state >= PERF_EVENT_STATE_INACTIVE) {
545 update_context_time(ctx);
546 update_group_times(event);
547 if (event == event->group_leader)
548 group_sched_out(event, cpuctx, ctx);
549 else
550 event_sched_out(event, cpuctx, ctx);
551 event->state = PERF_EVENT_STATE_OFF;
554 raw_spin_unlock(&ctx->lock);
558 * Disable a event.
560 * If event->ctx is a cloned context, callers must make sure that
561 * every task struct that event->ctx->task could possibly point to
562 * remains valid. This condition is satisifed when called through
563 * perf_event_for_each_child or perf_event_for_each because they
564 * hold the top-level event's child_mutex, so any descendant that
565 * goes to exit will block in sync_child_event.
566 * When called from perf_pending_event it's OK because event->ctx
567 * is the current context on this CPU and preemption is disabled,
568 * hence we can't get into perf_event_task_sched_out for this context.
570 void perf_event_disable(struct perf_event *event)
572 struct perf_event_context *ctx = event->ctx;
573 struct task_struct *task = ctx->task;
575 if (!task) {
577 * Disable the event on the cpu that it's on
579 smp_call_function_single(event->cpu, __perf_event_disable,
580 event, 1);
581 return;
584 retry:
585 task_oncpu_function_call(task, __perf_event_disable, event);
587 raw_spin_lock_irq(&ctx->lock);
589 * If the event is still active, we need to retry the cross-call.
591 if (event->state == PERF_EVENT_STATE_ACTIVE) {
592 raw_spin_unlock_irq(&ctx->lock);
593 goto retry;
597 * Since we have the lock this context can't be scheduled
598 * in, so we can change the state safely.
600 if (event->state == PERF_EVENT_STATE_INACTIVE) {
601 update_group_times(event);
602 event->state = PERF_EVENT_STATE_OFF;
605 raw_spin_unlock_irq(&ctx->lock);
608 static int
609 event_sched_in(struct perf_event *event,
610 struct perf_cpu_context *cpuctx,
611 struct perf_event_context *ctx,
612 int cpu)
614 if (event->state <= PERF_EVENT_STATE_OFF)
615 return 0;
617 event->state = PERF_EVENT_STATE_ACTIVE;
618 event->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
620 * The new state must be visible before we turn it on in the hardware:
622 smp_wmb();
624 if (event->pmu->enable(event)) {
625 event->state = PERF_EVENT_STATE_INACTIVE;
626 event->oncpu = -1;
627 return -EAGAIN;
630 event->tstamp_running += ctx->time - event->tstamp_stopped;
632 if (!is_software_event(event))
633 cpuctx->active_oncpu++;
634 ctx->nr_active++;
636 if (event->attr.exclusive)
637 cpuctx->exclusive = 1;
639 return 0;
642 static int
643 group_sched_in(struct perf_event *group_event,
644 struct perf_cpu_context *cpuctx,
645 struct perf_event_context *ctx,
646 int cpu)
648 struct perf_event *event, *partial_group;
649 int ret;
651 if (group_event->state == PERF_EVENT_STATE_OFF)
652 return 0;
654 ret = hw_perf_group_sched_in(group_event, cpuctx, ctx, cpu);
655 if (ret)
656 return ret < 0 ? ret : 0;
658 if (event_sched_in(group_event, cpuctx, ctx, cpu))
659 return -EAGAIN;
662 * Schedule in siblings as one group (if any):
664 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
665 if (event_sched_in(event, cpuctx, ctx, cpu)) {
666 partial_group = event;
667 goto group_error;
671 return 0;
673 group_error:
675 * Groups can be scheduled in as one unit only, so undo any
676 * partial group before returning:
678 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
679 if (event == partial_group)
680 break;
681 event_sched_out(event, cpuctx, ctx);
683 event_sched_out(group_event, cpuctx, ctx);
685 return -EAGAIN;
689 * Return 1 for a group consisting entirely of software events,
690 * 0 if the group contains any hardware events.
692 static int is_software_only_group(struct perf_event *leader)
694 struct perf_event *event;
696 if (!is_software_event(leader))
697 return 0;
699 list_for_each_entry(event, &leader->sibling_list, group_entry)
700 if (!is_software_event(event))
701 return 0;
703 return 1;
707 * Work out whether we can put this event group on the CPU now.
709 static int group_can_go_on(struct perf_event *event,
710 struct perf_cpu_context *cpuctx,
711 int can_add_hw)
714 * Groups consisting entirely of software events can always go on.
716 if (is_software_only_group(event))
717 return 1;
719 * If an exclusive group is already on, no other hardware
720 * events can go on.
722 if (cpuctx->exclusive)
723 return 0;
725 * If this group is exclusive and there are already
726 * events on the CPU, it can't go on.
728 if (event->attr.exclusive && cpuctx->active_oncpu)
729 return 0;
731 * Otherwise, try to add it if all previous groups were able
732 * to go on.
734 return can_add_hw;
737 static void add_event_to_ctx(struct perf_event *event,
738 struct perf_event_context *ctx)
740 list_add_event(event, ctx);
741 event->tstamp_enabled = ctx->time;
742 event->tstamp_running = ctx->time;
743 event->tstamp_stopped = ctx->time;
747 * Cross CPU call to install and enable a performance event
749 * Must be called with ctx->mutex held
751 static void __perf_install_in_context(void *info)
753 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
754 struct perf_event *event = info;
755 struct perf_event_context *ctx = event->ctx;
756 struct perf_event *leader = event->group_leader;
757 int cpu = smp_processor_id();
758 int err;
761 * If this is a task context, we need to check whether it is
762 * the current task context of this cpu. If not it has been
763 * scheduled out before the smp call arrived.
764 * Or possibly this is the right context but it isn't
765 * on this cpu because it had no events.
767 if (ctx->task && cpuctx->task_ctx != ctx) {
768 if (cpuctx->task_ctx || ctx->task != current)
769 return;
770 cpuctx->task_ctx = ctx;
773 raw_spin_lock(&ctx->lock);
774 ctx->is_active = 1;
775 update_context_time(ctx);
778 * Protect the list operation against NMI by disabling the
779 * events on a global level. NOP for non NMI based events.
781 perf_disable();
783 add_event_to_ctx(event, ctx);
785 if (event->cpu != -1 && event->cpu != smp_processor_id())
786 goto unlock;
789 * Don't put the event on if it is disabled or if
790 * it is in a group and the group isn't on.
792 if (event->state != PERF_EVENT_STATE_INACTIVE ||
793 (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE))
794 goto unlock;
797 * An exclusive event can't go on if there are already active
798 * hardware events, and no hardware event can go on if there
799 * is already an exclusive event on.
801 if (!group_can_go_on(event, cpuctx, 1))
802 err = -EEXIST;
803 else
804 err = event_sched_in(event, cpuctx, ctx, cpu);
806 if (err) {
808 * This event couldn't go on. If it is in a group
809 * then we have to pull the whole group off.
810 * If the event group is pinned then put it in error state.
812 if (leader != event)
813 group_sched_out(leader, cpuctx, ctx);
814 if (leader->attr.pinned) {
815 update_group_times(leader);
816 leader->state = PERF_EVENT_STATE_ERROR;
820 if (!err && !ctx->task && cpuctx->max_pertask)
821 cpuctx->max_pertask--;
823 unlock:
824 perf_enable();
826 raw_spin_unlock(&ctx->lock);
830 * Attach a performance event to a context
832 * First we add the event to the list with the hardware enable bit
833 * in event->hw_config cleared.
835 * If the event is attached to a task which is on a CPU we use a smp
836 * call to enable it in the task context. The task might have been
837 * scheduled away, but we check this in the smp call again.
839 * Must be called with ctx->mutex held.
841 static void
842 perf_install_in_context(struct perf_event_context *ctx,
843 struct perf_event *event,
844 int cpu)
846 struct task_struct *task = ctx->task;
848 if (!task) {
850 * Per cpu events are installed via an smp call and
851 * the install is always successful.
853 smp_call_function_single(cpu, __perf_install_in_context,
854 event, 1);
855 return;
858 retry:
859 task_oncpu_function_call(task, __perf_install_in_context,
860 event);
862 raw_spin_lock_irq(&ctx->lock);
864 * we need to retry the smp call.
866 if (ctx->is_active && list_empty(&event->group_entry)) {
867 raw_spin_unlock_irq(&ctx->lock);
868 goto retry;
872 * The lock prevents that this context is scheduled in so we
873 * can add the event safely, if it the call above did not
874 * succeed.
876 if (list_empty(&event->group_entry))
877 add_event_to_ctx(event, ctx);
878 raw_spin_unlock_irq(&ctx->lock);
882 * Put a event into inactive state and update time fields.
883 * Enabling the leader of a group effectively enables all
884 * the group members that aren't explicitly disabled, so we
885 * have to update their ->tstamp_enabled also.
886 * Note: this works for group members as well as group leaders
887 * since the non-leader members' sibling_lists will be empty.
889 static void __perf_event_mark_enabled(struct perf_event *event,
890 struct perf_event_context *ctx)
892 struct perf_event *sub;
894 event->state = PERF_EVENT_STATE_INACTIVE;
895 event->tstamp_enabled = ctx->time - event->total_time_enabled;
896 list_for_each_entry(sub, &event->sibling_list, group_entry)
897 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
898 sub->tstamp_enabled =
899 ctx->time - sub->total_time_enabled;
903 * Cross CPU call to enable a performance event
905 static void __perf_event_enable(void *info)
907 struct perf_event *event = info;
908 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
909 struct perf_event_context *ctx = event->ctx;
910 struct perf_event *leader = event->group_leader;
911 int err;
914 * If this is a per-task event, need to check whether this
915 * event's task is the current task on this cpu.
917 if (ctx->task && cpuctx->task_ctx != ctx) {
918 if (cpuctx->task_ctx || ctx->task != current)
919 return;
920 cpuctx->task_ctx = ctx;
923 raw_spin_lock(&ctx->lock);
924 ctx->is_active = 1;
925 update_context_time(ctx);
927 if (event->state >= PERF_EVENT_STATE_INACTIVE)
928 goto unlock;
929 __perf_event_mark_enabled(event, ctx);
931 if (event->cpu != -1 && event->cpu != smp_processor_id())
932 goto unlock;
935 * If the event is in a group and isn't the group leader,
936 * then don't put it on unless the group is on.
938 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
939 goto unlock;
941 if (!group_can_go_on(event, cpuctx, 1)) {
942 err = -EEXIST;
943 } else {
944 perf_disable();
945 if (event == leader)
946 err = group_sched_in(event, cpuctx, ctx,
947 smp_processor_id());
948 else
949 err = event_sched_in(event, cpuctx, ctx,
950 smp_processor_id());
951 perf_enable();
954 if (err) {
956 * If this event can't go on and it's part of a
957 * group, then the whole group has to come off.
959 if (leader != event)
960 group_sched_out(leader, cpuctx, ctx);
961 if (leader->attr.pinned) {
962 update_group_times(leader);
963 leader->state = PERF_EVENT_STATE_ERROR;
967 unlock:
968 raw_spin_unlock(&ctx->lock);
972 * Enable a event.
974 * If event->ctx is a cloned context, callers must make sure that
975 * every task struct that event->ctx->task could possibly point to
976 * remains valid. This condition is satisfied when called through
977 * perf_event_for_each_child or perf_event_for_each as described
978 * for perf_event_disable.
980 void perf_event_enable(struct perf_event *event)
982 struct perf_event_context *ctx = event->ctx;
983 struct task_struct *task = ctx->task;
985 if (!task) {
987 * Enable the event on the cpu that it's on
989 smp_call_function_single(event->cpu, __perf_event_enable,
990 event, 1);
991 return;
994 raw_spin_lock_irq(&ctx->lock);
995 if (event->state >= PERF_EVENT_STATE_INACTIVE)
996 goto out;
999 * If the event is in error state, clear that first.
1000 * That way, if we see the event in error state below, we
1001 * know that it has gone back into error state, as distinct
1002 * from the task having been scheduled away before the
1003 * cross-call arrived.
1005 if (event->state == PERF_EVENT_STATE_ERROR)
1006 event->state = PERF_EVENT_STATE_OFF;
1008 retry:
1009 raw_spin_unlock_irq(&ctx->lock);
1010 task_oncpu_function_call(task, __perf_event_enable, event);
1012 raw_spin_lock_irq(&ctx->lock);
1015 * If the context is active and the event is still off,
1016 * we need to retry the cross-call.
1018 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF)
1019 goto retry;
1022 * Since we have the lock this context can't be scheduled
1023 * in, so we can change the state safely.
1025 if (event->state == PERF_EVENT_STATE_OFF)
1026 __perf_event_mark_enabled(event, ctx);
1028 out:
1029 raw_spin_unlock_irq(&ctx->lock);
1032 static int perf_event_refresh(struct perf_event *event, int refresh)
1035 * not supported on inherited events
1037 if (event->attr.inherit)
1038 return -EINVAL;
1040 atomic_add(refresh, &event->event_limit);
1041 perf_event_enable(event);
1043 return 0;
1046 void __perf_event_sched_out(struct perf_event_context *ctx,
1047 struct perf_cpu_context *cpuctx)
1049 struct perf_event *event;
1051 raw_spin_lock(&ctx->lock);
1052 ctx->is_active = 0;
1053 if (likely(!ctx->nr_events))
1054 goto out;
1055 update_context_time(ctx);
1057 perf_disable();
1058 if (ctx->nr_active) {
1059 list_for_each_entry(event, &ctx->group_list, group_entry)
1060 group_sched_out(event, cpuctx, ctx);
1062 perf_enable();
1063 out:
1064 raw_spin_unlock(&ctx->lock);
1068 * Test whether two contexts are equivalent, i.e. whether they
1069 * have both been cloned from the same version of the same context
1070 * and they both have the same number of enabled events.
1071 * If the number of enabled events is the same, then the set
1072 * of enabled events should be the same, because these are both
1073 * inherited contexts, therefore we can't access individual events
1074 * in them directly with an fd; we can only enable/disable all
1075 * events via prctl, or enable/disable all events in a family
1076 * via ioctl, which will have the same effect on both contexts.
1078 static int context_equiv(struct perf_event_context *ctx1,
1079 struct perf_event_context *ctx2)
1081 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
1082 && ctx1->parent_gen == ctx2->parent_gen
1083 && !ctx1->pin_count && !ctx2->pin_count;
1086 static void __perf_event_sync_stat(struct perf_event *event,
1087 struct perf_event *next_event)
1089 u64 value;
1091 if (!event->attr.inherit_stat)
1092 return;
1095 * Update the event value, we cannot use perf_event_read()
1096 * because we're in the middle of a context switch and have IRQs
1097 * disabled, which upsets smp_call_function_single(), however
1098 * we know the event must be on the current CPU, therefore we
1099 * don't need to use it.
1101 switch (event->state) {
1102 case PERF_EVENT_STATE_ACTIVE:
1103 event->pmu->read(event);
1104 /* fall-through */
1106 case PERF_EVENT_STATE_INACTIVE:
1107 update_event_times(event);
1108 break;
1110 default:
1111 break;
1115 * In order to keep per-task stats reliable we need to flip the event
1116 * values when we flip the contexts.
1118 value = atomic64_read(&next_event->count);
1119 value = atomic64_xchg(&event->count, value);
1120 atomic64_set(&next_event->count, value);
1122 swap(event->total_time_enabled, next_event->total_time_enabled);
1123 swap(event->total_time_running, next_event->total_time_running);
1126 * Since we swizzled the values, update the user visible data too.
1128 perf_event_update_userpage(event);
1129 perf_event_update_userpage(next_event);
1132 #define list_next_entry(pos, member) \
1133 list_entry(pos->member.next, typeof(*pos), member)
1135 static void perf_event_sync_stat(struct perf_event_context *ctx,
1136 struct perf_event_context *next_ctx)
1138 struct perf_event *event, *next_event;
1140 if (!ctx->nr_stat)
1141 return;
1143 update_context_time(ctx);
1145 event = list_first_entry(&ctx->event_list,
1146 struct perf_event, event_entry);
1148 next_event = list_first_entry(&next_ctx->event_list,
1149 struct perf_event, event_entry);
1151 while (&event->event_entry != &ctx->event_list &&
1152 &next_event->event_entry != &next_ctx->event_list) {
1154 __perf_event_sync_stat(event, next_event);
1156 event = list_next_entry(event, event_entry);
1157 next_event = list_next_entry(next_event, event_entry);
1162 * Called from scheduler to remove the events of the current task,
1163 * with interrupts disabled.
1165 * We stop each event and update the event value in event->count.
1167 * This does not protect us against NMI, but disable()
1168 * sets the disabled bit in the control field of event _before_
1169 * accessing the event control register. If a NMI hits, then it will
1170 * not restart the event.
1172 void perf_event_task_sched_out(struct task_struct *task,
1173 struct task_struct *next)
1175 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1176 struct perf_event_context *ctx = task->perf_event_ctxp;
1177 struct perf_event_context *next_ctx;
1178 struct perf_event_context *parent;
1179 struct pt_regs *regs;
1180 int do_switch = 1;
1182 regs = task_pt_regs(task);
1183 perf_sw_event(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 1, regs, 0);
1185 if (likely(!ctx || !cpuctx->task_ctx))
1186 return;
1188 rcu_read_lock();
1189 parent = rcu_dereference(ctx->parent_ctx);
1190 next_ctx = next->perf_event_ctxp;
1191 if (parent && next_ctx &&
1192 rcu_dereference(next_ctx->parent_ctx) == parent) {
1194 * Looks like the two contexts are clones, so we might be
1195 * able to optimize the context switch. We lock both
1196 * contexts and check that they are clones under the
1197 * lock (including re-checking that neither has been
1198 * uncloned in the meantime). It doesn't matter which
1199 * order we take the locks because no other cpu could
1200 * be trying to lock both of these tasks.
1202 raw_spin_lock(&ctx->lock);
1203 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
1204 if (context_equiv(ctx, next_ctx)) {
1206 * XXX do we need a memory barrier of sorts
1207 * wrt to rcu_dereference() of perf_event_ctxp
1209 task->perf_event_ctxp = next_ctx;
1210 next->perf_event_ctxp = ctx;
1211 ctx->task = next;
1212 next_ctx->task = task;
1213 do_switch = 0;
1215 perf_event_sync_stat(ctx, next_ctx);
1217 raw_spin_unlock(&next_ctx->lock);
1218 raw_spin_unlock(&ctx->lock);
1220 rcu_read_unlock();
1222 if (do_switch) {
1223 __perf_event_sched_out(ctx, cpuctx);
1224 cpuctx->task_ctx = NULL;
1229 * Called with IRQs disabled
1231 static void __perf_event_task_sched_out(struct perf_event_context *ctx)
1233 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1235 if (!cpuctx->task_ctx)
1236 return;
1238 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
1239 return;
1241 __perf_event_sched_out(ctx, cpuctx);
1242 cpuctx->task_ctx = NULL;
1246 * Called with IRQs disabled
1248 static void perf_event_cpu_sched_out(struct perf_cpu_context *cpuctx)
1250 __perf_event_sched_out(&cpuctx->ctx, cpuctx);
1253 static void
1254 __perf_event_sched_in(struct perf_event_context *ctx,
1255 struct perf_cpu_context *cpuctx)
1257 int cpu = smp_processor_id();
1258 struct perf_event *event;
1259 int can_add_hw = 1;
1261 raw_spin_lock(&ctx->lock);
1262 ctx->is_active = 1;
1263 if (likely(!ctx->nr_events))
1264 goto out;
1266 ctx->timestamp = perf_clock();
1268 perf_disable();
1271 * First go through the list and put on any pinned groups
1272 * in order to give them the best chance of going on.
1274 list_for_each_entry(event, &ctx->group_list, group_entry) {
1275 if (event->state <= PERF_EVENT_STATE_OFF ||
1276 !event->attr.pinned)
1277 continue;
1278 if (event->cpu != -1 && event->cpu != cpu)
1279 continue;
1281 if (group_can_go_on(event, cpuctx, 1))
1282 group_sched_in(event, cpuctx, ctx, cpu);
1285 * If this pinned group hasn't been scheduled,
1286 * put it in error state.
1288 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1289 update_group_times(event);
1290 event->state = PERF_EVENT_STATE_ERROR;
1294 list_for_each_entry(event, &ctx->group_list, group_entry) {
1296 * Ignore events in OFF or ERROR state, and
1297 * ignore pinned events since we did them already.
1299 if (event->state <= PERF_EVENT_STATE_OFF ||
1300 event->attr.pinned)
1301 continue;
1304 * Listen to the 'cpu' scheduling filter constraint
1305 * of events:
1307 if (event->cpu != -1 && event->cpu != cpu)
1308 continue;
1310 if (group_can_go_on(event, cpuctx, can_add_hw))
1311 if (group_sched_in(event, cpuctx, ctx, cpu))
1312 can_add_hw = 0;
1314 perf_enable();
1315 out:
1316 raw_spin_unlock(&ctx->lock);
1320 * Called from scheduler to add the events of the current task
1321 * with interrupts disabled.
1323 * We restore the event value and then enable it.
1325 * This does not protect us against NMI, but enable()
1326 * sets the enabled bit in the control field of event _before_
1327 * accessing the event control register. If a NMI hits, then it will
1328 * keep the event running.
1330 void perf_event_task_sched_in(struct task_struct *task)
1332 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1333 struct perf_event_context *ctx = task->perf_event_ctxp;
1335 if (likely(!ctx))
1336 return;
1337 if (cpuctx->task_ctx == ctx)
1338 return;
1339 __perf_event_sched_in(ctx, cpuctx);
1340 cpuctx->task_ctx = ctx;
1343 static void perf_event_cpu_sched_in(struct perf_cpu_context *cpuctx)
1345 struct perf_event_context *ctx = &cpuctx->ctx;
1347 __perf_event_sched_in(ctx, cpuctx);
1350 #define MAX_INTERRUPTS (~0ULL)
1352 static void perf_log_throttle(struct perf_event *event, int enable);
1354 static void perf_adjust_period(struct perf_event *event, u64 events)
1356 struct hw_perf_event *hwc = &event->hw;
1357 u64 period, sample_period;
1358 s64 delta;
1360 events *= hwc->sample_period;
1361 period = div64_u64(events, event->attr.sample_freq);
1363 delta = (s64)(period - hwc->sample_period);
1364 delta = (delta + 7) / 8; /* low pass filter */
1366 sample_period = hwc->sample_period + delta;
1368 if (!sample_period)
1369 sample_period = 1;
1371 hwc->sample_period = sample_period;
1374 static void perf_ctx_adjust_freq(struct perf_event_context *ctx)
1376 struct perf_event *event;
1377 struct hw_perf_event *hwc;
1378 u64 interrupts, freq;
1380 raw_spin_lock(&ctx->lock);
1381 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
1382 if (event->state != PERF_EVENT_STATE_ACTIVE)
1383 continue;
1385 if (event->cpu != -1 && event->cpu != smp_processor_id())
1386 continue;
1388 hwc = &event->hw;
1390 interrupts = hwc->interrupts;
1391 hwc->interrupts = 0;
1394 * unthrottle events on the tick
1396 if (interrupts == MAX_INTERRUPTS) {
1397 perf_log_throttle(event, 1);
1398 event->pmu->unthrottle(event);
1399 interrupts = 2*sysctl_perf_event_sample_rate/HZ;
1402 if (!event->attr.freq || !event->attr.sample_freq)
1403 continue;
1406 * if the specified freq < HZ then we need to skip ticks
1408 if (event->attr.sample_freq < HZ) {
1409 freq = event->attr.sample_freq;
1411 hwc->freq_count += freq;
1412 hwc->freq_interrupts += interrupts;
1414 if (hwc->freq_count < HZ)
1415 continue;
1417 interrupts = hwc->freq_interrupts;
1418 hwc->freq_interrupts = 0;
1419 hwc->freq_count -= HZ;
1420 } else
1421 freq = HZ;
1423 perf_adjust_period(event, freq * interrupts);
1426 * In order to avoid being stalled by an (accidental) huge
1427 * sample period, force reset the sample period if we didn't
1428 * get any events in this freq period.
1430 if (!interrupts) {
1431 perf_disable();
1432 event->pmu->disable(event);
1433 atomic64_set(&hwc->period_left, 0);
1434 event->pmu->enable(event);
1435 perf_enable();
1438 raw_spin_unlock(&ctx->lock);
1442 * Round-robin a context's events:
1444 static void rotate_ctx(struct perf_event_context *ctx)
1446 struct perf_event *event;
1448 if (!ctx->nr_events)
1449 return;
1451 raw_spin_lock(&ctx->lock);
1453 * Rotate the first entry last (works just fine for group events too):
1455 perf_disable();
1456 list_for_each_entry(event, &ctx->group_list, group_entry) {
1457 list_move_tail(&event->group_entry, &ctx->group_list);
1458 break;
1460 perf_enable();
1462 raw_spin_unlock(&ctx->lock);
1465 void perf_event_task_tick(struct task_struct *curr)
1467 struct perf_cpu_context *cpuctx;
1468 struct perf_event_context *ctx;
1470 if (!atomic_read(&nr_events))
1471 return;
1473 cpuctx = &__get_cpu_var(perf_cpu_context);
1474 ctx = curr->perf_event_ctxp;
1476 perf_ctx_adjust_freq(&cpuctx->ctx);
1477 if (ctx)
1478 perf_ctx_adjust_freq(ctx);
1480 perf_event_cpu_sched_out(cpuctx);
1481 if (ctx)
1482 __perf_event_task_sched_out(ctx);
1484 rotate_ctx(&cpuctx->ctx);
1485 if (ctx)
1486 rotate_ctx(ctx);
1488 perf_event_cpu_sched_in(cpuctx);
1489 if (ctx)
1490 perf_event_task_sched_in(curr);
1494 * Enable all of a task's events that have been marked enable-on-exec.
1495 * This expects task == current.
1497 static void perf_event_enable_on_exec(struct task_struct *task)
1499 struct perf_event_context *ctx;
1500 struct perf_event *event;
1501 unsigned long flags;
1502 int enabled = 0;
1504 local_irq_save(flags);
1505 ctx = task->perf_event_ctxp;
1506 if (!ctx || !ctx->nr_events)
1507 goto out;
1509 __perf_event_task_sched_out(ctx);
1511 raw_spin_lock(&ctx->lock);
1513 list_for_each_entry(event, &ctx->group_list, group_entry) {
1514 if (!event->attr.enable_on_exec)
1515 continue;
1516 event->attr.enable_on_exec = 0;
1517 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1518 continue;
1519 __perf_event_mark_enabled(event, ctx);
1520 enabled = 1;
1524 * Unclone this context if we enabled any event.
1526 if (enabled)
1527 unclone_ctx(ctx);
1529 raw_spin_unlock(&ctx->lock);
1531 perf_event_task_sched_in(task);
1532 out:
1533 local_irq_restore(flags);
1537 * Cross CPU call to read the hardware event
1539 static void __perf_event_read(void *info)
1541 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1542 struct perf_event *event = info;
1543 struct perf_event_context *ctx = event->ctx;
1546 * If this is a task context, we need to check whether it is
1547 * the current task context of this cpu. If not it has been
1548 * scheduled out before the smp call arrived. In that case
1549 * event->count would have been updated to a recent sample
1550 * when the event was scheduled out.
1552 if (ctx->task && cpuctx->task_ctx != ctx)
1553 return;
1555 raw_spin_lock(&ctx->lock);
1556 update_context_time(ctx);
1557 update_event_times(event);
1558 raw_spin_unlock(&ctx->lock);
1560 event->pmu->read(event);
1563 static u64 perf_event_read(struct perf_event *event)
1566 * If event is enabled and currently active on a CPU, update the
1567 * value in the event structure:
1569 if (event->state == PERF_EVENT_STATE_ACTIVE) {
1570 smp_call_function_single(event->oncpu,
1571 __perf_event_read, event, 1);
1572 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
1573 struct perf_event_context *ctx = event->ctx;
1574 unsigned long flags;
1576 raw_spin_lock_irqsave(&ctx->lock, flags);
1577 update_context_time(ctx);
1578 update_event_times(event);
1579 raw_spin_unlock_irqrestore(&ctx->lock, flags);
1582 return atomic64_read(&event->count);
1586 * Initialize the perf_event context in a task_struct:
1588 static void
1589 __perf_event_init_context(struct perf_event_context *ctx,
1590 struct task_struct *task)
1592 raw_spin_lock_init(&ctx->lock);
1593 mutex_init(&ctx->mutex);
1594 INIT_LIST_HEAD(&ctx->group_list);
1595 INIT_LIST_HEAD(&ctx->event_list);
1596 atomic_set(&ctx->refcount, 1);
1597 ctx->task = task;
1600 static struct perf_event_context *find_get_context(pid_t pid, int cpu)
1602 struct perf_event_context *ctx;
1603 struct perf_cpu_context *cpuctx;
1604 struct task_struct *task;
1605 unsigned long flags;
1606 int err;
1608 if (pid == -1 && cpu != -1) {
1609 /* Must be root to operate on a CPU event: */
1610 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
1611 return ERR_PTR(-EACCES);
1613 if (cpu < 0 || cpu >= nr_cpumask_bits)
1614 return ERR_PTR(-EINVAL);
1617 * We could be clever and allow to attach a event to an
1618 * offline CPU and activate it when the CPU comes up, but
1619 * that's for later.
1621 if (!cpu_online(cpu))
1622 return ERR_PTR(-ENODEV);
1624 cpuctx = &per_cpu(perf_cpu_context, cpu);
1625 ctx = &cpuctx->ctx;
1626 get_ctx(ctx);
1628 return ctx;
1631 rcu_read_lock();
1632 if (!pid)
1633 task = current;
1634 else
1635 task = find_task_by_vpid(pid);
1636 if (task)
1637 get_task_struct(task);
1638 rcu_read_unlock();
1640 if (!task)
1641 return ERR_PTR(-ESRCH);
1644 * Can't attach events to a dying task.
1646 err = -ESRCH;
1647 if (task->flags & PF_EXITING)
1648 goto errout;
1650 /* Reuse ptrace permission checks for now. */
1651 err = -EACCES;
1652 if (!ptrace_may_access(task, PTRACE_MODE_READ))
1653 goto errout;
1655 retry:
1656 ctx = perf_lock_task_context(task, &flags);
1657 if (ctx) {
1658 unclone_ctx(ctx);
1659 raw_spin_unlock_irqrestore(&ctx->lock, flags);
1662 if (!ctx) {
1663 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
1664 err = -ENOMEM;
1665 if (!ctx)
1666 goto errout;
1667 __perf_event_init_context(ctx, task);
1668 get_ctx(ctx);
1669 if (cmpxchg(&task->perf_event_ctxp, NULL, ctx)) {
1671 * We raced with some other task; use
1672 * the context they set.
1674 kfree(ctx);
1675 goto retry;
1677 get_task_struct(task);
1680 put_task_struct(task);
1681 return ctx;
1683 errout:
1684 put_task_struct(task);
1685 return ERR_PTR(err);
1688 static void perf_event_free_filter(struct perf_event *event);
1690 static void free_event_rcu(struct rcu_head *head)
1692 struct perf_event *event;
1694 event = container_of(head, struct perf_event, rcu_head);
1695 if (event->ns)
1696 put_pid_ns(event->ns);
1697 perf_event_free_filter(event);
1698 kfree(event);
1701 static void perf_pending_sync(struct perf_event *event);
1703 static void free_event(struct perf_event *event)
1705 perf_pending_sync(event);
1707 if (!event->parent) {
1708 atomic_dec(&nr_events);
1709 if (event->attr.mmap)
1710 atomic_dec(&nr_mmap_events);
1711 if (event->attr.comm)
1712 atomic_dec(&nr_comm_events);
1713 if (event->attr.task)
1714 atomic_dec(&nr_task_events);
1717 if (event->output) {
1718 fput(event->output->filp);
1719 event->output = NULL;
1722 if (event->destroy)
1723 event->destroy(event);
1725 put_ctx(event->ctx);
1726 call_rcu(&event->rcu_head, free_event_rcu);
1729 int perf_event_release_kernel(struct perf_event *event)
1731 struct perf_event_context *ctx = event->ctx;
1733 WARN_ON_ONCE(ctx->parent_ctx);
1734 mutex_lock(&ctx->mutex);
1735 perf_event_remove_from_context(event);
1736 mutex_unlock(&ctx->mutex);
1738 mutex_lock(&event->owner->perf_event_mutex);
1739 list_del_init(&event->owner_entry);
1740 mutex_unlock(&event->owner->perf_event_mutex);
1741 put_task_struct(event->owner);
1743 free_event(event);
1745 return 0;
1747 EXPORT_SYMBOL_GPL(perf_event_release_kernel);
1750 * Called when the last reference to the file is gone.
1752 static int perf_release(struct inode *inode, struct file *file)
1754 struct perf_event *event = file->private_data;
1756 file->private_data = NULL;
1758 return perf_event_release_kernel(event);
1761 static int perf_event_read_size(struct perf_event *event)
1763 int entry = sizeof(u64); /* value */
1764 int size = 0;
1765 int nr = 1;
1767 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1768 size += sizeof(u64);
1770 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1771 size += sizeof(u64);
1773 if (event->attr.read_format & PERF_FORMAT_ID)
1774 entry += sizeof(u64);
1776 if (event->attr.read_format & PERF_FORMAT_GROUP) {
1777 nr += event->group_leader->nr_siblings;
1778 size += sizeof(u64);
1781 size += entry * nr;
1783 return size;
1786 u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
1788 struct perf_event *child;
1789 u64 total = 0;
1791 *enabled = 0;
1792 *running = 0;
1794 mutex_lock(&event->child_mutex);
1795 total += perf_event_read(event);
1796 *enabled += event->total_time_enabled +
1797 atomic64_read(&event->child_total_time_enabled);
1798 *running += event->total_time_running +
1799 atomic64_read(&event->child_total_time_running);
1801 list_for_each_entry(child, &event->child_list, child_list) {
1802 total += perf_event_read(child);
1803 *enabled += child->total_time_enabled;
1804 *running += child->total_time_running;
1806 mutex_unlock(&event->child_mutex);
1808 return total;
1810 EXPORT_SYMBOL_GPL(perf_event_read_value);
1812 static int perf_event_read_group(struct perf_event *event,
1813 u64 read_format, char __user *buf)
1815 struct perf_event *leader = event->group_leader, *sub;
1816 int n = 0, size = 0, ret = -EFAULT;
1817 struct perf_event_context *ctx = leader->ctx;
1818 u64 values[5];
1819 u64 count, enabled, running;
1821 mutex_lock(&ctx->mutex);
1822 count = perf_event_read_value(leader, &enabled, &running);
1824 values[n++] = 1 + leader->nr_siblings;
1825 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1826 values[n++] = enabled;
1827 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1828 values[n++] = running;
1829 values[n++] = count;
1830 if (read_format & PERF_FORMAT_ID)
1831 values[n++] = primary_event_id(leader);
1833 size = n * sizeof(u64);
1835 if (copy_to_user(buf, values, size))
1836 goto unlock;
1838 ret = size;
1840 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
1841 n = 0;
1843 values[n++] = perf_event_read_value(sub, &enabled, &running);
1844 if (read_format & PERF_FORMAT_ID)
1845 values[n++] = primary_event_id(sub);
1847 size = n * sizeof(u64);
1849 if (copy_to_user(buf + ret, values, size)) {
1850 ret = -EFAULT;
1851 goto unlock;
1854 ret += size;
1856 unlock:
1857 mutex_unlock(&ctx->mutex);
1859 return ret;
1862 static int perf_event_read_one(struct perf_event *event,
1863 u64 read_format, char __user *buf)
1865 u64 enabled, running;
1866 u64 values[4];
1867 int n = 0;
1869 values[n++] = perf_event_read_value(event, &enabled, &running);
1870 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1871 values[n++] = enabled;
1872 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1873 values[n++] = running;
1874 if (read_format & PERF_FORMAT_ID)
1875 values[n++] = primary_event_id(event);
1877 if (copy_to_user(buf, values, n * sizeof(u64)))
1878 return -EFAULT;
1880 return n * sizeof(u64);
1884 * Read the performance event - simple non blocking version for now
1886 static ssize_t
1887 perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
1889 u64 read_format = event->attr.read_format;
1890 int ret;
1893 * Return end-of-file for a read on a event that is in
1894 * error state (i.e. because it was pinned but it couldn't be
1895 * scheduled on to the CPU at some point).
1897 if (event->state == PERF_EVENT_STATE_ERROR)
1898 return 0;
1900 if (count < perf_event_read_size(event))
1901 return -ENOSPC;
1903 WARN_ON_ONCE(event->ctx->parent_ctx);
1904 if (read_format & PERF_FORMAT_GROUP)
1905 ret = perf_event_read_group(event, read_format, buf);
1906 else
1907 ret = perf_event_read_one(event, read_format, buf);
1909 return ret;
1912 static ssize_t
1913 perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
1915 struct perf_event *event = file->private_data;
1917 return perf_read_hw(event, buf, count);
1920 static unsigned int perf_poll(struct file *file, poll_table *wait)
1922 struct perf_event *event = file->private_data;
1923 struct perf_mmap_data *data;
1924 unsigned int events = POLL_HUP;
1926 rcu_read_lock();
1927 data = rcu_dereference(event->data);
1928 if (data)
1929 events = atomic_xchg(&data->poll, 0);
1930 rcu_read_unlock();
1932 poll_wait(file, &event->waitq, wait);
1934 return events;
1937 static void perf_event_reset(struct perf_event *event)
1939 (void)perf_event_read(event);
1940 atomic64_set(&event->count, 0);
1941 perf_event_update_userpage(event);
1945 * Holding the top-level event's child_mutex means that any
1946 * descendant process that has inherited this event will block
1947 * in sync_child_event if it goes to exit, thus satisfying the
1948 * task existence requirements of perf_event_enable/disable.
1950 static void perf_event_for_each_child(struct perf_event *event,
1951 void (*func)(struct perf_event *))
1953 struct perf_event *child;
1955 WARN_ON_ONCE(event->ctx->parent_ctx);
1956 mutex_lock(&event->child_mutex);
1957 func(event);
1958 list_for_each_entry(child, &event->child_list, child_list)
1959 func(child);
1960 mutex_unlock(&event->child_mutex);
1963 static void perf_event_for_each(struct perf_event *event,
1964 void (*func)(struct perf_event *))
1966 struct perf_event_context *ctx = event->ctx;
1967 struct perf_event *sibling;
1969 WARN_ON_ONCE(ctx->parent_ctx);
1970 mutex_lock(&ctx->mutex);
1971 event = event->group_leader;
1973 perf_event_for_each_child(event, func);
1974 func(event);
1975 list_for_each_entry(sibling, &event->sibling_list, group_entry)
1976 perf_event_for_each_child(event, func);
1977 mutex_unlock(&ctx->mutex);
1980 static int perf_event_period(struct perf_event *event, u64 __user *arg)
1982 struct perf_event_context *ctx = event->ctx;
1983 unsigned long size;
1984 int ret = 0;
1985 u64 value;
1987 if (!event->attr.sample_period)
1988 return -EINVAL;
1990 size = copy_from_user(&value, arg, sizeof(value));
1991 if (size != sizeof(value))
1992 return -EFAULT;
1994 if (!value)
1995 return -EINVAL;
1997 raw_spin_lock_irq(&ctx->lock);
1998 if (event->attr.freq) {
1999 if (value > sysctl_perf_event_sample_rate) {
2000 ret = -EINVAL;
2001 goto unlock;
2004 event->attr.sample_freq = value;
2005 } else {
2006 event->attr.sample_period = value;
2007 event->hw.sample_period = value;
2009 unlock:
2010 raw_spin_unlock_irq(&ctx->lock);
2012 return ret;
2015 static int perf_event_set_output(struct perf_event *event, int output_fd);
2016 static int perf_event_set_filter(struct perf_event *event, void __user *arg);
2018 static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2020 struct perf_event *event = file->private_data;
2021 void (*func)(struct perf_event *);
2022 u32 flags = arg;
2024 switch (cmd) {
2025 case PERF_EVENT_IOC_ENABLE:
2026 func = perf_event_enable;
2027 break;
2028 case PERF_EVENT_IOC_DISABLE:
2029 func = perf_event_disable;
2030 break;
2031 case PERF_EVENT_IOC_RESET:
2032 func = perf_event_reset;
2033 break;
2035 case PERF_EVENT_IOC_REFRESH:
2036 return perf_event_refresh(event, arg);
2038 case PERF_EVENT_IOC_PERIOD:
2039 return perf_event_period(event, (u64 __user *)arg);
2041 case PERF_EVENT_IOC_SET_OUTPUT:
2042 return perf_event_set_output(event, arg);
2044 case PERF_EVENT_IOC_SET_FILTER:
2045 return perf_event_set_filter(event, (void __user *)arg);
2047 default:
2048 return -ENOTTY;
2051 if (flags & PERF_IOC_FLAG_GROUP)
2052 perf_event_for_each(event, func);
2053 else
2054 perf_event_for_each_child(event, func);
2056 return 0;
2059 int perf_event_task_enable(void)
2061 struct perf_event *event;
2063 mutex_lock(&current->perf_event_mutex);
2064 list_for_each_entry(event, &current->perf_event_list, owner_entry)
2065 perf_event_for_each_child(event, perf_event_enable);
2066 mutex_unlock(&current->perf_event_mutex);
2068 return 0;
2071 int perf_event_task_disable(void)
2073 struct perf_event *event;
2075 mutex_lock(&current->perf_event_mutex);
2076 list_for_each_entry(event, &current->perf_event_list, owner_entry)
2077 perf_event_for_each_child(event, perf_event_disable);
2078 mutex_unlock(&current->perf_event_mutex);
2080 return 0;
2083 #ifndef PERF_EVENT_INDEX_OFFSET
2084 # define PERF_EVENT_INDEX_OFFSET 0
2085 #endif
2087 static int perf_event_index(struct perf_event *event)
2089 if (event->state != PERF_EVENT_STATE_ACTIVE)
2090 return 0;
2092 return event->hw.idx + 1 - PERF_EVENT_INDEX_OFFSET;
2096 * Callers need to ensure there can be no nesting of this function, otherwise
2097 * the seqlock logic goes bad. We can not serialize this because the arch
2098 * code calls this from NMI context.
2100 void perf_event_update_userpage(struct perf_event *event)
2102 struct perf_event_mmap_page *userpg;
2103 struct perf_mmap_data *data;
2105 rcu_read_lock();
2106 data = rcu_dereference(event->data);
2107 if (!data)
2108 goto unlock;
2110 userpg = data->user_page;
2113 * Disable preemption so as to not let the corresponding user-space
2114 * spin too long if we get preempted.
2116 preempt_disable();
2117 ++userpg->lock;
2118 barrier();
2119 userpg->index = perf_event_index(event);
2120 userpg->offset = atomic64_read(&event->count);
2121 if (event->state == PERF_EVENT_STATE_ACTIVE)
2122 userpg->offset -= atomic64_read(&event->hw.prev_count);
2124 userpg->time_enabled = event->total_time_enabled +
2125 atomic64_read(&event->child_total_time_enabled);
2127 userpg->time_running = event->total_time_running +
2128 atomic64_read(&event->child_total_time_running);
2130 barrier();
2131 ++userpg->lock;
2132 preempt_enable();
2133 unlock:
2134 rcu_read_unlock();
2137 static unsigned long perf_data_size(struct perf_mmap_data *data)
2139 return data->nr_pages << (PAGE_SHIFT + data->data_order);
2142 #ifndef CONFIG_PERF_USE_VMALLOC
2145 * Back perf_mmap() with regular GFP_KERNEL-0 pages.
2148 static struct page *
2149 perf_mmap_to_page(struct perf_mmap_data *data, unsigned long pgoff)
2151 if (pgoff > data->nr_pages)
2152 return NULL;
2154 if (pgoff == 0)
2155 return virt_to_page(data->user_page);
2157 return virt_to_page(data->data_pages[pgoff - 1]);
2160 static struct perf_mmap_data *
2161 perf_mmap_data_alloc(struct perf_event *event, int nr_pages)
2163 struct perf_mmap_data *data;
2164 unsigned long size;
2165 int i;
2167 WARN_ON(atomic_read(&event->mmap_count));
2169 size = sizeof(struct perf_mmap_data);
2170 size += nr_pages * sizeof(void *);
2172 data = kzalloc(size, GFP_KERNEL);
2173 if (!data)
2174 goto fail;
2176 data->user_page = (void *)get_zeroed_page(GFP_KERNEL);
2177 if (!data->user_page)
2178 goto fail_user_page;
2180 for (i = 0; i < nr_pages; i++) {
2181 data->data_pages[i] = (void *)get_zeroed_page(GFP_KERNEL);
2182 if (!data->data_pages[i])
2183 goto fail_data_pages;
2186 data->data_order = 0;
2187 data->nr_pages = nr_pages;
2189 return data;
2191 fail_data_pages:
2192 for (i--; i >= 0; i--)
2193 free_page((unsigned long)data->data_pages[i]);
2195 free_page((unsigned long)data->user_page);
2197 fail_user_page:
2198 kfree(data);
2200 fail:
2201 return NULL;
2204 static void perf_mmap_free_page(unsigned long addr)
2206 struct page *page = virt_to_page((void *)addr);
2208 page->mapping = NULL;
2209 __free_page(page);
2212 static void perf_mmap_data_free(struct perf_mmap_data *data)
2214 int i;
2216 perf_mmap_free_page((unsigned long)data->user_page);
2217 for (i = 0; i < data->nr_pages; i++)
2218 perf_mmap_free_page((unsigned long)data->data_pages[i]);
2219 kfree(data);
2222 #else
2225 * Back perf_mmap() with vmalloc memory.
2227 * Required for architectures that have d-cache aliasing issues.
2230 static struct page *
2231 perf_mmap_to_page(struct perf_mmap_data *data, unsigned long pgoff)
2233 if (pgoff > (1UL << data->data_order))
2234 return NULL;
2236 return vmalloc_to_page((void *)data->user_page + pgoff * PAGE_SIZE);
2239 static void perf_mmap_unmark_page(void *addr)
2241 struct page *page = vmalloc_to_page(addr);
2243 page->mapping = NULL;
2246 static void perf_mmap_data_free_work(struct work_struct *work)
2248 struct perf_mmap_data *data;
2249 void *base;
2250 int i, nr;
2252 data = container_of(work, struct perf_mmap_data, work);
2253 nr = 1 << data->data_order;
2255 base = data->user_page;
2256 for (i = 0; i < nr + 1; i++)
2257 perf_mmap_unmark_page(base + (i * PAGE_SIZE));
2259 vfree(base);
2260 kfree(data);
2263 static void perf_mmap_data_free(struct perf_mmap_data *data)
2265 schedule_work(&data->work);
2268 static struct perf_mmap_data *
2269 perf_mmap_data_alloc(struct perf_event *event, int nr_pages)
2271 struct perf_mmap_data *data;
2272 unsigned long size;
2273 void *all_buf;
2275 WARN_ON(atomic_read(&event->mmap_count));
2277 size = sizeof(struct perf_mmap_data);
2278 size += sizeof(void *);
2280 data = kzalloc(size, GFP_KERNEL);
2281 if (!data)
2282 goto fail;
2284 INIT_WORK(&data->work, perf_mmap_data_free_work);
2286 all_buf = vmalloc_user((nr_pages + 1) * PAGE_SIZE);
2287 if (!all_buf)
2288 goto fail_all_buf;
2290 data->user_page = all_buf;
2291 data->data_pages[0] = all_buf + PAGE_SIZE;
2292 data->data_order = ilog2(nr_pages);
2293 data->nr_pages = 1;
2295 return data;
2297 fail_all_buf:
2298 kfree(data);
2300 fail:
2301 return NULL;
2304 #endif
2306 static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
2308 struct perf_event *event = vma->vm_file->private_data;
2309 struct perf_mmap_data *data;
2310 int ret = VM_FAULT_SIGBUS;
2312 if (vmf->flags & FAULT_FLAG_MKWRITE) {
2313 if (vmf->pgoff == 0)
2314 ret = 0;
2315 return ret;
2318 rcu_read_lock();
2319 data = rcu_dereference(event->data);
2320 if (!data)
2321 goto unlock;
2323 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
2324 goto unlock;
2326 vmf->page = perf_mmap_to_page(data, vmf->pgoff);
2327 if (!vmf->page)
2328 goto unlock;
2330 get_page(vmf->page);
2331 vmf->page->mapping = vma->vm_file->f_mapping;
2332 vmf->page->index = vmf->pgoff;
2334 ret = 0;
2335 unlock:
2336 rcu_read_unlock();
2338 return ret;
2341 static void
2342 perf_mmap_data_init(struct perf_event *event, struct perf_mmap_data *data)
2344 long max_size = perf_data_size(data);
2346 atomic_set(&data->lock, -1);
2348 if (event->attr.watermark) {
2349 data->watermark = min_t(long, max_size,
2350 event->attr.wakeup_watermark);
2353 if (!data->watermark)
2354 data->watermark = max_size / 2;
2357 rcu_assign_pointer(event->data, data);
2360 static void perf_mmap_data_free_rcu(struct rcu_head *rcu_head)
2362 struct perf_mmap_data *data;
2364 data = container_of(rcu_head, struct perf_mmap_data, rcu_head);
2365 perf_mmap_data_free(data);
2368 static void perf_mmap_data_release(struct perf_event *event)
2370 struct perf_mmap_data *data = event->data;
2372 WARN_ON(atomic_read(&event->mmap_count));
2374 rcu_assign_pointer(event->data, NULL);
2375 call_rcu(&data->rcu_head, perf_mmap_data_free_rcu);
2378 static void perf_mmap_open(struct vm_area_struct *vma)
2380 struct perf_event *event = vma->vm_file->private_data;
2382 atomic_inc(&event->mmap_count);
2385 static void perf_mmap_close(struct vm_area_struct *vma)
2387 struct perf_event *event = vma->vm_file->private_data;
2389 WARN_ON_ONCE(event->ctx->parent_ctx);
2390 if (atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) {
2391 unsigned long size = perf_data_size(event->data);
2392 struct user_struct *user = current_user();
2394 atomic_long_sub((size >> PAGE_SHIFT) + 1, &user->locked_vm);
2395 vma->vm_mm->locked_vm -= event->data->nr_locked;
2396 perf_mmap_data_release(event);
2397 mutex_unlock(&event->mmap_mutex);
2401 static const struct vm_operations_struct perf_mmap_vmops = {
2402 .open = perf_mmap_open,
2403 .close = perf_mmap_close,
2404 .fault = perf_mmap_fault,
2405 .page_mkwrite = perf_mmap_fault,
2408 static int perf_mmap(struct file *file, struct vm_area_struct *vma)
2410 struct perf_event *event = file->private_data;
2411 unsigned long user_locked, user_lock_limit;
2412 struct user_struct *user = current_user();
2413 unsigned long locked, lock_limit;
2414 struct perf_mmap_data *data;
2415 unsigned long vma_size;
2416 unsigned long nr_pages;
2417 long user_extra, extra;
2418 int ret = 0;
2420 if (!(vma->vm_flags & VM_SHARED))
2421 return -EINVAL;
2423 vma_size = vma->vm_end - vma->vm_start;
2424 nr_pages = (vma_size / PAGE_SIZE) - 1;
2427 * If we have data pages ensure they're a power-of-two number, so we
2428 * can do bitmasks instead of modulo.
2430 if (nr_pages != 0 && !is_power_of_2(nr_pages))
2431 return -EINVAL;
2433 if (vma_size != PAGE_SIZE * (1 + nr_pages))
2434 return -EINVAL;
2436 if (vma->vm_pgoff != 0)
2437 return -EINVAL;
2439 WARN_ON_ONCE(event->ctx->parent_ctx);
2440 mutex_lock(&event->mmap_mutex);
2441 if (event->output) {
2442 ret = -EINVAL;
2443 goto unlock;
2446 if (atomic_inc_not_zero(&event->mmap_count)) {
2447 if (nr_pages != event->data->nr_pages)
2448 ret = -EINVAL;
2449 goto unlock;
2452 user_extra = nr_pages + 1;
2453 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
2456 * Increase the limit linearly with more CPUs:
2458 user_lock_limit *= num_online_cpus();
2460 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
2462 extra = 0;
2463 if (user_locked > user_lock_limit)
2464 extra = user_locked - user_lock_limit;
2466 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
2467 lock_limit >>= PAGE_SHIFT;
2468 locked = vma->vm_mm->locked_vm + extra;
2470 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
2471 !capable(CAP_IPC_LOCK)) {
2472 ret = -EPERM;
2473 goto unlock;
2476 WARN_ON(event->data);
2478 data = perf_mmap_data_alloc(event, nr_pages);
2479 ret = -ENOMEM;
2480 if (!data)
2481 goto unlock;
2483 ret = 0;
2484 perf_mmap_data_init(event, data);
2486 atomic_set(&event->mmap_count, 1);
2487 atomic_long_add(user_extra, &user->locked_vm);
2488 vma->vm_mm->locked_vm += extra;
2489 event->data->nr_locked = extra;
2490 if (vma->vm_flags & VM_WRITE)
2491 event->data->writable = 1;
2493 unlock:
2494 mutex_unlock(&event->mmap_mutex);
2496 vma->vm_flags |= VM_RESERVED;
2497 vma->vm_ops = &perf_mmap_vmops;
2499 return ret;
2502 static int perf_fasync(int fd, struct file *filp, int on)
2504 struct inode *inode = filp->f_path.dentry->d_inode;
2505 struct perf_event *event = filp->private_data;
2506 int retval;
2508 mutex_lock(&inode->i_mutex);
2509 retval = fasync_helper(fd, filp, on, &event->fasync);
2510 mutex_unlock(&inode->i_mutex);
2512 if (retval < 0)
2513 return retval;
2515 return 0;
2518 static const struct file_operations perf_fops = {
2519 .release = perf_release,
2520 .read = perf_read,
2521 .poll = perf_poll,
2522 .unlocked_ioctl = perf_ioctl,
2523 .compat_ioctl = perf_ioctl,
2524 .mmap = perf_mmap,
2525 .fasync = perf_fasync,
2529 * Perf event wakeup
2531 * If there's data, ensure we set the poll() state and publish everything
2532 * to user-space before waking everybody up.
2535 void perf_event_wakeup(struct perf_event *event)
2537 wake_up_all(&event->waitq);
2539 if (event->pending_kill) {
2540 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
2541 event->pending_kill = 0;
2546 * Pending wakeups
2548 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
2550 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
2551 * single linked list and use cmpxchg() to add entries lockless.
2554 static void perf_pending_event(struct perf_pending_entry *entry)
2556 struct perf_event *event = container_of(entry,
2557 struct perf_event, pending);
2559 if (event->pending_disable) {
2560 event->pending_disable = 0;
2561 __perf_event_disable(event);
2564 if (event->pending_wakeup) {
2565 event->pending_wakeup = 0;
2566 perf_event_wakeup(event);
2570 #define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
2572 static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
2573 PENDING_TAIL,
2576 static void perf_pending_queue(struct perf_pending_entry *entry,
2577 void (*func)(struct perf_pending_entry *))
2579 struct perf_pending_entry **head;
2581 if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
2582 return;
2584 entry->func = func;
2586 head = &get_cpu_var(perf_pending_head);
2588 do {
2589 entry->next = *head;
2590 } while (cmpxchg(head, entry->next, entry) != entry->next);
2592 set_perf_event_pending();
2594 put_cpu_var(perf_pending_head);
2597 static int __perf_pending_run(void)
2599 struct perf_pending_entry *list;
2600 int nr = 0;
2602 list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
2603 while (list != PENDING_TAIL) {
2604 void (*func)(struct perf_pending_entry *);
2605 struct perf_pending_entry *entry = list;
2607 list = list->next;
2609 func = entry->func;
2610 entry->next = NULL;
2612 * Ensure we observe the unqueue before we issue the wakeup,
2613 * so that we won't be waiting forever.
2614 * -- see perf_not_pending().
2616 smp_wmb();
2618 func(entry);
2619 nr++;
2622 return nr;
2625 static inline int perf_not_pending(struct perf_event *event)
2628 * If we flush on whatever cpu we run, there is a chance we don't
2629 * need to wait.
2631 get_cpu();
2632 __perf_pending_run();
2633 put_cpu();
2636 * Ensure we see the proper queue state before going to sleep
2637 * so that we do not miss the wakeup. -- see perf_pending_handle()
2639 smp_rmb();
2640 return event->pending.next == NULL;
2643 static void perf_pending_sync(struct perf_event *event)
2645 wait_event(event->waitq, perf_not_pending(event));
2648 void perf_event_do_pending(void)
2650 __perf_pending_run();
2654 * Callchain support -- arch specific
2657 __weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
2659 return NULL;
2663 * Output
2665 static bool perf_output_space(struct perf_mmap_data *data, unsigned long tail,
2666 unsigned long offset, unsigned long head)
2668 unsigned long mask;
2670 if (!data->writable)
2671 return true;
2673 mask = perf_data_size(data) - 1;
2675 offset = (offset - tail) & mask;
2676 head = (head - tail) & mask;
2678 if ((int)(head - offset) < 0)
2679 return false;
2681 return true;
2684 static void perf_output_wakeup(struct perf_output_handle *handle)
2686 atomic_set(&handle->data->poll, POLL_IN);
2688 if (handle->nmi) {
2689 handle->event->pending_wakeup = 1;
2690 perf_pending_queue(&handle->event->pending,
2691 perf_pending_event);
2692 } else
2693 perf_event_wakeup(handle->event);
2697 * Curious locking construct.
2699 * We need to ensure a later event_id doesn't publish a head when a former
2700 * event_id isn't done writing. However since we need to deal with NMIs we
2701 * cannot fully serialize things.
2703 * What we do is serialize between CPUs so we only have to deal with NMI
2704 * nesting on a single CPU.
2706 * We only publish the head (and generate a wakeup) when the outer-most
2707 * event_id completes.
2709 static void perf_output_lock(struct perf_output_handle *handle)
2711 struct perf_mmap_data *data = handle->data;
2712 int cur, cpu = get_cpu();
2714 handle->locked = 0;
2716 for (;;) {
2717 cur = atomic_cmpxchg(&data->lock, -1, cpu);
2718 if (cur == -1) {
2719 handle->locked = 1;
2720 break;
2722 if (cur == cpu)
2723 break;
2725 cpu_relax();
2729 static void perf_output_unlock(struct perf_output_handle *handle)
2731 struct perf_mmap_data *data = handle->data;
2732 unsigned long head;
2733 int cpu;
2735 data->done_head = data->head;
2737 if (!handle->locked)
2738 goto out;
2740 again:
2742 * The xchg implies a full barrier that ensures all writes are done
2743 * before we publish the new head, matched by a rmb() in userspace when
2744 * reading this position.
2746 while ((head = atomic_long_xchg(&data->done_head, 0)))
2747 data->user_page->data_head = head;
2750 * NMI can happen here, which means we can miss a done_head update.
2753 cpu = atomic_xchg(&data->lock, -1);
2754 WARN_ON_ONCE(cpu != smp_processor_id());
2757 * Therefore we have to validate we did not indeed do so.
2759 if (unlikely(atomic_long_read(&data->done_head))) {
2761 * Since we had it locked, we can lock it again.
2763 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
2764 cpu_relax();
2766 goto again;
2769 if (atomic_xchg(&data->wakeup, 0))
2770 perf_output_wakeup(handle);
2771 out:
2772 put_cpu();
2775 void perf_output_copy(struct perf_output_handle *handle,
2776 const void *buf, unsigned int len)
2778 unsigned int pages_mask;
2779 unsigned long offset;
2780 unsigned int size;
2781 void **pages;
2783 offset = handle->offset;
2784 pages_mask = handle->data->nr_pages - 1;
2785 pages = handle->data->data_pages;
2787 do {
2788 unsigned long page_offset;
2789 unsigned long page_size;
2790 int nr;
2792 nr = (offset >> PAGE_SHIFT) & pages_mask;
2793 page_size = 1UL << (handle->data->data_order + PAGE_SHIFT);
2794 page_offset = offset & (page_size - 1);
2795 size = min_t(unsigned int, page_size - page_offset, len);
2797 memcpy(pages[nr] + page_offset, buf, size);
2799 len -= size;
2800 buf += size;
2801 offset += size;
2802 } while (len);
2804 handle->offset = offset;
2807 * Check we didn't copy past our reservation window, taking the
2808 * possible unsigned int wrap into account.
2810 WARN_ON_ONCE(((long)(handle->head - handle->offset)) < 0);
2813 int perf_output_begin(struct perf_output_handle *handle,
2814 struct perf_event *event, unsigned int size,
2815 int nmi, int sample)
2817 struct perf_event *output_event;
2818 struct perf_mmap_data *data;
2819 unsigned long tail, offset, head;
2820 int have_lost;
2821 struct {
2822 struct perf_event_header header;
2823 u64 id;
2824 u64 lost;
2825 } lost_event;
2827 rcu_read_lock();
2829 * For inherited events we send all the output towards the parent.
2831 if (event->parent)
2832 event = event->parent;
2834 output_event = rcu_dereference(event->output);
2835 if (output_event)
2836 event = output_event;
2838 data = rcu_dereference(event->data);
2839 if (!data)
2840 goto out;
2842 handle->data = data;
2843 handle->event = event;
2844 handle->nmi = nmi;
2845 handle->sample = sample;
2847 if (!data->nr_pages)
2848 goto fail;
2850 have_lost = atomic_read(&data->lost);
2851 if (have_lost)
2852 size += sizeof(lost_event);
2854 perf_output_lock(handle);
2856 do {
2858 * Userspace could choose to issue a mb() before updating the
2859 * tail pointer. So that all reads will be completed before the
2860 * write is issued.
2862 tail = ACCESS_ONCE(data->user_page->data_tail);
2863 smp_rmb();
2864 offset = head = atomic_long_read(&data->head);
2865 head += size;
2866 if (unlikely(!perf_output_space(data, tail, offset, head)))
2867 goto fail;
2868 } while (atomic_long_cmpxchg(&data->head, offset, head) != offset);
2870 handle->offset = offset;
2871 handle->head = head;
2873 if (head - tail > data->watermark)
2874 atomic_set(&data->wakeup, 1);
2876 if (have_lost) {
2877 lost_event.header.type = PERF_RECORD_LOST;
2878 lost_event.header.misc = 0;
2879 lost_event.header.size = sizeof(lost_event);
2880 lost_event.id = event->id;
2881 lost_event.lost = atomic_xchg(&data->lost, 0);
2883 perf_output_put(handle, lost_event);
2886 return 0;
2888 fail:
2889 atomic_inc(&data->lost);
2890 perf_output_unlock(handle);
2891 out:
2892 rcu_read_unlock();
2894 return -ENOSPC;
2897 void perf_output_end(struct perf_output_handle *handle)
2899 struct perf_event *event = handle->event;
2900 struct perf_mmap_data *data = handle->data;
2902 int wakeup_events = event->attr.wakeup_events;
2904 if (handle->sample && wakeup_events) {
2905 int events = atomic_inc_return(&data->events);
2906 if (events >= wakeup_events) {
2907 atomic_sub(wakeup_events, &data->events);
2908 atomic_set(&data->wakeup, 1);
2912 perf_output_unlock(handle);
2913 rcu_read_unlock();
2916 static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
2919 * only top level events have the pid namespace they were created in
2921 if (event->parent)
2922 event = event->parent;
2924 return task_tgid_nr_ns(p, event->ns);
2927 static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
2930 * only top level events have the pid namespace they were created in
2932 if (event->parent)
2933 event = event->parent;
2935 return task_pid_nr_ns(p, event->ns);
2938 static void perf_output_read_one(struct perf_output_handle *handle,
2939 struct perf_event *event)
2941 u64 read_format = event->attr.read_format;
2942 u64 values[4];
2943 int n = 0;
2945 values[n++] = atomic64_read(&event->count);
2946 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
2947 values[n++] = event->total_time_enabled +
2948 atomic64_read(&event->child_total_time_enabled);
2950 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
2951 values[n++] = event->total_time_running +
2952 atomic64_read(&event->child_total_time_running);
2954 if (read_format & PERF_FORMAT_ID)
2955 values[n++] = primary_event_id(event);
2957 perf_output_copy(handle, values, n * sizeof(u64));
2961 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
2963 static void perf_output_read_group(struct perf_output_handle *handle,
2964 struct perf_event *event)
2966 struct perf_event *leader = event->group_leader, *sub;
2967 u64 read_format = event->attr.read_format;
2968 u64 values[5];
2969 int n = 0;
2971 values[n++] = 1 + leader->nr_siblings;
2973 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2974 values[n++] = leader->total_time_enabled;
2976 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2977 values[n++] = leader->total_time_running;
2979 if (leader != event)
2980 leader->pmu->read(leader);
2982 values[n++] = atomic64_read(&leader->count);
2983 if (read_format & PERF_FORMAT_ID)
2984 values[n++] = primary_event_id(leader);
2986 perf_output_copy(handle, values, n * sizeof(u64));
2988 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
2989 n = 0;
2991 if (sub != event)
2992 sub->pmu->read(sub);
2994 values[n++] = atomic64_read(&sub->count);
2995 if (read_format & PERF_FORMAT_ID)
2996 values[n++] = primary_event_id(sub);
2998 perf_output_copy(handle, values, n * sizeof(u64));
3002 static void perf_output_read(struct perf_output_handle *handle,
3003 struct perf_event *event)
3005 if (event->attr.read_format & PERF_FORMAT_GROUP)
3006 perf_output_read_group(handle, event);
3007 else
3008 perf_output_read_one(handle, event);
3011 void perf_output_sample(struct perf_output_handle *handle,
3012 struct perf_event_header *header,
3013 struct perf_sample_data *data,
3014 struct perf_event *event)
3016 u64 sample_type = data->type;
3018 perf_output_put(handle, *header);
3020 if (sample_type & PERF_SAMPLE_IP)
3021 perf_output_put(handle, data->ip);
3023 if (sample_type & PERF_SAMPLE_TID)
3024 perf_output_put(handle, data->tid_entry);
3026 if (sample_type & PERF_SAMPLE_TIME)
3027 perf_output_put(handle, data->time);
3029 if (sample_type & PERF_SAMPLE_ADDR)
3030 perf_output_put(handle, data->addr);
3032 if (sample_type & PERF_SAMPLE_ID)
3033 perf_output_put(handle, data->id);
3035 if (sample_type & PERF_SAMPLE_STREAM_ID)
3036 perf_output_put(handle, data->stream_id);
3038 if (sample_type & PERF_SAMPLE_CPU)
3039 perf_output_put(handle, data->cpu_entry);
3041 if (sample_type & PERF_SAMPLE_PERIOD)
3042 perf_output_put(handle, data->period);
3044 if (sample_type & PERF_SAMPLE_READ)
3045 perf_output_read(handle, event);
3047 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3048 if (data->callchain) {
3049 int size = 1;
3051 if (data->callchain)
3052 size += data->callchain->nr;
3054 size *= sizeof(u64);
3056 perf_output_copy(handle, data->callchain, size);
3057 } else {
3058 u64 nr = 0;
3059 perf_output_put(handle, nr);
3063 if (sample_type & PERF_SAMPLE_RAW) {
3064 if (data->raw) {
3065 perf_output_put(handle, data->raw->size);
3066 perf_output_copy(handle, data->raw->data,
3067 data->raw->size);
3068 } else {
3069 struct {
3070 u32 size;
3071 u32 data;
3072 } raw = {
3073 .size = sizeof(u32),
3074 .data = 0,
3076 perf_output_put(handle, raw);
3081 void perf_prepare_sample(struct perf_event_header *header,
3082 struct perf_sample_data *data,
3083 struct perf_event *event,
3084 struct pt_regs *regs)
3086 u64 sample_type = event->attr.sample_type;
3088 data->type = sample_type;
3090 header->type = PERF_RECORD_SAMPLE;
3091 header->size = sizeof(*header);
3093 header->misc = 0;
3094 header->misc |= perf_misc_flags(regs);
3096 if (sample_type & PERF_SAMPLE_IP) {
3097 data->ip = perf_instruction_pointer(regs);
3099 header->size += sizeof(data->ip);
3102 if (sample_type & PERF_SAMPLE_TID) {
3103 /* namespace issues */
3104 data->tid_entry.pid = perf_event_pid(event, current);
3105 data->tid_entry.tid = perf_event_tid(event, current);
3107 header->size += sizeof(data->tid_entry);
3110 if (sample_type & PERF_SAMPLE_TIME) {
3111 data->time = perf_clock();
3113 header->size += sizeof(data->time);
3116 if (sample_type & PERF_SAMPLE_ADDR)
3117 header->size += sizeof(data->addr);
3119 if (sample_type & PERF_SAMPLE_ID) {
3120 data->id = primary_event_id(event);
3122 header->size += sizeof(data->id);
3125 if (sample_type & PERF_SAMPLE_STREAM_ID) {
3126 data->stream_id = event->id;
3128 header->size += sizeof(data->stream_id);
3131 if (sample_type & PERF_SAMPLE_CPU) {
3132 data->cpu_entry.cpu = raw_smp_processor_id();
3133 data->cpu_entry.reserved = 0;
3135 header->size += sizeof(data->cpu_entry);
3138 if (sample_type & PERF_SAMPLE_PERIOD)
3139 header->size += sizeof(data->period);
3141 if (sample_type & PERF_SAMPLE_READ)
3142 header->size += perf_event_read_size(event);
3144 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3145 int size = 1;
3147 data->callchain = perf_callchain(regs);
3149 if (data->callchain)
3150 size += data->callchain->nr;
3152 header->size += size * sizeof(u64);
3155 if (sample_type & PERF_SAMPLE_RAW) {
3156 int size = sizeof(u32);
3158 if (data->raw)
3159 size += data->raw->size;
3160 else
3161 size += sizeof(u32);
3163 WARN_ON_ONCE(size & (sizeof(u64)-1));
3164 header->size += size;
3168 static void perf_event_output(struct perf_event *event, int nmi,
3169 struct perf_sample_data *data,
3170 struct pt_regs *regs)
3172 struct perf_output_handle handle;
3173 struct perf_event_header header;
3175 perf_prepare_sample(&header, data, event, regs);
3177 if (perf_output_begin(&handle, event, header.size, nmi, 1))
3178 return;
3180 perf_output_sample(&handle, &header, data, event);
3182 perf_output_end(&handle);
3186 * read event_id
3189 struct perf_read_event {
3190 struct perf_event_header header;
3192 u32 pid;
3193 u32 tid;
3196 static void
3197 perf_event_read_event(struct perf_event *event,
3198 struct task_struct *task)
3200 struct perf_output_handle handle;
3201 struct perf_read_event read_event = {
3202 .header = {
3203 .type = PERF_RECORD_READ,
3204 .misc = 0,
3205 .size = sizeof(read_event) + perf_event_read_size(event),
3207 .pid = perf_event_pid(event, task),
3208 .tid = perf_event_tid(event, task),
3210 int ret;
3212 ret = perf_output_begin(&handle, event, read_event.header.size, 0, 0);
3213 if (ret)
3214 return;
3216 perf_output_put(&handle, read_event);
3217 perf_output_read(&handle, event);
3219 perf_output_end(&handle);
3223 * task tracking -- fork/exit
3225 * enabled by: attr.comm | attr.mmap | attr.task
3228 struct perf_task_event {
3229 struct task_struct *task;
3230 struct perf_event_context *task_ctx;
3232 struct {
3233 struct perf_event_header header;
3235 u32 pid;
3236 u32 ppid;
3237 u32 tid;
3238 u32 ptid;
3239 u64 time;
3240 } event_id;
3243 static void perf_event_task_output(struct perf_event *event,
3244 struct perf_task_event *task_event)
3246 struct perf_output_handle handle;
3247 int size;
3248 struct task_struct *task = task_event->task;
3249 int ret;
3251 size = task_event->event_id.header.size;
3252 ret = perf_output_begin(&handle, event, size, 0, 0);
3254 if (ret)
3255 return;
3257 task_event->event_id.pid = perf_event_pid(event, task);
3258 task_event->event_id.ppid = perf_event_pid(event, current);
3260 task_event->event_id.tid = perf_event_tid(event, task);
3261 task_event->event_id.ptid = perf_event_tid(event, current);
3263 task_event->event_id.time = perf_clock();
3265 perf_output_put(&handle, task_event->event_id);
3267 perf_output_end(&handle);
3270 static int perf_event_task_match(struct perf_event *event)
3272 if (event->cpu != -1 && event->cpu != smp_processor_id())
3273 return 0;
3275 if (event->attr.comm || event->attr.mmap || event->attr.task)
3276 return 1;
3278 return 0;
3281 static void perf_event_task_ctx(struct perf_event_context *ctx,
3282 struct perf_task_event *task_event)
3284 struct perf_event *event;
3286 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3287 if (perf_event_task_match(event))
3288 perf_event_task_output(event, task_event);
3292 static void perf_event_task_event(struct perf_task_event *task_event)
3294 struct perf_cpu_context *cpuctx;
3295 struct perf_event_context *ctx = task_event->task_ctx;
3297 rcu_read_lock();
3298 cpuctx = &get_cpu_var(perf_cpu_context);
3299 perf_event_task_ctx(&cpuctx->ctx, task_event);
3300 if (!ctx)
3301 ctx = rcu_dereference(task_event->task->perf_event_ctxp);
3302 if (ctx)
3303 perf_event_task_ctx(ctx, task_event);
3304 put_cpu_var(perf_cpu_context);
3305 rcu_read_unlock();
3308 static void perf_event_task(struct task_struct *task,
3309 struct perf_event_context *task_ctx,
3310 int new)
3312 struct perf_task_event task_event;
3314 if (!atomic_read(&nr_comm_events) &&
3315 !atomic_read(&nr_mmap_events) &&
3316 !atomic_read(&nr_task_events))
3317 return;
3319 task_event = (struct perf_task_event){
3320 .task = task,
3321 .task_ctx = task_ctx,
3322 .event_id = {
3323 .header = {
3324 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
3325 .misc = 0,
3326 .size = sizeof(task_event.event_id),
3328 /* .pid */
3329 /* .ppid */
3330 /* .tid */
3331 /* .ptid */
3335 perf_event_task_event(&task_event);
3338 void perf_event_fork(struct task_struct *task)
3340 perf_event_task(task, NULL, 1);
3344 * comm tracking
3347 struct perf_comm_event {
3348 struct task_struct *task;
3349 char *comm;
3350 int comm_size;
3352 struct {
3353 struct perf_event_header header;
3355 u32 pid;
3356 u32 tid;
3357 } event_id;
3360 static void perf_event_comm_output(struct perf_event *event,
3361 struct perf_comm_event *comm_event)
3363 struct perf_output_handle handle;
3364 int size = comm_event->event_id.header.size;
3365 int ret = perf_output_begin(&handle, event, size, 0, 0);
3367 if (ret)
3368 return;
3370 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
3371 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
3373 perf_output_put(&handle, comm_event->event_id);
3374 perf_output_copy(&handle, comm_event->comm,
3375 comm_event->comm_size);
3376 perf_output_end(&handle);
3379 static int perf_event_comm_match(struct perf_event *event)
3381 if (event->cpu != -1 && event->cpu != smp_processor_id())
3382 return 0;
3384 if (event->attr.comm)
3385 return 1;
3387 return 0;
3390 static void perf_event_comm_ctx(struct perf_event_context *ctx,
3391 struct perf_comm_event *comm_event)
3393 struct perf_event *event;
3395 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3396 if (perf_event_comm_match(event))
3397 perf_event_comm_output(event, comm_event);
3401 static void perf_event_comm_event(struct perf_comm_event *comm_event)
3403 struct perf_cpu_context *cpuctx;
3404 struct perf_event_context *ctx;
3405 unsigned int size;
3406 char comm[TASK_COMM_LEN];
3408 memset(comm, 0, sizeof(comm));
3409 strlcpy(comm, comm_event->task->comm, sizeof(comm));
3410 size = ALIGN(strlen(comm)+1, sizeof(u64));
3412 comm_event->comm = comm;
3413 comm_event->comm_size = size;
3415 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
3417 rcu_read_lock();
3418 cpuctx = &get_cpu_var(perf_cpu_context);
3419 perf_event_comm_ctx(&cpuctx->ctx, comm_event);
3420 ctx = rcu_dereference(current->perf_event_ctxp);
3421 if (ctx)
3422 perf_event_comm_ctx(ctx, comm_event);
3423 put_cpu_var(perf_cpu_context);
3424 rcu_read_unlock();
3427 void perf_event_comm(struct task_struct *task)
3429 struct perf_comm_event comm_event;
3431 if (task->perf_event_ctxp)
3432 perf_event_enable_on_exec(task);
3434 if (!atomic_read(&nr_comm_events))
3435 return;
3437 comm_event = (struct perf_comm_event){
3438 .task = task,
3439 /* .comm */
3440 /* .comm_size */
3441 .event_id = {
3442 .header = {
3443 .type = PERF_RECORD_COMM,
3444 .misc = 0,
3445 /* .size */
3447 /* .pid */
3448 /* .tid */
3452 perf_event_comm_event(&comm_event);
3456 * mmap tracking
3459 struct perf_mmap_event {
3460 struct vm_area_struct *vma;
3462 const char *file_name;
3463 int file_size;
3465 struct {
3466 struct perf_event_header header;
3468 u32 pid;
3469 u32 tid;
3470 u64 start;
3471 u64 len;
3472 u64 pgoff;
3473 } event_id;
3476 static void perf_event_mmap_output(struct perf_event *event,
3477 struct perf_mmap_event *mmap_event)
3479 struct perf_output_handle handle;
3480 int size = mmap_event->event_id.header.size;
3481 int ret = perf_output_begin(&handle, event, size, 0, 0);
3483 if (ret)
3484 return;
3486 mmap_event->event_id.pid = perf_event_pid(event, current);
3487 mmap_event->event_id.tid = perf_event_tid(event, current);
3489 perf_output_put(&handle, mmap_event->event_id);
3490 perf_output_copy(&handle, mmap_event->file_name,
3491 mmap_event->file_size);
3492 perf_output_end(&handle);
3495 static int perf_event_mmap_match(struct perf_event *event,
3496 struct perf_mmap_event *mmap_event)
3498 if (event->cpu != -1 && event->cpu != smp_processor_id())
3499 return 0;
3501 if (event->attr.mmap)
3502 return 1;
3504 return 0;
3507 static void perf_event_mmap_ctx(struct perf_event_context *ctx,
3508 struct perf_mmap_event *mmap_event)
3510 struct perf_event *event;
3512 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3513 if (perf_event_mmap_match(event, mmap_event))
3514 perf_event_mmap_output(event, mmap_event);
3518 static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
3520 struct perf_cpu_context *cpuctx;
3521 struct perf_event_context *ctx;
3522 struct vm_area_struct *vma = mmap_event->vma;
3523 struct file *file = vma->vm_file;
3524 unsigned int size;
3525 char tmp[16];
3526 char *buf = NULL;
3527 const char *name;
3529 memset(tmp, 0, sizeof(tmp));
3531 if (file) {
3533 * d_path works from the end of the buffer backwards, so we
3534 * need to add enough zero bytes after the string to handle
3535 * the 64bit alignment we do later.
3537 buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
3538 if (!buf) {
3539 name = strncpy(tmp, "//enomem", sizeof(tmp));
3540 goto got_name;
3542 name = d_path(&file->f_path, buf, PATH_MAX);
3543 if (IS_ERR(name)) {
3544 name = strncpy(tmp, "//toolong", sizeof(tmp));
3545 goto got_name;
3547 } else {
3548 if (arch_vma_name(mmap_event->vma)) {
3549 name = strncpy(tmp, arch_vma_name(mmap_event->vma),
3550 sizeof(tmp));
3551 goto got_name;
3554 if (!vma->vm_mm) {
3555 name = strncpy(tmp, "[vdso]", sizeof(tmp));
3556 goto got_name;
3559 name = strncpy(tmp, "//anon", sizeof(tmp));
3560 goto got_name;
3563 got_name:
3564 size = ALIGN(strlen(name)+1, sizeof(u64));
3566 mmap_event->file_name = name;
3567 mmap_event->file_size = size;
3569 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
3571 rcu_read_lock();
3572 cpuctx = &get_cpu_var(perf_cpu_context);
3573 perf_event_mmap_ctx(&cpuctx->ctx, mmap_event);
3574 ctx = rcu_dereference(current->perf_event_ctxp);
3575 if (ctx)
3576 perf_event_mmap_ctx(ctx, mmap_event);
3577 put_cpu_var(perf_cpu_context);
3578 rcu_read_unlock();
3580 kfree(buf);
3583 void __perf_event_mmap(struct vm_area_struct *vma)
3585 struct perf_mmap_event mmap_event;
3587 if (!atomic_read(&nr_mmap_events))
3588 return;
3590 mmap_event = (struct perf_mmap_event){
3591 .vma = vma,
3592 /* .file_name */
3593 /* .file_size */
3594 .event_id = {
3595 .header = {
3596 .type = PERF_RECORD_MMAP,
3597 .misc = 0,
3598 /* .size */
3600 /* .pid */
3601 /* .tid */
3602 .start = vma->vm_start,
3603 .len = vma->vm_end - vma->vm_start,
3604 .pgoff = vma->vm_pgoff,
3608 perf_event_mmap_event(&mmap_event);
3612 * IRQ throttle logging
3615 static void perf_log_throttle(struct perf_event *event, int enable)
3617 struct perf_output_handle handle;
3618 int ret;
3620 struct {
3621 struct perf_event_header header;
3622 u64 time;
3623 u64 id;
3624 u64 stream_id;
3625 } throttle_event = {
3626 .header = {
3627 .type = PERF_RECORD_THROTTLE,
3628 .misc = 0,
3629 .size = sizeof(throttle_event),
3631 .time = perf_clock(),
3632 .id = primary_event_id(event),
3633 .stream_id = event->id,
3636 if (enable)
3637 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
3639 ret = perf_output_begin(&handle, event, sizeof(throttle_event), 1, 0);
3640 if (ret)
3641 return;
3643 perf_output_put(&handle, throttle_event);
3644 perf_output_end(&handle);
3648 * Generic event overflow handling, sampling.
3651 static int __perf_event_overflow(struct perf_event *event, int nmi,
3652 int throttle, struct perf_sample_data *data,
3653 struct pt_regs *regs)
3655 int events = atomic_read(&event->event_limit);
3656 struct hw_perf_event *hwc = &event->hw;
3657 int ret = 0;
3659 throttle = (throttle && event->pmu->unthrottle != NULL);
3661 if (!throttle) {
3662 hwc->interrupts++;
3663 } else {
3664 if (hwc->interrupts != MAX_INTERRUPTS) {
3665 hwc->interrupts++;
3666 if (HZ * hwc->interrupts >
3667 (u64)sysctl_perf_event_sample_rate) {
3668 hwc->interrupts = MAX_INTERRUPTS;
3669 perf_log_throttle(event, 0);
3670 ret = 1;
3672 } else {
3674 * Keep re-disabling events even though on the previous
3675 * pass we disabled it - just in case we raced with a
3676 * sched-in and the event got enabled again:
3678 ret = 1;
3682 if (event->attr.freq) {
3683 u64 now = perf_clock();
3684 s64 delta = now - hwc->freq_stamp;
3686 hwc->freq_stamp = now;
3688 if (delta > 0 && delta < TICK_NSEC)
3689 perf_adjust_period(event, NSEC_PER_SEC / (int)delta);
3693 * XXX event_limit might not quite work as expected on inherited
3694 * events
3697 event->pending_kill = POLL_IN;
3698 if (events && atomic_dec_and_test(&event->event_limit)) {
3699 ret = 1;
3700 event->pending_kill = POLL_HUP;
3701 if (nmi) {
3702 event->pending_disable = 1;
3703 perf_pending_queue(&event->pending,
3704 perf_pending_event);
3705 } else
3706 perf_event_disable(event);
3709 if (event->overflow_handler)
3710 event->overflow_handler(event, nmi, data, regs);
3711 else
3712 perf_event_output(event, nmi, data, regs);
3714 return ret;
3717 int perf_event_overflow(struct perf_event *event, int nmi,
3718 struct perf_sample_data *data,
3719 struct pt_regs *regs)
3721 return __perf_event_overflow(event, nmi, 1, data, regs);
3725 * Generic software event infrastructure
3729 * We directly increment event->count and keep a second value in
3730 * event->hw.period_left to count intervals. This period event
3731 * is kept in the range [-sample_period, 0] so that we can use the
3732 * sign as trigger.
3735 static u64 perf_swevent_set_period(struct perf_event *event)
3737 struct hw_perf_event *hwc = &event->hw;
3738 u64 period = hwc->last_period;
3739 u64 nr, offset;
3740 s64 old, val;
3742 hwc->last_period = hwc->sample_period;
3744 again:
3745 old = val = atomic64_read(&hwc->period_left);
3746 if (val < 0)
3747 return 0;
3749 nr = div64_u64(period + val, period);
3750 offset = nr * period;
3751 val -= offset;
3752 if (atomic64_cmpxchg(&hwc->period_left, old, val) != old)
3753 goto again;
3755 return nr;
3758 static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
3759 int nmi, struct perf_sample_data *data,
3760 struct pt_regs *regs)
3762 struct hw_perf_event *hwc = &event->hw;
3763 int throttle = 0;
3765 data->period = event->hw.last_period;
3766 if (!overflow)
3767 overflow = perf_swevent_set_period(event);
3769 if (hwc->interrupts == MAX_INTERRUPTS)
3770 return;
3772 for (; overflow; overflow--) {
3773 if (__perf_event_overflow(event, nmi, throttle,
3774 data, regs)) {
3776 * We inhibit the overflow from happening when
3777 * hwc->interrupts == MAX_INTERRUPTS.
3779 break;
3781 throttle = 1;
3785 static void perf_swevent_unthrottle(struct perf_event *event)
3788 * Nothing to do, we already reset hwc->interrupts.
3792 static void perf_swevent_add(struct perf_event *event, u64 nr,
3793 int nmi, struct perf_sample_data *data,
3794 struct pt_regs *regs)
3796 struct hw_perf_event *hwc = &event->hw;
3798 atomic64_add(nr, &event->count);
3800 if (!regs)
3801 return;
3803 if (!hwc->sample_period)
3804 return;
3806 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
3807 return perf_swevent_overflow(event, 1, nmi, data, regs);
3809 if (atomic64_add_negative(nr, &hwc->period_left))
3810 return;
3812 perf_swevent_overflow(event, 0, nmi, data, regs);
3815 static int perf_swevent_is_counting(struct perf_event *event)
3818 * The event is active, we're good!
3820 if (event->state == PERF_EVENT_STATE_ACTIVE)
3821 return 1;
3824 * The event is off/error, not counting.
3826 if (event->state != PERF_EVENT_STATE_INACTIVE)
3827 return 0;
3830 * The event is inactive, if the context is active
3831 * we're part of a group that didn't make it on the 'pmu',
3832 * not counting.
3834 if (event->ctx->is_active)
3835 return 0;
3838 * We're inactive and the context is too, this means the
3839 * task is scheduled out, we're counting events that happen
3840 * to us, like migration events.
3842 return 1;
3845 static int perf_tp_event_match(struct perf_event *event,
3846 struct perf_sample_data *data);
3848 static int perf_exclude_event(struct perf_event *event,
3849 struct pt_regs *regs)
3851 if (regs) {
3852 if (event->attr.exclude_user && user_mode(regs))
3853 return 1;
3855 if (event->attr.exclude_kernel && !user_mode(regs))
3856 return 1;
3859 return 0;
3862 static int perf_swevent_match(struct perf_event *event,
3863 enum perf_type_id type,
3864 u32 event_id,
3865 struct perf_sample_data *data,
3866 struct pt_regs *regs)
3868 if (event->cpu != -1 && event->cpu != smp_processor_id())
3869 return 0;
3871 if (!perf_swevent_is_counting(event))
3872 return 0;
3874 if (event->attr.type != type)
3875 return 0;
3877 if (event->attr.config != event_id)
3878 return 0;
3880 if (perf_exclude_event(event, regs))
3881 return 0;
3883 if (event->attr.type == PERF_TYPE_TRACEPOINT &&
3884 !perf_tp_event_match(event, data))
3885 return 0;
3887 return 1;
3890 static void perf_swevent_ctx_event(struct perf_event_context *ctx,
3891 enum perf_type_id type,
3892 u32 event_id, u64 nr, int nmi,
3893 struct perf_sample_data *data,
3894 struct pt_regs *regs)
3896 struct perf_event *event;
3898 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3899 if (perf_swevent_match(event, type, event_id, data, regs))
3900 perf_swevent_add(event, nr, nmi, data, regs);
3904 int perf_swevent_get_recursion_context(void)
3906 struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
3907 int rctx;
3909 if (in_nmi())
3910 rctx = 3;
3911 else if (in_irq())
3912 rctx = 2;
3913 else if (in_softirq())
3914 rctx = 1;
3915 else
3916 rctx = 0;
3918 if (cpuctx->recursion[rctx]) {
3919 put_cpu_var(perf_cpu_context);
3920 return -1;
3923 cpuctx->recursion[rctx]++;
3924 barrier();
3926 return rctx;
3928 EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
3930 void perf_swevent_put_recursion_context(int rctx)
3932 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
3933 barrier();
3934 cpuctx->recursion[rctx]--;
3935 put_cpu_var(perf_cpu_context);
3937 EXPORT_SYMBOL_GPL(perf_swevent_put_recursion_context);
3939 static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
3940 u64 nr, int nmi,
3941 struct perf_sample_data *data,
3942 struct pt_regs *regs)
3944 struct perf_cpu_context *cpuctx;
3945 struct perf_event_context *ctx;
3947 cpuctx = &__get_cpu_var(perf_cpu_context);
3948 rcu_read_lock();
3949 perf_swevent_ctx_event(&cpuctx->ctx, type, event_id,
3950 nr, nmi, data, regs);
3952 * doesn't really matter which of the child contexts the
3953 * events ends up in.
3955 ctx = rcu_dereference(current->perf_event_ctxp);
3956 if (ctx)
3957 perf_swevent_ctx_event(ctx, type, event_id, nr, nmi, data, regs);
3958 rcu_read_unlock();
3961 void __perf_sw_event(u32 event_id, u64 nr, int nmi,
3962 struct pt_regs *regs, u64 addr)
3964 struct perf_sample_data data;
3965 int rctx;
3967 rctx = perf_swevent_get_recursion_context();
3968 if (rctx < 0)
3969 return;
3971 data.addr = addr;
3972 data.raw = NULL;
3974 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, nmi, &data, regs);
3976 perf_swevent_put_recursion_context(rctx);
3979 static void perf_swevent_read(struct perf_event *event)
3983 static int perf_swevent_enable(struct perf_event *event)
3985 struct hw_perf_event *hwc = &event->hw;
3987 if (hwc->sample_period) {
3988 hwc->last_period = hwc->sample_period;
3989 perf_swevent_set_period(event);
3991 return 0;
3994 static void perf_swevent_disable(struct perf_event *event)
3998 static const struct pmu perf_ops_generic = {
3999 .enable = perf_swevent_enable,
4000 .disable = perf_swevent_disable,
4001 .read = perf_swevent_read,
4002 .unthrottle = perf_swevent_unthrottle,
4006 * hrtimer based swevent callback
4009 static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
4011 enum hrtimer_restart ret = HRTIMER_RESTART;
4012 struct perf_sample_data data;
4013 struct pt_regs *regs;
4014 struct perf_event *event;
4015 u64 period;
4017 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
4018 event->pmu->read(event);
4020 data.addr = 0;
4021 data.raw = NULL;
4022 data.period = event->hw.last_period;
4023 regs = get_irq_regs();
4025 * In case we exclude kernel IPs or are somehow not in interrupt
4026 * context, provide the next best thing, the user IP.
4028 if ((event->attr.exclude_kernel || !regs) &&
4029 !event->attr.exclude_user)
4030 regs = task_pt_regs(current);
4032 if (regs) {
4033 if (!(event->attr.exclude_idle && current->pid == 0))
4034 if (perf_event_overflow(event, 0, &data, regs))
4035 ret = HRTIMER_NORESTART;
4038 period = max_t(u64, 10000, event->hw.sample_period);
4039 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
4041 return ret;
4044 static void perf_swevent_start_hrtimer(struct perf_event *event)
4046 struct hw_perf_event *hwc = &event->hw;
4048 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
4049 hwc->hrtimer.function = perf_swevent_hrtimer;
4050 if (hwc->sample_period) {
4051 u64 period;
4053 if (hwc->remaining) {
4054 if (hwc->remaining < 0)
4055 period = 10000;
4056 else
4057 period = hwc->remaining;
4058 hwc->remaining = 0;
4059 } else {
4060 period = max_t(u64, 10000, hwc->sample_period);
4062 __hrtimer_start_range_ns(&hwc->hrtimer,
4063 ns_to_ktime(period), 0,
4064 HRTIMER_MODE_REL, 0);
4068 static void perf_swevent_cancel_hrtimer(struct perf_event *event)
4070 struct hw_perf_event *hwc = &event->hw;
4072 if (hwc->sample_period) {
4073 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
4074 hwc->remaining = ktime_to_ns(remaining);
4076 hrtimer_cancel(&hwc->hrtimer);
4081 * Software event: cpu wall time clock
4084 static void cpu_clock_perf_event_update(struct perf_event *event)
4086 int cpu = raw_smp_processor_id();
4087 s64 prev;
4088 u64 now;
4090 now = cpu_clock(cpu);
4091 prev = atomic64_xchg(&event->hw.prev_count, now);
4092 atomic64_add(now - prev, &event->count);
4095 static int cpu_clock_perf_event_enable(struct perf_event *event)
4097 struct hw_perf_event *hwc = &event->hw;
4098 int cpu = raw_smp_processor_id();
4100 atomic64_set(&hwc->prev_count, cpu_clock(cpu));
4101 perf_swevent_start_hrtimer(event);
4103 return 0;
4106 static void cpu_clock_perf_event_disable(struct perf_event *event)
4108 perf_swevent_cancel_hrtimer(event);
4109 cpu_clock_perf_event_update(event);
4112 static void cpu_clock_perf_event_read(struct perf_event *event)
4114 cpu_clock_perf_event_update(event);
4117 static const struct pmu perf_ops_cpu_clock = {
4118 .enable = cpu_clock_perf_event_enable,
4119 .disable = cpu_clock_perf_event_disable,
4120 .read = cpu_clock_perf_event_read,
4124 * Software event: task time clock
4127 static void task_clock_perf_event_update(struct perf_event *event, u64 now)
4129 u64 prev;
4130 s64 delta;
4132 prev = atomic64_xchg(&event->hw.prev_count, now);
4133 delta = now - prev;
4134 atomic64_add(delta, &event->count);
4137 static int task_clock_perf_event_enable(struct perf_event *event)
4139 struct hw_perf_event *hwc = &event->hw;
4140 u64 now;
4142 now = event->ctx->time;
4144 atomic64_set(&hwc->prev_count, now);
4146 perf_swevent_start_hrtimer(event);
4148 return 0;
4151 static void task_clock_perf_event_disable(struct perf_event *event)
4153 perf_swevent_cancel_hrtimer(event);
4154 task_clock_perf_event_update(event, event->ctx->time);
4158 static void task_clock_perf_event_read(struct perf_event *event)
4160 u64 time;
4162 if (!in_nmi()) {
4163 update_context_time(event->ctx);
4164 time = event->ctx->time;
4165 } else {
4166 u64 now = perf_clock();
4167 u64 delta = now - event->ctx->timestamp;
4168 time = event->ctx->time + delta;
4171 task_clock_perf_event_update(event, time);
4174 static const struct pmu perf_ops_task_clock = {
4175 .enable = task_clock_perf_event_enable,
4176 .disable = task_clock_perf_event_disable,
4177 .read = task_clock_perf_event_read,
4180 #ifdef CONFIG_EVENT_TRACING
4182 void perf_tp_event(int event_id, u64 addr, u64 count, void *record,
4183 int entry_size)
4185 struct perf_raw_record raw = {
4186 .size = entry_size,
4187 .data = record,
4190 struct perf_sample_data data = {
4191 .addr = addr,
4192 .raw = &raw,
4195 struct pt_regs *regs = get_irq_regs();
4197 if (!regs)
4198 regs = task_pt_regs(current);
4200 /* Trace events already protected against recursion */
4201 do_perf_sw_event(PERF_TYPE_TRACEPOINT, event_id, count, 1,
4202 &data, regs);
4204 EXPORT_SYMBOL_GPL(perf_tp_event);
4206 static int perf_tp_event_match(struct perf_event *event,
4207 struct perf_sample_data *data)
4209 void *record = data->raw->data;
4211 if (likely(!event->filter) || filter_match_preds(event->filter, record))
4212 return 1;
4213 return 0;
4216 static void tp_perf_event_destroy(struct perf_event *event)
4218 ftrace_profile_disable(event->attr.config);
4221 static const struct pmu *tp_perf_event_init(struct perf_event *event)
4224 * Raw tracepoint data is a severe data leak, only allow root to
4225 * have these.
4227 if ((event->attr.sample_type & PERF_SAMPLE_RAW) &&
4228 perf_paranoid_tracepoint_raw() &&
4229 !capable(CAP_SYS_ADMIN))
4230 return ERR_PTR(-EPERM);
4232 if (ftrace_profile_enable(event->attr.config))
4233 return NULL;
4235 event->destroy = tp_perf_event_destroy;
4237 return &perf_ops_generic;
4240 static int perf_event_set_filter(struct perf_event *event, void __user *arg)
4242 char *filter_str;
4243 int ret;
4245 if (event->attr.type != PERF_TYPE_TRACEPOINT)
4246 return -EINVAL;
4248 filter_str = strndup_user(arg, PAGE_SIZE);
4249 if (IS_ERR(filter_str))
4250 return PTR_ERR(filter_str);
4252 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
4254 kfree(filter_str);
4255 return ret;
4258 static void perf_event_free_filter(struct perf_event *event)
4260 ftrace_profile_free_filter(event);
4263 #else
4265 static int perf_tp_event_match(struct perf_event *event,
4266 struct perf_sample_data *data)
4268 return 1;
4271 static const struct pmu *tp_perf_event_init(struct perf_event *event)
4273 return NULL;
4276 static int perf_event_set_filter(struct perf_event *event, void __user *arg)
4278 return -ENOENT;
4281 static void perf_event_free_filter(struct perf_event *event)
4285 #endif /* CONFIG_EVENT_TRACING */
4287 #ifdef CONFIG_HAVE_HW_BREAKPOINT
4288 static void bp_perf_event_destroy(struct perf_event *event)
4290 release_bp_slot(event);
4293 static const struct pmu *bp_perf_event_init(struct perf_event *bp)
4295 int err;
4297 err = register_perf_hw_breakpoint(bp);
4298 if (err)
4299 return ERR_PTR(err);
4301 bp->destroy = bp_perf_event_destroy;
4303 return &perf_ops_bp;
4306 void perf_bp_event(struct perf_event *bp, void *data)
4308 struct perf_sample_data sample;
4309 struct pt_regs *regs = data;
4311 sample.raw = NULL;
4312 sample.addr = bp->attr.bp_addr;
4314 if (!perf_exclude_event(bp, regs))
4315 perf_swevent_add(bp, 1, 1, &sample, regs);
4317 #else
4318 static const struct pmu *bp_perf_event_init(struct perf_event *bp)
4320 return NULL;
4323 void perf_bp_event(struct perf_event *bp, void *regs)
4326 #endif
4328 atomic_t perf_swevent_enabled[PERF_COUNT_SW_MAX];
4330 static void sw_perf_event_destroy(struct perf_event *event)
4332 u64 event_id = event->attr.config;
4334 WARN_ON(event->parent);
4336 atomic_dec(&perf_swevent_enabled[event_id]);
4339 static const struct pmu *sw_perf_event_init(struct perf_event *event)
4341 const struct pmu *pmu = NULL;
4342 u64 event_id = event->attr.config;
4345 * Software events (currently) can't in general distinguish
4346 * between user, kernel and hypervisor events.
4347 * However, context switches and cpu migrations are considered
4348 * to be kernel events, and page faults are never hypervisor
4349 * events.
4351 switch (event_id) {
4352 case PERF_COUNT_SW_CPU_CLOCK:
4353 pmu = &perf_ops_cpu_clock;
4355 break;
4356 case PERF_COUNT_SW_TASK_CLOCK:
4358 * If the user instantiates this as a per-cpu event,
4359 * use the cpu_clock event instead.
4361 if (event->ctx->task)
4362 pmu = &perf_ops_task_clock;
4363 else
4364 pmu = &perf_ops_cpu_clock;
4366 break;
4367 case PERF_COUNT_SW_PAGE_FAULTS:
4368 case PERF_COUNT_SW_PAGE_FAULTS_MIN:
4369 case PERF_COUNT_SW_PAGE_FAULTS_MAJ:
4370 case PERF_COUNT_SW_CONTEXT_SWITCHES:
4371 case PERF_COUNT_SW_CPU_MIGRATIONS:
4372 case PERF_COUNT_SW_ALIGNMENT_FAULTS:
4373 case PERF_COUNT_SW_EMULATION_FAULTS:
4374 if (!event->parent) {
4375 atomic_inc(&perf_swevent_enabled[event_id]);
4376 event->destroy = sw_perf_event_destroy;
4378 pmu = &perf_ops_generic;
4379 break;
4382 return pmu;
4386 * Allocate and initialize a event structure
4388 static struct perf_event *
4389 perf_event_alloc(struct perf_event_attr *attr,
4390 int cpu,
4391 struct perf_event_context *ctx,
4392 struct perf_event *group_leader,
4393 struct perf_event *parent_event,
4394 perf_overflow_handler_t overflow_handler,
4395 gfp_t gfpflags)
4397 const struct pmu *pmu;
4398 struct perf_event *event;
4399 struct hw_perf_event *hwc;
4400 long err;
4402 event = kzalloc(sizeof(*event), gfpflags);
4403 if (!event)
4404 return ERR_PTR(-ENOMEM);
4407 * Single events are their own group leaders, with an
4408 * empty sibling list:
4410 if (!group_leader)
4411 group_leader = event;
4413 mutex_init(&event->child_mutex);
4414 INIT_LIST_HEAD(&event->child_list);
4416 INIT_LIST_HEAD(&event->group_entry);
4417 INIT_LIST_HEAD(&event->event_entry);
4418 INIT_LIST_HEAD(&event->sibling_list);
4419 init_waitqueue_head(&event->waitq);
4421 mutex_init(&event->mmap_mutex);
4423 event->cpu = cpu;
4424 event->attr = *attr;
4425 event->group_leader = group_leader;
4426 event->pmu = NULL;
4427 event->ctx = ctx;
4428 event->oncpu = -1;
4430 event->parent = parent_event;
4432 event->ns = get_pid_ns(current->nsproxy->pid_ns);
4433 event->id = atomic64_inc_return(&perf_event_id);
4435 event->state = PERF_EVENT_STATE_INACTIVE;
4437 if (!overflow_handler && parent_event)
4438 overflow_handler = parent_event->overflow_handler;
4440 event->overflow_handler = overflow_handler;
4442 if (attr->disabled)
4443 event->state = PERF_EVENT_STATE_OFF;
4445 pmu = NULL;
4447 hwc = &event->hw;
4448 hwc->sample_period = attr->sample_period;
4449 if (attr->freq && attr->sample_freq)
4450 hwc->sample_period = 1;
4451 hwc->last_period = hwc->sample_period;
4453 atomic64_set(&hwc->period_left, hwc->sample_period);
4456 * we currently do not support PERF_FORMAT_GROUP on inherited events
4458 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
4459 goto done;
4461 switch (attr->type) {
4462 case PERF_TYPE_RAW:
4463 case PERF_TYPE_HARDWARE:
4464 case PERF_TYPE_HW_CACHE:
4465 pmu = hw_perf_event_init(event);
4466 break;
4468 case PERF_TYPE_SOFTWARE:
4469 pmu = sw_perf_event_init(event);
4470 break;
4472 case PERF_TYPE_TRACEPOINT:
4473 pmu = tp_perf_event_init(event);
4474 break;
4476 case PERF_TYPE_BREAKPOINT:
4477 pmu = bp_perf_event_init(event);
4478 break;
4481 default:
4482 break;
4484 done:
4485 err = 0;
4486 if (!pmu)
4487 err = -EINVAL;
4488 else if (IS_ERR(pmu))
4489 err = PTR_ERR(pmu);
4491 if (err) {
4492 if (event->ns)
4493 put_pid_ns(event->ns);
4494 kfree(event);
4495 return ERR_PTR(err);
4498 event->pmu = pmu;
4500 if (!event->parent) {
4501 atomic_inc(&nr_events);
4502 if (event->attr.mmap)
4503 atomic_inc(&nr_mmap_events);
4504 if (event->attr.comm)
4505 atomic_inc(&nr_comm_events);
4506 if (event->attr.task)
4507 atomic_inc(&nr_task_events);
4510 return event;
4513 static int perf_copy_attr(struct perf_event_attr __user *uattr,
4514 struct perf_event_attr *attr)
4516 u32 size;
4517 int ret;
4519 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
4520 return -EFAULT;
4523 * zero the full structure, so that a short copy will be nice.
4525 memset(attr, 0, sizeof(*attr));
4527 ret = get_user(size, &uattr->size);
4528 if (ret)
4529 return ret;
4531 if (size > PAGE_SIZE) /* silly large */
4532 goto err_size;
4534 if (!size) /* abi compat */
4535 size = PERF_ATTR_SIZE_VER0;
4537 if (size < PERF_ATTR_SIZE_VER0)
4538 goto err_size;
4541 * If we're handed a bigger struct than we know of,
4542 * ensure all the unknown bits are 0 - i.e. new
4543 * user-space does not rely on any kernel feature
4544 * extensions we dont know about yet.
4546 if (size > sizeof(*attr)) {
4547 unsigned char __user *addr;
4548 unsigned char __user *end;
4549 unsigned char val;
4551 addr = (void __user *)uattr + sizeof(*attr);
4552 end = (void __user *)uattr + size;
4554 for (; addr < end; addr++) {
4555 ret = get_user(val, addr);
4556 if (ret)
4557 return ret;
4558 if (val)
4559 goto err_size;
4561 size = sizeof(*attr);
4564 ret = copy_from_user(attr, uattr, size);
4565 if (ret)
4566 return -EFAULT;
4569 * If the type exists, the corresponding creation will verify
4570 * the attr->config.
4572 if (attr->type >= PERF_TYPE_MAX)
4573 return -EINVAL;
4575 if (attr->__reserved_1 || attr->__reserved_2)
4576 return -EINVAL;
4578 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
4579 return -EINVAL;
4581 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
4582 return -EINVAL;
4584 out:
4585 return ret;
4587 err_size:
4588 put_user(sizeof(*attr), &uattr->size);
4589 ret = -E2BIG;
4590 goto out;
4593 static int perf_event_set_output(struct perf_event *event, int output_fd)
4595 struct perf_event *output_event = NULL;
4596 struct file *output_file = NULL;
4597 struct perf_event *old_output;
4598 int fput_needed = 0;
4599 int ret = -EINVAL;
4601 if (!output_fd)
4602 goto set;
4604 output_file = fget_light(output_fd, &fput_needed);
4605 if (!output_file)
4606 return -EBADF;
4608 if (output_file->f_op != &perf_fops)
4609 goto out;
4611 output_event = output_file->private_data;
4613 /* Don't chain output fds */
4614 if (output_event->output)
4615 goto out;
4617 /* Don't set an output fd when we already have an output channel */
4618 if (event->data)
4619 goto out;
4621 atomic_long_inc(&output_file->f_count);
4623 set:
4624 mutex_lock(&event->mmap_mutex);
4625 old_output = event->output;
4626 rcu_assign_pointer(event->output, output_event);
4627 mutex_unlock(&event->mmap_mutex);
4629 if (old_output) {
4631 * we need to make sure no existing perf_output_*()
4632 * is still referencing this event.
4634 synchronize_rcu();
4635 fput(old_output->filp);
4638 ret = 0;
4639 out:
4640 fput_light(output_file, fput_needed);
4641 return ret;
4645 * sys_perf_event_open - open a performance event, associate it to a task/cpu
4647 * @attr_uptr: event_id type attributes for monitoring/sampling
4648 * @pid: target pid
4649 * @cpu: target cpu
4650 * @group_fd: group leader event fd
4652 SYSCALL_DEFINE5(perf_event_open,
4653 struct perf_event_attr __user *, attr_uptr,
4654 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
4656 struct perf_event *event, *group_leader;
4657 struct perf_event_attr attr;
4658 struct perf_event_context *ctx;
4659 struct file *event_file = NULL;
4660 struct file *group_file = NULL;
4661 int fput_needed = 0;
4662 int fput_needed2 = 0;
4663 int err;
4665 /* for future expandability... */
4666 if (flags & ~(PERF_FLAG_FD_NO_GROUP | PERF_FLAG_FD_OUTPUT))
4667 return -EINVAL;
4669 err = perf_copy_attr(attr_uptr, &attr);
4670 if (err)
4671 return err;
4673 if (!attr.exclude_kernel) {
4674 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
4675 return -EACCES;
4678 if (attr.freq) {
4679 if (attr.sample_freq > sysctl_perf_event_sample_rate)
4680 return -EINVAL;
4684 * Get the target context (task or percpu):
4686 ctx = find_get_context(pid, cpu);
4687 if (IS_ERR(ctx))
4688 return PTR_ERR(ctx);
4691 * Look up the group leader (we will attach this event to it):
4693 group_leader = NULL;
4694 if (group_fd != -1 && !(flags & PERF_FLAG_FD_NO_GROUP)) {
4695 err = -EINVAL;
4696 group_file = fget_light(group_fd, &fput_needed);
4697 if (!group_file)
4698 goto err_put_context;
4699 if (group_file->f_op != &perf_fops)
4700 goto err_put_context;
4702 group_leader = group_file->private_data;
4704 * Do not allow a recursive hierarchy (this new sibling
4705 * becoming part of another group-sibling):
4707 if (group_leader->group_leader != group_leader)
4708 goto err_put_context;
4710 * Do not allow to attach to a group in a different
4711 * task or CPU context:
4713 if (group_leader->ctx != ctx)
4714 goto err_put_context;
4716 * Only a group leader can be exclusive or pinned
4718 if (attr.exclusive || attr.pinned)
4719 goto err_put_context;
4722 event = perf_event_alloc(&attr, cpu, ctx, group_leader,
4723 NULL, NULL, GFP_KERNEL);
4724 err = PTR_ERR(event);
4725 if (IS_ERR(event))
4726 goto err_put_context;
4728 err = anon_inode_getfd("[perf_event]", &perf_fops, event, O_RDWR);
4729 if (err < 0)
4730 goto err_free_put_context;
4732 event_file = fget_light(err, &fput_needed2);
4733 if (!event_file)
4734 goto err_free_put_context;
4736 if (flags & PERF_FLAG_FD_OUTPUT) {
4737 err = perf_event_set_output(event, group_fd);
4738 if (err)
4739 goto err_fput_free_put_context;
4742 event->filp = event_file;
4743 WARN_ON_ONCE(ctx->parent_ctx);
4744 mutex_lock(&ctx->mutex);
4745 perf_install_in_context(ctx, event, cpu);
4746 ++ctx->generation;
4747 mutex_unlock(&ctx->mutex);
4749 event->owner = current;
4750 get_task_struct(current);
4751 mutex_lock(&current->perf_event_mutex);
4752 list_add_tail(&event->owner_entry, &current->perf_event_list);
4753 mutex_unlock(&current->perf_event_mutex);
4755 err_fput_free_put_context:
4756 fput_light(event_file, fput_needed2);
4758 err_free_put_context:
4759 if (err < 0)
4760 kfree(event);
4762 err_put_context:
4763 if (err < 0)
4764 put_ctx(ctx);
4766 fput_light(group_file, fput_needed);
4768 return err;
4772 * perf_event_create_kernel_counter
4774 * @attr: attributes of the counter to create
4775 * @cpu: cpu in which the counter is bound
4776 * @pid: task to profile
4778 struct perf_event *
4779 perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
4780 pid_t pid,
4781 perf_overflow_handler_t overflow_handler)
4783 struct perf_event *event;
4784 struct perf_event_context *ctx;
4785 int err;
4788 * Get the target context (task or percpu):
4791 ctx = find_get_context(pid, cpu);
4792 if (IS_ERR(ctx)) {
4793 err = PTR_ERR(ctx);
4794 goto err_exit;
4797 event = perf_event_alloc(attr, cpu, ctx, NULL,
4798 NULL, overflow_handler, GFP_KERNEL);
4799 if (IS_ERR(event)) {
4800 err = PTR_ERR(event);
4801 goto err_put_context;
4804 event->filp = NULL;
4805 WARN_ON_ONCE(ctx->parent_ctx);
4806 mutex_lock(&ctx->mutex);
4807 perf_install_in_context(ctx, event, cpu);
4808 ++ctx->generation;
4809 mutex_unlock(&ctx->mutex);
4811 event->owner = current;
4812 get_task_struct(current);
4813 mutex_lock(&current->perf_event_mutex);
4814 list_add_tail(&event->owner_entry, &current->perf_event_list);
4815 mutex_unlock(&current->perf_event_mutex);
4817 return event;
4819 err_put_context:
4820 put_ctx(ctx);
4821 err_exit:
4822 return ERR_PTR(err);
4824 EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
4827 * inherit a event from parent task to child task:
4829 static struct perf_event *
4830 inherit_event(struct perf_event *parent_event,
4831 struct task_struct *parent,
4832 struct perf_event_context *parent_ctx,
4833 struct task_struct *child,
4834 struct perf_event *group_leader,
4835 struct perf_event_context *child_ctx)
4837 struct perf_event *child_event;
4840 * Instead of creating recursive hierarchies of events,
4841 * we link inherited events back to the original parent,
4842 * which has a filp for sure, which we use as the reference
4843 * count:
4845 if (parent_event->parent)
4846 parent_event = parent_event->parent;
4848 child_event = perf_event_alloc(&parent_event->attr,
4849 parent_event->cpu, child_ctx,
4850 group_leader, parent_event,
4851 NULL, GFP_KERNEL);
4852 if (IS_ERR(child_event))
4853 return child_event;
4854 get_ctx(child_ctx);
4857 * Make the child state follow the state of the parent event,
4858 * not its attr.disabled bit. We hold the parent's mutex,
4859 * so we won't race with perf_event_{en, dis}able_family.
4861 if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
4862 child_event->state = PERF_EVENT_STATE_INACTIVE;
4863 else
4864 child_event->state = PERF_EVENT_STATE_OFF;
4866 if (parent_event->attr.freq)
4867 child_event->hw.sample_period = parent_event->hw.sample_period;
4869 child_event->overflow_handler = parent_event->overflow_handler;
4872 * Link it up in the child's context:
4874 add_event_to_ctx(child_event, child_ctx);
4877 * Get a reference to the parent filp - we will fput it
4878 * when the child event exits. This is safe to do because
4879 * we are in the parent and we know that the filp still
4880 * exists and has a nonzero count:
4882 atomic_long_inc(&parent_event->filp->f_count);
4885 * Link this into the parent event's child list
4887 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
4888 mutex_lock(&parent_event->child_mutex);
4889 list_add_tail(&child_event->child_list, &parent_event->child_list);
4890 mutex_unlock(&parent_event->child_mutex);
4892 return child_event;
4895 static int inherit_group(struct perf_event *parent_event,
4896 struct task_struct *parent,
4897 struct perf_event_context *parent_ctx,
4898 struct task_struct *child,
4899 struct perf_event_context *child_ctx)
4901 struct perf_event *leader;
4902 struct perf_event *sub;
4903 struct perf_event *child_ctr;
4905 leader = inherit_event(parent_event, parent, parent_ctx,
4906 child, NULL, child_ctx);
4907 if (IS_ERR(leader))
4908 return PTR_ERR(leader);
4909 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
4910 child_ctr = inherit_event(sub, parent, parent_ctx,
4911 child, leader, child_ctx);
4912 if (IS_ERR(child_ctr))
4913 return PTR_ERR(child_ctr);
4915 return 0;
4918 static void sync_child_event(struct perf_event *child_event,
4919 struct task_struct *child)
4921 struct perf_event *parent_event = child_event->parent;
4922 u64 child_val;
4924 if (child_event->attr.inherit_stat)
4925 perf_event_read_event(child_event, child);
4927 child_val = atomic64_read(&child_event->count);
4930 * Add back the child's count to the parent's count:
4932 atomic64_add(child_val, &parent_event->count);
4933 atomic64_add(child_event->total_time_enabled,
4934 &parent_event->child_total_time_enabled);
4935 atomic64_add(child_event->total_time_running,
4936 &parent_event->child_total_time_running);
4939 * Remove this event from the parent's list
4941 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
4942 mutex_lock(&parent_event->child_mutex);
4943 list_del_init(&child_event->child_list);
4944 mutex_unlock(&parent_event->child_mutex);
4947 * Release the parent event, if this was the last
4948 * reference to it.
4950 fput(parent_event->filp);
4953 static void
4954 __perf_event_exit_task(struct perf_event *child_event,
4955 struct perf_event_context *child_ctx,
4956 struct task_struct *child)
4958 struct perf_event *parent_event;
4960 perf_event_remove_from_context(child_event);
4962 parent_event = child_event->parent;
4964 * It can happen that parent exits first, and has events
4965 * that are still around due to the child reference. These
4966 * events need to be zapped - but otherwise linger.
4968 if (parent_event) {
4969 sync_child_event(child_event, child);
4970 free_event(child_event);
4975 * When a child task exits, feed back event values to parent events.
4977 void perf_event_exit_task(struct task_struct *child)
4979 struct perf_event *child_event, *tmp;
4980 struct perf_event_context *child_ctx;
4981 unsigned long flags;
4983 if (likely(!child->perf_event_ctxp)) {
4984 perf_event_task(child, NULL, 0);
4985 return;
4988 local_irq_save(flags);
4990 * We can't reschedule here because interrupts are disabled,
4991 * and either child is current or it is a task that can't be
4992 * scheduled, so we are now safe from rescheduling changing
4993 * our context.
4995 child_ctx = child->perf_event_ctxp;
4996 __perf_event_task_sched_out(child_ctx);
4999 * Take the context lock here so that if find_get_context is
5000 * reading child->perf_event_ctxp, we wait until it has
5001 * incremented the context's refcount before we do put_ctx below.
5003 raw_spin_lock(&child_ctx->lock);
5004 child->perf_event_ctxp = NULL;
5006 * If this context is a clone; unclone it so it can't get
5007 * swapped to another process while we're removing all
5008 * the events from it.
5010 unclone_ctx(child_ctx);
5011 update_context_time(child_ctx);
5012 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
5015 * Report the task dead after unscheduling the events so that we
5016 * won't get any samples after PERF_RECORD_EXIT. We can however still
5017 * get a few PERF_RECORD_READ events.
5019 perf_event_task(child, child_ctx, 0);
5022 * We can recurse on the same lock type through:
5024 * __perf_event_exit_task()
5025 * sync_child_event()
5026 * fput(parent_event->filp)
5027 * perf_release()
5028 * mutex_lock(&ctx->mutex)
5030 * But since its the parent context it won't be the same instance.
5032 mutex_lock_nested(&child_ctx->mutex, SINGLE_DEPTH_NESTING);
5034 again:
5035 list_for_each_entry_safe(child_event, tmp, &child_ctx->group_list,
5036 group_entry)
5037 __perf_event_exit_task(child_event, child_ctx, child);
5040 * If the last event was a group event, it will have appended all
5041 * its siblings to the list, but we obtained 'tmp' before that which
5042 * will still point to the list head terminating the iteration.
5044 if (!list_empty(&child_ctx->group_list))
5045 goto again;
5047 mutex_unlock(&child_ctx->mutex);
5049 put_ctx(child_ctx);
5053 * free an unexposed, unused context as created by inheritance by
5054 * init_task below, used by fork() in case of fail.
5056 void perf_event_free_task(struct task_struct *task)
5058 struct perf_event_context *ctx = task->perf_event_ctxp;
5059 struct perf_event *event, *tmp;
5061 if (!ctx)
5062 return;
5064 mutex_lock(&ctx->mutex);
5065 again:
5066 list_for_each_entry_safe(event, tmp, &ctx->group_list, group_entry) {
5067 struct perf_event *parent = event->parent;
5069 if (WARN_ON_ONCE(!parent))
5070 continue;
5072 mutex_lock(&parent->child_mutex);
5073 list_del_init(&event->child_list);
5074 mutex_unlock(&parent->child_mutex);
5076 fput(parent->filp);
5078 list_del_event(event, ctx);
5079 free_event(event);
5082 if (!list_empty(&ctx->group_list))
5083 goto again;
5085 mutex_unlock(&ctx->mutex);
5087 put_ctx(ctx);
5091 * Initialize the perf_event context in task_struct
5093 int perf_event_init_task(struct task_struct *child)
5095 struct perf_event_context *child_ctx = NULL, *parent_ctx;
5096 struct perf_event_context *cloned_ctx;
5097 struct perf_event *event;
5098 struct task_struct *parent = current;
5099 int inherited_all = 1;
5100 int ret = 0;
5102 child->perf_event_ctxp = NULL;
5104 mutex_init(&child->perf_event_mutex);
5105 INIT_LIST_HEAD(&child->perf_event_list);
5107 if (likely(!parent->perf_event_ctxp))
5108 return 0;
5111 * If the parent's context is a clone, pin it so it won't get
5112 * swapped under us.
5114 parent_ctx = perf_pin_task_context(parent);
5117 * No need to check if parent_ctx != NULL here; since we saw
5118 * it non-NULL earlier, the only reason for it to become NULL
5119 * is if we exit, and since we're currently in the middle of
5120 * a fork we can't be exiting at the same time.
5124 * Lock the parent list. No need to lock the child - not PID
5125 * hashed yet and not running, so nobody can access it.
5127 mutex_lock(&parent_ctx->mutex);
5130 * We dont have to disable NMIs - we are only looking at
5131 * the list, not manipulating it:
5133 list_for_each_entry(event, &parent_ctx->group_list, group_entry) {
5135 if (!event->attr.inherit) {
5136 inherited_all = 0;
5137 continue;
5140 if (!child->perf_event_ctxp) {
5142 * This is executed from the parent task context, so
5143 * inherit events that have been marked for cloning.
5144 * First allocate and initialize a context for the
5145 * child.
5148 child_ctx = kzalloc(sizeof(struct perf_event_context),
5149 GFP_KERNEL);
5150 if (!child_ctx) {
5151 ret = -ENOMEM;
5152 break;
5155 __perf_event_init_context(child_ctx, child);
5156 child->perf_event_ctxp = child_ctx;
5157 get_task_struct(child);
5160 ret = inherit_group(event, parent, parent_ctx,
5161 child, child_ctx);
5162 if (ret) {
5163 inherited_all = 0;
5164 break;
5168 if (child_ctx && inherited_all) {
5170 * Mark the child context as a clone of the parent
5171 * context, or of whatever the parent is a clone of.
5172 * Note that if the parent is a clone, it could get
5173 * uncloned at any point, but that doesn't matter
5174 * because the list of events and the generation
5175 * count can't have changed since we took the mutex.
5177 cloned_ctx = rcu_dereference(parent_ctx->parent_ctx);
5178 if (cloned_ctx) {
5179 child_ctx->parent_ctx = cloned_ctx;
5180 child_ctx->parent_gen = parent_ctx->parent_gen;
5181 } else {
5182 child_ctx->parent_ctx = parent_ctx;
5183 child_ctx->parent_gen = parent_ctx->generation;
5185 get_ctx(child_ctx->parent_ctx);
5188 mutex_unlock(&parent_ctx->mutex);
5190 perf_unpin_context(parent_ctx);
5192 return ret;
5195 static void __cpuinit perf_event_init_cpu(int cpu)
5197 struct perf_cpu_context *cpuctx;
5199 cpuctx = &per_cpu(perf_cpu_context, cpu);
5200 __perf_event_init_context(&cpuctx->ctx, NULL);
5202 spin_lock(&perf_resource_lock);
5203 cpuctx->max_pertask = perf_max_events - perf_reserved_percpu;
5204 spin_unlock(&perf_resource_lock);
5206 hw_perf_event_setup(cpu);
5209 #ifdef CONFIG_HOTPLUG_CPU
5210 static void __perf_event_exit_cpu(void *info)
5212 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
5213 struct perf_event_context *ctx = &cpuctx->ctx;
5214 struct perf_event *event, *tmp;
5216 list_for_each_entry_safe(event, tmp, &ctx->group_list, group_entry)
5217 __perf_event_remove_from_context(event);
5219 static void perf_event_exit_cpu(int cpu)
5221 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
5222 struct perf_event_context *ctx = &cpuctx->ctx;
5224 mutex_lock(&ctx->mutex);
5225 smp_call_function_single(cpu, __perf_event_exit_cpu, NULL, 1);
5226 mutex_unlock(&ctx->mutex);
5228 #else
5229 static inline void perf_event_exit_cpu(int cpu) { }
5230 #endif
5232 static int __cpuinit
5233 perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
5235 unsigned int cpu = (long)hcpu;
5237 switch (action) {
5239 case CPU_UP_PREPARE:
5240 case CPU_UP_PREPARE_FROZEN:
5241 perf_event_init_cpu(cpu);
5242 break;
5244 case CPU_ONLINE:
5245 case CPU_ONLINE_FROZEN:
5246 hw_perf_event_setup_online(cpu);
5247 break;
5249 case CPU_DOWN_PREPARE:
5250 case CPU_DOWN_PREPARE_FROZEN:
5251 perf_event_exit_cpu(cpu);
5252 break;
5254 default:
5255 break;
5258 return NOTIFY_OK;
5262 * This has to have a higher priority than migration_notifier in sched.c.
5264 static struct notifier_block __cpuinitdata perf_cpu_nb = {
5265 .notifier_call = perf_cpu_notify,
5266 .priority = 20,
5269 void __init perf_event_init(void)
5271 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
5272 (void *)(long)smp_processor_id());
5273 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_ONLINE,
5274 (void *)(long)smp_processor_id());
5275 register_cpu_notifier(&perf_cpu_nb);
5278 static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
5280 return sprintf(buf, "%d\n", perf_reserved_percpu);
5283 static ssize_t
5284 perf_set_reserve_percpu(struct sysdev_class *class,
5285 const char *buf,
5286 size_t count)
5288 struct perf_cpu_context *cpuctx;
5289 unsigned long val;
5290 int err, cpu, mpt;
5292 err = strict_strtoul(buf, 10, &val);
5293 if (err)
5294 return err;
5295 if (val > perf_max_events)
5296 return -EINVAL;
5298 spin_lock(&perf_resource_lock);
5299 perf_reserved_percpu = val;
5300 for_each_online_cpu(cpu) {
5301 cpuctx = &per_cpu(perf_cpu_context, cpu);
5302 raw_spin_lock_irq(&cpuctx->ctx.lock);
5303 mpt = min(perf_max_events - cpuctx->ctx.nr_events,
5304 perf_max_events - perf_reserved_percpu);
5305 cpuctx->max_pertask = mpt;
5306 raw_spin_unlock_irq(&cpuctx->ctx.lock);
5308 spin_unlock(&perf_resource_lock);
5310 return count;
5313 static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
5315 return sprintf(buf, "%d\n", perf_overcommit);
5318 static ssize_t
5319 perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
5321 unsigned long val;
5322 int err;
5324 err = strict_strtoul(buf, 10, &val);
5325 if (err)
5326 return err;
5327 if (val > 1)
5328 return -EINVAL;
5330 spin_lock(&perf_resource_lock);
5331 perf_overcommit = val;
5332 spin_unlock(&perf_resource_lock);
5334 return count;
5337 static SYSDEV_CLASS_ATTR(
5338 reserve_percpu,
5339 0644,
5340 perf_show_reserve_percpu,
5341 perf_set_reserve_percpu
5344 static SYSDEV_CLASS_ATTR(
5345 overcommit,
5346 0644,
5347 perf_show_overcommit,
5348 perf_set_overcommit
5351 static struct attribute *perfclass_attrs[] = {
5352 &attr_reserve_percpu.attr,
5353 &attr_overcommit.attr,
5354 NULL
5357 static struct attribute_group perfclass_attr_group = {
5358 .attrs = perfclass_attrs,
5359 .name = "perf_events",
5362 static int __init perf_event_sysfs_init(void)
5364 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
5365 &perfclass_attr_group);
5367 device_initcall(perf_event_sysfs_init);