intel-gtt: introduce pte write function for gen6
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / kernel / perf_event.c
blobdb5b56064687e453c0df1cc118b975ea047bdcae
1 /*
2 * Performance events core code:
4 * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
5 * Copyright (C) 2008-2009 Red Hat, Inc., Ingo Molnar
6 * Copyright (C) 2008-2009 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
7 * Copyright © 2009 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
9 * For licensing details see kernel-base/COPYING
12 #include <linux/fs.h>
13 #include <linux/mm.h>
14 #include <linux/cpu.h>
15 #include <linux/smp.h>
16 #include <linux/file.h>
17 #include <linux/poll.h>
18 #include <linux/slab.h>
19 #include <linux/hash.h>
20 #include <linux/sysfs.h>
21 #include <linux/dcache.h>
22 #include <linux/percpu.h>
23 #include <linux/ptrace.h>
24 #include <linux/vmstat.h>
25 #include <linux/vmalloc.h>
26 #include <linux/hardirq.h>
27 #include <linux/rculist.h>
28 #include <linux/uaccess.h>
29 #include <linux/syscalls.h>
30 #include <linux/anon_inodes.h>
31 #include <linux/kernel_stat.h>
32 #include <linux/perf_event.h>
33 #include <linux/ftrace_event.h>
34 #include <linux/hw_breakpoint.h>
36 #include <asm/irq_regs.h>
39 * Each CPU has a list of per CPU events:
41 static DEFINE_PER_CPU(struct perf_cpu_context, perf_cpu_context);
43 int perf_max_events __read_mostly = 1;
44 static int perf_reserved_percpu __read_mostly;
45 static int perf_overcommit __read_mostly = 1;
47 static atomic_t nr_events __read_mostly;
48 static atomic_t nr_mmap_events __read_mostly;
49 static atomic_t nr_comm_events __read_mostly;
50 static atomic_t nr_task_events __read_mostly;
53 * perf event paranoia level:
54 * -1 - not paranoid at all
55 * 0 - disallow raw tracepoint access for unpriv
56 * 1 - disallow cpu events for unpriv
57 * 2 - disallow kernel profiling for unpriv
59 int sysctl_perf_event_paranoid __read_mostly = 1;
61 int sysctl_perf_event_mlock __read_mostly = 512; /* 'free' kb per user */
64 * max perf event sample rate
66 int sysctl_perf_event_sample_rate __read_mostly = 100000;
68 static atomic64_t perf_event_id;
71 * Lock for (sysadmin-configurable) event reservations:
73 static DEFINE_SPINLOCK(perf_resource_lock);
76 * Architecture provided APIs - weak aliases:
78 extern __weak const struct pmu *hw_perf_event_init(struct perf_event *event)
80 return NULL;
83 void __weak hw_perf_disable(void) { barrier(); }
84 void __weak hw_perf_enable(void) { barrier(); }
86 void __weak perf_event_print_debug(void) { }
88 static DEFINE_PER_CPU(int, perf_disable_count);
90 void perf_disable(void)
92 if (!__get_cpu_var(perf_disable_count)++)
93 hw_perf_disable();
96 void perf_enable(void)
98 if (!--__get_cpu_var(perf_disable_count))
99 hw_perf_enable();
102 static void get_ctx(struct perf_event_context *ctx)
104 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
107 static void free_ctx(struct rcu_head *head)
109 struct perf_event_context *ctx;
111 ctx = container_of(head, struct perf_event_context, rcu_head);
112 kfree(ctx);
115 static void put_ctx(struct perf_event_context *ctx)
117 if (atomic_dec_and_test(&ctx->refcount)) {
118 if (ctx->parent_ctx)
119 put_ctx(ctx->parent_ctx);
120 if (ctx->task)
121 put_task_struct(ctx->task);
122 call_rcu(&ctx->rcu_head, free_ctx);
126 static void unclone_ctx(struct perf_event_context *ctx)
128 if (ctx->parent_ctx) {
129 put_ctx(ctx->parent_ctx);
130 ctx->parent_ctx = NULL;
135 * If we inherit events we want to return the parent event id
136 * to userspace.
138 static u64 primary_event_id(struct perf_event *event)
140 u64 id = event->id;
142 if (event->parent)
143 id = event->parent->id;
145 return id;
149 * Get the perf_event_context for a task and lock it.
150 * This has to cope with with the fact that until it is locked,
151 * the context could get moved to another task.
153 static struct perf_event_context *
154 perf_lock_task_context(struct task_struct *task, unsigned long *flags)
156 struct perf_event_context *ctx;
158 rcu_read_lock();
159 retry:
160 ctx = rcu_dereference(task->perf_event_ctxp);
161 if (ctx) {
163 * If this context is a clone of another, it might
164 * get swapped for another underneath us by
165 * perf_event_task_sched_out, though the
166 * rcu_read_lock() protects us from any context
167 * getting freed. Lock the context and check if it
168 * got swapped before we could get the lock, and retry
169 * if so. If we locked the right context, then it
170 * can't get swapped on us any more.
172 raw_spin_lock_irqsave(&ctx->lock, *flags);
173 if (ctx != rcu_dereference(task->perf_event_ctxp)) {
174 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
175 goto retry;
178 if (!atomic_inc_not_zero(&ctx->refcount)) {
179 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
180 ctx = NULL;
183 rcu_read_unlock();
184 return ctx;
188 * Get the context for a task and increment its pin_count so it
189 * can't get swapped to another task. This also increments its
190 * reference count so that the context can't get freed.
192 static struct perf_event_context *perf_pin_task_context(struct task_struct *task)
194 struct perf_event_context *ctx;
195 unsigned long flags;
197 ctx = perf_lock_task_context(task, &flags);
198 if (ctx) {
199 ++ctx->pin_count;
200 raw_spin_unlock_irqrestore(&ctx->lock, flags);
202 return ctx;
205 static void perf_unpin_context(struct perf_event_context *ctx)
207 unsigned long flags;
209 raw_spin_lock_irqsave(&ctx->lock, flags);
210 --ctx->pin_count;
211 raw_spin_unlock_irqrestore(&ctx->lock, flags);
212 put_ctx(ctx);
215 static inline u64 perf_clock(void)
217 return local_clock();
221 * Update the record of the current time in a context.
223 static void update_context_time(struct perf_event_context *ctx)
225 u64 now = perf_clock();
227 ctx->time += now - ctx->timestamp;
228 ctx->timestamp = now;
232 * Update the total_time_enabled and total_time_running fields for a event.
234 static void update_event_times(struct perf_event *event)
236 struct perf_event_context *ctx = event->ctx;
237 u64 run_end;
239 if (event->state < PERF_EVENT_STATE_INACTIVE ||
240 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
241 return;
243 if (ctx->is_active)
244 run_end = ctx->time;
245 else
246 run_end = event->tstamp_stopped;
248 event->total_time_enabled = run_end - event->tstamp_enabled;
250 if (event->state == PERF_EVENT_STATE_INACTIVE)
251 run_end = event->tstamp_stopped;
252 else
253 run_end = ctx->time;
255 event->total_time_running = run_end - event->tstamp_running;
259 * Update total_time_enabled and total_time_running for all events in a group.
261 static void update_group_times(struct perf_event *leader)
263 struct perf_event *event;
265 update_event_times(leader);
266 list_for_each_entry(event, &leader->sibling_list, group_entry)
267 update_event_times(event);
270 static struct list_head *
271 ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
273 if (event->attr.pinned)
274 return &ctx->pinned_groups;
275 else
276 return &ctx->flexible_groups;
280 * Add a event from the lists for its context.
281 * Must be called with ctx->mutex and ctx->lock held.
283 static void
284 list_add_event(struct perf_event *event, struct perf_event_context *ctx)
286 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
287 event->attach_state |= PERF_ATTACH_CONTEXT;
290 * If we're a stand alone event or group leader, we go to the context
291 * list, group events are kept attached to the group so that
292 * perf_group_detach can, at all times, locate all siblings.
294 if (event->group_leader == event) {
295 struct list_head *list;
297 if (is_software_event(event))
298 event->group_flags |= PERF_GROUP_SOFTWARE;
300 list = ctx_group_list(event, ctx);
301 list_add_tail(&event->group_entry, list);
304 list_add_rcu(&event->event_entry, &ctx->event_list);
305 ctx->nr_events++;
306 if (event->attr.inherit_stat)
307 ctx->nr_stat++;
310 static void perf_group_attach(struct perf_event *event)
312 struct perf_event *group_leader = event->group_leader;
314 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_GROUP);
315 event->attach_state |= PERF_ATTACH_GROUP;
317 if (group_leader == event)
318 return;
320 if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
321 !is_software_event(event))
322 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
324 list_add_tail(&event->group_entry, &group_leader->sibling_list);
325 group_leader->nr_siblings++;
329 * Remove a event from the lists for its context.
330 * Must be called with ctx->mutex and ctx->lock held.
332 static void
333 list_del_event(struct perf_event *event, struct perf_event_context *ctx)
336 * We can have double detach due to exit/hot-unplug + close.
338 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
339 return;
341 event->attach_state &= ~PERF_ATTACH_CONTEXT;
343 ctx->nr_events--;
344 if (event->attr.inherit_stat)
345 ctx->nr_stat--;
347 list_del_rcu(&event->event_entry);
349 if (event->group_leader == event)
350 list_del_init(&event->group_entry);
352 update_group_times(event);
355 * If event was in error state, then keep it
356 * that way, otherwise bogus counts will be
357 * returned on read(). The only way to get out
358 * of error state is by explicit re-enabling
359 * of the event
361 if (event->state > PERF_EVENT_STATE_OFF)
362 event->state = PERF_EVENT_STATE_OFF;
365 static void perf_group_detach(struct perf_event *event)
367 struct perf_event *sibling, *tmp;
368 struct list_head *list = NULL;
371 * We can have double detach due to exit/hot-unplug + close.
373 if (!(event->attach_state & PERF_ATTACH_GROUP))
374 return;
376 event->attach_state &= ~PERF_ATTACH_GROUP;
379 * If this is a sibling, remove it from its group.
381 if (event->group_leader != event) {
382 list_del_init(&event->group_entry);
383 event->group_leader->nr_siblings--;
384 return;
387 if (!list_empty(&event->group_entry))
388 list = &event->group_entry;
391 * If this was a group event with sibling events then
392 * upgrade the siblings to singleton events by adding them
393 * to whatever list we are on.
395 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
396 if (list)
397 list_move_tail(&sibling->group_entry, list);
398 sibling->group_leader = sibling;
400 /* Inherit group flags from the previous leader */
401 sibling->group_flags = event->group_flags;
405 static inline int
406 event_filter_match(struct perf_event *event)
408 return event->cpu == -1 || event->cpu == smp_processor_id();
411 static void
412 event_sched_out(struct perf_event *event,
413 struct perf_cpu_context *cpuctx,
414 struct perf_event_context *ctx)
416 u64 delta;
418 * An event which could not be activated because of
419 * filter mismatch still needs to have its timings
420 * maintained, otherwise bogus information is return
421 * via read() for time_enabled, time_running:
423 if (event->state == PERF_EVENT_STATE_INACTIVE
424 && !event_filter_match(event)) {
425 delta = ctx->time - event->tstamp_stopped;
426 event->tstamp_running += delta;
427 event->tstamp_stopped = ctx->time;
430 if (event->state != PERF_EVENT_STATE_ACTIVE)
431 return;
433 event->state = PERF_EVENT_STATE_INACTIVE;
434 if (event->pending_disable) {
435 event->pending_disable = 0;
436 event->state = PERF_EVENT_STATE_OFF;
438 event->tstamp_stopped = ctx->time;
439 event->pmu->disable(event);
440 event->oncpu = -1;
442 if (!is_software_event(event))
443 cpuctx->active_oncpu--;
444 ctx->nr_active--;
445 if (event->attr.exclusive || !cpuctx->active_oncpu)
446 cpuctx->exclusive = 0;
449 static void
450 group_sched_out(struct perf_event *group_event,
451 struct perf_cpu_context *cpuctx,
452 struct perf_event_context *ctx)
454 struct perf_event *event;
455 int state = group_event->state;
457 event_sched_out(group_event, cpuctx, ctx);
460 * Schedule out siblings (if any):
462 list_for_each_entry(event, &group_event->sibling_list, group_entry)
463 event_sched_out(event, cpuctx, ctx);
465 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
466 cpuctx->exclusive = 0;
470 * Cross CPU call to remove a performance event
472 * We disable the event on the hardware level first. After that we
473 * remove it from the context list.
475 static void __perf_event_remove_from_context(void *info)
477 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
478 struct perf_event *event = info;
479 struct perf_event_context *ctx = event->ctx;
482 * If this is a task context, we need to check whether it is
483 * the current task context of this cpu. If not it has been
484 * scheduled out before the smp call arrived.
486 if (ctx->task && cpuctx->task_ctx != ctx)
487 return;
489 raw_spin_lock(&ctx->lock);
491 * Protect the list operation against NMI by disabling the
492 * events on a global level.
494 perf_disable();
496 event_sched_out(event, cpuctx, ctx);
498 list_del_event(event, ctx);
500 if (!ctx->task) {
502 * Allow more per task events with respect to the
503 * reservation:
505 cpuctx->max_pertask =
506 min(perf_max_events - ctx->nr_events,
507 perf_max_events - perf_reserved_percpu);
510 perf_enable();
511 raw_spin_unlock(&ctx->lock);
516 * Remove the event from a task's (or a CPU's) list of events.
518 * Must be called with ctx->mutex held.
520 * CPU events are removed with a smp call. For task events we only
521 * call when the task is on a CPU.
523 * If event->ctx is a cloned context, callers must make sure that
524 * every task struct that event->ctx->task could possibly point to
525 * remains valid. This is OK when called from perf_release since
526 * that only calls us on the top-level context, which can't be a clone.
527 * When called from perf_event_exit_task, it's OK because the
528 * context has been detached from its task.
530 static void perf_event_remove_from_context(struct perf_event *event)
532 struct perf_event_context *ctx = event->ctx;
533 struct task_struct *task = ctx->task;
535 if (!task) {
537 * Per cpu events are removed via an smp call and
538 * the removal is always successful.
540 smp_call_function_single(event->cpu,
541 __perf_event_remove_from_context,
542 event, 1);
543 return;
546 retry:
547 task_oncpu_function_call(task, __perf_event_remove_from_context,
548 event);
550 raw_spin_lock_irq(&ctx->lock);
552 * If the context is active we need to retry the smp call.
554 if (ctx->nr_active && !list_empty(&event->group_entry)) {
555 raw_spin_unlock_irq(&ctx->lock);
556 goto retry;
560 * The lock prevents that this context is scheduled in so we
561 * can remove the event safely, if the call above did not
562 * succeed.
564 if (!list_empty(&event->group_entry))
565 list_del_event(event, ctx);
566 raw_spin_unlock_irq(&ctx->lock);
570 * Cross CPU call to disable a performance event
572 static void __perf_event_disable(void *info)
574 struct perf_event *event = info;
575 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
576 struct perf_event_context *ctx = event->ctx;
579 * If this is a per-task event, need to check whether this
580 * event's task is the current task on this cpu.
582 if (ctx->task && cpuctx->task_ctx != ctx)
583 return;
585 raw_spin_lock(&ctx->lock);
588 * If the event is on, turn it off.
589 * If it is in error state, leave it in error state.
591 if (event->state >= PERF_EVENT_STATE_INACTIVE) {
592 update_context_time(ctx);
593 update_group_times(event);
594 if (event == event->group_leader)
595 group_sched_out(event, cpuctx, ctx);
596 else
597 event_sched_out(event, cpuctx, ctx);
598 event->state = PERF_EVENT_STATE_OFF;
601 raw_spin_unlock(&ctx->lock);
605 * Disable a event.
607 * If event->ctx is a cloned context, callers must make sure that
608 * every task struct that event->ctx->task could possibly point to
609 * remains valid. This condition is satisifed when called through
610 * perf_event_for_each_child or perf_event_for_each because they
611 * hold the top-level event's child_mutex, so any descendant that
612 * goes to exit will block in sync_child_event.
613 * When called from perf_pending_event it's OK because event->ctx
614 * is the current context on this CPU and preemption is disabled,
615 * hence we can't get into perf_event_task_sched_out for this context.
617 void perf_event_disable(struct perf_event *event)
619 struct perf_event_context *ctx = event->ctx;
620 struct task_struct *task = ctx->task;
622 if (!task) {
624 * Disable the event on the cpu that it's on
626 smp_call_function_single(event->cpu, __perf_event_disable,
627 event, 1);
628 return;
631 retry:
632 task_oncpu_function_call(task, __perf_event_disable, event);
634 raw_spin_lock_irq(&ctx->lock);
636 * If the event is still active, we need to retry the cross-call.
638 if (event->state == PERF_EVENT_STATE_ACTIVE) {
639 raw_spin_unlock_irq(&ctx->lock);
640 goto retry;
644 * Since we have the lock this context can't be scheduled
645 * in, so we can change the state safely.
647 if (event->state == PERF_EVENT_STATE_INACTIVE) {
648 update_group_times(event);
649 event->state = PERF_EVENT_STATE_OFF;
652 raw_spin_unlock_irq(&ctx->lock);
655 static int
656 event_sched_in(struct perf_event *event,
657 struct perf_cpu_context *cpuctx,
658 struct perf_event_context *ctx)
660 if (event->state <= PERF_EVENT_STATE_OFF)
661 return 0;
663 event->state = PERF_EVENT_STATE_ACTIVE;
664 event->oncpu = smp_processor_id();
666 * The new state must be visible before we turn it on in the hardware:
668 smp_wmb();
670 if (event->pmu->enable(event)) {
671 event->state = PERF_EVENT_STATE_INACTIVE;
672 event->oncpu = -1;
673 return -EAGAIN;
676 event->tstamp_running += ctx->time - event->tstamp_stopped;
678 if (!is_software_event(event))
679 cpuctx->active_oncpu++;
680 ctx->nr_active++;
682 if (event->attr.exclusive)
683 cpuctx->exclusive = 1;
685 return 0;
688 static int
689 group_sched_in(struct perf_event *group_event,
690 struct perf_cpu_context *cpuctx,
691 struct perf_event_context *ctx)
693 struct perf_event *event, *partial_group = NULL;
694 const struct pmu *pmu = group_event->pmu;
695 bool txn = false;
697 if (group_event->state == PERF_EVENT_STATE_OFF)
698 return 0;
700 /* Check if group transaction availabe */
701 if (pmu->start_txn)
702 txn = true;
704 if (txn)
705 pmu->start_txn(pmu);
707 if (event_sched_in(group_event, cpuctx, ctx)) {
708 if (txn)
709 pmu->cancel_txn(pmu);
710 return -EAGAIN;
714 * Schedule in siblings as one group (if any):
716 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
717 if (event_sched_in(event, cpuctx, ctx)) {
718 partial_group = event;
719 goto group_error;
723 if (!txn || !pmu->commit_txn(pmu))
724 return 0;
726 group_error:
728 * Groups can be scheduled in as one unit only, so undo any
729 * partial group before returning:
731 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
732 if (event == partial_group)
733 break;
734 event_sched_out(event, cpuctx, ctx);
736 event_sched_out(group_event, cpuctx, ctx);
738 if (txn)
739 pmu->cancel_txn(pmu);
741 return -EAGAIN;
745 * Work out whether we can put this event group on the CPU now.
747 static int group_can_go_on(struct perf_event *event,
748 struct perf_cpu_context *cpuctx,
749 int can_add_hw)
752 * Groups consisting entirely of software events can always go on.
754 if (event->group_flags & PERF_GROUP_SOFTWARE)
755 return 1;
757 * If an exclusive group is already on, no other hardware
758 * events can go on.
760 if (cpuctx->exclusive)
761 return 0;
763 * If this group is exclusive and there are already
764 * events on the CPU, it can't go on.
766 if (event->attr.exclusive && cpuctx->active_oncpu)
767 return 0;
769 * Otherwise, try to add it if all previous groups were able
770 * to go on.
772 return can_add_hw;
775 static void add_event_to_ctx(struct perf_event *event,
776 struct perf_event_context *ctx)
778 list_add_event(event, ctx);
779 perf_group_attach(event);
780 event->tstamp_enabled = ctx->time;
781 event->tstamp_running = ctx->time;
782 event->tstamp_stopped = ctx->time;
786 * Cross CPU call to install and enable a performance event
788 * Must be called with ctx->mutex held
790 static void __perf_install_in_context(void *info)
792 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
793 struct perf_event *event = info;
794 struct perf_event_context *ctx = event->ctx;
795 struct perf_event *leader = event->group_leader;
796 int err;
799 * If this is a task context, we need to check whether it is
800 * the current task context of this cpu. If not it has been
801 * scheduled out before the smp call arrived.
802 * Or possibly this is the right context but it isn't
803 * on this cpu because it had no events.
805 if (ctx->task && cpuctx->task_ctx != ctx) {
806 if (cpuctx->task_ctx || ctx->task != current)
807 return;
808 cpuctx->task_ctx = ctx;
811 raw_spin_lock(&ctx->lock);
812 ctx->is_active = 1;
813 update_context_time(ctx);
816 * Protect the list operation against NMI by disabling the
817 * events on a global level. NOP for non NMI based events.
819 perf_disable();
821 add_event_to_ctx(event, ctx);
823 if (event->cpu != -1 && event->cpu != smp_processor_id())
824 goto unlock;
827 * Don't put the event on if it is disabled or if
828 * it is in a group and the group isn't on.
830 if (event->state != PERF_EVENT_STATE_INACTIVE ||
831 (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE))
832 goto unlock;
835 * An exclusive event can't go on if there are already active
836 * hardware events, and no hardware event can go on if there
837 * is already an exclusive event on.
839 if (!group_can_go_on(event, cpuctx, 1))
840 err = -EEXIST;
841 else
842 err = event_sched_in(event, cpuctx, ctx);
844 if (err) {
846 * This event couldn't go on. If it is in a group
847 * then we have to pull the whole group off.
848 * If the event group is pinned then put it in error state.
850 if (leader != event)
851 group_sched_out(leader, cpuctx, ctx);
852 if (leader->attr.pinned) {
853 update_group_times(leader);
854 leader->state = PERF_EVENT_STATE_ERROR;
858 if (!err && !ctx->task && cpuctx->max_pertask)
859 cpuctx->max_pertask--;
861 unlock:
862 perf_enable();
864 raw_spin_unlock(&ctx->lock);
868 * Attach a performance event to a context
870 * First we add the event to the list with the hardware enable bit
871 * in event->hw_config cleared.
873 * If the event is attached to a task which is on a CPU we use a smp
874 * call to enable it in the task context. The task might have been
875 * scheduled away, but we check this in the smp call again.
877 * Must be called with ctx->mutex held.
879 static void
880 perf_install_in_context(struct perf_event_context *ctx,
881 struct perf_event *event,
882 int cpu)
884 struct task_struct *task = ctx->task;
886 if (!task) {
888 * Per cpu events are installed via an smp call and
889 * the install is always successful.
891 smp_call_function_single(cpu, __perf_install_in_context,
892 event, 1);
893 return;
896 retry:
897 task_oncpu_function_call(task, __perf_install_in_context,
898 event);
900 raw_spin_lock_irq(&ctx->lock);
902 * we need to retry the smp call.
904 if (ctx->is_active && list_empty(&event->group_entry)) {
905 raw_spin_unlock_irq(&ctx->lock);
906 goto retry;
910 * The lock prevents that this context is scheduled in so we
911 * can add the event safely, if it the call above did not
912 * succeed.
914 if (list_empty(&event->group_entry))
915 add_event_to_ctx(event, ctx);
916 raw_spin_unlock_irq(&ctx->lock);
920 * Put a event into inactive state and update time fields.
921 * Enabling the leader of a group effectively enables all
922 * the group members that aren't explicitly disabled, so we
923 * have to update their ->tstamp_enabled also.
924 * Note: this works for group members as well as group leaders
925 * since the non-leader members' sibling_lists will be empty.
927 static void __perf_event_mark_enabled(struct perf_event *event,
928 struct perf_event_context *ctx)
930 struct perf_event *sub;
932 event->state = PERF_EVENT_STATE_INACTIVE;
933 event->tstamp_enabled = ctx->time - event->total_time_enabled;
934 list_for_each_entry(sub, &event->sibling_list, group_entry)
935 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
936 sub->tstamp_enabled =
937 ctx->time - sub->total_time_enabled;
941 * Cross CPU call to enable a performance event
943 static void __perf_event_enable(void *info)
945 struct perf_event *event = info;
946 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
947 struct perf_event_context *ctx = event->ctx;
948 struct perf_event *leader = event->group_leader;
949 int err;
952 * If this is a per-task event, need to check whether this
953 * event's task is the current task on this cpu.
955 if (ctx->task && cpuctx->task_ctx != ctx) {
956 if (cpuctx->task_ctx || ctx->task != current)
957 return;
958 cpuctx->task_ctx = ctx;
961 raw_spin_lock(&ctx->lock);
962 ctx->is_active = 1;
963 update_context_time(ctx);
965 if (event->state >= PERF_EVENT_STATE_INACTIVE)
966 goto unlock;
967 __perf_event_mark_enabled(event, ctx);
969 if (event->cpu != -1 && event->cpu != smp_processor_id())
970 goto unlock;
973 * If the event is in a group and isn't the group leader,
974 * then don't put it on unless the group is on.
976 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
977 goto unlock;
979 if (!group_can_go_on(event, cpuctx, 1)) {
980 err = -EEXIST;
981 } else {
982 perf_disable();
983 if (event == leader)
984 err = group_sched_in(event, cpuctx, ctx);
985 else
986 err = event_sched_in(event, cpuctx, ctx);
987 perf_enable();
990 if (err) {
992 * If this event can't go on and it's part of a
993 * group, then the whole group has to come off.
995 if (leader != event)
996 group_sched_out(leader, cpuctx, ctx);
997 if (leader->attr.pinned) {
998 update_group_times(leader);
999 leader->state = PERF_EVENT_STATE_ERROR;
1003 unlock:
1004 raw_spin_unlock(&ctx->lock);
1008 * Enable a event.
1010 * If event->ctx is a cloned context, callers must make sure that
1011 * every task struct that event->ctx->task could possibly point to
1012 * remains valid. This condition is satisfied when called through
1013 * perf_event_for_each_child or perf_event_for_each as described
1014 * for perf_event_disable.
1016 void perf_event_enable(struct perf_event *event)
1018 struct perf_event_context *ctx = event->ctx;
1019 struct task_struct *task = ctx->task;
1021 if (!task) {
1023 * Enable the event on the cpu that it's on
1025 smp_call_function_single(event->cpu, __perf_event_enable,
1026 event, 1);
1027 return;
1030 raw_spin_lock_irq(&ctx->lock);
1031 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1032 goto out;
1035 * If the event is in error state, clear that first.
1036 * That way, if we see the event in error state below, we
1037 * know that it has gone back into error state, as distinct
1038 * from the task having been scheduled away before the
1039 * cross-call arrived.
1041 if (event->state == PERF_EVENT_STATE_ERROR)
1042 event->state = PERF_EVENT_STATE_OFF;
1044 retry:
1045 raw_spin_unlock_irq(&ctx->lock);
1046 task_oncpu_function_call(task, __perf_event_enable, event);
1048 raw_spin_lock_irq(&ctx->lock);
1051 * If the context is active and the event is still off,
1052 * we need to retry the cross-call.
1054 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF)
1055 goto retry;
1058 * Since we have the lock this context can't be scheduled
1059 * in, so we can change the state safely.
1061 if (event->state == PERF_EVENT_STATE_OFF)
1062 __perf_event_mark_enabled(event, ctx);
1064 out:
1065 raw_spin_unlock_irq(&ctx->lock);
1068 static int perf_event_refresh(struct perf_event *event, int refresh)
1071 * not supported on inherited events
1073 if (event->attr.inherit)
1074 return -EINVAL;
1076 atomic_add(refresh, &event->event_limit);
1077 perf_event_enable(event);
1079 return 0;
1082 enum event_type_t {
1083 EVENT_FLEXIBLE = 0x1,
1084 EVENT_PINNED = 0x2,
1085 EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
1088 static void ctx_sched_out(struct perf_event_context *ctx,
1089 struct perf_cpu_context *cpuctx,
1090 enum event_type_t event_type)
1092 struct perf_event *event;
1094 raw_spin_lock(&ctx->lock);
1095 ctx->is_active = 0;
1096 if (likely(!ctx->nr_events))
1097 goto out;
1098 update_context_time(ctx);
1100 perf_disable();
1101 if (!ctx->nr_active)
1102 goto out_enable;
1104 if (event_type & EVENT_PINNED)
1105 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
1106 group_sched_out(event, cpuctx, ctx);
1108 if (event_type & EVENT_FLEXIBLE)
1109 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
1110 group_sched_out(event, cpuctx, ctx);
1112 out_enable:
1113 perf_enable();
1114 out:
1115 raw_spin_unlock(&ctx->lock);
1119 * Test whether two contexts are equivalent, i.e. whether they
1120 * have both been cloned from the same version of the same context
1121 * and they both have the same number of enabled events.
1122 * If the number of enabled events is the same, then the set
1123 * of enabled events should be the same, because these are both
1124 * inherited contexts, therefore we can't access individual events
1125 * in them directly with an fd; we can only enable/disable all
1126 * events via prctl, or enable/disable all events in a family
1127 * via ioctl, which will have the same effect on both contexts.
1129 static int context_equiv(struct perf_event_context *ctx1,
1130 struct perf_event_context *ctx2)
1132 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
1133 && ctx1->parent_gen == ctx2->parent_gen
1134 && !ctx1->pin_count && !ctx2->pin_count;
1137 static void __perf_event_sync_stat(struct perf_event *event,
1138 struct perf_event *next_event)
1140 u64 value;
1142 if (!event->attr.inherit_stat)
1143 return;
1146 * Update the event value, we cannot use perf_event_read()
1147 * because we're in the middle of a context switch and have IRQs
1148 * disabled, which upsets smp_call_function_single(), however
1149 * we know the event must be on the current CPU, therefore we
1150 * don't need to use it.
1152 switch (event->state) {
1153 case PERF_EVENT_STATE_ACTIVE:
1154 event->pmu->read(event);
1155 /* fall-through */
1157 case PERF_EVENT_STATE_INACTIVE:
1158 update_event_times(event);
1159 break;
1161 default:
1162 break;
1166 * In order to keep per-task stats reliable we need to flip the event
1167 * values when we flip the contexts.
1169 value = local64_read(&next_event->count);
1170 value = local64_xchg(&event->count, value);
1171 local64_set(&next_event->count, value);
1173 swap(event->total_time_enabled, next_event->total_time_enabled);
1174 swap(event->total_time_running, next_event->total_time_running);
1177 * Since we swizzled the values, update the user visible data too.
1179 perf_event_update_userpage(event);
1180 perf_event_update_userpage(next_event);
1183 #define list_next_entry(pos, member) \
1184 list_entry(pos->member.next, typeof(*pos), member)
1186 static void perf_event_sync_stat(struct perf_event_context *ctx,
1187 struct perf_event_context *next_ctx)
1189 struct perf_event *event, *next_event;
1191 if (!ctx->nr_stat)
1192 return;
1194 update_context_time(ctx);
1196 event = list_first_entry(&ctx->event_list,
1197 struct perf_event, event_entry);
1199 next_event = list_first_entry(&next_ctx->event_list,
1200 struct perf_event, event_entry);
1202 while (&event->event_entry != &ctx->event_list &&
1203 &next_event->event_entry != &next_ctx->event_list) {
1205 __perf_event_sync_stat(event, next_event);
1207 event = list_next_entry(event, event_entry);
1208 next_event = list_next_entry(next_event, event_entry);
1213 * Called from scheduler to remove the events of the current task,
1214 * with interrupts disabled.
1216 * We stop each event and update the event value in event->count.
1218 * This does not protect us against NMI, but disable()
1219 * sets the disabled bit in the control field of event _before_
1220 * accessing the event control register. If a NMI hits, then it will
1221 * not restart the event.
1223 void perf_event_task_sched_out(struct task_struct *task,
1224 struct task_struct *next)
1226 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1227 struct perf_event_context *ctx = task->perf_event_ctxp;
1228 struct perf_event_context *next_ctx;
1229 struct perf_event_context *parent;
1230 int do_switch = 1;
1232 perf_sw_event(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 1, NULL, 0);
1234 if (likely(!ctx || !cpuctx->task_ctx))
1235 return;
1237 rcu_read_lock();
1238 parent = rcu_dereference(ctx->parent_ctx);
1239 next_ctx = next->perf_event_ctxp;
1240 if (parent && next_ctx &&
1241 rcu_dereference(next_ctx->parent_ctx) == parent) {
1243 * Looks like the two contexts are clones, so we might be
1244 * able to optimize the context switch. We lock both
1245 * contexts and check that they are clones under the
1246 * lock (including re-checking that neither has been
1247 * uncloned in the meantime). It doesn't matter which
1248 * order we take the locks because no other cpu could
1249 * be trying to lock both of these tasks.
1251 raw_spin_lock(&ctx->lock);
1252 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
1253 if (context_equiv(ctx, next_ctx)) {
1255 * XXX do we need a memory barrier of sorts
1256 * wrt to rcu_dereference() of perf_event_ctxp
1258 task->perf_event_ctxp = next_ctx;
1259 next->perf_event_ctxp = ctx;
1260 ctx->task = next;
1261 next_ctx->task = task;
1262 do_switch = 0;
1264 perf_event_sync_stat(ctx, next_ctx);
1266 raw_spin_unlock(&next_ctx->lock);
1267 raw_spin_unlock(&ctx->lock);
1269 rcu_read_unlock();
1271 if (do_switch) {
1272 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
1273 cpuctx->task_ctx = NULL;
1277 static void task_ctx_sched_out(struct perf_event_context *ctx,
1278 enum event_type_t event_type)
1280 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1282 if (!cpuctx->task_ctx)
1283 return;
1285 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
1286 return;
1288 ctx_sched_out(ctx, cpuctx, event_type);
1289 cpuctx->task_ctx = NULL;
1293 * Called with IRQs disabled
1295 static void __perf_event_task_sched_out(struct perf_event_context *ctx)
1297 task_ctx_sched_out(ctx, EVENT_ALL);
1301 * Called with IRQs disabled
1303 static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
1304 enum event_type_t event_type)
1306 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
1309 static void
1310 ctx_pinned_sched_in(struct perf_event_context *ctx,
1311 struct perf_cpu_context *cpuctx)
1313 struct perf_event *event;
1315 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
1316 if (event->state <= PERF_EVENT_STATE_OFF)
1317 continue;
1318 if (event->cpu != -1 && event->cpu != smp_processor_id())
1319 continue;
1321 if (group_can_go_on(event, cpuctx, 1))
1322 group_sched_in(event, cpuctx, ctx);
1325 * If this pinned group hasn't been scheduled,
1326 * put it in error state.
1328 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1329 update_group_times(event);
1330 event->state = PERF_EVENT_STATE_ERROR;
1335 static void
1336 ctx_flexible_sched_in(struct perf_event_context *ctx,
1337 struct perf_cpu_context *cpuctx)
1339 struct perf_event *event;
1340 int can_add_hw = 1;
1342 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
1343 /* Ignore events in OFF or ERROR state */
1344 if (event->state <= PERF_EVENT_STATE_OFF)
1345 continue;
1347 * Listen to the 'cpu' scheduling filter constraint
1348 * of events:
1350 if (event->cpu != -1 && event->cpu != smp_processor_id())
1351 continue;
1353 if (group_can_go_on(event, cpuctx, can_add_hw))
1354 if (group_sched_in(event, cpuctx, ctx))
1355 can_add_hw = 0;
1359 static void
1360 ctx_sched_in(struct perf_event_context *ctx,
1361 struct perf_cpu_context *cpuctx,
1362 enum event_type_t event_type)
1364 raw_spin_lock(&ctx->lock);
1365 ctx->is_active = 1;
1366 if (likely(!ctx->nr_events))
1367 goto out;
1369 ctx->timestamp = perf_clock();
1371 perf_disable();
1374 * First go through the list and put on any pinned groups
1375 * in order to give them the best chance of going on.
1377 if (event_type & EVENT_PINNED)
1378 ctx_pinned_sched_in(ctx, cpuctx);
1380 /* Then walk through the lower prio flexible groups */
1381 if (event_type & EVENT_FLEXIBLE)
1382 ctx_flexible_sched_in(ctx, cpuctx);
1384 perf_enable();
1385 out:
1386 raw_spin_unlock(&ctx->lock);
1389 static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
1390 enum event_type_t event_type)
1392 struct perf_event_context *ctx = &cpuctx->ctx;
1394 ctx_sched_in(ctx, cpuctx, event_type);
1397 static void task_ctx_sched_in(struct task_struct *task,
1398 enum event_type_t event_type)
1400 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1401 struct perf_event_context *ctx = task->perf_event_ctxp;
1403 if (likely(!ctx))
1404 return;
1405 if (cpuctx->task_ctx == ctx)
1406 return;
1407 ctx_sched_in(ctx, cpuctx, event_type);
1408 cpuctx->task_ctx = ctx;
1411 * Called from scheduler to add the events of the current task
1412 * with interrupts disabled.
1414 * We restore the event value and then enable it.
1416 * This does not protect us against NMI, but enable()
1417 * sets the enabled bit in the control field of event _before_
1418 * accessing the event control register. If a NMI hits, then it will
1419 * keep the event running.
1421 void perf_event_task_sched_in(struct task_struct *task)
1423 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1424 struct perf_event_context *ctx = task->perf_event_ctxp;
1426 if (likely(!ctx))
1427 return;
1429 if (cpuctx->task_ctx == ctx)
1430 return;
1432 perf_disable();
1435 * We want to keep the following priority order:
1436 * cpu pinned (that don't need to move), task pinned,
1437 * cpu flexible, task flexible.
1439 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
1441 ctx_sched_in(ctx, cpuctx, EVENT_PINNED);
1442 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE);
1443 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE);
1445 cpuctx->task_ctx = ctx;
1447 perf_enable();
1450 #define MAX_INTERRUPTS (~0ULL)
1452 static void perf_log_throttle(struct perf_event *event, int enable);
1454 static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
1456 u64 frequency = event->attr.sample_freq;
1457 u64 sec = NSEC_PER_SEC;
1458 u64 divisor, dividend;
1460 int count_fls, nsec_fls, frequency_fls, sec_fls;
1462 count_fls = fls64(count);
1463 nsec_fls = fls64(nsec);
1464 frequency_fls = fls64(frequency);
1465 sec_fls = 30;
1468 * We got @count in @nsec, with a target of sample_freq HZ
1469 * the target period becomes:
1471 * @count * 10^9
1472 * period = -------------------
1473 * @nsec * sample_freq
1478 * Reduce accuracy by one bit such that @a and @b converge
1479 * to a similar magnitude.
1481 #define REDUCE_FLS(a, b) \
1482 do { \
1483 if (a##_fls > b##_fls) { \
1484 a >>= 1; \
1485 a##_fls--; \
1486 } else { \
1487 b >>= 1; \
1488 b##_fls--; \
1490 } while (0)
1493 * Reduce accuracy until either term fits in a u64, then proceed with
1494 * the other, so that finally we can do a u64/u64 division.
1496 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
1497 REDUCE_FLS(nsec, frequency);
1498 REDUCE_FLS(sec, count);
1501 if (count_fls + sec_fls > 64) {
1502 divisor = nsec * frequency;
1504 while (count_fls + sec_fls > 64) {
1505 REDUCE_FLS(count, sec);
1506 divisor >>= 1;
1509 dividend = count * sec;
1510 } else {
1511 dividend = count * sec;
1513 while (nsec_fls + frequency_fls > 64) {
1514 REDUCE_FLS(nsec, frequency);
1515 dividend >>= 1;
1518 divisor = nsec * frequency;
1521 if (!divisor)
1522 return dividend;
1524 return div64_u64(dividend, divisor);
1527 static void perf_event_stop(struct perf_event *event)
1529 if (!event->pmu->stop)
1530 return event->pmu->disable(event);
1532 return event->pmu->stop(event);
1535 static int perf_event_start(struct perf_event *event)
1537 if (!event->pmu->start)
1538 return event->pmu->enable(event);
1540 return event->pmu->start(event);
1543 static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count)
1545 struct hw_perf_event *hwc = &event->hw;
1546 s64 period, sample_period;
1547 s64 delta;
1549 period = perf_calculate_period(event, nsec, count);
1551 delta = (s64)(period - hwc->sample_period);
1552 delta = (delta + 7) / 8; /* low pass filter */
1554 sample_period = hwc->sample_period + delta;
1556 if (!sample_period)
1557 sample_period = 1;
1559 hwc->sample_period = sample_period;
1561 if (local64_read(&hwc->period_left) > 8*sample_period) {
1562 perf_disable();
1563 perf_event_stop(event);
1564 local64_set(&hwc->period_left, 0);
1565 perf_event_start(event);
1566 perf_enable();
1570 static void perf_ctx_adjust_freq(struct perf_event_context *ctx)
1572 struct perf_event *event;
1573 struct hw_perf_event *hwc;
1574 u64 interrupts, now;
1575 s64 delta;
1577 raw_spin_lock(&ctx->lock);
1578 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
1579 if (event->state != PERF_EVENT_STATE_ACTIVE)
1580 continue;
1582 if (event->cpu != -1 && event->cpu != smp_processor_id())
1583 continue;
1585 hwc = &event->hw;
1587 interrupts = hwc->interrupts;
1588 hwc->interrupts = 0;
1591 * unthrottle events on the tick
1593 if (interrupts == MAX_INTERRUPTS) {
1594 perf_log_throttle(event, 1);
1595 perf_disable();
1596 event->pmu->unthrottle(event);
1597 perf_enable();
1600 if (!event->attr.freq || !event->attr.sample_freq)
1601 continue;
1603 perf_disable();
1604 event->pmu->read(event);
1605 now = local64_read(&event->count);
1606 delta = now - hwc->freq_count_stamp;
1607 hwc->freq_count_stamp = now;
1609 if (delta > 0)
1610 perf_adjust_period(event, TICK_NSEC, delta);
1611 perf_enable();
1613 raw_spin_unlock(&ctx->lock);
1617 * Round-robin a context's events:
1619 static void rotate_ctx(struct perf_event_context *ctx)
1621 raw_spin_lock(&ctx->lock);
1623 /* Rotate the first entry last of non-pinned groups */
1624 list_rotate_left(&ctx->flexible_groups);
1626 raw_spin_unlock(&ctx->lock);
1629 void perf_event_task_tick(struct task_struct *curr)
1631 struct perf_cpu_context *cpuctx;
1632 struct perf_event_context *ctx;
1633 int rotate = 0;
1635 if (!atomic_read(&nr_events))
1636 return;
1638 cpuctx = &__get_cpu_var(perf_cpu_context);
1639 if (cpuctx->ctx.nr_events &&
1640 cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
1641 rotate = 1;
1643 ctx = curr->perf_event_ctxp;
1644 if (ctx && ctx->nr_events && ctx->nr_events != ctx->nr_active)
1645 rotate = 1;
1647 perf_ctx_adjust_freq(&cpuctx->ctx);
1648 if (ctx)
1649 perf_ctx_adjust_freq(ctx);
1651 if (!rotate)
1652 return;
1654 perf_disable();
1655 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
1656 if (ctx)
1657 task_ctx_sched_out(ctx, EVENT_FLEXIBLE);
1659 rotate_ctx(&cpuctx->ctx);
1660 if (ctx)
1661 rotate_ctx(ctx);
1663 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE);
1664 if (ctx)
1665 task_ctx_sched_in(curr, EVENT_FLEXIBLE);
1666 perf_enable();
1669 static int event_enable_on_exec(struct perf_event *event,
1670 struct perf_event_context *ctx)
1672 if (!event->attr.enable_on_exec)
1673 return 0;
1675 event->attr.enable_on_exec = 0;
1676 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1677 return 0;
1679 __perf_event_mark_enabled(event, ctx);
1681 return 1;
1685 * Enable all of a task's events that have been marked enable-on-exec.
1686 * This expects task == current.
1688 static void perf_event_enable_on_exec(struct task_struct *task)
1690 struct perf_event_context *ctx;
1691 struct perf_event *event;
1692 unsigned long flags;
1693 int enabled = 0;
1694 int ret;
1696 local_irq_save(flags);
1697 ctx = task->perf_event_ctxp;
1698 if (!ctx || !ctx->nr_events)
1699 goto out;
1701 __perf_event_task_sched_out(ctx);
1703 raw_spin_lock(&ctx->lock);
1705 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
1706 ret = event_enable_on_exec(event, ctx);
1707 if (ret)
1708 enabled = 1;
1711 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
1712 ret = event_enable_on_exec(event, ctx);
1713 if (ret)
1714 enabled = 1;
1718 * Unclone this context if we enabled any event.
1720 if (enabled)
1721 unclone_ctx(ctx);
1723 raw_spin_unlock(&ctx->lock);
1725 perf_event_task_sched_in(task);
1726 out:
1727 local_irq_restore(flags);
1731 * Cross CPU call to read the hardware event
1733 static void __perf_event_read(void *info)
1735 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1736 struct perf_event *event = info;
1737 struct perf_event_context *ctx = event->ctx;
1740 * If this is a task context, we need to check whether it is
1741 * the current task context of this cpu. If not it has been
1742 * scheduled out before the smp call arrived. In that case
1743 * event->count would have been updated to a recent sample
1744 * when the event was scheduled out.
1746 if (ctx->task && cpuctx->task_ctx != ctx)
1747 return;
1749 raw_spin_lock(&ctx->lock);
1750 update_context_time(ctx);
1751 update_event_times(event);
1752 raw_spin_unlock(&ctx->lock);
1754 event->pmu->read(event);
1757 static inline u64 perf_event_count(struct perf_event *event)
1759 return local64_read(&event->count) + atomic64_read(&event->child_count);
1762 static u64 perf_event_read(struct perf_event *event)
1765 * If event is enabled and currently active on a CPU, update the
1766 * value in the event structure:
1768 if (event->state == PERF_EVENT_STATE_ACTIVE) {
1769 smp_call_function_single(event->oncpu,
1770 __perf_event_read, event, 1);
1771 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
1772 struct perf_event_context *ctx = event->ctx;
1773 unsigned long flags;
1775 raw_spin_lock_irqsave(&ctx->lock, flags);
1776 update_context_time(ctx);
1777 update_event_times(event);
1778 raw_spin_unlock_irqrestore(&ctx->lock, flags);
1781 return perf_event_count(event);
1785 * Initialize the perf_event context in a task_struct:
1787 static void
1788 __perf_event_init_context(struct perf_event_context *ctx,
1789 struct task_struct *task)
1791 raw_spin_lock_init(&ctx->lock);
1792 mutex_init(&ctx->mutex);
1793 INIT_LIST_HEAD(&ctx->pinned_groups);
1794 INIT_LIST_HEAD(&ctx->flexible_groups);
1795 INIT_LIST_HEAD(&ctx->event_list);
1796 atomic_set(&ctx->refcount, 1);
1797 ctx->task = task;
1800 static struct perf_event_context *find_get_context(pid_t pid, int cpu)
1802 struct perf_event_context *ctx;
1803 struct perf_cpu_context *cpuctx;
1804 struct task_struct *task;
1805 unsigned long flags;
1806 int err;
1808 if (pid == -1 && cpu != -1) {
1809 /* Must be root to operate on a CPU event: */
1810 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
1811 return ERR_PTR(-EACCES);
1813 if (cpu < 0 || cpu >= nr_cpumask_bits)
1814 return ERR_PTR(-EINVAL);
1817 * We could be clever and allow to attach a event to an
1818 * offline CPU and activate it when the CPU comes up, but
1819 * that's for later.
1821 if (!cpu_online(cpu))
1822 return ERR_PTR(-ENODEV);
1824 cpuctx = &per_cpu(perf_cpu_context, cpu);
1825 ctx = &cpuctx->ctx;
1826 get_ctx(ctx);
1828 return ctx;
1831 rcu_read_lock();
1832 if (!pid)
1833 task = current;
1834 else
1835 task = find_task_by_vpid(pid);
1836 if (task)
1837 get_task_struct(task);
1838 rcu_read_unlock();
1840 if (!task)
1841 return ERR_PTR(-ESRCH);
1844 * Can't attach events to a dying task.
1846 err = -ESRCH;
1847 if (task->flags & PF_EXITING)
1848 goto errout;
1850 /* Reuse ptrace permission checks for now. */
1851 err = -EACCES;
1852 if (!ptrace_may_access(task, PTRACE_MODE_READ))
1853 goto errout;
1855 retry:
1856 ctx = perf_lock_task_context(task, &flags);
1857 if (ctx) {
1858 unclone_ctx(ctx);
1859 raw_spin_unlock_irqrestore(&ctx->lock, flags);
1862 if (!ctx) {
1863 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
1864 err = -ENOMEM;
1865 if (!ctx)
1866 goto errout;
1867 __perf_event_init_context(ctx, task);
1868 get_ctx(ctx);
1869 if (cmpxchg(&task->perf_event_ctxp, NULL, ctx)) {
1871 * We raced with some other task; use
1872 * the context they set.
1874 kfree(ctx);
1875 goto retry;
1877 get_task_struct(task);
1880 put_task_struct(task);
1881 return ctx;
1883 errout:
1884 put_task_struct(task);
1885 return ERR_PTR(err);
1888 static void perf_event_free_filter(struct perf_event *event);
1890 static void free_event_rcu(struct rcu_head *head)
1892 struct perf_event *event;
1894 event = container_of(head, struct perf_event, rcu_head);
1895 if (event->ns)
1896 put_pid_ns(event->ns);
1897 perf_event_free_filter(event);
1898 kfree(event);
1901 static void perf_pending_sync(struct perf_event *event);
1902 static void perf_buffer_put(struct perf_buffer *buffer);
1904 static void free_event(struct perf_event *event)
1906 perf_pending_sync(event);
1908 if (!event->parent) {
1909 atomic_dec(&nr_events);
1910 if (event->attr.mmap || event->attr.mmap_data)
1911 atomic_dec(&nr_mmap_events);
1912 if (event->attr.comm)
1913 atomic_dec(&nr_comm_events);
1914 if (event->attr.task)
1915 atomic_dec(&nr_task_events);
1918 if (event->buffer) {
1919 perf_buffer_put(event->buffer);
1920 event->buffer = NULL;
1923 if (event->destroy)
1924 event->destroy(event);
1926 put_ctx(event->ctx);
1927 call_rcu(&event->rcu_head, free_event_rcu);
1930 int perf_event_release_kernel(struct perf_event *event)
1932 struct perf_event_context *ctx = event->ctx;
1935 * Remove from the PMU, can't get re-enabled since we got
1936 * here because the last ref went.
1938 perf_event_disable(event);
1940 WARN_ON_ONCE(ctx->parent_ctx);
1942 * There are two ways this annotation is useful:
1944 * 1) there is a lock recursion from perf_event_exit_task
1945 * see the comment there.
1947 * 2) there is a lock-inversion with mmap_sem through
1948 * perf_event_read_group(), which takes faults while
1949 * holding ctx->mutex, however this is called after
1950 * the last filedesc died, so there is no possibility
1951 * to trigger the AB-BA case.
1953 mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
1954 raw_spin_lock_irq(&ctx->lock);
1955 perf_group_detach(event);
1956 list_del_event(event, ctx);
1957 raw_spin_unlock_irq(&ctx->lock);
1958 mutex_unlock(&ctx->mutex);
1960 mutex_lock(&event->owner->perf_event_mutex);
1961 list_del_init(&event->owner_entry);
1962 mutex_unlock(&event->owner->perf_event_mutex);
1963 put_task_struct(event->owner);
1965 free_event(event);
1967 return 0;
1969 EXPORT_SYMBOL_GPL(perf_event_release_kernel);
1972 * Called when the last reference to the file is gone.
1974 static int perf_release(struct inode *inode, struct file *file)
1976 struct perf_event *event = file->private_data;
1978 file->private_data = NULL;
1980 return perf_event_release_kernel(event);
1983 static int perf_event_read_size(struct perf_event *event)
1985 int entry = sizeof(u64); /* value */
1986 int size = 0;
1987 int nr = 1;
1989 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1990 size += sizeof(u64);
1992 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1993 size += sizeof(u64);
1995 if (event->attr.read_format & PERF_FORMAT_ID)
1996 entry += sizeof(u64);
1998 if (event->attr.read_format & PERF_FORMAT_GROUP) {
1999 nr += event->group_leader->nr_siblings;
2000 size += sizeof(u64);
2003 size += entry * nr;
2005 return size;
2008 u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
2010 struct perf_event *child;
2011 u64 total = 0;
2013 *enabled = 0;
2014 *running = 0;
2016 mutex_lock(&event->child_mutex);
2017 total += perf_event_read(event);
2018 *enabled += event->total_time_enabled +
2019 atomic64_read(&event->child_total_time_enabled);
2020 *running += event->total_time_running +
2021 atomic64_read(&event->child_total_time_running);
2023 list_for_each_entry(child, &event->child_list, child_list) {
2024 total += perf_event_read(child);
2025 *enabled += child->total_time_enabled;
2026 *running += child->total_time_running;
2028 mutex_unlock(&event->child_mutex);
2030 return total;
2032 EXPORT_SYMBOL_GPL(perf_event_read_value);
2034 static int perf_event_read_group(struct perf_event *event,
2035 u64 read_format, char __user *buf)
2037 struct perf_event *leader = event->group_leader, *sub;
2038 int n = 0, size = 0, ret = -EFAULT;
2039 struct perf_event_context *ctx = leader->ctx;
2040 u64 values[5];
2041 u64 count, enabled, running;
2043 mutex_lock(&ctx->mutex);
2044 count = perf_event_read_value(leader, &enabled, &running);
2046 values[n++] = 1 + leader->nr_siblings;
2047 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2048 values[n++] = enabled;
2049 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2050 values[n++] = running;
2051 values[n++] = count;
2052 if (read_format & PERF_FORMAT_ID)
2053 values[n++] = primary_event_id(leader);
2055 size = n * sizeof(u64);
2057 if (copy_to_user(buf, values, size))
2058 goto unlock;
2060 ret = size;
2062 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
2063 n = 0;
2065 values[n++] = perf_event_read_value(sub, &enabled, &running);
2066 if (read_format & PERF_FORMAT_ID)
2067 values[n++] = primary_event_id(sub);
2069 size = n * sizeof(u64);
2071 if (copy_to_user(buf + ret, values, size)) {
2072 ret = -EFAULT;
2073 goto unlock;
2076 ret += size;
2078 unlock:
2079 mutex_unlock(&ctx->mutex);
2081 return ret;
2084 static int perf_event_read_one(struct perf_event *event,
2085 u64 read_format, char __user *buf)
2087 u64 enabled, running;
2088 u64 values[4];
2089 int n = 0;
2091 values[n++] = perf_event_read_value(event, &enabled, &running);
2092 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2093 values[n++] = enabled;
2094 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2095 values[n++] = running;
2096 if (read_format & PERF_FORMAT_ID)
2097 values[n++] = primary_event_id(event);
2099 if (copy_to_user(buf, values, n * sizeof(u64)))
2100 return -EFAULT;
2102 return n * sizeof(u64);
2106 * Read the performance event - simple non blocking version for now
2108 static ssize_t
2109 perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
2111 u64 read_format = event->attr.read_format;
2112 int ret;
2115 * Return end-of-file for a read on a event that is in
2116 * error state (i.e. because it was pinned but it couldn't be
2117 * scheduled on to the CPU at some point).
2119 if (event->state == PERF_EVENT_STATE_ERROR)
2120 return 0;
2122 if (count < perf_event_read_size(event))
2123 return -ENOSPC;
2125 WARN_ON_ONCE(event->ctx->parent_ctx);
2126 if (read_format & PERF_FORMAT_GROUP)
2127 ret = perf_event_read_group(event, read_format, buf);
2128 else
2129 ret = perf_event_read_one(event, read_format, buf);
2131 return ret;
2134 static ssize_t
2135 perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
2137 struct perf_event *event = file->private_data;
2139 return perf_read_hw(event, buf, count);
2142 static unsigned int perf_poll(struct file *file, poll_table *wait)
2144 struct perf_event *event = file->private_data;
2145 struct perf_buffer *buffer;
2146 unsigned int events = POLL_HUP;
2148 rcu_read_lock();
2149 buffer = rcu_dereference(event->buffer);
2150 if (buffer)
2151 events = atomic_xchg(&buffer->poll, 0);
2152 rcu_read_unlock();
2154 poll_wait(file, &event->waitq, wait);
2156 return events;
2159 static void perf_event_reset(struct perf_event *event)
2161 (void)perf_event_read(event);
2162 local64_set(&event->count, 0);
2163 perf_event_update_userpage(event);
2167 * Holding the top-level event's child_mutex means that any
2168 * descendant process that has inherited this event will block
2169 * in sync_child_event if it goes to exit, thus satisfying the
2170 * task existence requirements of perf_event_enable/disable.
2172 static void perf_event_for_each_child(struct perf_event *event,
2173 void (*func)(struct perf_event *))
2175 struct perf_event *child;
2177 WARN_ON_ONCE(event->ctx->parent_ctx);
2178 mutex_lock(&event->child_mutex);
2179 func(event);
2180 list_for_each_entry(child, &event->child_list, child_list)
2181 func(child);
2182 mutex_unlock(&event->child_mutex);
2185 static void perf_event_for_each(struct perf_event *event,
2186 void (*func)(struct perf_event *))
2188 struct perf_event_context *ctx = event->ctx;
2189 struct perf_event *sibling;
2191 WARN_ON_ONCE(ctx->parent_ctx);
2192 mutex_lock(&ctx->mutex);
2193 event = event->group_leader;
2195 perf_event_for_each_child(event, func);
2196 func(event);
2197 list_for_each_entry(sibling, &event->sibling_list, group_entry)
2198 perf_event_for_each_child(event, func);
2199 mutex_unlock(&ctx->mutex);
2202 static int perf_event_period(struct perf_event *event, u64 __user *arg)
2204 struct perf_event_context *ctx = event->ctx;
2205 unsigned long size;
2206 int ret = 0;
2207 u64 value;
2209 if (!event->attr.sample_period)
2210 return -EINVAL;
2212 size = copy_from_user(&value, arg, sizeof(value));
2213 if (size != sizeof(value))
2214 return -EFAULT;
2216 if (!value)
2217 return -EINVAL;
2219 raw_spin_lock_irq(&ctx->lock);
2220 if (event->attr.freq) {
2221 if (value > sysctl_perf_event_sample_rate) {
2222 ret = -EINVAL;
2223 goto unlock;
2226 event->attr.sample_freq = value;
2227 } else {
2228 event->attr.sample_period = value;
2229 event->hw.sample_period = value;
2231 unlock:
2232 raw_spin_unlock_irq(&ctx->lock);
2234 return ret;
2237 static const struct file_operations perf_fops;
2239 static struct perf_event *perf_fget_light(int fd, int *fput_needed)
2241 struct file *file;
2243 file = fget_light(fd, fput_needed);
2244 if (!file)
2245 return ERR_PTR(-EBADF);
2247 if (file->f_op != &perf_fops) {
2248 fput_light(file, *fput_needed);
2249 *fput_needed = 0;
2250 return ERR_PTR(-EBADF);
2253 return file->private_data;
2256 static int perf_event_set_output(struct perf_event *event,
2257 struct perf_event *output_event);
2258 static int perf_event_set_filter(struct perf_event *event, void __user *arg);
2260 static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2262 struct perf_event *event = file->private_data;
2263 void (*func)(struct perf_event *);
2264 u32 flags = arg;
2266 switch (cmd) {
2267 case PERF_EVENT_IOC_ENABLE:
2268 func = perf_event_enable;
2269 break;
2270 case PERF_EVENT_IOC_DISABLE:
2271 func = perf_event_disable;
2272 break;
2273 case PERF_EVENT_IOC_RESET:
2274 func = perf_event_reset;
2275 break;
2277 case PERF_EVENT_IOC_REFRESH:
2278 return perf_event_refresh(event, arg);
2280 case PERF_EVENT_IOC_PERIOD:
2281 return perf_event_period(event, (u64 __user *)arg);
2283 case PERF_EVENT_IOC_SET_OUTPUT:
2285 struct perf_event *output_event = NULL;
2286 int fput_needed = 0;
2287 int ret;
2289 if (arg != -1) {
2290 output_event = perf_fget_light(arg, &fput_needed);
2291 if (IS_ERR(output_event))
2292 return PTR_ERR(output_event);
2295 ret = perf_event_set_output(event, output_event);
2296 if (output_event)
2297 fput_light(output_event->filp, fput_needed);
2299 return ret;
2302 case PERF_EVENT_IOC_SET_FILTER:
2303 return perf_event_set_filter(event, (void __user *)arg);
2305 default:
2306 return -ENOTTY;
2309 if (flags & PERF_IOC_FLAG_GROUP)
2310 perf_event_for_each(event, func);
2311 else
2312 perf_event_for_each_child(event, func);
2314 return 0;
2317 int perf_event_task_enable(void)
2319 struct perf_event *event;
2321 mutex_lock(&current->perf_event_mutex);
2322 list_for_each_entry(event, &current->perf_event_list, owner_entry)
2323 perf_event_for_each_child(event, perf_event_enable);
2324 mutex_unlock(&current->perf_event_mutex);
2326 return 0;
2329 int perf_event_task_disable(void)
2331 struct perf_event *event;
2333 mutex_lock(&current->perf_event_mutex);
2334 list_for_each_entry(event, &current->perf_event_list, owner_entry)
2335 perf_event_for_each_child(event, perf_event_disable);
2336 mutex_unlock(&current->perf_event_mutex);
2338 return 0;
2341 #ifndef PERF_EVENT_INDEX_OFFSET
2342 # define PERF_EVENT_INDEX_OFFSET 0
2343 #endif
2345 static int perf_event_index(struct perf_event *event)
2347 if (event->state != PERF_EVENT_STATE_ACTIVE)
2348 return 0;
2350 return event->hw.idx + 1 - PERF_EVENT_INDEX_OFFSET;
2354 * Callers need to ensure there can be no nesting of this function, otherwise
2355 * the seqlock logic goes bad. We can not serialize this because the arch
2356 * code calls this from NMI context.
2358 void perf_event_update_userpage(struct perf_event *event)
2360 struct perf_event_mmap_page *userpg;
2361 struct perf_buffer *buffer;
2363 rcu_read_lock();
2364 buffer = rcu_dereference(event->buffer);
2365 if (!buffer)
2366 goto unlock;
2368 userpg = buffer->user_page;
2371 * Disable preemption so as to not let the corresponding user-space
2372 * spin too long if we get preempted.
2374 preempt_disable();
2375 ++userpg->lock;
2376 barrier();
2377 userpg->index = perf_event_index(event);
2378 userpg->offset = perf_event_count(event);
2379 if (event->state == PERF_EVENT_STATE_ACTIVE)
2380 userpg->offset -= local64_read(&event->hw.prev_count);
2382 userpg->time_enabled = event->total_time_enabled +
2383 atomic64_read(&event->child_total_time_enabled);
2385 userpg->time_running = event->total_time_running +
2386 atomic64_read(&event->child_total_time_running);
2388 barrier();
2389 ++userpg->lock;
2390 preempt_enable();
2391 unlock:
2392 rcu_read_unlock();
2395 static unsigned long perf_data_size(struct perf_buffer *buffer);
2397 static void
2398 perf_buffer_init(struct perf_buffer *buffer, long watermark, int flags)
2400 long max_size = perf_data_size(buffer);
2402 if (watermark)
2403 buffer->watermark = min(max_size, watermark);
2405 if (!buffer->watermark)
2406 buffer->watermark = max_size / 2;
2408 if (flags & PERF_BUFFER_WRITABLE)
2409 buffer->writable = 1;
2411 atomic_set(&buffer->refcount, 1);
2414 #ifndef CONFIG_PERF_USE_VMALLOC
2417 * Back perf_mmap() with regular GFP_KERNEL-0 pages.
2420 static struct page *
2421 perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
2423 if (pgoff > buffer->nr_pages)
2424 return NULL;
2426 if (pgoff == 0)
2427 return virt_to_page(buffer->user_page);
2429 return virt_to_page(buffer->data_pages[pgoff - 1]);
2432 static void *perf_mmap_alloc_page(int cpu)
2434 struct page *page;
2435 int node;
2437 node = (cpu == -1) ? cpu : cpu_to_node(cpu);
2438 page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
2439 if (!page)
2440 return NULL;
2442 return page_address(page);
2445 static struct perf_buffer *
2446 perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
2448 struct perf_buffer *buffer;
2449 unsigned long size;
2450 int i;
2452 size = sizeof(struct perf_buffer);
2453 size += nr_pages * sizeof(void *);
2455 buffer = kzalloc(size, GFP_KERNEL);
2456 if (!buffer)
2457 goto fail;
2459 buffer->user_page = perf_mmap_alloc_page(cpu);
2460 if (!buffer->user_page)
2461 goto fail_user_page;
2463 for (i = 0; i < nr_pages; i++) {
2464 buffer->data_pages[i] = perf_mmap_alloc_page(cpu);
2465 if (!buffer->data_pages[i])
2466 goto fail_data_pages;
2469 buffer->nr_pages = nr_pages;
2471 perf_buffer_init(buffer, watermark, flags);
2473 return buffer;
2475 fail_data_pages:
2476 for (i--; i >= 0; i--)
2477 free_page((unsigned long)buffer->data_pages[i]);
2479 free_page((unsigned long)buffer->user_page);
2481 fail_user_page:
2482 kfree(buffer);
2484 fail:
2485 return NULL;
2488 static void perf_mmap_free_page(unsigned long addr)
2490 struct page *page = virt_to_page((void *)addr);
2492 page->mapping = NULL;
2493 __free_page(page);
2496 static void perf_buffer_free(struct perf_buffer *buffer)
2498 int i;
2500 perf_mmap_free_page((unsigned long)buffer->user_page);
2501 for (i = 0; i < buffer->nr_pages; i++)
2502 perf_mmap_free_page((unsigned long)buffer->data_pages[i]);
2503 kfree(buffer);
2506 static inline int page_order(struct perf_buffer *buffer)
2508 return 0;
2511 #else
2514 * Back perf_mmap() with vmalloc memory.
2516 * Required for architectures that have d-cache aliasing issues.
2519 static inline int page_order(struct perf_buffer *buffer)
2521 return buffer->page_order;
2524 static struct page *
2525 perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
2527 if (pgoff > (1UL << page_order(buffer)))
2528 return NULL;
2530 return vmalloc_to_page((void *)buffer->user_page + pgoff * PAGE_SIZE);
2533 static void perf_mmap_unmark_page(void *addr)
2535 struct page *page = vmalloc_to_page(addr);
2537 page->mapping = NULL;
2540 static void perf_buffer_free_work(struct work_struct *work)
2542 struct perf_buffer *buffer;
2543 void *base;
2544 int i, nr;
2546 buffer = container_of(work, struct perf_buffer, work);
2547 nr = 1 << page_order(buffer);
2549 base = buffer->user_page;
2550 for (i = 0; i < nr + 1; i++)
2551 perf_mmap_unmark_page(base + (i * PAGE_SIZE));
2553 vfree(base);
2554 kfree(buffer);
2557 static void perf_buffer_free(struct perf_buffer *buffer)
2559 schedule_work(&buffer->work);
2562 static struct perf_buffer *
2563 perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
2565 struct perf_buffer *buffer;
2566 unsigned long size;
2567 void *all_buf;
2569 size = sizeof(struct perf_buffer);
2570 size += sizeof(void *);
2572 buffer = kzalloc(size, GFP_KERNEL);
2573 if (!buffer)
2574 goto fail;
2576 INIT_WORK(&buffer->work, perf_buffer_free_work);
2578 all_buf = vmalloc_user((nr_pages + 1) * PAGE_SIZE);
2579 if (!all_buf)
2580 goto fail_all_buf;
2582 buffer->user_page = all_buf;
2583 buffer->data_pages[0] = all_buf + PAGE_SIZE;
2584 buffer->page_order = ilog2(nr_pages);
2585 buffer->nr_pages = 1;
2587 perf_buffer_init(buffer, watermark, flags);
2589 return buffer;
2591 fail_all_buf:
2592 kfree(buffer);
2594 fail:
2595 return NULL;
2598 #endif
2600 static unsigned long perf_data_size(struct perf_buffer *buffer)
2602 return buffer->nr_pages << (PAGE_SHIFT + page_order(buffer));
2605 static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
2607 struct perf_event *event = vma->vm_file->private_data;
2608 struct perf_buffer *buffer;
2609 int ret = VM_FAULT_SIGBUS;
2611 if (vmf->flags & FAULT_FLAG_MKWRITE) {
2612 if (vmf->pgoff == 0)
2613 ret = 0;
2614 return ret;
2617 rcu_read_lock();
2618 buffer = rcu_dereference(event->buffer);
2619 if (!buffer)
2620 goto unlock;
2622 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
2623 goto unlock;
2625 vmf->page = perf_mmap_to_page(buffer, vmf->pgoff);
2626 if (!vmf->page)
2627 goto unlock;
2629 get_page(vmf->page);
2630 vmf->page->mapping = vma->vm_file->f_mapping;
2631 vmf->page->index = vmf->pgoff;
2633 ret = 0;
2634 unlock:
2635 rcu_read_unlock();
2637 return ret;
2640 static void perf_buffer_free_rcu(struct rcu_head *rcu_head)
2642 struct perf_buffer *buffer;
2644 buffer = container_of(rcu_head, struct perf_buffer, rcu_head);
2645 perf_buffer_free(buffer);
2648 static struct perf_buffer *perf_buffer_get(struct perf_event *event)
2650 struct perf_buffer *buffer;
2652 rcu_read_lock();
2653 buffer = rcu_dereference(event->buffer);
2654 if (buffer) {
2655 if (!atomic_inc_not_zero(&buffer->refcount))
2656 buffer = NULL;
2658 rcu_read_unlock();
2660 return buffer;
2663 static void perf_buffer_put(struct perf_buffer *buffer)
2665 if (!atomic_dec_and_test(&buffer->refcount))
2666 return;
2668 call_rcu(&buffer->rcu_head, perf_buffer_free_rcu);
2671 static void perf_mmap_open(struct vm_area_struct *vma)
2673 struct perf_event *event = vma->vm_file->private_data;
2675 atomic_inc(&event->mmap_count);
2678 static void perf_mmap_close(struct vm_area_struct *vma)
2680 struct perf_event *event = vma->vm_file->private_data;
2682 if (atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) {
2683 unsigned long size = perf_data_size(event->buffer);
2684 struct user_struct *user = event->mmap_user;
2685 struct perf_buffer *buffer = event->buffer;
2687 atomic_long_sub((size >> PAGE_SHIFT) + 1, &user->locked_vm);
2688 vma->vm_mm->locked_vm -= event->mmap_locked;
2689 rcu_assign_pointer(event->buffer, NULL);
2690 mutex_unlock(&event->mmap_mutex);
2692 perf_buffer_put(buffer);
2693 free_uid(user);
2697 static const struct vm_operations_struct perf_mmap_vmops = {
2698 .open = perf_mmap_open,
2699 .close = perf_mmap_close,
2700 .fault = perf_mmap_fault,
2701 .page_mkwrite = perf_mmap_fault,
2704 static int perf_mmap(struct file *file, struct vm_area_struct *vma)
2706 struct perf_event *event = file->private_data;
2707 unsigned long user_locked, user_lock_limit;
2708 struct user_struct *user = current_user();
2709 unsigned long locked, lock_limit;
2710 struct perf_buffer *buffer;
2711 unsigned long vma_size;
2712 unsigned long nr_pages;
2713 long user_extra, extra;
2714 int ret = 0, flags = 0;
2717 * Don't allow mmap() of inherited per-task counters. This would
2718 * create a performance issue due to all children writing to the
2719 * same buffer.
2721 if (event->cpu == -1 && event->attr.inherit)
2722 return -EINVAL;
2724 if (!(vma->vm_flags & VM_SHARED))
2725 return -EINVAL;
2727 vma_size = vma->vm_end - vma->vm_start;
2728 nr_pages = (vma_size / PAGE_SIZE) - 1;
2731 * If we have buffer pages ensure they're a power-of-two number, so we
2732 * can do bitmasks instead of modulo.
2734 if (nr_pages != 0 && !is_power_of_2(nr_pages))
2735 return -EINVAL;
2737 if (vma_size != PAGE_SIZE * (1 + nr_pages))
2738 return -EINVAL;
2740 if (vma->vm_pgoff != 0)
2741 return -EINVAL;
2743 WARN_ON_ONCE(event->ctx->parent_ctx);
2744 mutex_lock(&event->mmap_mutex);
2745 if (event->buffer) {
2746 if (event->buffer->nr_pages == nr_pages)
2747 atomic_inc(&event->buffer->refcount);
2748 else
2749 ret = -EINVAL;
2750 goto unlock;
2753 user_extra = nr_pages + 1;
2754 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
2757 * Increase the limit linearly with more CPUs:
2759 user_lock_limit *= num_online_cpus();
2761 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
2763 extra = 0;
2764 if (user_locked > user_lock_limit)
2765 extra = user_locked - user_lock_limit;
2767 lock_limit = rlimit(RLIMIT_MEMLOCK);
2768 lock_limit >>= PAGE_SHIFT;
2769 locked = vma->vm_mm->locked_vm + extra;
2771 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
2772 !capable(CAP_IPC_LOCK)) {
2773 ret = -EPERM;
2774 goto unlock;
2777 WARN_ON(event->buffer);
2779 if (vma->vm_flags & VM_WRITE)
2780 flags |= PERF_BUFFER_WRITABLE;
2782 buffer = perf_buffer_alloc(nr_pages, event->attr.wakeup_watermark,
2783 event->cpu, flags);
2784 if (!buffer) {
2785 ret = -ENOMEM;
2786 goto unlock;
2788 rcu_assign_pointer(event->buffer, buffer);
2790 atomic_long_add(user_extra, &user->locked_vm);
2791 event->mmap_locked = extra;
2792 event->mmap_user = get_current_user();
2793 vma->vm_mm->locked_vm += event->mmap_locked;
2795 unlock:
2796 if (!ret)
2797 atomic_inc(&event->mmap_count);
2798 mutex_unlock(&event->mmap_mutex);
2800 vma->vm_flags |= VM_RESERVED;
2801 vma->vm_ops = &perf_mmap_vmops;
2803 return ret;
2806 static int perf_fasync(int fd, struct file *filp, int on)
2808 struct inode *inode = filp->f_path.dentry->d_inode;
2809 struct perf_event *event = filp->private_data;
2810 int retval;
2812 mutex_lock(&inode->i_mutex);
2813 retval = fasync_helper(fd, filp, on, &event->fasync);
2814 mutex_unlock(&inode->i_mutex);
2816 if (retval < 0)
2817 return retval;
2819 return 0;
2822 static const struct file_operations perf_fops = {
2823 .llseek = no_llseek,
2824 .release = perf_release,
2825 .read = perf_read,
2826 .poll = perf_poll,
2827 .unlocked_ioctl = perf_ioctl,
2828 .compat_ioctl = perf_ioctl,
2829 .mmap = perf_mmap,
2830 .fasync = perf_fasync,
2834 * Perf event wakeup
2836 * If there's data, ensure we set the poll() state and publish everything
2837 * to user-space before waking everybody up.
2840 void perf_event_wakeup(struct perf_event *event)
2842 wake_up_all(&event->waitq);
2844 if (event->pending_kill) {
2845 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
2846 event->pending_kill = 0;
2851 * Pending wakeups
2853 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
2855 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
2856 * single linked list and use cmpxchg() to add entries lockless.
2859 static void perf_pending_event(struct perf_pending_entry *entry)
2861 struct perf_event *event = container_of(entry,
2862 struct perf_event, pending);
2864 if (event->pending_disable) {
2865 event->pending_disable = 0;
2866 __perf_event_disable(event);
2869 if (event->pending_wakeup) {
2870 event->pending_wakeup = 0;
2871 perf_event_wakeup(event);
2875 #define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
2877 static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
2878 PENDING_TAIL,
2881 static void perf_pending_queue(struct perf_pending_entry *entry,
2882 void (*func)(struct perf_pending_entry *))
2884 struct perf_pending_entry **head;
2886 if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
2887 return;
2889 entry->func = func;
2891 head = &get_cpu_var(perf_pending_head);
2893 do {
2894 entry->next = *head;
2895 } while (cmpxchg(head, entry->next, entry) != entry->next);
2897 set_perf_event_pending();
2899 put_cpu_var(perf_pending_head);
2902 static int __perf_pending_run(void)
2904 struct perf_pending_entry *list;
2905 int nr = 0;
2907 list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
2908 while (list != PENDING_TAIL) {
2909 void (*func)(struct perf_pending_entry *);
2910 struct perf_pending_entry *entry = list;
2912 list = list->next;
2914 func = entry->func;
2915 entry->next = NULL;
2917 * Ensure we observe the unqueue before we issue the wakeup,
2918 * so that we won't be waiting forever.
2919 * -- see perf_not_pending().
2921 smp_wmb();
2923 func(entry);
2924 nr++;
2927 return nr;
2930 static inline int perf_not_pending(struct perf_event *event)
2933 * If we flush on whatever cpu we run, there is a chance we don't
2934 * need to wait.
2936 get_cpu();
2937 __perf_pending_run();
2938 put_cpu();
2941 * Ensure we see the proper queue state before going to sleep
2942 * so that we do not miss the wakeup. -- see perf_pending_handle()
2944 smp_rmb();
2945 return event->pending.next == NULL;
2948 static void perf_pending_sync(struct perf_event *event)
2950 wait_event(event->waitq, perf_not_pending(event));
2953 void perf_event_do_pending(void)
2955 __perf_pending_run();
2959 * Callchain support -- arch specific
2962 __weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
2964 return NULL;
2969 * We assume there is only KVM supporting the callbacks.
2970 * Later on, we might change it to a list if there is
2971 * another virtualization implementation supporting the callbacks.
2973 struct perf_guest_info_callbacks *perf_guest_cbs;
2975 int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
2977 perf_guest_cbs = cbs;
2978 return 0;
2980 EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
2982 int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
2984 perf_guest_cbs = NULL;
2985 return 0;
2987 EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
2990 * Output
2992 static bool perf_output_space(struct perf_buffer *buffer, unsigned long tail,
2993 unsigned long offset, unsigned long head)
2995 unsigned long mask;
2997 if (!buffer->writable)
2998 return true;
3000 mask = perf_data_size(buffer) - 1;
3002 offset = (offset - tail) & mask;
3003 head = (head - tail) & mask;
3005 if ((int)(head - offset) < 0)
3006 return false;
3008 return true;
3011 static void perf_output_wakeup(struct perf_output_handle *handle)
3013 atomic_set(&handle->buffer->poll, POLL_IN);
3015 if (handle->nmi) {
3016 handle->event->pending_wakeup = 1;
3017 perf_pending_queue(&handle->event->pending,
3018 perf_pending_event);
3019 } else
3020 perf_event_wakeup(handle->event);
3024 * We need to ensure a later event_id doesn't publish a head when a former
3025 * event isn't done writing. However since we need to deal with NMIs we
3026 * cannot fully serialize things.
3028 * We only publish the head (and generate a wakeup) when the outer-most
3029 * event completes.
3031 static void perf_output_get_handle(struct perf_output_handle *handle)
3033 struct perf_buffer *buffer = handle->buffer;
3035 preempt_disable();
3036 local_inc(&buffer->nest);
3037 handle->wakeup = local_read(&buffer->wakeup);
3040 static void perf_output_put_handle(struct perf_output_handle *handle)
3042 struct perf_buffer *buffer = handle->buffer;
3043 unsigned long head;
3045 again:
3046 head = local_read(&buffer->head);
3049 * IRQ/NMI can happen here, which means we can miss a head update.
3052 if (!local_dec_and_test(&buffer->nest))
3053 goto out;
3056 * Publish the known good head. Rely on the full barrier implied
3057 * by atomic_dec_and_test() order the buffer->head read and this
3058 * write.
3060 buffer->user_page->data_head = head;
3063 * Now check if we missed an update, rely on the (compiler)
3064 * barrier in atomic_dec_and_test() to re-read buffer->head.
3066 if (unlikely(head != local_read(&buffer->head))) {
3067 local_inc(&buffer->nest);
3068 goto again;
3071 if (handle->wakeup != local_read(&buffer->wakeup))
3072 perf_output_wakeup(handle);
3074 out:
3075 preempt_enable();
3078 __always_inline void perf_output_copy(struct perf_output_handle *handle,
3079 const void *buf, unsigned int len)
3081 do {
3082 unsigned long size = min_t(unsigned long, handle->size, len);
3084 memcpy(handle->addr, buf, size);
3086 len -= size;
3087 handle->addr += size;
3088 buf += size;
3089 handle->size -= size;
3090 if (!handle->size) {
3091 struct perf_buffer *buffer = handle->buffer;
3093 handle->page++;
3094 handle->page &= buffer->nr_pages - 1;
3095 handle->addr = buffer->data_pages[handle->page];
3096 handle->size = PAGE_SIZE << page_order(buffer);
3098 } while (len);
3101 int perf_output_begin(struct perf_output_handle *handle,
3102 struct perf_event *event, unsigned int size,
3103 int nmi, int sample)
3105 struct perf_buffer *buffer;
3106 unsigned long tail, offset, head;
3107 int have_lost;
3108 struct {
3109 struct perf_event_header header;
3110 u64 id;
3111 u64 lost;
3112 } lost_event;
3114 rcu_read_lock();
3116 * For inherited events we send all the output towards the parent.
3118 if (event->parent)
3119 event = event->parent;
3121 buffer = rcu_dereference(event->buffer);
3122 if (!buffer)
3123 goto out;
3125 handle->buffer = buffer;
3126 handle->event = event;
3127 handle->nmi = nmi;
3128 handle->sample = sample;
3130 if (!buffer->nr_pages)
3131 goto out;
3133 have_lost = local_read(&buffer->lost);
3134 if (have_lost)
3135 size += sizeof(lost_event);
3137 perf_output_get_handle(handle);
3139 do {
3141 * Userspace could choose to issue a mb() before updating the
3142 * tail pointer. So that all reads will be completed before the
3143 * write is issued.
3145 tail = ACCESS_ONCE(buffer->user_page->data_tail);
3146 smp_rmb();
3147 offset = head = local_read(&buffer->head);
3148 head += size;
3149 if (unlikely(!perf_output_space(buffer, tail, offset, head)))
3150 goto fail;
3151 } while (local_cmpxchg(&buffer->head, offset, head) != offset);
3153 if (head - local_read(&buffer->wakeup) > buffer->watermark)
3154 local_add(buffer->watermark, &buffer->wakeup);
3156 handle->page = offset >> (PAGE_SHIFT + page_order(buffer));
3157 handle->page &= buffer->nr_pages - 1;
3158 handle->size = offset & ((PAGE_SIZE << page_order(buffer)) - 1);
3159 handle->addr = buffer->data_pages[handle->page];
3160 handle->addr += handle->size;
3161 handle->size = (PAGE_SIZE << page_order(buffer)) - handle->size;
3163 if (have_lost) {
3164 lost_event.header.type = PERF_RECORD_LOST;
3165 lost_event.header.misc = 0;
3166 lost_event.header.size = sizeof(lost_event);
3167 lost_event.id = event->id;
3168 lost_event.lost = local_xchg(&buffer->lost, 0);
3170 perf_output_put(handle, lost_event);
3173 return 0;
3175 fail:
3176 local_inc(&buffer->lost);
3177 perf_output_put_handle(handle);
3178 out:
3179 rcu_read_unlock();
3181 return -ENOSPC;
3184 void perf_output_end(struct perf_output_handle *handle)
3186 struct perf_event *event = handle->event;
3187 struct perf_buffer *buffer = handle->buffer;
3189 int wakeup_events = event->attr.wakeup_events;
3191 if (handle->sample && wakeup_events) {
3192 int events = local_inc_return(&buffer->events);
3193 if (events >= wakeup_events) {
3194 local_sub(wakeup_events, &buffer->events);
3195 local_inc(&buffer->wakeup);
3199 perf_output_put_handle(handle);
3200 rcu_read_unlock();
3203 static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
3206 * only top level events have the pid namespace they were created in
3208 if (event->parent)
3209 event = event->parent;
3211 return task_tgid_nr_ns(p, event->ns);
3214 static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
3217 * only top level events have the pid namespace they were created in
3219 if (event->parent)
3220 event = event->parent;
3222 return task_pid_nr_ns(p, event->ns);
3225 static void perf_output_read_one(struct perf_output_handle *handle,
3226 struct perf_event *event)
3228 u64 read_format = event->attr.read_format;
3229 u64 values[4];
3230 int n = 0;
3232 values[n++] = perf_event_count(event);
3233 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
3234 values[n++] = event->total_time_enabled +
3235 atomic64_read(&event->child_total_time_enabled);
3237 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
3238 values[n++] = event->total_time_running +
3239 atomic64_read(&event->child_total_time_running);
3241 if (read_format & PERF_FORMAT_ID)
3242 values[n++] = primary_event_id(event);
3244 perf_output_copy(handle, values, n * sizeof(u64));
3248 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
3250 static void perf_output_read_group(struct perf_output_handle *handle,
3251 struct perf_event *event)
3253 struct perf_event *leader = event->group_leader, *sub;
3254 u64 read_format = event->attr.read_format;
3255 u64 values[5];
3256 int n = 0;
3258 values[n++] = 1 + leader->nr_siblings;
3260 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3261 values[n++] = leader->total_time_enabled;
3263 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3264 values[n++] = leader->total_time_running;
3266 if (leader != event)
3267 leader->pmu->read(leader);
3269 values[n++] = perf_event_count(leader);
3270 if (read_format & PERF_FORMAT_ID)
3271 values[n++] = primary_event_id(leader);
3273 perf_output_copy(handle, values, n * sizeof(u64));
3275 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
3276 n = 0;
3278 if (sub != event)
3279 sub->pmu->read(sub);
3281 values[n++] = perf_event_count(sub);
3282 if (read_format & PERF_FORMAT_ID)
3283 values[n++] = primary_event_id(sub);
3285 perf_output_copy(handle, values, n * sizeof(u64));
3289 static void perf_output_read(struct perf_output_handle *handle,
3290 struct perf_event *event)
3292 if (event->attr.read_format & PERF_FORMAT_GROUP)
3293 perf_output_read_group(handle, event);
3294 else
3295 perf_output_read_one(handle, event);
3298 void perf_output_sample(struct perf_output_handle *handle,
3299 struct perf_event_header *header,
3300 struct perf_sample_data *data,
3301 struct perf_event *event)
3303 u64 sample_type = data->type;
3305 perf_output_put(handle, *header);
3307 if (sample_type & PERF_SAMPLE_IP)
3308 perf_output_put(handle, data->ip);
3310 if (sample_type & PERF_SAMPLE_TID)
3311 perf_output_put(handle, data->tid_entry);
3313 if (sample_type & PERF_SAMPLE_TIME)
3314 perf_output_put(handle, data->time);
3316 if (sample_type & PERF_SAMPLE_ADDR)
3317 perf_output_put(handle, data->addr);
3319 if (sample_type & PERF_SAMPLE_ID)
3320 perf_output_put(handle, data->id);
3322 if (sample_type & PERF_SAMPLE_STREAM_ID)
3323 perf_output_put(handle, data->stream_id);
3325 if (sample_type & PERF_SAMPLE_CPU)
3326 perf_output_put(handle, data->cpu_entry);
3328 if (sample_type & PERF_SAMPLE_PERIOD)
3329 perf_output_put(handle, data->period);
3331 if (sample_type & PERF_SAMPLE_READ)
3332 perf_output_read(handle, event);
3334 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3335 if (data->callchain) {
3336 int size = 1;
3338 if (data->callchain)
3339 size += data->callchain->nr;
3341 size *= sizeof(u64);
3343 perf_output_copy(handle, data->callchain, size);
3344 } else {
3345 u64 nr = 0;
3346 perf_output_put(handle, nr);
3350 if (sample_type & PERF_SAMPLE_RAW) {
3351 if (data->raw) {
3352 perf_output_put(handle, data->raw->size);
3353 perf_output_copy(handle, data->raw->data,
3354 data->raw->size);
3355 } else {
3356 struct {
3357 u32 size;
3358 u32 data;
3359 } raw = {
3360 .size = sizeof(u32),
3361 .data = 0,
3363 perf_output_put(handle, raw);
3368 void perf_prepare_sample(struct perf_event_header *header,
3369 struct perf_sample_data *data,
3370 struct perf_event *event,
3371 struct pt_regs *regs)
3373 u64 sample_type = event->attr.sample_type;
3375 data->type = sample_type;
3377 header->type = PERF_RECORD_SAMPLE;
3378 header->size = sizeof(*header);
3380 header->misc = 0;
3381 header->misc |= perf_misc_flags(regs);
3383 if (sample_type & PERF_SAMPLE_IP) {
3384 data->ip = perf_instruction_pointer(regs);
3386 header->size += sizeof(data->ip);
3389 if (sample_type & PERF_SAMPLE_TID) {
3390 /* namespace issues */
3391 data->tid_entry.pid = perf_event_pid(event, current);
3392 data->tid_entry.tid = perf_event_tid(event, current);
3394 header->size += sizeof(data->tid_entry);
3397 if (sample_type & PERF_SAMPLE_TIME) {
3398 data->time = perf_clock();
3400 header->size += sizeof(data->time);
3403 if (sample_type & PERF_SAMPLE_ADDR)
3404 header->size += sizeof(data->addr);
3406 if (sample_type & PERF_SAMPLE_ID) {
3407 data->id = primary_event_id(event);
3409 header->size += sizeof(data->id);
3412 if (sample_type & PERF_SAMPLE_STREAM_ID) {
3413 data->stream_id = event->id;
3415 header->size += sizeof(data->stream_id);
3418 if (sample_type & PERF_SAMPLE_CPU) {
3419 data->cpu_entry.cpu = raw_smp_processor_id();
3420 data->cpu_entry.reserved = 0;
3422 header->size += sizeof(data->cpu_entry);
3425 if (sample_type & PERF_SAMPLE_PERIOD)
3426 header->size += sizeof(data->period);
3428 if (sample_type & PERF_SAMPLE_READ)
3429 header->size += perf_event_read_size(event);
3431 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3432 int size = 1;
3434 data->callchain = perf_callchain(regs);
3436 if (data->callchain)
3437 size += data->callchain->nr;
3439 header->size += size * sizeof(u64);
3442 if (sample_type & PERF_SAMPLE_RAW) {
3443 int size = sizeof(u32);
3445 if (data->raw)
3446 size += data->raw->size;
3447 else
3448 size += sizeof(u32);
3450 WARN_ON_ONCE(size & (sizeof(u64)-1));
3451 header->size += size;
3455 static void perf_event_output(struct perf_event *event, int nmi,
3456 struct perf_sample_data *data,
3457 struct pt_regs *regs)
3459 struct perf_output_handle handle;
3460 struct perf_event_header header;
3462 perf_prepare_sample(&header, data, event, regs);
3464 if (perf_output_begin(&handle, event, header.size, nmi, 1))
3465 return;
3467 perf_output_sample(&handle, &header, data, event);
3469 perf_output_end(&handle);
3473 * read event_id
3476 struct perf_read_event {
3477 struct perf_event_header header;
3479 u32 pid;
3480 u32 tid;
3483 static void
3484 perf_event_read_event(struct perf_event *event,
3485 struct task_struct *task)
3487 struct perf_output_handle handle;
3488 struct perf_read_event read_event = {
3489 .header = {
3490 .type = PERF_RECORD_READ,
3491 .misc = 0,
3492 .size = sizeof(read_event) + perf_event_read_size(event),
3494 .pid = perf_event_pid(event, task),
3495 .tid = perf_event_tid(event, task),
3497 int ret;
3499 ret = perf_output_begin(&handle, event, read_event.header.size, 0, 0);
3500 if (ret)
3501 return;
3503 perf_output_put(&handle, read_event);
3504 perf_output_read(&handle, event);
3506 perf_output_end(&handle);
3510 * task tracking -- fork/exit
3512 * enabled by: attr.comm | attr.mmap | attr.mmap_data | attr.task
3515 struct perf_task_event {
3516 struct task_struct *task;
3517 struct perf_event_context *task_ctx;
3519 struct {
3520 struct perf_event_header header;
3522 u32 pid;
3523 u32 ppid;
3524 u32 tid;
3525 u32 ptid;
3526 u64 time;
3527 } event_id;
3530 static void perf_event_task_output(struct perf_event *event,
3531 struct perf_task_event *task_event)
3533 struct perf_output_handle handle;
3534 struct task_struct *task = task_event->task;
3535 int size, ret;
3537 size = task_event->event_id.header.size;
3538 ret = perf_output_begin(&handle, event, size, 0, 0);
3540 if (ret)
3541 return;
3543 task_event->event_id.pid = perf_event_pid(event, task);
3544 task_event->event_id.ppid = perf_event_pid(event, current);
3546 task_event->event_id.tid = perf_event_tid(event, task);
3547 task_event->event_id.ptid = perf_event_tid(event, current);
3549 perf_output_put(&handle, task_event->event_id);
3551 perf_output_end(&handle);
3554 static int perf_event_task_match(struct perf_event *event)
3556 if (event->state < PERF_EVENT_STATE_INACTIVE)
3557 return 0;
3559 if (event->cpu != -1 && event->cpu != smp_processor_id())
3560 return 0;
3562 if (event->attr.comm || event->attr.mmap ||
3563 event->attr.mmap_data || event->attr.task)
3564 return 1;
3566 return 0;
3569 static void perf_event_task_ctx(struct perf_event_context *ctx,
3570 struct perf_task_event *task_event)
3572 struct perf_event *event;
3574 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3575 if (perf_event_task_match(event))
3576 perf_event_task_output(event, task_event);
3580 static void perf_event_task_event(struct perf_task_event *task_event)
3582 struct perf_cpu_context *cpuctx;
3583 struct perf_event_context *ctx = task_event->task_ctx;
3585 rcu_read_lock();
3586 cpuctx = &get_cpu_var(perf_cpu_context);
3587 perf_event_task_ctx(&cpuctx->ctx, task_event);
3588 if (!ctx)
3589 ctx = rcu_dereference(current->perf_event_ctxp);
3590 if (ctx)
3591 perf_event_task_ctx(ctx, task_event);
3592 put_cpu_var(perf_cpu_context);
3593 rcu_read_unlock();
3596 static void perf_event_task(struct task_struct *task,
3597 struct perf_event_context *task_ctx,
3598 int new)
3600 struct perf_task_event task_event;
3602 if (!atomic_read(&nr_comm_events) &&
3603 !atomic_read(&nr_mmap_events) &&
3604 !atomic_read(&nr_task_events))
3605 return;
3607 task_event = (struct perf_task_event){
3608 .task = task,
3609 .task_ctx = task_ctx,
3610 .event_id = {
3611 .header = {
3612 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
3613 .misc = 0,
3614 .size = sizeof(task_event.event_id),
3616 /* .pid */
3617 /* .ppid */
3618 /* .tid */
3619 /* .ptid */
3620 .time = perf_clock(),
3624 perf_event_task_event(&task_event);
3627 void perf_event_fork(struct task_struct *task)
3629 perf_event_task(task, NULL, 1);
3633 * comm tracking
3636 struct perf_comm_event {
3637 struct task_struct *task;
3638 char *comm;
3639 int comm_size;
3641 struct {
3642 struct perf_event_header header;
3644 u32 pid;
3645 u32 tid;
3646 } event_id;
3649 static void perf_event_comm_output(struct perf_event *event,
3650 struct perf_comm_event *comm_event)
3652 struct perf_output_handle handle;
3653 int size = comm_event->event_id.header.size;
3654 int ret = perf_output_begin(&handle, event, size, 0, 0);
3656 if (ret)
3657 return;
3659 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
3660 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
3662 perf_output_put(&handle, comm_event->event_id);
3663 perf_output_copy(&handle, comm_event->comm,
3664 comm_event->comm_size);
3665 perf_output_end(&handle);
3668 static int perf_event_comm_match(struct perf_event *event)
3670 if (event->state < PERF_EVENT_STATE_INACTIVE)
3671 return 0;
3673 if (event->cpu != -1 && event->cpu != smp_processor_id())
3674 return 0;
3676 if (event->attr.comm)
3677 return 1;
3679 return 0;
3682 static void perf_event_comm_ctx(struct perf_event_context *ctx,
3683 struct perf_comm_event *comm_event)
3685 struct perf_event *event;
3687 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3688 if (perf_event_comm_match(event))
3689 perf_event_comm_output(event, comm_event);
3693 static void perf_event_comm_event(struct perf_comm_event *comm_event)
3695 struct perf_cpu_context *cpuctx;
3696 struct perf_event_context *ctx;
3697 unsigned int size;
3698 char comm[TASK_COMM_LEN];
3700 memset(comm, 0, sizeof(comm));
3701 strlcpy(comm, comm_event->task->comm, sizeof(comm));
3702 size = ALIGN(strlen(comm)+1, sizeof(u64));
3704 comm_event->comm = comm;
3705 comm_event->comm_size = size;
3707 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
3709 rcu_read_lock();
3710 cpuctx = &get_cpu_var(perf_cpu_context);
3711 perf_event_comm_ctx(&cpuctx->ctx, comm_event);
3712 ctx = rcu_dereference(current->perf_event_ctxp);
3713 if (ctx)
3714 perf_event_comm_ctx(ctx, comm_event);
3715 put_cpu_var(perf_cpu_context);
3716 rcu_read_unlock();
3719 void perf_event_comm(struct task_struct *task)
3721 struct perf_comm_event comm_event;
3723 if (task->perf_event_ctxp)
3724 perf_event_enable_on_exec(task);
3726 if (!atomic_read(&nr_comm_events))
3727 return;
3729 comm_event = (struct perf_comm_event){
3730 .task = task,
3731 /* .comm */
3732 /* .comm_size */
3733 .event_id = {
3734 .header = {
3735 .type = PERF_RECORD_COMM,
3736 .misc = 0,
3737 /* .size */
3739 /* .pid */
3740 /* .tid */
3744 perf_event_comm_event(&comm_event);
3748 * mmap tracking
3751 struct perf_mmap_event {
3752 struct vm_area_struct *vma;
3754 const char *file_name;
3755 int file_size;
3757 struct {
3758 struct perf_event_header header;
3760 u32 pid;
3761 u32 tid;
3762 u64 start;
3763 u64 len;
3764 u64 pgoff;
3765 } event_id;
3768 static void perf_event_mmap_output(struct perf_event *event,
3769 struct perf_mmap_event *mmap_event)
3771 struct perf_output_handle handle;
3772 int size = mmap_event->event_id.header.size;
3773 int ret = perf_output_begin(&handle, event, size, 0, 0);
3775 if (ret)
3776 return;
3778 mmap_event->event_id.pid = perf_event_pid(event, current);
3779 mmap_event->event_id.tid = perf_event_tid(event, current);
3781 perf_output_put(&handle, mmap_event->event_id);
3782 perf_output_copy(&handle, mmap_event->file_name,
3783 mmap_event->file_size);
3784 perf_output_end(&handle);
3787 static int perf_event_mmap_match(struct perf_event *event,
3788 struct perf_mmap_event *mmap_event,
3789 int executable)
3791 if (event->state < PERF_EVENT_STATE_INACTIVE)
3792 return 0;
3794 if (event->cpu != -1 && event->cpu != smp_processor_id())
3795 return 0;
3797 if ((!executable && event->attr.mmap_data) ||
3798 (executable && event->attr.mmap))
3799 return 1;
3801 return 0;
3804 static void perf_event_mmap_ctx(struct perf_event_context *ctx,
3805 struct perf_mmap_event *mmap_event,
3806 int executable)
3808 struct perf_event *event;
3810 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3811 if (perf_event_mmap_match(event, mmap_event, executable))
3812 perf_event_mmap_output(event, mmap_event);
3816 static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
3818 struct perf_cpu_context *cpuctx;
3819 struct perf_event_context *ctx;
3820 struct vm_area_struct *vma = mmap_event->vma;
3821 struct file *file = vma->vm_file;
3822 unsigned int size;
3823 char tmp[16];
3824 char *buf = NULL;
3825 const char *name;
3827 memset(tmp, 0, sizeof(tmp));
3829 if (file) {
3831 * d_path works from the end of the buffer backwards, so we
3832 * need to add enough zero bytes after the string to handle
3833 * the 64bit alignment we do later.
3835 buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
3836 if (!buf) {
3837 name = strncpy(tmp, "//enomem", sizeof(tmp));
3838 goto got_name;
3840 name = d_path(&file->f_path, buf, PATH_MAX);
3841 if (IS_ERR(name)) {
3842 name = strncpy(tmp, "//toolong", sizeof(tmp));
3843 goto got_name;
3845 } else {
3846 if (arch_vma_name(mmap_event->vma)) {
3847 name = strncpy(tmp, arch_vma_name(mmap_event->vma),
3848 sizeof(tmp));
3849 goto got_name;
3852 if (!vma->vm_mm) {
3853 name = strncpy(tmp, "[vdso]", sizeof(tmp));
3854 goto got_name;
3855 } else if (vma->vm_start <= vma->vm_mm->start_brk &&
3856 vma->vm_end >= vma->vm_mm->brk) {
3857 name = strncpy(tmp, "[heap]", sizeof(tmp));
3858 goto got_name;
3859 } else if (vma->vm_start <= vma->vm_mm->start_stack &&
3860 vma->vm_end >= vma->vm_mm->start_stack) {
3861 name = strncpy(tmp, "[stack]", sizeof(tmp));
3862 goto got_name;
3865 name = strncpy(tmp, "//anon", sizeof(tmp));
3866 goto got_name;
3869 got_name:
3870 size = ALIGN(strlen(name)+1, sizeof(u64));
3872 mmap_event->file_name = name;
3873 mmap_event->file_size = size;
3875 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
3877 rcu_read_lock();
3878 cpuctx = &get_cpu_var(perf_cpu_context);
3879 perf_event_mmap_ctx(&cpuctx->ctx, mmap_event, vma->vm_flags & VM_EXEC);
3880 ctx = rcu_dereference(current->perf_event_ctxp);
3881 if (ctx)
3882 perf_event_mmap_ctx(ctx, mmap_event, vma->vm_flags & VM_EXEC);
3883 put_cpu_var(perf_cpu_context);
3884 rcu_read_unlock();
3886 kfree(buf);
3889 void perf_event_mmap(struct vm_area_struct *vma)
3891 struct perf_mmap_event mmap_event;
3893 if (!atomic_read(&nr_mmap_events))
3894 return;
3896 mmap_event = (struct perf_mmap_event){
3897 .vma = vma,
3898 /* .file_name */
3899 /* .file_size */
3900 .event_id = {
3901 .header = {
3902 .type = PERF_RECORD_MMAP,
3903 .misc = PERF_RECORD_MISC_USER,
3904 /* .size */
3906 /* .pid */
3907 /* .tid */
3908 .start = vma->vm_start,
3909 .len = vma->vm_end - vma->vm_start,
3910 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
3914 perf_event_mmap_event(&mmap_event);
3918 * IRQ throttle logging
3921 static void perf_log_throttle(struct perf_event *event, int enable)
3923 struct perf_output_handle handle;
3924 int ret;
3926 struct {
3927 struct perf_event_header header;
3928 u64 time;
3929 u64 id;
3930 u64 stream_id;
3931 } throttle_event = {
3932 .header = {
3933 .type = PERF_RECORD_THROTTLE,
3934 .misc = 0,
3935 .size = sizeof(throttle_event),
3937 .time = perf_clock(),
3938 .id = primary_event_id(event),
3939 .stream_id = event->id,
3942 if (enable)
3943 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
3945 ret = perf_output_begin(&handle, event, sizeof(throttle_event), 1, 0);
3946 if (ret)
3947 return;
3949 perf_output_put(&handle, throttle_event);
3950 perf_output_end(&handle);
3954 * Generic event overflow handling, sampling.
3957 static int __perf_event_overflow(struct perf_event *event, int nmi,
3958 int throttle, struct perf_sample_data *data,
3959 struct pt_regs *regs)
3961 int events = atomic_read(&event->event_limit);
3962 struct hw_perf_event *hwc = &event->hw;
3963 int ret = 0;
3965 throttle = (throttle && event->pmu->unthrottle != NULL);
3967 if (!throttle) {
3968 hwc->interrupts++;
3969 } else {
3970 if (hwc->interrupts != MAX_INTERRUPTS) {
3971 hwc->interrupts++;
3972 if (HZ * hwc->interrupts >
3973 (u64)sysctl_perf_event_sample_rate) {
3974 hwc->interrupts = MAX_INTERRUPTS;
3975 perf_log_throttle(event, 0);
3976 ret = 1;
3978 } else {
3980 * Keep re-disabling events even though on the previous
3981 * pass we disabled it - just in case we raced with a
3982 * sched-in and the event got enabled again:
3984 ret = 1;
3988 if (event->attr.freq) {
3989 u64 now = perf_clock();
3990 s64 delta = now - hwc->freq_time_stamp;
3992 hwc->freq_time_stamp = now;
3994 if (delta > 0 && delta < 2*TICK_NSEC)
3995 perf_adjust_period(event, delta, hwc->last_period);
3999 * XXX event_limit might not quite work as expected on inherited
4000 * events
4003 event->pending_kill = POLL_IN;
4004 if (events && atomic_dec_and_test(&event->event_limit)) {
4005 ret = 1;
4006 event->pending_kill = POLL_HUP;
4007 if (nmi) {
4008 event->pending_disable = 1;
4009 perf_pending_queue(&event->pending,
4010 perf_pending_event);
4011 } else
4012 perf_event_disable(event);
4015 if (event->overflow_handler)
4016 event->overflow_handler(event, nmi, data, regs);
4017 else
4018 perf_event_output(event, nmi, data, regs);
4020 return ret;
4023 int perf_event_overflow(struct perf_event *event, int nmi,
4024 struct perf_sample_data *data,
4025 struct pt_regs *regs)
4027 return __perf_event_overflow(event, nmi, 1, data, regs);
4031 * Generic software event infrastructure
4035 * We directly increment event->count and keep a second value in
4036 * event->hw.period_left to count intervals. This period event
4037 * is kept in the range [-sample_period, 0] so that we can use the
4038 * sign as trigger.
4041 static u64 perf_swevent_set_period(struct perf_event *event)
4043 struct hw_perf_event *hwc = &event->hw;
4044 u64 period = hwc->last_period;
4045 u64 nr, offset;
4046 s64 old, val;
4048 hwc->last_period = hwc->sample_period;
4050 again:
4051 old = val = local64_read(&hwc->period_left);
4052 if (val < 0)
4053 return 0;
4055 nr = div64_u64(period + val, period);
4056 offset = nr * period;
4057 val -= offset;
4058 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
4059 goto again;
4061 return nr;
4064 static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
4065 int nmi, struct perf_sample_data *data,
4066 struct pt_regs *regs)
4068 struct hw_perf_event *hwc = &event->hw;
4069 int throttle = 0;
4071 data->period = event->hw.last_period;
4072 if (!overflow)
4073 overflow = perf_swevent_set_period(event);
4075 if (hwc->interrupts == MAX_INTERRUPTS)
4076 return;
4078 for (; overflow; overflow--) {
4079 if (__perf_event_overflow(event, nmi, throttle,
4080 data, regs)) {
4082 * We inhibit the overflow from happening when
4083 * hwc->interrupts == MAX_INTERRUPTS.
4085 break;
4087 throttle = 1;
4091 static void perf_swevent_add(struct perf_event *event, u64 nr,
4092 int nmi, struct perf_sample_data *data,
4093 struct pt_regs *regs)
4095 struct hw_perf_event *hwc = &event->hw;
4097 local64_add(nr, &event->count);
4099 if (!regs)
4100 return;
4102 if (!hwc->sample_period)
4103 return;
4105 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
4106 return perf_swevent_overflow(event, 1, nmi, data, regs);
4108 if (local64_add_negative(nr, &hwc->period_left))
4109 return;
4111 perf_swevent_overflow(event, 0, nmi, data, regs);
4114 static int perf_exclude_event(struct perf_event *event,
4115 struct pt_regs *regs)
4117 if (regs) {
4118 if (event->attr.exclude_user && user_mode(regs))
4119 return 1;
4121 if (event->attr.exclude_kernel && !user_mode(regs))
4122 return 1;
4125 return 0;
4128 static int perf_swevent_match(struct perf_event *event,
4129 enum perf_type_id type,
4130 u32 event_id,
4131 struct perf_sample_data *data,
4132 struct pt_regs *regs)
4134 if (event->attr.type != type)
4135 return 0;
4137 if (event->attr.config != event_id)
4138 return 0;
4140 if (perf_exclude_event(event, regs))
4141 return 0;
4143 return 1;
4146 static inline u64 swevent_hash(u64 type, u32 event_id)
4148 u64 val = event_id | (type << 32);
4150 return hash_64(val, SWEVENT_HLIST_BITS);
4153 static inline struct hlist_head *
4154 __find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
4156 u64 hash = swevent_hash(type, event_id);
4158 return &hlist->heads[hash];
4161 /* For the read side: events when they trigger */
4162 static inline struct hlist_head *
4163 find_swevent_head_rcu(struct perf_cpu_context *ctx, u64 type, u32 event_id)
4165 struct swevent_hlist *hlist;
4167 hlist = rcu_dereference(ctx->swevent_hlist);
4168 if (!hlist)
4169 return NULL;
4171 return __find_swevent_head(hlist, type, event_id);
4174 /* For the event head insertion and removal in the hlist */
4175 static inline struct hlist_head *
4176 find_swevent_head(struct perf_cpu_context *ctx, struct perf_event *event)
4178 struct swevent_hlist *hlist;
4179 u32 event_id = event->attr.config;
4180 u64 type = event->attr.type;
4183 * Event scheduling is always serialized against hlist allocation
4184 * and release. Which makes the protected version suitable here.
4185 * The context lock guarantees that.
4187 hlist = rcu_dereference_protected(ctx->swevent_hlist,
4188 lockdep_is_held(&event->ctx->lock));
4189 if (!hlist)
4190 return NULL;
4192 return __find_swevent_head(hlist, type, event_id);
4195 static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
4196 u64 nr, int nmi,
4197 struct perf_sample_data *data,
4198 struct pt_regs *regs)
4200 struct perf_cpu_context *cpuctx;
4201 struct perf_event *event;
4202 struct hlist_node *node;
4203 struct hlist_head *head;
4205 cpuctx = &__get_cpu_var(perf_cpu_context);
4207 rcu_read_lock();
4209 head = find_swevent_head_rcu(cpuctx, type, event_id);
4211 if (!head)
4212 goto end;
4214 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
4215 if (perf_swevent_match(event, type, event_id, data, regs))
4216 perf_swevent_add(event, nr, nmi, data, regs);
4218 end:
4219 rcu_read_unlock();
4222 int perf_swevent_get_recursion_context(void)
4224 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
4225 int rctx;
4227 if (in_nmi())
4228 rctx = 3;
4229 else if (in_irq())
4230 rctx = 2;
4231 else if (in_softirq())
4232 rctx = 1;
4233 else
4234 rctx = 0;
4236 if (cpuctx->recursion[rctx])
4237 return -1;
4239 cpuctx->recursion[rctx]++;
4240 barrier();
4242 return rctx;
4244 EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
4246 void inline perf_swevent_put_recursion_context(int rctx)
4248 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
4249 barrier();
4250 cpuctx->recursion[rctx]--;
4253 void __perf_sw_event(u32 event_id, u64 nr, int nmi,
4254 struct pt_regs *regs, u64 addr)
4256 struct perf_sample_data data;
4257 int rctx;
4259 preempt_disable_notrace();
4260 rctx = perf_swevent_get_recursion_context();
4261 if (rctx < 0)
4262 return;
4264 perf_sample_data_init(&data, addr);
4266 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, nmi, &data, regs);
4268 perf_swevent_put_recursion_context(rctx);
4269 preempt_enable_notrace();
4272 static void perf_swevent_read(struct perf_event *event)
4276 static int perf_swevent_enable(struct perf_event *event)
4278 struct hw_perf_event *hwc = &event->hw;
4279 struct perf_cpu_context *cpuctx;
4280 struct hlist_head *head;
4282 cpuctx = &__get_cpu_var(perf_cpu_context);
4284 if (hwc->sample_period) {
4285 hwc->last_period = hwc->sample_period;
4286 perf_swevent_set_period(event);
4289 head = find_swevent_head(cpuctx, event);
4290 if (WARN_ON_ONCE(!head))
4291 return -EINVAL;
4293 hlist_add_head_rcu(&event->hlist_entry, head);
4295 return 0;
4298 static void perf_swevent_disable(struct perf_event *event)
4300 hlist_del_rcu(&event->hlist_entry);
4303 static void perf_swevent_void(struct perf_event *event)
4307 static int perf_swevent_int(struct perf_event *event)
4309 return 0;
4312 static const struct pmu perf_ops_generic = {
4313 .enable = perf_swevent_enable,
4314 .disable = perf_swevent_disable,
4315 .start = perf_swevent_int,
4316 .stop = perf_swevent_void,
4317 .read = perf_swevent_read,
4318 .unthrottle = perf_swevent_void, /* hwc->interrupts already reset */
4322 * hrtimer based swevent callback
4325 static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
4327 enum hrtimer_restart ret = HRTIMER_RESTART;
4328 struct perf_sample_data data;
4329 struct pt_regs *regs;
4330 struct perf_event *event;
4331 u64 period;
4333 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
4334 event->pmu->read(event);
4336 perf_sample_data_init(&data, 0);
4337 data.period = event->hw.last_period;
4338 regs = get_irq_regs();
4340 if (regs && !perf_exclude_event(event, regs)) {
4341 if (!(event->attr.exclude_idle && current->pid == 0))
4342 if (perf_event_overflow(event, 0, &data, regs))
4343 ret = HRTIMER_NORESTART;
4346 period = max_t(u64, 10000, event->hw.sample_period);
4347 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
4349 return ret;
4352 static void perf_swevent_start_hrtimer(struct perf_event *event)
4354 struct hw_perf_event *hwc = &event->hw;
4356 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
4357 hwc->hrtimer.function = perf_swevent_hrtimer;
4358 if (hwc->sample_period) {
4359 u64 period;
4361 if (hwc->remaining) {
4362 if (hwc->remaining < 0)
4363 period = 10000;
4364 else
4365 period = hwc->remaining;
4366 hwc->remaining = 0;
4367 } else {
4368 period = max_t(u64, 10000, hwc->sample_period);
4370 __hrtimer_start_range_ns(&hwc->hrtimer,
4371 ns_to_ktime(period), 0,
4372 HRTIMER_MODE_REL, 0);
4376 static void perf_swevent_cancel_hrtimer(struct perf_event *event)
4378 struct hw_perf_event *hwc = &event->hw;
4380 if (hwc->sample_period) {
4381 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
4382 hwc->remaining = ktime_to_ns(remaining);
4384 hrtimer_cancel(&hwc->hrtimer);
4389 * Software event: cpu wall time clock
4392 static void cpu_clock_perf_event_update(struct perf_event *event)
4394 int cpu = raw_smp_processor_id();
4395 s64 prev;
4396 u64 now;
4398 now = cpu_clock(cpu);
4399 prev = local64_xchg(&event->hw.prev_count, now);
4400 local64_add(now - prev, &event->count);
4403 static int cpu_clock_perf_event_enable(struct perf_event *event)
4405 struct hw_perf_event *hwc = &event->hw;
4406 int cpu = raw_smp_processor_id();
4408 local64_set(&hwc->prev_count, cpu_clock(cpu));
4409 perf_swevent_start_hrtimer(event);
4411 return 0;
4414 static void cpu_clock_perf_event_disable(struct perf_event *event)
4416 perf_swevent_cancel_hrtimer(event);
4417 cpu_clock_perf_event_update(event);
4420 static void cpu_clock_perf_event_read(struct perf_event *event)
4422 cpu_clock_perf_event_update(event);
4425 static const struct pmu perf_ops_cpu_clock = {
4426 .enable = cpu_clock_perf_event_enable,
4427 .disable = cpu_clock_perf_event_disable,
4428 .read = cpu_clock_perf_event_read,
4432 * Software event: task time clock
4435 static void task_clock_perf_event_update(struct perf_event *event, u64 now)
4437 u64 prev;
4438 s64 delta;
4440 prev = local64_xchg(&event->hw.prev_count, now);
4441 delta = now - prev;
4442 local64_add(delta, &event->count);
4445 static int task_clock_perf_event_enable(struct perf_event *event)
4447 struct hw_perf_event *hwc = &event->hw;
4448 u64 now;
4450 now = event->ctx->time;
4452 local64_set(&hwc->prev_count, now);
4454 perf_swevent_start_hrtimer(event);
4456 return 0;
4459 static void task_clock_perf_event_disable(struct perf_event *event)
4461 perf_swevent_cancel_hrtimer(event);
4462 task_clock_perf_event_update(event, event->ctx->time);
4466 static void task_clock_perf_event_read(struct perf_event *event)
4468 u64 time;
4470 if (!in_nmi()) {
4471 update_context_time(event->ctx);
4472 time = event->ctx->time;
4473 } else {
4474 u64 now = perf_clock();
4475 u64 delta = now - event->ctx->timestamp;
4476 time = event->ctx->time + delta;
4479 task_clock_perf_event_update(event, time);
4482 static const struct pmu perf_ops_task_clock = {
4483 .enable = task_clock_perf_event_enable,
4484 .disable = task_clock_perf_event_disable,
4485 .read = task_clock_perf_event_read,
4488 /* Deref the hlist from the update side */
4489 static inline struct swevent_hlist *
4490 swevent_hlist_deref(struct perf_cpu_context *cpuctx)
4492 return rcu_dereference_protected(cpuctx->swevent_hlist,
4493 lockdep_is_held(&cpuctx->hlist_mutex));
4496 static void swevent_hlist_release_rcu(struct rcu_head *rcu_head)
4498 struct swevent_hlist *hlist;
4500 hlist = container_of(rcu_head, struct swevent_hlist, rcu_head);
4501 kfree(hlist);
4504 static void swevent_hlist_release(struct perf_cpu_context *cpuctx)
4506 struct swevent_hlist *hlist = swevent_hlist_deref(cpuctx);
4508 if (!hlist)
4509 return;
4511 rcu_assign_pointer(cpuctx->swevent_hlist, NULL);
4512 call_rcu(&hlist->rcu_head, swevent_hlist_release_rcu);
4515 static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
4517 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
4519 mutex_lock(&cpuctx->hlist_mutex);
4521 if (!--cpuctx->hlist_refcount)
4522 swevent_hlist_release(cpuctx);
4524 mutex_unlock(&cpuctx->hlist_mutex);
4527 static void swevent_hlist_put(struct perf_event *event)
4529 int cpu;
4531 if (event->cpu != -1) {
4532 swevent_hlist_put_cpu(event, event->cpu);
4533 return;
4536 for_each_possible_cpu(cpu)
4537 swevent_hlist_put_cpu(event, cpu);
4540 static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
4542 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
4543 int err = 0;
4545 mutex_lock(&cpuctx->hlist_mutex);
4547 if (!swevent_hlist_deref(cpuctx) && cpu_online(cpu)) {
4548 struct swevent_hlist *hlist;
4550 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
4551 if (!hlist) {
4552 err = -ENOMEM;
4553 goto exit;
4555 rcu_assign_pointer(cpuctx->swevent_hlist, hlist);
4557 cpuctx->hlist_refcount++;
4558 exit:
4559 mutex_unlock(&cpuctx->hlist_mutex);
4561 return err;
4564 static int swevent_hlist_get(struct perf_event *event)
4566 int err;
4567 int cpu, failed_cpu;
4569 if (event->cpu != -1)
4570 return swevent_hlist_get_cpu(event, event->cpu);
4572 get_online_cpus();
4573 for_each_possible_cpu(cpu) {
4574 err = swevent_hlist_get_cpu(event, cpu);
4575 if (err) {
4576 failed_cpu = cpu;
4577 goto fail;
4580 put_online_cpus();
4582 return 0;
4583 fail:
4584 for_each_possible_cpu(cpu) {
4585 if (cpu == failed_cpu)
4586 break;
4587 swevent_hlist_put_cpu(event, cpu);
4590 put_online_cpus();
4591 return err;
4594 #ifdef CONFIG_EVENT_TRACING
4596 static const struct pmu perf_ops_tracepoint = {
4597 .enable = perf_trace_enable,
4598 .disable = perf_trace_disable,
4599 .start = perf_swevent_int,
4600 .stop = perf_swevent_void,
4601 .read = perf_swevent_read,
4602 .unthrottle = perf_swevent_void,
4605 static int perf_tp_filter_match(struct perf_event *event,
4606 struct perf_sample_data *data)
4608 void *record = data->raw->data;
4610 if (likely(!event->filter) || filter_match_preds(event->filter, record))
4611 return 1;
4612 return 0;
4615 static int perf_tp_event_match(struct perf_event *event,
4616 struct perf_sample_data *data,
4617 struct pt_regs *regs)
4620 * All tracepoints are from kernel-space.
4622 if (event->attr.exclude_kernel)
4623 return 0;
4625 if (!perf_tp_filter_match(event, data))
4626 return 0;
4628 return 1;
4631 void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
4632 struct pt_regs *regs, struct hlist_head *head, int rctx)
4634 struct perf_sample_data data;
4635 struct perf_event *event;
4636 struct hlist_node *node;
4638 struct perf_raw_record raw = {
4639 .size = entry_size,
4640 .data = record,
4643 perf_sample_data_init(&data, addr);
4644 data.raw = &raw;
4646 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
4647 if (perf_tp_event_match(event, &data, regs))
4648 perf_swevent_add(event, count, 1, &data, regs);
4651 perf_swevent_put_recursion_context(rctx);
4653 EXPORT_SYMBOL_GPL(perf_tp_event);
4655 static void tp_perf_event_destroy(struct perf_event *event)
4657 perf_trace_destroy(event);
4660 static const struct pmu *tp_perf_event_init(struct perf_event *event)
4662 int err;
4665 * Raw tracepoint data is a severe data leak, only allow root to
4666 * have these.
4668 if ((event->attr.sample_type & PERF_SAMPLE_RAW) &&
4669 perf_paranoid_tracepoint_raw() &&
4670 !capable(CAP_SYS_ADMIN))
4671 return ERR_PTR(-EPERM);
4673 err = perf_trace_init(event);
4674 if (err)
4675 return NULL;
4677 event->destroy = tp_perf_event_destroy;
4679 return &perf_ops_tracepoint;
4682 static int perf_event_set_filter(struct perf_event *event, void __user *arg)
4684 char *filter_str;
4685 int ret;
4687 if (event->attr.type != PERF_TYPE_TRACEPOINT)
4688 return -EINVAL;
4690 filter_str = strndup_user(arg, PAGE_SIZE);
4691 if (IS_ERR(filter_str))
4692 return PTR_ERR(filter_str);
4694 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
4696 kfree(filter_str);
4697 return ret;
4700 static void perf_event_free_filter(struct perf_event *event)
4702 ftrace_profile_free_filter(event);
4705 #else
4707 static const struct pmu *tp_perf_event_init(struct perf_event *event)
4709 return NULL;
4712 static int perf_event_set_filter(struct perf_event *event, void __user *arg)
4714 return -ENOENT;
4717 static void perf_event_free_filter(struct perf_event *event)
4721 #endif /* CONFIG_EVENT_TRACING */
4723 #ifdef CONFIG_HAVE_HW_BREAKPOINT
4724 static void bp_perf_event_destroy(struct perf_event *event)
4726 release_bp_slot(event);
4729 static const struct pmu *bp_perf_event_init(struct perf_event *bp)
4731 int err;
4733 err = register_perf_hw_breakpoint(bp);
4734 if (err)
4735 return ERR_PTR(err);
4737 bp->destroy = bp_perf_event_destroy;
4739 return &perf_ops_bp;
4742 void perf_bp_event(struct perf_event *bp, void *data)
4744 struct perf_sample_data sample;
4745 struct pt_regs *regs = data;
4747 perf_sample_data_init(&sample, bp->attr.bp_addr);
4749 if (!perf_exclude_event(bp, regs))
4750 perf_swevent_add(bp, 1, 1, &sample, regs);
4752 #else
4753 static const struct pmu *bp_perf_event_init(struct perf_event *bp)
4755 return NULL;
4758 void perf_bp_event(struct perf_event *bp, void *regs)
4761 #endif
4763 atomic_t perf_swevent_enabled[PERF_COUNT_SW_MAX];
4765 static void sw_perf_event_destroy(struct perf_event *event)
4767 u64 event_id = event->attr.config;
4769 WARN_ON(event->parent);
4771 atomic_dec(&perf_swevent_enabled[event_id]);
4772 swevent_hlist_put(event);
4775 static const struct pmu *sw_perf_event_init(struct perf_event *event)
4777 const struct pmu *pmu = NULL;
4778 u64 event_id = event->attr.config;
4781 * Software events (currently) can't in general distinguish
4782 * between user, kernel and hypervisor events.
4783 * However, context switches and cpu migrations are considered
4784 * to be kernel events, and page faults are never hypervisor
4785 * events.
4787 switch (event_id) {
4788 case PERF_COUNT_SW_CPU_CLOCK:
4789 pmu = &perf_ops_cpu_clock;
4791 break;
4792 case PERF_COUNT_SW_TASK_CLOCK:
4794 * If the user instantiates this as a per-cpu event,
4795 * use the cpu_clock event instead.
4797 if (event->ctx->task)
4798 pmu = &perf_ops_task_clock;
4799 else
4800 pmu = &perf_ops_cpu_clock;
4802 break;
4803 case PERF_COUNT_SW_PAGE_FAULTS:
4804 case PERF_COUNT_SW_PAGE_FAULTS_MIN:
4805 case PERF_COUNT_SW_PAGE_FAULTS_MAJ:
4806 case PERF_COUNT_SW_CONTEXT_SWITCHES:
4807 case PERF_COUNT_SW_CPU_MIGRATIONS:
4808 case PERF_COUNT_SW_ALIGNMENT_FAULTS:
4809 case PERF_COUNT_SW_EMULATION_FAULTS:
4810 if (!event->parent) {
4811 int err;
4813 err = swevent_hlist_get(event);
4814 if (err)
4815 return ERR_PTR(err);
4817 atomic_inc(&perf_swevent_enabled[event_id]);
4818 event->destroy = sw_perf_event_destroy;
4820 pmu = &perf_ops_generic;
4821 break;
4824 return pmu;
4828 * Allocate and initialize a event structure
4830 static struct perf_event *
4831 perf_event_alloc(struct perf_event_attr *attr,
4832 int cpu,
4833 struct perf_event_context *ctx,
4834 struct perf_event *group_leader,
4835 struct perf_event *parent_event,
4836 perf_overflow_handler_t overflow_handler,
4837 gfp_t gfpflags)
4839 const struct pmu *pmu;
4840 struct perf_event *event;
4841 struct hw_perf_event *hwc;
4842 long err;
4844 event = kzalloc(sizeof(*event), gfpflags);
4845 if (!event)
4846 return ERR_PTR(-ENOMEM);
4849 * Single events are their own group leaders, with an
4850 * empty sibling list:
4852 if (!group_leader)
4853 group_leader = event;
4855 mutex_init(&event->child_mutex);
4856 INIT_LIST_HEAD(&event->child_list);
4858 INIT_LIST_HEAD(&event->group_entry);
4859 INIT_LIST_HEAD(&event->event_entry);
4860 INIT_LIST_HEAD(&event->sibling_list);
4861 init_waitqueue_head(&event->waitq);
4863 mutex_init(&event->mmap_mutex);
4865 event->cpu = cpu;
4866 event->attr = *attr;
4867 event->group_leader = group_leader;
4868 event->pmu = NULL;
4869 event->ctx = ctx;
4870 event->oncpu = -1;
4872 event->parent = parent_event;
4874 event->ns = get_pid_ns(current->nsproxy->pid_ns);
4875 event->id = atomic64_inc_return(&perf_event_id);
4877 event->state = PERF_EVENT_STATE_INACTIVE;
4879 if (!overflow_handler && parent_event)
4880 overflow_handler = parent_event->overflow_handler;
4882 event->overflow_handler = overflow_handler;
4884 if (attr->disabled)
4885 event->state = PERF_EVENT_STATE_OFF;
4887 pmu = NULL;
4889 hwc = &event->hw;
4890 hwc->sample_period = attr->sample_period;
4891 if (attr->freq && attr->sample_freq)
4892 hwc->sample_period = 1;
4893 hwc->last_period = hwc->sample_period;
4895 local64_set(&hwc->period_left, hwc->sample_period);
4898 * we currently do not support PERF_FORMAT_GROUP on inherited events
4900 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
4901 goto done;
4903 switch (attr->type) {
4904 case PERF_TYPE_RAW:
4905 case PERF_TYPE_HARDWARE:
4906 case PERF_TYPE_HW_CACHE:
4907 pmu = hw_perf_event_init(event);
4908 break;
4910 case PERF_TYPE_SOFTWARE:
4911 pmu = sw_perf_event_init(event);
4912 break;
4914 case PERF_TYPE_TRACEPOINT:
4915 pmu = tp_perf_event_init(event);
4916 break;
4918 case PERF_TYPE_BREAKPOINT:
4919 pmu = bp_perf_event_init(event);
4920 break;
4923 default:
4924 break;
4926 done:
4927 err = 0;
4928 if (!pmu)
4929 err = -EINVAL;
4930 else if (IS_ERR(pmu))
4931 err = PTR_ERR(pmu);
4933 if (err) {
4934 if (event->ns)
4935 put_pid_ns(event->ns);
4936 kfree(event);
4937 return ERR_PTR(err);
4940 event->pmu = pmu;
4942 if (!event->parent) {
4943 atomic_inc(&nr_events);
4944 if (event->attr.mmap || event->attr.mmap_data)
4945 atomic_inc(&nr_mmap_events);
4946 if (event->attr.comm)
4947 atomic_inc(&nr_comm_events);
4948 if (event->attr.task)
4949 atomic_inc(&nr_task_events);
4952 return event;
4955 static int perf_copy_attr(struct perf_event_attr __user *uattr,
4956 struct perf_event_attr *attr)
4958 u32 size;
4959 int ret;
4961 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
4962 return -EFAULT;
4965 * zero the full structure, so that a short copy will be nice.
4967 memset(attr, 0, sizeof(*attr));
4969 ret = get_user(size, &uattr->size);
4970 if (ret)
4971 return ret;
4973 if (size > PAGE_SIZE) /* silly large */
4974 goto err_size;
4976 if (!size) /* abi compat */
4977 size = PERF_ATTR_SIZE_VER0;
4979 if (size < PERF_ATTR_SIZE_VER0)
4980 goto err_size;
4983 * If we're handed a bigger struct than we know of,
4984 * ensure all the unknown bits are 0 - i.e. new
4985 * user-space does not rely on any kernel feature
4986 * extensions we dont know about yet.
4988 if (size > sizeof(*attr)) {
4989 unsigned char __user *addr;
4990 unsigned char __user *end;
4991 unsigned char val;
4993 addr = (void __user *)uattr + sizeof(*attr);
4994 end = (void __user *)uattr + size;
4996 for (; addr < end; addr++) {
4997 ret = get_user(val, addr);
4998 if (ret)
4999 return ret;
5000 if (val)
5001 goto err_size;
5003 size = sizeof(*attr);
5006 ret = copy_from_user(attr, uattr, size);
5007 if (ret)
5008 return -EFAULT;
5011 * If the type exists, the corresponding creation will verify
5012 * the attr->config.
5014 if (attr->type >= PERF_TYPE_MAX)
5015 return -EINVAL;
5017 if (attr->__reserved_1)
5018 return -EINVAL;
5020 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
5021 return -EINVAL;
5023 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
5024 return -EINVAL;
5026 out:
5027 return ret;
5029 err_size:
5030 put_user(sizeof(*attr), &uattr->size);
5031 ret = -E2BIG;
5032 goto out;
5035 static int
5036 perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
5038 struct perf_buffer *buffer = NULL, *old_buffer = NULL;
5039 int ret = -EINVAL;
5041 if (!output_event)
5042 goto set;
5044 /* don't allow circular references */
5045 if (event == output_event)
5046 goto out;
5049 * Don't allow cross-cpu buffers
5051 if (output_event->cpu != event->cpu)
5052 goto out;
5055 * If its not a per-cpu buffer, it must be the same task.
5057 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
5058 goto out;
5060 set:
5061 mutex_lock(&event->mmap_mutex);
5062 /* Can't redirect output if we've got an active mmap() */
5063 if (atomic_read(&event->mmap_count))
5064 goto unlock;
5066 if (output_event) {
5067 /* get the buffer we want to redirect to */
5068 buffer = perf_buffer_get(output_event);
5069 if (!buffer)
5070 goto unlock;
5073 old_buffer = event->buffer;
5074 rcu_assign_pointer(event->buffer, buffer);
5075 ret = 0;
5076 unlock:
5077 mutex_unlock(&event->mmap_mutex);
5079 if (old_buffer)
5080 perf_buffer_put(old_buffer);
5081 out:
5082 return ret;
5086 * sys_perf_event_open - open a performance event, associate it to a task/cpu
5088 * @attr_uptr: event_id type attributes for monitoring/sampling
5089 * @pid: target pid
5090 * @cpu: target cpu
5091 * @group_fd: group leader event fd
5093 SYSCALL_DEFINE5(perf_event_open,
5094 struct perf_event_attr __user *, attr_uptr,
5095 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
5097 struct perf_event *event, *group_leader = NULL, *output_event = NULL;
5098 struct perf_event_attr attr;
5099 struct perf_event_context *ctx;
5100 struct file *event_file = NULL;
5101 struct file *group_file = NULL;
5102 int event_fd;
5103 int fput_needed = 0;
5104 int err;
5106 /* for future expandability... */
5107 if (flags & ~(PERF_FLAG_FD_NO_GROUP | PERF_FLAG_FD_OUTPUT))
5108 return -EINVAL;
5110 err = perf_copy_attr(attr_uptr, &attr);
5111 if (err)
5112 return err;
5114 if (!attr.exclude_kernel) {
5115 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
5116 return -EACCES;
5119 if (attr.freq) {
5120 if (attr.sample_freq > sysctl_perf_event_sample_rate)
5121 return -EINVAL;
5124 event_fd = get_unused_fd_flags(O_RDWR);
5125 if (event_fd < 0)
5126 return event_fd;
5129 * Get the target context (task or percpu):
5131 ctx = find_get_context(pid, cpu);
5132 if (IS_ERR(ctx)) {
5133 err = PTR_ERR(ctx);
5134 goto err_fd;
5137 if (group_fd != -1) {
5138 group_leader = perf_fget_light(group_fd, &fput_needed);
5139 if (IS_ERR(group_leader)) {
5140 err = PTR_ERR(group_leader);
5141 goto err_put_context;
5143 group_file = group_leader->filp;
5144 if (flags & PERF_FLAG_FD_OUTPUT)
5145 output_event = group_leader;
5146 if (flags & PERF_FLAG_FD_NO_GROUP)
5147 group_leader = NULL;
5151 * Look up the group leader (we will attach this event to it):
5153 if (group_leader) {
5154 err = -EINVAL;
5157 * Do not allow a recursive hierarchy (this new sibling
5158 * becoming part of another group-sibling):
5160 if (group_leader->group_leader != group_leader)
5161 goto err_put_context;
5163 * Do not allow to attach to a group in a different
5164 * task or CPU context:
5166 if (group_leader->ctx != ctx)
5167 goto err_put_context;
5169 * Only a group leader can be exclusive or pinned
5171 if (attr.exclusive || attr.pinned)
5172 goto err_put_context;
5175 event = perf_event_alloc(&attr, cpu, ctx, group_leader,
5176 NULL, NULL, GFP_KERNEL);
5177 if (IS_ERR(event)) {
5178 err = PTR_ERR(event);
5179 goto err_put_context;
5182 if (output_event) {
5183 err = perf_event_set_output(event, output_event);
5184 if (err)
5185 goto err_free_put_context;
5188 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, O_RDWR);
5189 if (IS_ERR(event_file)) {
5190 err = PTR_ERR(event_file);
5191 goto err_free_put_context;
5194 event->filp = event_file;
5195 WARN_ON_ONCE(ctx->parent_ctx);
5196 mutex_lock(&ctx->mutex);
5197 perf_install_in_context(ctx, event, cpu);
5198 ++ctx->generation;
5199 mutex_unlock(&ctx->mutex);
5201 event->owner = current;
5202 get_task_struct(current);
5203 mutex_lock(&current->perf_event_mutex);
5204 list_add_tail(&event->owner_entry, &current->perf_event_list);
5205 mutex_unlock(&current->perf_event_mutex);
5208 * Drop the reference on the group_event after placing the
5209 * new event on the sibling_list. This ensures destruction
5210 * of the group leader will find the pointer to itself in
5211 * perf_group_detach().
5213 fput_light(group_file, fput_needed);
5214 fd_install(event_fd, event_file);
5215 return event_fd;
5217 err_free_put_context:
5218 free_event(event);
5219 err_put_context:
5220 fput_light(group_file, fput_needed);
5221 put_ctx(ctx);
5222 err_fd:
5223 put_unused_fd(event_fd);
5224 return err;
5228 * perf_event_create_kernel_counter
5230 * @attr: attributes of the counter to create
5231 * @cpu: cpu in which the counter is bound
5232 * @pid: task to profile
5234 struct perf_event *
5235 perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
5236 pid_t pid,
5237 perf_overflow_handler_t overflow_handler)
5239 struct perf_event *event;
5240 struct perf_event_context *ctx;
5241 int err;
5244 * Get the target context (task or percpu):
5247 ctx = find_get_context(pid, cpu);
5248 if (IS_ERR(ctx)) {
5249 err = PTR_ERR(ctx);
5250 goto err_exit;
5253 event = perf_event_alloc(attr, cpu, ctx, NULL,
5254 NULL, overflow_handler, GFP_KERNEL);
5255 if (IS_ERR(event)) {
5256 err = PTR_ERR(event);
5257 goto err_put_context;
5260 event->filp = NULL;
5261 WARN_ON_ONCE(ctx->parent_ctx);
5262 mutex_lock(&ctx->mutex);
5263 perf_install_in_context(ctx, event, cpu);
5264 ++ctx->generation;
5265 mutex_unlock(&ctx->mutex);
5267 event->owner = current;
5268 get_task_struct(current);
5269 mutex_lock(&current->perf_event_mutex);
5270 list_add_tail(&event->owner_entry, &current->perf_event_list);
5271 mutex_unlock(&current->perf_event_mutex);
5273 return event;
5275 err_put_context:
5276 put_ctx(ctx);
5277 err_exit:
5278 return ERR_PTR(err);
5280 EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
5283 * inherit a event from parent task to child task:
5285 static struct perf_event *
5286 inherit_event(struct perf_event *parent_event,
5287 struct task_struct *parent,
5288 struct perf_event_context *parent_ctx,
5289 struct task_struct *child,
5290 struct perf_event *group_leader,
5291 struct perf_event_context *child_ctx)
5293 struct perf_event *child_event;
5296 * Instead of creating recursive hierarchies of events,
5297 * we link inherited events back to the original parent,
5298 * which has a filp for sure, which we use as the reference
5299 * count:
5301 if (parent_event->parent)
5302 parent_event = parent_event->parent;
5304 child_event = perf_event_alloc(&parent_event->attr,
5305 parent_event->cpu, child_ctx,
5306 group_leader, parent_event,
5307 NULL, GFP_KERNEL);
5308 if (IS_ERR(child_event))
5309 return child_event;
5310 get_ctx(child_ctx);
5313 * Make the child state follow the state of the parent event,
5314 * not its attr.disabled bit. We hold the parent's mutex,
5315 * so we won't race with perf_event_{en, dis}able_family.
5317 if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
5318 child_event->state = PERF_EVENT_STATE_INACTIVE;
5319 else
5320 child_event->state = PERF_EVENT_STATE_OFF;
5322 if (parent_event->attr.freq) {
5323 u64 sample_period = parent_event->hw.sample_period;
5324 struct hw_perf_event *hwc = &child_event->hw;
5326 hwc->sample_period = sample_period;
5327 hwc->last_period = sample_period;
5329 local64_set(&hwc->period_left, sample_period);
5332 child_event->overflow_handler = parent_event->overflow_handler;
5335 * Link it up in the child's context:
5337 add_event_to_ctx(child_event, child_ctx);
5340 * Get a reference to the parent filp - we will fput it
5341 * when the child event exits. This is safe to do because
5342 * we are in the parent and we know that the filp still
5343 * exists and has a nonzero count:
5345 atomic_long_inc(&parent_event->filp->f_count);
5348 * Link this into the parent event's child list
5350 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
5351 mutex_lock(&parent_event->child_mutex);
5352 list_add_tail(&child_event->child_list, &parent_event->child_list);
5353 mutex_unlock(&parent_event->child_mutex);
5355 return child_event;
5358 static int inherit_group(struct perf_event *parent_event,
5359 struct task_struct *parent,
5360 struct perf_event_context *parent_ctx,
5361 struct task_struct *child,
5362 struct perf_event_context *child_ctx)
5364 struct perf_event *leader;
5365 struct perf_event *sub;
5366 struct perf_event *child_ctr;
5368 leader = inherit_event(parent_event, parent, parent_ctx,
5369 child, NULL, child_ctx);
5370 if (IS_ERR(leader))
5371 return PTR_ERR(leader);
5372 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
5373 child_ctr = inherit_event(sub, parent, parent_ctx,
5374 child, leader, child_ctx);
5375 if (IS_ERR(child_ctr))
5376 return PTR_ERR(child_ctr);
5378 return 0;
5381 static void sync_child_event(struct perf_event *child_event,
5382 struct task_struct *child)
5384 struct perf_event *parent_event = child_event->parent;
5385 u64 child_val;
5387 if (child_event->attr.inherit_stat)
5388 perf_event_read_event(child_event, child);
5390 child_val = perf_event_count(child_event);
5393 * Add back the child's count to the parent's count:
5395 atomic64_add(child_val, &parent_event->child_count);
5396 atomic64_add(child_event->total_time_enabled,
5397 &parent_event->child_total_time_enabled);
5398 atomic64_add(child_event->total_time_running,
5399 &parent_event->child_total_time_running);
5402 * Remove this event from the parent's list
5404 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
5405 mutex_lock(&parent_event->child_mutex);
5406 list_del_init(&child_event->child_list);
5407 mutex_unlock(&parent_event->child_mutex);
5410 * Release the parent event, if this was the last
5411 * reference to it.
5413 fput(parent_event->filp);
5416 static void
5417 __perf_event_exit_task(struct perf_event *child_event,
5418 struct perf_event_context *child_ctx,
5419 struct task_struct *child)
5421 struct perf_event *parent_event;
5423 perf_event_remove_from_context(child_event);
5425 parent_event = child_event->parent;
5427 * It can happen that parent exits first, and has events
5428 * that are still around due to the child reference. These
5429 * events need to be zapped - but otherwise linger.
5431 if (parent_event) {
5432 sync_child_event(child_event, child);
5433 free_event(child_event);
5438 * When a child task exits, feed back event values to parent events.
5440 void perf_event_exit_task(struct task_struct *child)
5442 struct perf_event *child_event, *tmp;
5443 struct perf_event_context *child_ctx;
5444 unsigned long flags;
5446 if (likely(!child->perf_event_ctxp)) {
5447 perf_event_task(child, NULL, 0);
5448 return;
5451 local_irq_save(flags);
5453 * We can't reschedule here because interrupts are disabled,
5454 * and either child is current or it is a task that can't be
5455 * scheduled, so we are now safe from rescheduling changing
5456 * our context.
5458 child_ctx = child->perf_event_ctxp;
5459 __perf_event_task_sched_out(child_ctx);
5462 * Take the context lock here so that if find_get_context is
5463 * reading child->perf_event_ctxp, we wait until it has
5464 * incremented the context's refcount before we do put_ctx below.
5466 raw_spin_lock(&child_ctx->lock);
5467 child->perf_event_ctxp = NULL;
5469 * If this context is a clone; unclone it so it can't get
5470 * swapped to another process while we're removing all
5471 * the events from it.
5473 unclone_ctx(child_ctx);
5474 update_context_time(child_ctx);
5475 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
5478 * Report the task dead after unscheduling the events so that we
5479 * won't get any samples after PERF_RECORD_EXIT. We can however still
5480 * get a few PERF_RECORD_READ events.
5482 perf_event_task(child, child_ctx, 0);
5485 * We can recurse on the same lock type through:
5487 * __perf_event_exit_task()
5488 * sync_child_event()
5489 * fput(parent_event->filp)
5490 * perf_release()
5491 * mutex_lock(&ctx->mutex)
5493 * But since its the parent context it won't be the same instance.
5495 mutex_lock(&child_ctx->mutex);
5497 again:
5498 list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
5499 group_entry)
5500 __perf_event_exit_task(child_event, child_ctx, child);
5502 list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
5503 group_entry)
5504 __perf_event_exit_task(child_event, child_ctx, child);
5507 * If the last event was a group event, it will have appended all
5508 * its siblings to the list, but we obtained 'tmp' before that which
5509 * will still point to the list head terminating the iteration.
5511 if (!list_empty(&child_ctx->pinned_groups) ||
5512 !list_empty(&child_ctx->flexible_groups))
5513 goto again;
5515 mutex_unlock(&child_ctx->mutex);
5517 put_ctx(child_ctx);
5520 static void perf_free_event(struct perf_event *event,
5521 struct perf_event_context *ctx)
5523 struct perf_event *parent = event->parent;
5525 if (WARN_ON_ONCE(!parent))
5526 return;
5528 mutex_lock(&parent->child_mutex);
5529 list_del_init(&event->child_list);
5530 mutex_unlock(&parent->child_mutex);
5532 fput(parent->filp);
5534 perf_group_detach(event);
5535 list_del_event(event, ctx);
5536 free_event(event);
5540 * free an unexposed, unused context as created by inheritance by
5541 * init_task below, used by fork() in case of fail.
5543 void perf_event_free_task(struct task_struct *task)
5545 struct perf_event_context *ctx = task->perf_event_ctxp;
5546 struct perf_event *event, *tmp;
5548 if (!ctx)
5549 return;
5551 mutex_lock(&ctx->mutex);
5552 again:
5553 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
5554 perf_free_event(event, ctx);
5556 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
5557 group_entry)
5558 perf_free_event(event, ctx);
5560 if (!list_empty(&ctx->pinned_groups) ||
5561 !list_empty(&ctx->flexible_groups))
5562 goto again;
5564 mutex_unlock(&ctx->mutex);
5566 put_ctx(ctx);
5569 static int
5570 inherit_task_group(struct perf_event *event, struct task_struct *parent,
5571 struct perf_event_context *parent_ctx,
5572 struct task_struct *child,
5573 int *inherited_all)
5575 int ret;
5576 struct perf_event_context *child_ctx = child->perf_event_ctxp;
5578 if (!event->attr.inherit) {
5579 *inherited_all = 0;
5580 return 0;
5583 if (!child_ctx) {
5585 * This is executed from the parent task context, so
5586 * inherit events that have been marked for cloning.
5587 * First allocate and initialize a context for the
5588 * child.
5591 child_ctx = kzalloc(sizeof(struct perf_event_context),
5592 GFP_KERNEL);
5593 if (!child_ctx)
5594 return -ENOMEM;
5596 __perf_event_init_context(child_ctx, child);
5597 child->perf_event_ctxp = child_ctx;
5598 get_task_struct(child);
5601 ret = inherit_group(event, parent, parent_ctx,
5602 child, child_ctx);
5604 if (ret)
5605 *inherited_all = 0;
5607 return ret;
5612 * Initialize the perf_event context in task_struct
5614 int perf_event_init_task(struct task_struct *child)
5616 struct perf_event_context *child_ctx, *parent_ctx;
5617 struct perf_event_context *cloned_ctx;
5618 struct perf_event *event;
5619 struct task_struct *parent = current;
5620 int inherited_all = 1;
5621 int ret = 0;
5623 child->perf_event_ctxp = NULL;
5625 mutex_init(&child->perf_event_mutex);
5626 INIT_LIST_HEAD(&child->perf_event_list);
5628 if (likely(!parent->perf_event_ctxp))
5629 return 0;
5632 * If the parent's context is a clone, pin it so it won't get
5633 * swapped under us.
5635 parent_ctx = perf_pin_task_context(parent);
5638 * No need to check if parent_ctx != NULL here; since we saw
5639 * it non-NULL earlier, the only reason for it to become NULL
5640 * is if we exit, and since we're currently in the middle of
5641 * a fork we can't be exiting at the same time.
5645 * Lock the parent list. No need to lock the child - not PID
5646 * hashed yet and not running, so nobody can access it.
5648 mutex_lock(&parent_ctx->mutex);
5651 * We dont have to disable NMIs - we are only looking at
5652 * the list, not manipulating it:
5654 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
5655 ret = inherit_task_group(event, parent, parent_ctx, child,
5656 &inherited_all);
5657 if (ret)
5658 break;
5661 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
5662 ret = inherit_task_group(event, parent, parent_ctx, child,
5663 &inherited_all);
5664 if (ret)
5665 break;
5668 child_ctx = child->perf_event_ctxp;
5670 if (child_ctx && inherited_all) {
5672 * Mark the child context as a clone of the parent
5673 * context, or of whatever the parent is a clone of.
5674 * Note that if the parent is a clone, it could get
5675 * uncloned at any point, but that doesn't matter
5676 * because the list of events and the generation
5677 * count can't have changed since we took the mutex.
5679 cloned_ctx = rcu_dereference(parent_ctx->parent_ctx);
5680 if (cloned_ctx) {
5681 child_ctx->parent_ctx = cloned_ctx;
5682 child_ctx->parent_gen = parent_ctx->parent_gen;
5683 } else {
5684 child_ctx->parent_ctx = parent_ctx;
5685 child_ctx->parent_gen = parent_ctx->generation;
5687 get_ctx(child_ctx->parent_ctx);
5690 mutex_unlock(&parent_ctx->mutex);
5692 perf_unpin_context(parent_ctx);
5694 return ret;
5697 static void __init perf_event_init_all_cpus(void)
5699 int cpu;
5700 struct perf_cpu_context *cpuctx;
5702 for_each_possible_cpu(cpu) {
5703 cpuctx = &per_cpu(perf_cpu_context, cpu);
5704 mutex_init(&cpuctx->hlist_mutex);
5705 __perf_event_init_context(&cpuctx->ctx, NULL);
5709 static void __cpuinit perf_event_init_cpu(int cpu)
5711 struct perf_cpu_context *cpuctx;
5713 cpuctx = &per_cpu(perf_cpu_context, cpu);
5715 spin_lock(&perf_resource_lock);
5716 cpuctx->max_pertask = perf_max_events - perf_reserved_percpu;
5717 spin_unlock(&perf_resource_lock);
5719 mutex_lock(&cpuctx->hlist_mutex);
5720 if (cpuctx->hlist_refcount > 0) {
5721 struct swevent_hlist *hlist;
5723 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
5724 WARN_ON_ONCE(!hlist);
5725 rcu_assign_pointer(cpuctx->swevent_hlist, hlist);
5727 mutex_unlock(&cpuctx->hlist_mutex);
5730 #ifdef CONFIG_HOTPLUG_CPU
5731 static void __perf_event_exit_cpu(void *info)
5733 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
5734 struct perf_event_context *ctx = &cpuctx->ctx;
5735 struct perf_event *event, *tmp;
5737 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
5738 __perf_event_remove_from_context(event);
5739 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, group_entry)
5740 __perf_event_remove_from_context(event);
5742 static void perf_event_exit_cpu(int cpu)
5744 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
5745 struct perf_event_context *ctx = &cpuctx->ctx;
5747 mutex_lock(&cpuctx->hlist_mutex);
5748 swevent_hlist_release(cpuctx);
5749 mutex_unlock(&cpuctx->hlist_mutex);
5751 mutex_lock(&ctx->mutex);
5752 smp_call_function_single(cpu, __perf_event_exit_cpu, NULL, 1);
5753 mutex_unlock(&ctx->mutex);
5755 #else
5756 static inline void perf_event_exit_cpu(int cpu) { }
5757 #endif
5759 static int __cpuinit
5760 perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
5762 unsigned int cpu = (long)hcpu;
5764 switch (action & ~CPU_TASKS_FROZEN) {
5766 case CPU_UP_PREPARE:
5767 case CPU_DOWN_FAILED:
5768 perf_event_init_cpu(cpu);
5769 break;
5771 case CPU_UP_CANCELED:
5772 case CPU_DOWN_PREPARE:
5773 perf_event_exit_cpu(cpu);
5774 break;
5776 default:
5777 break;
5780 return NOTIFY_OK;
5784 * This has to have a higher priority than migration_notifier in sched.c.
5786 static struct notifier_block __cpuinitdata perf_cpu_nb = {
5787 .notifier_call = perf_cpu_notify,
5788 .priority = 20,
5791 void __init perf_event_init(void)
5793 perf_event_init_all_cpus();
5794 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
5795 (void *)(long)smp_processor_id());
5796 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_ONLINE,
5797 (void *)(long)smp_processor_id());
5798 register_cpu_notifier(&perf_cpu_nb);
5801 static ssize_t perf_show_reserve_percpu(struct sysdev_class *class,
5802 struct sysdev_class_attribute *attr,
5803 char *buf)
5805 return sprintf(buf, "%d\n", perf_reserved_percpu);
5808 static ssize_t
5809 perf_set_reserve_percpu(struct sysdev_class *class,
5810 struct sysdev_class_attribute *attr,
5811 const char *buf,
5812 size_t count)
5814 struct perf_cpu_context *cpuctx;
5815 unsigned long val;
5816 int err, cpu, mpt;
5818 err = strict_strtoul(buf, 10, &val);
5819 if (err)
5820 return err;
5821 if (val > perf_max_events)
5822 return -EINVAL;
5824 spin_lock(&perf_resource_lock);
5825 perf_reserved_percpu = val;
5826 for_each_online_cpu(cpu) {
5827 cpuctx = &per_cpu(perf_cpu_context, cpu);
5828 raw_spin_lock_irq(&cpuctx->ctx.lock);
5829 mpt = min(perf_max_events - cpuctx->ctx.nr_events,
5830 perf_max_events - perf_reserved_percpu);
5831 cpuctx->max_pertask = mpt;
5832 raw_spin_unlock_irq(&cpuctx->ctx.lock);
5834 spin_unlock(&perf_resource_lock);
5836 return count;
5839 static ssize_t perf_show_overcommit(struct sysdev_class *class,
5840 struct sysdev_class_attribute *attr,
5841 char *buf)
5843 return sprintf(buf, "%d\n", perf_overcommit);
5846 static ssize_t
5847 perf_set_overcommit(struct sysdev_class *class,
5848 struct sysdev_class_attribute *attr,
5849 const char *buf, size_t count)
5851 unsigned long val;
5852 int err;
5854 err = strict_strtoul(buf, 10, &val);
5855 if (err)
5856 return err;
5857 if (val > 1)
5858 return -EINVAL;
5860 spin_lock(&perf_resource_lock);
5861 perf_overcommit = val;
5862 spin_unlock(&perf_resource_lock);
5864 return count;
5867 static SYSDEV_CLASS_ATTR(
5868 reserve_percpu,
5869 0644,
5870 perf_show_reserve_percpu,
5871 perf_set_reserve_percpu
5874 static SYSDEV_CLASS_ATTR(
5875 overcommit,
5876 0644,
5877 perf_show_overcommit,
5878 perf_set_overcommit
5881 static struct attribute *perfclass_attrs[] = {
5882 &attr_reserve_percpu.attr,
5883 &attr_overcommit.attr,
5884 NULL
5887 static struct attribute_group perfclass_attr_group = {
5888 .attrs = perfclass_attrs,
5889 .name = "perf_events",
5892 static int __init perf_event_sysfs_init(void)
5894 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
5895 &perfclass_attr_group);
5897 device_initcall(perf_event_sysfs_init);