sched: Add a PF flag for ksoftirqd identification
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / kernel / perf_event.c
blobd1a6c04d8399a05526bc8576a511045e53e645c4
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 cpu_clock(raw_smp_processor_id());
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 void
406 event_sched_out(struct perf_event *event,
407 struct perf_cpu_context *cpuctx,
408 struct perf_event_context *ctx)
410 if (event->state != PERF_EVENT_STATE_ACTIVE)
411 return;
413 event->state = PERF_EVENT_STATE_INACTIVE;
414 if (event->pending_disable) {
415 event->pending_disable = 0;
416 event->state = PERF_EVENT_STATE_OFF;
418 event->tstamp_stopped = ctx->time;
419 event->pmu->disable(event);
420 event->oncpu = -1;
422 if (!is_software_event(event))
423 cpuctx->active_oncpu--;
424 ctx->nr_active--;
425 if (event->attr.exclusive || !cpuctx->active_oncpu)
426 cpuctx->exclusive = 0;
429 static void
430 group_sched_out(struct perf_event *group_event,
431 struct perf_cpu_context *cpuctx,
432 struct perf_event_context *ctx)
434 struct perf_event *event;
436 if (group_event->state != PERF_EVENT_STATE_ACTIVE)
437 return;
439 event_sched_out(group_event, cpuctx, ctx);
442 * Schedule out siblings (if any):
444 list_for_each_entry(event, &group_event->sibling_list, group_entry)
445 event_sched_out(event, cpuctx, ctx);
447 if (group_event->attr.exclusive)
448 cpuctx->exclusive = 0;
452 * Cross CPU call to remove a performance event
454 * We disable the event on the hardware level first. After that we
455 * remove it from the context list.
457 static void __perf_event_remove_from_context(void *info)
459 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
460 struct perf_event *event = info;
461 struct perf_event_context *ctx = event->ctx;
464 * If this is a task context, we need to check whether it is
465 * the current task context of this cpu. If not it has been
466 * scheduled out before the smp call arrived.
468 if (ctx->task && cpuctx->task_ctx != ctx)
469 return;
471 raw_spin_lock(&ctx->lock);
473 * Protect the list operation against NMI by disabling the
474 * events on a global level.
476 perf_disable();
478 event_sched_out(event, cpuctx, ctx);
480 list_del_event(event, ctx);
482 if (!ctx->task) {
484 * Allow more per task events with respect to the
485 * reservation:
487 cpuctx->max_pertask =
488 min(perf_max_events - ctx->nr_events,
489 perf_max_events - perf_reserved_percpu);
492 perf_enable();
493 raw_spin_unlock(&ctx->lock);
498 * Remove the event from a task's (or a CPU's) list of events.
500 * Must be called with ctx->mutex held.
502 * CPU events are removed with a smp call. For task events we only
503 * call when the task is on a CPU.
505 * If event->ctx is a cloned context, callers must make sure that
506 * every task struct that event->ctx->task could possibly point to
507 * remains valid. This is OK when called from perf_release since
508 * that only calls us on the top-level context, which can't be a clone.
509 * When called from perf_event_exit_task, it's OK because the
510 * context has been detached from its task.
512 static void perf_event_remove_from_context(struct perf_event *event)
514 struct perf_event_context *ctx = event->ctx;
515 struct task_struct *task = ctx->task;
517 if (!task) {
519 * Per cpu events are removed via an smp call and
520 * the removal is always successful.
522 smp_call_function_single(event->cpu,
523 __perf_event_remove_from_context,
524 event, 1);
525 return;
528 retry:
529 task_oncpu_function_call(task, __perf_event_remove_from_context,
530 event);
532 raw_spin_lock_irq(&ctx->lock);
534 * If the context is active we need to retry the smp call.
536 if (ctx->nr_active && !list_empty(&event->group_entry)) {
537 raw_spin_unlock_irq(&ctx->lock);
538 goto retry;
542 * The lock prevents that this context is scheduled in so we
543 * can remove the event safely, if the call above did not
544 * succeed.
546 if (!list_empty(&event->group_entry))
547 list_del_event(event, ctx);
548 raw_spin_unlock_irq(&ctx->lock);
552 * Cross CPU call to disable a performance event
554 static void __perf_event_disable(void *info)
556 struct perf_event *event = info;
557 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
558 struct perf_event_context *ctx = event->ctx;
561 * If this is a per-task event, need to check whether this
562 * event's task is the current task on this cpu.
564 if (ctx->task && cpuctx->task_ctx != ctx)
565 return;
567 raw_spin_lock(&ctx->lock);
570 * If the event is on, turn it off.
571 * If it is in error state, leave it in error state.
573 if (event->state >= PERF_EVENT_STATE_INACTIVE) {
574 update_context_time(ctx);
575 update_group_times(event);
576 if (event == event->group_leader)
577 group_sched_out(event, cpuctx, ctx);
578 else
579 event_sched_out(event, cpuctx, ctx);
580 event->state = PERF_EVENT_STATE_OFF;
583 raw_spin_unlock(&ctx->lock);
587 * Disable a event.
589 * If event->ctx is a cloned context, callers must make sure that
590 * every task struct that event->ctx->task could possibly point to
591 * remains valid. This condition is satisifed when called through
592 * perf_event_for_each_child or perf_event_for_each because they
593 * hold the top-level event's child_mutex, so any descendant that
594 * goes to exit will block in sync_child_event.
595 * When called from perf_pending_event it's OK because event->ctx
596 * is the current context on this CPU and preemption is disabled,
597 * hence we can't get into perf_event_task_sched_out for this context.
599 void perf_event_disable(struct perf_event *event)
601 struct perf_event_context *ctx = event->ctx;
602 struct task_struct *task = ctx->task;
604 if (!task) {
606 * Disable the event on the cpu that it's on
608 smp_call_function_single(event->cpu, __perf_event_disable,
609 event, 1);
610 return;
613 retry:
614 task_oncpu_function_call(task, __perf_event_disable, event);
616 raw_spin_lock_irq(&ctx->lock);
618 * If the event is still active, we need to retry the cross-call.
620 if (event->state == PERF_EVENT_STATE_ACTIVE) {
621 raw_spin_unlock_irq(&ctx->lock);
622 goto retry;
626 * Since we have the lock this context can't be scheduled
627 * in, so we can change the state safely.
629 if (event->state == PERF_EVENT_STATE_INACTIVE) {
630 update_group_times(event);
631 event->state = PERF_EVENT_STATE_OFF;
634 raw_spin_unlock_irq(&ctx->lock);
637 static int
638 event_sched_in(struct perf_event *event,
639 struct perf_cpu_context *cpuctx,
640 struct perf_event_context *ctx)
642 if (event->state <= PERF_EVENT_STATE_OFF)
643 return 0;
645 event->state = PERF_EVENT_STATE_ACTIVE;
646 event->oncpu = smp_processor_id();
648 * The new state must be visible before we turn it on in the hardware:
650 smp_wmb();
652 if (event->pmu->enable(event)) {
653 event->state = PERF_EVENT_STATE_INACTIVE;
654 event->oncpu = -1;
655 return -EAGAIN;
658 event->tstamp_running += ctx->time - event->tstamp_stopped;
660 if (!is_software_event(event))
661 cpuctx->active_oncpu++;
662 ctx->nr_active++;
664 if (event->attr.exclusive)
665 cpuctx->exclusive = 1;
667 return 0;
670 static int
671 group_sched_in(struct perf_event *group_event,
672 struct perf_cpu_context *cpuctx,
673 struct perf_event_context *ctx)
675 struct perf_event *event, *partial_group = NULL;
676 const struct pmu *pmu = group_event->pmu;
677 bool txn = false;
678 int ret;
680 if (group_event->state == PERF_EVENT_STATE_OFF)
681 return 0;
683 /* Check if group transaction availabe */
684 if (pmu->start_txn)
685 txn = true;
687 if (txn)
688 pmu->start_txn(pmu);
690 if (event_sched_in(group_event, cpuctx, ctx)) {
691 if (txn)
692 pmu->cancel_txn(pmu);
693 return -EAGAIN;
697 * Schedule in siblings as one group (if any):
699 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
700 if (event_sched_in(event, cpuctx, ctx)) {
701 partial_group = event;
702 goto group_error;
706 if (!txn)
707 return 0;
709 ret = pmu->commit_txn(pmu);
710 if (!ret) {
711 pmu->cancel_txn(pmu);
712 return 0;
715 group_error:
717 * Groups can be scheduled in as one unit only, so undo any
718 * partial group before returning:
720 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
721 if (event == partial_group)
722 break;
723 event_sched_out(event, cpuctx, ctx);
725 event_sched_out(group_event, cpuctx, ctx);
727 if (txn)
728 pmu->cancel_txn(pmu);
730 return -EAGAIN;
734 * Work out whether we can put this event group on the CPU now.
736 static int group_can_go_on(struct perf_event *event,
737 struct perf_cpu_context *cpuctx,
738 int can_add_hw)
741 * Groups consisting entirely of software events can always go on.
743 if (event->group_flags & PERF_GROUP_SOFTWARE)
744 return 1;
746 * If an exclusive group is already on, no other hardware
747 * events can go on.
749 if (cpuctx->exclusive)
750 return 0;
752 * If this group is exclusive and there are already
753 * events on the CPU, it can't go on.
755 if (event->attr.exclusive && cpuctx->active_oncpu)
756 return 0;
758 * Otherwise, try to add it if all previous groups were able
759 * to go on.
761 return can_add_hw;
764 static void add_event_to_ctx(struct perf_event *event,
765 struct perf_event_context *ctx)
767 list_add_event(event, ctx);
768 perf_group_attach(event);
769 event->tstamp_enabled = ctx->time;
770 event->tstamp_running = ctx->time;
771 event->tstamp_stopped = ctx->time;
775 * Cross CPU call to install and enable a performance event
777 * Must be called with ctx->mutex held
779 static void __perf_install_in_context(void *info)
781 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
782 struct perf_event *event = info;
783 struct perf_event_context *ctx = event->ctx;
784 struct perf_event *leader = event->group_leader;
785 int err;
788 * If this is a task context, we need to check whether it is
789 * the current task context of this cpu. If not it has been
790 * scheduled out before the smp call arrived.
791 * Or possibly this is the right context but it isn't
792 * on this cpu because it had no events.
794 if (ctx->task && cpuctx->task_ctx != ctx) {
795 if (cpuctx->task_ctx || ctx->task != current)
796 return;
797 cpuctx->task_ctx = ctx;
800 raw_spin_lock(&ctx->lock);
801 ctx->is_active = 1;
802 update_context_time(ctx);
805 * Protect the list operation against NMI by disabling the
806 * events on a global level. NOP for non NMI based events.
808 perf_disable();
810 add_event_to_ctx(event, ctx);
812 if (event->cpu != -1 && event->cpu != smp_processor_id())
813 goto unlock;
816 * Don't put the event on if it is disabled or if
817 * it is in a group and the group isn't on.
819 if (event->state != PERF_EVENT_STATE_INACTIVE ||
820 (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE))
821 goto unlock;
824 * An exclusive event can't go on if there are already active
825 * hardware events, and no hardware event can go on if there
826 * is already an exclusive event on.
828 if (!group_can_go_on(event, cpuctx, 1))
829 err = -EEXIST;
830 else
831 err = event_sched_in(event, cpuctx, ctx);
833 if (err) {
835 * This event couldn't go on. If it is in a group
836 * then we have to pull the whole group off.
837 * If the event group is pinned then put it in error state.
839 if (leader != event)
840 group_sched_out(leader, cpuctx, ctx);
841 if (leader->attr.pinned) {
842 update_group_times(leader);
843 leader->state = PERF_EVENT_STATE_ERROR;
847 if (!err && !ctx->task && cpuctx->max_pertask)
848 cpuctx->max_pertask--;
850 unlock:
851 perf_enable();
853 raw_spin_unlock(&ctx->lock);
857 * Attach a performance event to a context
859 * First we add the event to the list with the hardware enable bit
860 * in event->hw_config cleared.
862 * If the event is attached to a task which is on a CPU we use a smp
863 * call to enable it in the task context. The task might have been
864 * scheduled away, but we check this in the smp call again.
866 * Must be called with ctx->mutex held.
868 static void
869 perf_install_in_context(struct perf_event_context *ctx,
870 struct perf_event *event,
871 int cpu)
873 struct task_struct *task = ctx->task;
875 if (!task) {
877 * Per cpu events are installed via an smp call and
878 * the install is always successful.
880 smp_call_function_single(cpu, __perf_install_in_context,
881 event, 1);
882 return;
885 retry:
886 task_oncpu_function_call(task, __perf_install_in_context,
887 event);
889 raw_spin_lock_irq(&ctx->lock);
891 * we need to retry the smp call.
893 if (ctx->is_active && list_empty(&event->group_entry)) {
894 raw_spin_unlock_irq(&ctx->lock);
895 goto retry;
899 * The lock prevents that this context is scheduled in so we
900 * can add the event safely, if it the call above did not
901 * succeed.
903 if (list_empty(&event->group_entry))
904 add_event_to_ctx(event, ctx);
905 raw_spin_unlock_irq(&ctx->lock);
909 * Put a event into inactive state and update time fields.
910 * Enabling the leader of a group effectively enables all
911 * the group members that aren't explicitly disabled, so we
912 * have to update their ->tstamp_enabled also.
913 * Note: this works for group members as well as group leaders
914 * since the non-leader members' sibling_lists will be empty.
916 static void __perf_event_mark_enabled(struct perf_event *event,
917 struct perf_event_context *ctx)
919 struct perf_event *sub;
921 event->state = PERF_EVENT_STATE_INACTIVE;
922 event->tstamp_enabled = ctx->time - event->total_time_enabled;
923 list_for_each_entry(sub, &event->sibling_list, group_entry)
924 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
925 sub->tstamp_enabled =
926 ctx->time - sub->total_time_enabled;
930 * Cross CPU call to enable a performance event
932 static void __perf_event_enable(void *info)
934 struct perf_event *event = info;
935 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
936 struct perf_event_context *ctx = event->ctx;
937 struct perf_event *leader = event->group_leader;
938 int err;
941 * If this is a per-task event, need to check whether this
942 * event's task is the current task on this cpu.
944 if (ctx->task && cpuctx->task_ctx != ctx) {
945 if (cpuctx->task_ctx || ctx->task != current)
946 return;
947 cpuctx->task_ctx = ctx;
950 raw_spin_lock(&ctx->lock);
951 ctx->is_active = 1;
952 update_context_time(ctx);
954 if (event->state >= PERF_EVENT_STATE_INACTIVE)
955 goto unlock;
956 __perf_event_mark_enabled(event, ctx);
958 if (event->cpu != -1 && event->cpu != smp_processor_id())
959 goto unlock;
962 * If the event is in a group and isn't the group leader,
963 * then don't put it on unless the group is on.
965 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
966 goto unlock;
968 if (!group_can_go_on(event, cpuctx, 1)) {
969 err = -EEXIST;
970 } else {
971 perf_disable();
972 if (event == leader)
973 err = group_sched_in(event, cpuctx, ctx);
974 else
975 err = event_sched_in(event, cpuctx, ctx);
976 perf_enable();
979 if (err) {
981 * If this event can't go on and it's part of a
982 * group, then the whole group has to come off.
984 if (leader != event)
985 group_sched_out(leader, cpuctx, ctx);
986 if (leader->attr.pinned) {
987 update_group_times(leader);
988 leader->state = PERF_EVENT_STATE_ERROR;
992 unlock:
993 raw_spin_unlock(&ctx->lock);
997 * Enable a event.
999 * If event->ctx is a cloned context, callers must make sure that
1000 * every task struct that event->ctx->task could possibly point to
1001 * remains valid. This condition is satisfied when called through
1002 * perf_event_for_each_child or perf_event_for_each as described
1003 * for perf_event_disable.
1005 void perf_event_enable(struct perf_event *event)
1007 struct perf_event_context *ctx = event->ctx;
1008 struct task_struct *task = ctx->task;
1010 if (!task) {
1012 * Enable the event on the cpu that it's on
1014 smp_call_function_single(event->cpu, __perf_event_enable,
1015 event, 1);
1016 return;
1019 raw_spin_lock_irq(&ctx->lock);
1020 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1021 goto out;
1024 * If the event is in error state, clear that first.
1025 * That way, if we see the event in error state below, we
1026 * know that it has gone back into error state, as distinct
1027 * from the task having been scheduled away before the
1028 * cross-call arrived.
1030 if (event->state == PERF_EVENT_STATE_ERROR)
1031 event->state = PERF_EVENT_STATE_OFF;
1033 retry:
1034 raw_spin_unlock_irq(&ctx->lock);
1035 task_oncpu_function_call(task, __perf_event_enable, event);
1037 raw_spin_lock_irq(&ctx->lock);
1040 * If the context is active and the event is still off,
1041 * we need to retry the cross-call.
1043 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF)
1044 goto retry;
1047 * Since we have the lock this context can't be scheduled
1048 * in, so we can change the state safely.
1050 if (event->state == PERF_EVENT_STATE_OFF)
1051 __perf_event_mark_enabled(event, ctx);
1053 out:
1054 raw_spin_unlock_irq(&ctx->lock);
1057 static int perf_event_refresh(struct perf_event *event, int refresh)
1060 * not supported on inherited events
1062 if (event->attr.inherit)
1063 return -EINVAL;
1065 atomic_add(refresh, &event->event_limit);
1066 perf_event_enable(event);
1068 return 0;
1071 enum event_type_t {
1072 EVENT_FLEXIBLE = 0x1,
1073 EVENT_PINNED = 0x2,
1074 EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
1077 static void ctx_sched_out(struct perf_event_context *ctx,
1078 struct perf_cpu_context *cpuctx,
1079 enum event_type_t event_type)
1081 struct perf_event *event;
1083 raw_spin_lock(&ctx->lock);
1084 ctx->is_active = 0;
1085 if (likely(!ctx->nr_events))
1086 goto out;
1087 update_context_time(ctx);
1089 perf_disable();
1090 if (!ctx->nr_active)
1091 goto out_enable;
1093 if (event_type & EVENT_PINNED)
1094 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
1095 group_sched_out(event, cpuctx, ctx);
1097 if (event_type & EVENT_FLEXIBLE)
1098 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
1099 group_sched_out(event, cpuctx, ctx);
1101 out_enable:
1102 perf_enable();
1103 out:
1104 raw_spin_unlock(&ctx->lock);
1108 * Test whether two contexts are equivalent, i.e. whether they
1109 * have both been cloned from the same version of the same context
1110 * and they both have the same number of enabled events.
1111 * If the number of enabled events is the same, then the set
1112 * of enabled events should be the same, because these are both
1113 * inherited contexts, therefore we can't access individual events
1114 * in them directly with an fd; we can only enable/disable all
1115 * events via prctl, or enable/disable all events in a family
1116 * via ioctl, which will have the same effect on both contexts.
1118 static int context_equiv(struct perf_event_context *ctx1,
1119 struct perf_event_context *ctx2)
1121 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
1122 && ctx1->parent_gen == ctx2->parent_gen
1123 && !ctx1->pin_count && !ctx2->pin_count;
1126 static void __perf_event_sync_stat(struct perf_event *event,
1127 struct perf_event *next_event)
1129 u64 value;
1131 if (!event->attr.inherit_stat)
1132 return;
1135 * Update the event value, we cannot use perf_event_read()
1136 * because we're in the middle of a context switch and have IRQs
1137 * disabled, which upsets smp_call_function_single(), however
1138 * we know the event must be on the current CPU, therefore we
1139 * don't need to use it.
1141 switch (event->state) {
1142 case PERF_EVENT_STATE_ACTIVE:
1143 event->pmu->read(event);
1144 /* fall-through */
1146 case PERF_EVENT_STATE_INACTIVE:
1147 update_event_times(event);
1148 break;
1150 default:
1151 break;
1155 * In order to keep per-task stats reliable we need to flip the event
1156 * values when we flip the contexts.
1158 value = atomic64_read(&next_event->count);
1159 value = atomic64_xchg(&event->count, value);
1160 atomic64_set(&next_event->count, value);
1162 swap(event->total_time_enabled, next_event->total_time_enabled);
1163 swap(event->total_time_running, next_event->total_time_running);
1166 * Since we swizzled the values, update the user visible data too.
1168 perf_event_update_userpage(event);
1169 perf_event_update_userpage(next_event);
1172 #define list_next_entry(pos, member) \
1173 list_entry(pos->member.next, typeof(*pos), member)
1175 static void perf_event_sync_stat(struct perf_event_context *ctx,
1176 struct perf_event_context *next_ctx)
1178 struct perf_event *event, *next_event;
1180 if (!ctx->nr_stat)
1181 return;
1183 update_context_time(ctx);
1185 event = list_first_entry(&ctx->event_list,
1186 struct perf_event, event_entry);
1188 next_event = list_first_entry(&next_ctx->event_list,
1189 struct perf_event, event_entry);
1191 while (&event->event_entry != &ctx->event_list &&
1192 &next_event->event_entry != &next_ctx->event_list) {
1194 __perf_event_sync_stat(event, next_event);
1196 event = list_next_entry(event, event_entry);
1197 next_event = list_next_entry(next_event, event_entry);
1202 * Called from scheduler to remove the events of the current task,
1203 * with interrupts disabled.
1205 * We stop each event and update the event value in event->count.
1207 * This does not protect us against NMI, but disable()
1208 * sets the disabled bit in the control field of event _before_
1209 * accessing the event control register. If a NMI hits, then it will
1210 * not restart the event.
1212 void perf_event_task_sched_out(struct task_struct *task,
1213 struct task_struct *next)
1215 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1216 struct perf_event_context *ctx = task->perf_event_ctxp;
1217 struct perf_event_context *next_ctx;
1218 struct perf_event_context *parent;
1219 int do_switch = 1;
1221 perf_sw_event(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 1, NULL, 0);
1223 if (likely(!ctx || !cpuctx->task_ctx))
1224 return;
1226 rcu_read_lock();
1227 parent = rcu_dereference(ctx->parent_ctx);
1228 next_ctx = next->perf_event_ctxp;
1229 if (parent && next_ctx &&
1230 rcu_dereference(next_ctx->parent_ctx) == parent) {
1232 * Looks like the two contexts are clones, so we might be
1233 * able to optimize the context switch. We lock both
1234 * contexts and check that they are clones under the
1235 * lock (including re-checking that neither has been
1236 * uncloned in the meantime). It doesn't matter which
1237 * order we take the locks because no other cpu could
1238 * be trying to lock both of these tasks.
1240 raw_spin_lock(&ctx->lock);
1241 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
1242 if (context_equiv(ctx, next_ctx)) {
1244 * XXX do we need a memory barrier of sorts
1245 * wrt to rcu_dereference() of perf_event_ctxp
1247 task->perf_event_ctxp = next_ctx;
1248 next->perf_event_ctxp = ctx;
1249 ctx->task = next;
1250 next_ctx->task = task;
1251 do_switch = 0;
1253 perf_event_sync_stat(ctx, next_ctx);
1255 raw_spin_unlock(&next_ctx->lock);
1256 raw_spin_unlock(&ctx->lock);
1258 rcu_read_unlock();
1260 if (do_switch) {
1261 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
1262 cpuctx->task_ctx = NULL;
1266 static void task_ctx_sched_out(struct perf_event_context *ctx,
1267 enum event_type_t event_type)
1269 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1271 if (!cpuctx->task_ctx)
1272 return;
1274 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
1275 return;
1277 ctx_sched_out(ctx, cpuctx, event_type);
1278 cpuctx->task_ctx = NULL;
1282 * Called with IRQs disabled
1284 static void __perf_event_task_sched_out(struct perf_event_context *ctx)
1286 task_ctx_sched_out(ctx, EVENT_ALL);
1290 * Called with IRQs disabled
1292 static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
1293 enum event_type_t event_type)
1295 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
1298 static void
1299 ctx_pinned_sched_in(struct perf_event_context *ctx,
1300 struct perf_cpu_context *cpuctx)
1302 struct perf_event *event;
1304 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
1305 if (event->state <= PERF_EVENT_STATE_OFF)
1306 continue;
1307 if (event->cpu != -1 && event->cpu != smp_processor_id())
1308 continue;
1310 if (group_can_go_on(event, cpuctx, 1))
1311 group_sched_in(event, cpuctx, ctx);
1314 * If this pinned group hasn't been scheduled,
1315 * put it in error state.
1317 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1318 update_group_times(event);
1319 event->state = PERF_EVENT_STATE_ERROR;
1324 static void
1325 ctx_flexible_sched_in(struct perf_event_context *ctx,
1326 struct perf_cpu_context *cpuctx)
1328 struct perf_event *event;
1329 int can_add_hw = 1;
1331 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
1332 /* Ignore events in OFF or ERROR state */
1333 if (event->state <= PERF_EVENT_STATE_OFF)
1334 continue;
1336 * Listen to the 'cpu' scheduling filter constraint
1337 * of events:
1339 if (event->cpu != -1 && event->cpu != smp_processor_id())
1340 continue;
1342 if (group_can_go_on(event, cpuctx, can_add_hw))
1343 if (group_sched_in(event, cpuctx, ctx))
1344 can_add_hw = 0;
1348 static void
1349 ctx_sched_in(struct perf_event_context *ctx,
1350 struct perf_cpu_context *cpuctx,
1351 enum event_type_t event_type)
1353 raw_spin_lock(&ctx->lock);
1354 ctx->is_active = 1;
1355 if (likely(!ctx->nr_events))
1356 goto out;
1358 ctx->timestamp = perf_clock();
1360 perf_disable();
1363 * First go through the list and put on any pinned groups
1364 * in order to give them the best chance of going on.
1366 if (event_type & EVENT_PINNED)
1367 ctx_pinned_sched_in(ctx, cpuctx);
1369 /* Then walk through the lower prio flexible groups */
1370 if (event_type & EVENT_FLEXIBLE)
1371 ctx_flexible_sched_in(ctx, cpuctx);
1373 perf_enable();
1374 out:
1375 raw_spin_unlock(&ctx->lock);
1378 static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
1379 enum event_type_t event_type)
1381 struct perf_event_context *ctx = &cpuctx->ctx;
1383 ctx_sched_in(ctx, cpuctx, event_type);
1386 static void task_ctx_sched_in(struct task_struct *task,
1387 enum event_type_t event_type)
1389 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1390 struct perf_event_context *ctx = task->perf_event_ctxp;
1392 if (likely(!ctx))
1393 return;
1394 if (cpuctx->task_ctx == ctx)
1395 return;
1396 ctx_sched_in(ctx, cpuctx, event_type);
1397 cpuctx->task_ctx = ctx;
1400 * Called from scheduler to add the events of the current task
1401 * with interrupts disabled.
1403 * We restore the event value and then enable it.
1405 * This does not protect us against NMI, but enable()
1406 * sets the enabled bit in the control field of event _before_
1407 * accessing the event control register. If a NMI hits, then it will
1408 * keep the event running.
1410 void perf_event_task_sched_in(struct task_struct *task)
1412 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1413 struct perf_event_context *ctx = task->perf_event_ctxp;
1415 if (likely(!ctx))
1416 return;
1418 if (cpuctx->task_ctx == ctx)
1419 return;
1421 perf_disable();
1424 * We want to keep the following priority order:
1425 * cpu pinned (that don't need to move), task pinned,
1426 * cpu flexible, task flexible.
1428 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
1430 ctx_sched_in(ctx, cpuctx, EVENT_PINNED);
1431 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE);
1432 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE);
1434 cpuctx->task_ctx = ctx;
1436 perf_enable();
1439 #define MAX_INTERRUPTS (~0ULL)
1441 static void perf_log_throttle(struct perf_event *event, int enable);
1443 static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
1445 u64 frequency = event->attr.sample_freq;
1446 u64 sec = NSEC_PER_SEC;
1447 u64 divisor, dividend;
1449 int count_fls, nsec_fls, frequency_fls, sec_fls;
1451 count_fls = fls64(count);
1452 nsec_fls = fls64(nsec);
1453 frequency_fls = fls64(frequency);
1454 sec_fls = 30;
1457 * We got @count in @nsec, with a target of sample_freq HZ
1458 * the target period becomes:
1460 * @count * 10^9
1461 * period = -------------------
1462 * @nsec * sample_freq
1467 * Reduce accuracy by one bit such that @a and @b converge
1468 * to a similar magnitude.
1470 #define REDUCE_FLS(a, b) \
1471 do { \
1472 if (a##_fls > b##_fls) { \
1473 a >>= 1; \
1474 a##_fls--; \
1475 } else { \
1476 b >>= 1; \
1477 b##_fls--; \
1479 } while (0)
1482 * Reduce accuracy until either term fits in a u64, then proceed with
1483 * the other, so that finally we can do a u64/u64 division.
1485 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
1486 REDUCE_FLS(nsec, frequency);
1487 REDUCE_FLS(sec, count);
1490 if (count_fls + sec_fls > 64) {
1491 divisor = nsec * frequency;
1493 while (count_fls + sec_fls > 64) {
1494 REDUCE_FLS(count, sec);
1495 divisor >>= 1;
1498 dividend = count * sec;
1499 } else {
1500 dividend = count * sec;
1502 while (nsec_fls + frequency_fls > 64) {
1503 REDUCE_FLS(nsec, frequency);
1504 dividend >>= 1;
1507 divisor = nsec * frequency;
1510 if (!divisor)
1511 return dividend;
1513 return div64_u64(dividend, divisor);
1516 static void perf_event_stop(struct perf_event *event)
1518 if (!event->pmu->stop)
1519 return event->pmu->disable(event);
1521 return event->pmu->stop(event);
1524 static int perf_event_start(struct perf_event *event)
1526 if (!event->pmu->start)
1527 return event->pmu->enable(event);
1529 return event->pmu->start(event);
1532 static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count)
1534 struct hw_perf_event *hwc = &event->hw;
1535 s64 period, sample_period;
1536 s64 delta;
1538 period = perf_calculate_period(event, nsec, count);
1540 delta = (s64)(period - hwc->sample_period);
1541 delta = (delta + 7) / 8; /* low pass filter */
1543 sample_period = hwc->sample_period + delta;
1545 if (!sample_period)
1546 sample_period = 1;
1548 hwc->sample_period = sample_period;
1550 if (atomic64_read(&hwc->period_left) > 8*sample_period) {
1551 perf_disable();
1552 perf_event_stop(event);
1553 atomic64_set(&hwc->period_left, 0);
1554 perf_event_start(event);
1555 perf_enable();
1559 static void perf_ctx_adjust_freq(struct perf_event_context *ctx)
1561 struct perf_event *event;
1562 struct hw_perf_event *hwc;
1563 u64 interrupts, now;
1564 s64 delta;
1566 raw_spin_lock(&ctx->lock);
1567 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
1568 if (event->state != PERF_EVENT_STATE_ACTIVE)
1569 continue;
1571 if (event->cpu != -1 && event->cpu != smp_processor_id())
1572 continue;
1574 hwc = &event->hw;
1576 interrupts = hwc->interrupts;
1577 hwc->interrupts = 0;
1580 * unthrottle events on the tick
1582 if (interrupts == MAX_INTERRUPTS) {
1583 perf_log_throttle(event, 1);
1584 perf_disable();
1585 event->pmu->unthrottle(event);
1586 perf_enable();
1589 if (!event->attr.freq || !event->attr.sample_freq)
1590 continue;
1592 perf_disable();
1593 event->pmu->read(event);
1594 now = atomic64_read(&event->count);
1595 delta = now - hwc->freq_count_stamp;
1596 hwc->freq_count_stamp = now;
1598 if (delta > 0)
1599 perf_adjust_period(event, TICK_NSEC, delta);
1600 perf_enable();
1602 raw_spin_unlock(&ctx->lock);
1606 * Round-robin a context's events:
1608 static void rotate_ctx(struct perf_event_context *ctx)
1610 raw_spin_lock(&ctx->lock);
1613 * Rotate the first entry last of non-pinned groups. Rotation might be
1614 * disabled by the inheritance code.
1616 if (!ctx->rotate_disable)
1617 list_rotate_left(&ctx->flexible_groups);
1619 raw_spin_unlock(&ctx->lock);
1622 void perf_event_task_tick(struct task_struct *curr)
1624 struct perf_cpu_context *cpuctx;
1625 struct perf_event_context *ctx;
1626 int rotate = 0;
1628 if (!atomic_read(&nr_events))
1629 return;
1631 cpuctx = &__get_cpu_var(perf_cpu_context);
1632 if (cpuctx->ctx.nr_events &&
1633 cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
1634 rotate = 1;
1636 ctx = curr->perf_event_ctxp;
1637 if (ctx && ctx->nr_events && ctx->nr_events != ctx->nr_active)
1638 rotate = 1;
1640 perf_ctx_adjust_freq(&cpuctx->ctx);
1641 if (ctx)
1642 perf_ctx_adjust_freq(ctx);
1644 if (!rotate)
1645 return;
1647 perf_disable();
1648 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
1649 if (ctx)
1650 task_ctx_sched_out(ctx, EVENT_FLEXIBLE);
1652 rotate_ctx(&cpuctx->ctx);
1653 if (ctx)
1654 rotate_ctx(ctx);
1656 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE);
1657 if (ctx)
1658 task_ctx_sched_in(curr, EVENT_FLEXIBLE);
1659 perf_enable();
1662 static int event_enable_on_exec(struct perf_event *event,
1663 struct perf_event_context *ctx)
1665 if (!event->attr.enable_on_exec)
1666 return 0;
1668 event->attr.enable_on_exec = 0;
1669 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1670 return 0;
1672 __perf_event_mark_enabled(event, ctx);
1674 return 1;
1678 * Enable all of a task's events that have been marked enable-on-exec.
1679 * This expects task == current.
1681 static void perf_event_enable_on_exec(struct task_struct *task)
1683 struct perf_event_context *ctx;
1684 struct perf_event *event;
1685 unsigned long flags;
1686 int enabled = 0;
1687 int ret;
1689 local_irq_save(flags);
1690 ctx = task->perf_event_ctxp;
1691 if (!ctx || !ctx->nr_events)
1692 goto out;
1694 __perf_event_task_sched_out(ctx);
1696 raw_spin_lock(&ctx->lock);
1698 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
1699 ret = event_enable_on_exec(event, ctx);
1700 if (ret)
1701 enabled = 1;
1704 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
1705 ret = event_enable_on_exec(event, ctx);
1706 if (ret)
1707 enabled = 1;
1711 * Unclone this context if we enabled any event.
1713 if (enabled)
1714 unclone_ctx(ctx);
1716 raw_spin_unlock(&ctx->lock);
1718 perf_event_task_sched_in(task);
1719 out:
1720 local_irq_restore(flags);
1724 * Cross CPU call to read the hardware event
1726 static void __perf_event_read(void *info)
1728 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1729 struct perf_event *event = info;
1730 struct perf_event_context *ctx = event->ctx;
1733 * If this is a task context, we need to check whether it is
1734 * the current task context of this cpu. If not it has been
1735 * scheduled out before the smp call arrived. In that case
1736 * event->count would have been updated to a recent sample
1737 * when the event was scheduled out.
1739 if (ctx->task && cpuctx->task_ctx != ctx)
1740 return;
1742 raw_spin_lock(&ctx->lock);
1743 update_context_time(ctx);
1744 update_event_times(event);
1745 raw_spin_unlock(&ctx->lock);
1747 event->pmu->read(event);
1750 static u64 perf_event_read(struct perf_event *event)
1753 * If event is enabled and currently active on a CPU, update the
1754 * value in the event structure:
1756 if (event->state == PERF_EVENT_STATE_ACTIVE) {
1757 smp_call_function_single(event->oncpu,
1758 __perf_event_read, event, 1);
1759 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
1760 struct perf_event_context *ctx = event->ctx;
1761 unsigned long flags;
1763 raw_spin_lock_irqsave(&ctx->lock, flags);
1765 * may read while context is not active
1766 * (e.g., thread is blocked), in that case
1767 * we cannot update context time
1769 if (ctx->is_active)
1770 update_context_time(ctx);
1771 update_event_times(event);
1772 raw_spin_unlock_irqrestore(&ctx->lock, flags);
1775 return atomic64_read(&event->count);
1779 * Initialize the perf_event context in a task_struct:
1781 static void
1782 __perf_event_init_context(struct perf_event_context *ctx,
1783 struct task_struct *task)
1785 raw_spin_lock_init(&ctx->lock);
1786 mutex_init(&ctx->mutex);
1787 INIT_LIST_HEAD(&ctx->pinned_groups);
1788 INIT_LIST_HEAD(&ctx->flexible_groups);
1789 INIT_LIST_HEAD(&ctx->event_list);
1790 atomic_set(&ctx->refcount, 1);
1791 ctx->task = task;
1794 static struct perf_event_context *find_get_context(pid_t pid, int cpu)
1796 struct perf_event_context *ctx;
1797 struct perf_cpu_context *cpuctx;
1798 struct task_struct *task;
1799 unsigned long flags;
1800 int err;
1802 if (pid == -1 && cpu != -1) {
1803 /* Must be root to operate on a CPU event: */
1804 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
1805 return ERR_PTR(-EACCES);
1807 if (cpu < 0 || cpu >= nr_cpumask_bits)
1808 return ERR_PTR(-EINVAL);
1811 * We could be clever and allow to attach a event to an
1812 * offline CPU and activate it when the CPU comes up, but
1813 * that's for later.
1815 if (!cpu_online(cpu))
1816 return ERR_PTR(-ENODEV);
1818 cpuctx = &per_cpu(perf_cpu_context, cpu);
1819 ctx = &cpuctx->ctx;
1820 get_ctx(ctx);
1822 return ctx;
1825 rcu_read_lock();
1826 if (!pid)
1827 task = current;
1828 else
1829 task = find_task_by_vpid(pid);
1830 if (task)
1831 get_task_struct(task);
1832 rcu_read_unlock();
1834 if (!task)
1835 return ERR_PTR(-ESRCH);
1838 * Can't attach events to a dying task.
1840 err = -ESRCH;
1841 if (task->flags & PF_EXITING)
1842 goto errout;
1844 /* Reuse ptrace permission checks for now. */
1845 err = -EACCES;
1846 if (!ptrace_may_access(task, PTRACE_MODE_READ))
1847 goto errout;
1849 retry:
1850 ctx = perf_lock_task_context(task, &flags);
1851 if (ctx) {
1852 unclone_ctx(ctx);
1853 raw_spin_unlock_irqrestore(&ctx->lock, flags);
1856 if (!ctx) {
1857 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
1858 err = -ENOMEM;
1859 if (!ctx)
1860 goto errout;
1861 __perf_event_init_context(ctx, task);
1862 get_ctx(ctx);
1863 if (cmpxchg(&task->perf_event_ctxp, NULL, ctx)) {
1865 * We raced with some other task; use
1866 * the context they set.
1868 kfree(ctx);
1869 goto retry;
1871 get_task_struct(task);
1874 put_task_struct(task);
1875 return ctx;
1877 errout:
1878 put_task_struct(task);
1879 return ERR_PTR(err);
1882 static void perf_event_free_filter(struct perf_event *event);
1884 static void free_event_rcu(struct rcu_head *head)
1886 struct perf_event *event;
1888 event = container_of(head, struct perf_event, rcu_head);
1889 if (event->ns)
1890 put_pid_ns(event->ns);
1891 perf_event_free_filter(event);
1892 kfree(event);
1895 static void perf_pending_sync(struct perf_event *event);
1896 static void perf_mmap_data_put(struct perf_mmap_data *data);
1898 static void free_event(struct perf_event *event)
1900 perf_pending_sync(event);
1902 if (!event->parent) {
1903 atomic_dec(&nr_events);
1904 if (event->attr.mmap)
1905 atomic_dec(&nr_mmap_events);
1906 if (event->attr.comm)
1907 atomic_dec(&nr_comm_events);
1908 if (event->attr.task)
1909 atomic_dec(&nr_task_events);
1912 if (event->data) {
1913 perf_mmap_data_put(event->data);
1914 event->data = NULL;
1917 if (event->destroy)
1918 event->destroy(event);
1920 put_ctx(event->ctx);
1921 call_rcu(&event->rcu_head, free_event_rcu);
1924 int perf_event_release_kernel(struct perf_event *event)
1926 struct perf_event_context *ctx = event->ctx;
1929 * Remove from the PMU, can't get re-enabled since we got
1930 * here because the last ref went.
1932 perf_event_disable(event);
1934 WARN_ON_ONCE(ctx->parent_ctx);
1936 * There are two ways this annotation is useful:
1938 * 1) there is a lock recursion from perf_event_exit_task
1939 * see the comment there.
1941 * 2) there is a lock-inversion with mmap_sem through
1942 * perf_event_read_group(), which takes faults while
1943 * holding ctx->mutex, however this is called after
1944 * the last filedesc died, so there is no possibility
1945 * to trigger the AB-BA case.
1947 mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
1948 raw_spin_lock_irq(&ctx->lock);
1949 perf_group_detach(event);
1950 list_del_event(event, ctx);
1951 raw_spin_unlock_irq(&ctx->lock);
1952 mutex_unlock(&ctx->mutex);
1954 mutex_lock(&event->owner->perf_event_mutex);
1955 list_del_init(&event->owner_entry);
1956 mutex_unlock(&event->owner->perf_event_mutex);
1957 put_task_struct(event->owner);
1959 free_event(event);
1961 return 0;
1963 EXPORT_SYMBOL_GPL(perf_event_release_kernel);
1966 * Called when the last reference to the file is gone.
1968 static int perf_release(struct inode *inode, struct file *file)
1970 struct perf_event *event = file->private_data;
1972 file->private_data = NULL;
1974 return perf_event_release_kernel(event);
1977 static int perf_event_read_size(struct perf_event *event)
1979 int entry = sizeof(u64); /* value */
1980 int size = 0;
1981 int nr = 1;
1983 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1984 size += sizeof(u64);
1986 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1987 size += sizeof(u64);
1989 if (event->attr.read_format & PERF_FORMAT_ID)
1990 entry += sizeof(u64);
1992 if (event->attr.read_format & PERF_FORMAT_GROUP) {
1993 nr += event->group_leader->nr_siblings;
1994 size += sizeof(u64);
1997 size += entry * nr;
1999 return size;
2002 u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
2004 struct perf_event *child;
2005 u64 total = 0;
2007 *enabled = 0;
2008 *running = 0;
2010 mutex_lock(&event->child_mutex);
2011 total += perf_event_read(event);
2012 *enabled += event->total_time_enabled +
2013 atomic64_read(&event->child_total_time_enabled);
2014 *running += event->total_time_running +
2015 atomic64_read(&event->child_total_time_running);
2017 list_for_each_entry(child, &event->child_list, child_list) {
2018 total += perf_event_read(child);
2019 *enabled += child->total_time_enabled;
2020 *running += child->total_time_running;
2022 mutex_unlock(&event->child_mutex);
2024 return total;
2026 EXPORT_SYMBOL_GPL(perf_event_read_value);
2028 static int perf_event_read_group(struct perf_event *event,
2029 u64 read_format, char __user *buf)
2031 struct perf_event *leader = event->group_leader, *sub;
2032 int n = 0, size = 0, ret = -EFAULT;
2033 struct perf_event_context *ctx = leader->ctx;
2034 u64 values[5];
2035 u64 count, enabled, running;
2037 mutex_lock(&ctx->mutex);
2038 count = perf_event_read_value(leader, &enabled, &running);
2040 values[n++] = 1 + leader->nr_siblings;
2041 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2042 values[n++] = enabled;
2043 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2044 values[n++] = running;
2045 values[n++] = count;
2046 if (read_format & PERF_FORMAT_ID)
2047 values[n++] = primary_event_id(leader);
2049 size = n * sizeof(u64);
2051 if (copy_to_user(buf, values, size))
2052 goto unlock;
2054 ret = size;
2056 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
2057 n = 0;
2059 values[n++] = perf_event_read_value(sub, &enabled, &running);
2060 if (read_format & PERF_FORMAT_ID)
2061 values[n++] = primary_event_id(sub);
2063 size = n * sizeof(u64);
2065 if (copy_to_user(buf + ret, values, size)) {
2066 ret = -EFAULT;
2067 goto unlock;
2070 ret += size;
2072 unlock:
2073 mutex_unlock(&ctx->mutex);
2075 return ret;
2078 static int perf_event_read_one(struct perf_event *event,
2079 u64 read_format, char __user *buf)
2081 u64 enabled, running;
2082 u64 values[4];
2083 int n = 0;
2085 values[n++] = perf_event_read_value(event, &enabled, &running);
2086 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2087 values[n++] = enabled;
2088 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2089 values[n++] = running;
2090 if (read_format & PERF_FORMAT_ID)
2091 values[n++] = primary_event_id(event);
2093 if (copy_to_user(buf, values, n * sizeof(u64)))
2094 return -EFAULT;
2096 return n * sizeof(u64);
2100 * Read the performance event - simple non blocking version for now
2102 static ssize_t
2103 perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
2105 u64 read_format = event->attr.read_format;
2106 int ret;
2109 * Return end-of-file for a read on a event that is in
2110 * error state (i.e. because it was pinned but it couldn't be
2111 * scheduled on to the CPU at some point).
2113 if (event->state == PERF_EVENT_STATE_ERROR)
2114 return 0;
2116 if (count < perf_event_read_size(event))
2117 return -ENOSPC;
2119 WARN_ON_ONCE(event->ctx->parent_ctx);
2120 if (read_format & PERF_FORMAT_GROUP)
2121 ret = perf_event_read_group(event, read_format, buf);
2122 else
2123 ret = perf_event_read_one(event, read_format, buf);
2125 return ret;
2128 static ssize_t
2129 perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
2131 struct perf_event *event = file->private_data;
2133 return perf_read_hw(event, buf, count);
2136 static unsigned int perf_poll(struct file *file, poll_table *wait)
2138 struct perf_event *event = file->private_data;
2139 struct perf_mmap_data *data;
2140 unsigned int events = POLL_HUP;
2142 rcu_read_lock();
2143 data = rcu_dereference(event->data);
2144 if (data)
2145 events = atomic_xchg(&data->poll, 0);
2146 rcu_read_unlock();
2148 poll_wait(file, &event->waitq, wait);
2150 return events;
2153 static void perf_event_reset(struct perf_event *event)
2155 (void)perf_event_read(event);
2156 atomic64_set(&event->count, 0);
2157 perf_event_update_userpage(event);
2161 * Holding the top-level event's child_mutex means that any
2162 * descendant process that has inherited this event will block
2163 * in sync_child_event if it goes to exit, thus satisfying the
2164 * task existence requirements of perf_event_enable/disable.
2166 static void perf_event_for_each_child(struct perf_event *event,
2167 void (*func)(struct perf_event *))
2169 struct perf_event *child;
2171 WARN_ON_ONCE(event->ctx->parent_ctx);
2172 mutex_lock(&event->child_mutex);
2173 func(event);
2174 list_for_each_entry(child, &event->child_list, child_list)
2175 func(child);
2176 mutex_unlock(&event->child_mutex);
2179 static void perf_event_for_each(struct perf_event *event,
2180 void (*func)(struct perf_event *))
2182 struct perf_event_context *ctx = event->ctx;
2183 struct perf_event *sibling;
2185 WARN_ON_ONCE(ctx->parent_ctx);
2186 mutex_lock(&ctx->mutex);
2187 event = event->group_leader;
2189 perf_event_for_each_child(event, func);
2190 func(event);
2191 list_for_each_entry(sibling, &event->sibling_list, group_entry)
2192 perf_event_for_each_child(event, func);
2193 mutex_unlock(&ctx->mutex);
2196 static int perf_event_period(struct perf_event *event, u64 __user *arg)
2198 struct perf_event_context *ctx = event->ctx;
2199 unsigned long size;
2200 int ret = 0;
2201 u64 value;
2203 if (!event->attr.sample_period)
2204 return -EINVAL;
2206 size = copy_from_user(&value, arg, sizeof(value));
2207 if (size != sizeof(value))
2208 return -EFAULT;
2210 if (!value)
2211 return -EINVAL;
2213 raw_spin_lock_irq(&ctx->lock);
2214 if (event->attr.freq) {
2215 if (value > sysctl_perf_event_sample_rate) {
2216 ret = -EINVAL;
2217 goto unlock;
2220 event->attr.sample_freq = value;
2221 } else {
2222 event->attr.sample_period = value;
2223 event->hw.sample_period = value;
2225 unlock:
2226 raw_spin_unlock_irq(&ctx->lock);
2228 return ret;
2231 static const struct file_operations perf_fops;
2233 static struct perf_event *perf_fget_light(int fd, int *fput_needed)
2235 struct file *file;
2237 file = fget_light(fd, fput_needed);
2238 if (!file)
2239 return ERR_PTR(-EBADF);
2241 if (file->f_op != &perf_fops) {
2242 fput_light(file, *fput_needed);
2243 *fput_needed = 0;
2244 return ERR_PTR(-EBADF);
2247 return file->private_data;
2250 static int perf_event_set_output(struct perf_event *event,
2251 struct perf_event *output_event);
2252 static int perf_event_set_filter(struct perf_event *event, void __user *arg);
2254 static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2256 struct perf_event *event = file->private_data;
2257 void (*func)(struct perf_event *);
2258 u32 flags = arg;
2260 switch (cmd) {
2261 case PERF_EVENT_IOC_ENABLE:
2262 func = perf_event_enable;
2263 break;
2264 case PERF_EVENT_IOC_DISABLE:
2265 func = perf_event_disable;
2266 break;
2267 case PERF_EVENT_IOC_RESET:
2268 func = perf_event_reset;
2269 break;
2271 case PERF_EVENT_IOC_REFRESH:
2272 return perf_event_refresh(event, arg);
2274 case PERF_EVENT_IOC_PERIOD:
2275 return perf_event_period(event, (u64 __user *)arg);
2277 case PERF_EVENT_IOC_SET_OUTPUT:
2279 struct perf_event *output_event = NULL;
2280 int fput_needed = 0;
2281 int ret;
2283 if (arg != -1) {
2284 output_event = perf_fget_light(arg, &fput_needed);
2285 if (IS_ERR(output_event))
2286 return PTR_ERR(output_event);
2289 ret = perf_event_set_output(event, output_event);
2290 if (output_event)
2291 fput_light(output_event->filp, fput_needed);
2293 return ret;
2296 case PERF_EVENT_IOC_SET_FILTER:
2297 return perf_event_set_filter(event, (void __user *)arg);
2299 default:
2300 return -ENOTTY;
2303 if (flags & PERF_IOC_FLAG_GROUP)
2304 perf_event_for_each(event, func);
2305 else
2306 perf_event_for_each_child(event, func);
2308 return 0;
2311 int perf_event_task_enable(void)
2313 struct perf_event *event;
2315 mutex_lock(&current->perf_event_mutex);
2316 list_for_each_entry(event, &current->perf_event_list, owner_entry)
2317 perf_event_for_each_child(event, perf_event_enable);
2318 mutex_unlock(&current->perf_event_mutex);
2320 return 0;
2323 int perf_event_task_disable(void)
2325 struct perf_event *event;
2327 mutex_lock(&current->perf_event_mutex);
2328 list_for_each_entry(event, &current->perf_event_list, owner_entry)
2329 perf_event_for_each_child(event, perf_event_disable);
2330 mutex_unlock(&current->perf_event_mutex);
2332 return 0;
2335 #ifndef PERF_EVENT_INDEX_OFFSET
2336 # define PERF_EVENT_INDEX_OFFSET 0
2337 #endif
2339 static int perf_event_index(struct perf_event *event)
2341 if (event->state != PERF_EVENT_STATE_ACTIVE)
2342 return 0;
2344 return event->hw.idx + 1 - PERF_EVENT_INDEX_OFFSET;
2348 * Callers need to ensure there can be no nesting of this function, otherwise
2349 * the seqlock logic goes bad. We can not serialize this because the arch
2350 * code calls this from NMI context.
2352 void perf_event_update_userpage(struct perf_event *event)
2354 struct perf_event_mmap_page *userpg;
2355 struct perf_mmap_data *data;
2357 rcu_read_lock();
2358 data = rcu_dereference(event->data);
2359 if (!data)
2360 goto unlock;
2362 userpg = data->user_page;
2365 * Disable preemption so as to not let the corresponding user-space
2366 * spin too long if we get preempted.
2368 preempt_disable();
2369 ++userpg->lock;
2370 barrier();
2371 userpg->index = perf_event_index(event);
2372 userpg->offset = atomic64_read(&event->count);
2373 if (event->state == PERF_EVENT_STATE_ACTIVE)
2374 userpg->offset -= atomic64_read(&event->hw.prev_count);
2376 userpg->time_enabled = event->total_time_enabled +
2377 atomic64_read(&event->child_total_time_enabled);
2379 userpg->time_running = event->total_time_running +
2380 atomic64_read(&event->child_total_time_running);
2382 barrier();
2383 ++userpg->lock;
2384 preempt_enable();
2385 unlock:
2386 rcu_read_unlock();
2389 #ifndef CONFIG_PERF_USE_VMALLOC
2392 * Back perf_mmap() with regular GFP_KERNEL-0 pages.
2395 static struct page *
2396 perf_mmap_to_page(struct perf_mmap_data *data, unsigned long pgoff)
2398 if (pgoff > data->nr_pages)
2399 return NULL;
2401 if (pgoff == 0)
2402 return virt_to_page(data->user_page);
2404 return virt_to_page(data->data_pages[pgoff - 1]);
2407 static void *perf_mmap_alloc_page(int cpu)
2409 struct page *page;
2410 int node;
2412 node = (cpu == -1) ? cpu : cpu_to_node(cpu);
2413 page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
2414 if (!page)
2415 return NULL;
2417 return page_address(page);
2420 static struct perf_mmap_data *
2421 perf_mmap_data_alloc(struct perf_event *event, int nr_pages)
2423 struct perf_mmap_data *data;
2424 unsigned long size;
2425 int i;
2427 size = sizeof(struct perf_mmap_data);
2428 size += nr_pages * sizeof(void *);
2430 data = kzalloc(size, GFP_KERNEL);
2431 if (!data)
2432 goto fail;
2434 data->user_page = perf_mmap_alloc_page(event->cpu);
2435 if (!data->user_page)
2436 goto fail_user_page;
2438 for (i = 0; i < nr_pages; i++) {
2439 data->data_pages[i] = perf_mmap_alloc_page(event->cpu);
2440 if (!data->data_pages[i])
2441 goto fail_data_pages;
2444 data->nr_pages = nr_pages;
2446 return data;
2448 fail_data_pages:
2449 for (i--; i >= 0; i--)
2450 free_page((unsigned long)data->data_pages[i]);
2452 free_page((unsigned long)data->user_page);
2454 fail_user_page:
2455 kfree(data);
2457 fail:
2458 return NULL;
2461 static void perf_mmap_free_page(unsigned long addr)
2463 struct page *page = virt_to_page((void *)addr);
2465 page->mapping = NULL;
2466 __free_page(page);
2469 static void perf_mmap_data_free(struct perf_mmap_data *data)
2471 int i;
2473 perf_mmap_free_page((unsigned long)data->user_page);
2474 for (i = 0; i < data->nr_pages; i++)
2475 perf_mmap_free_page((unsigned long)data->data_pages[i]);
2476 kfree(data);
2479 static inline int page_order(struct perf_mmap_data *data)
2481 return 0;
2484 #else
2487 * Back perf_mmap() with vmalloc memory.
2489 * Required for architectures that have d-cache aliasing issues.
2492 static inline int page_order(struct perf_mmap_data *data)
2494 return data->page_order;
2497 static struct page *
2498 perf_mmap_to_page(struct perf_mmap_data *data, unsigned long pgoff)
2500 if (pgoff > (1UL << page_order(data)))
2501 return NULL;
2503 return vmalloc_to_page((void *)data->user_page + pgoff * PAGE_SIZE);
2506 static void perf_mmap_unmark_page(void *addr)
2508 struct page *page = vmalloc_to_page(addr);
2510 page->mapping = NULL;
2513 static void perf_mmap_data_free_work(struct work_struct *work)
2515 struct perf_mmap_data *data;
2516 void *base;
2517 int i, nr;
2519 data = container_of(work, struct perf_mmap_data, work);
2520 nr = 1 << page_order(data);
2522 base = data->user_page;
2523 for (i = 0; i < nr + 1; i++)
2524 perf_mmap_unmark_page(base + (i * PAGE_SIZE));
2526 vfree(base);
2527 kfree(data);
2530 static void perf_mmap_data_free(struct perf_mmap_data *data)
2532 schedule_work(&data->work);
2535 static struct perf_mmap_data *
2536 perf_mmap_data_alloc(struct perf_event *event, int nr_pages)
2538 struct perf_mmap_data *data;
2539 unsigned long size;
2540 void *all_buf;
2542 size = sizeof(struct perf_mmap_data);
2543 size += sizeof(void *);
2545 data = kzalloc(size, GFP_KERNEL);
2546 if (!data)
2547 goto fail;
2549 INIT_WORK(&data->work, perf_mmap_data_free_work);
2551 all_buf = vmalloc_user((nr_pages + 1) * PAGE_SIZE);
2552 if (!all_buf)
2553 goto fail_all_buf;
2555 data->user_page = all_buf;
2556 data->data_pages[0] = all_buf + PAGE_SIZE;
2557 data->page_order = ilog2(nr_pages);
2558 data->nr_pages = 1;
2560 return data;
2562 fail_all_buf:
2563 kfree(data);
2565 fail:
2566 return NULL;
2569 #endif
2571 static unsigned long perf_data_size(struct perf_mmap_data *data)
2573 return data->nr_pages << (PAGE_SHIFT + page_order(data));
2576 static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
2578 struct perf_event *event = vma->vm_file->private_data;
2579 struct perf_mmap_data *data;
2580 int ret = VM_FAULT_SIGBUS;
2582 if (vmf->flags & FAULT_FLAG_MKWRITE) {
2583 if (vmf->pgoff == 0)
2584 ret = 0;
2585 return ret;
2588 rcu_read_lock();
2589 data = rcu_dereference(event->data);
2590 if (!data)
2591 goto unlock;
2593 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
2594 goto unlock;
2596 vmf->page = perf_mmap_to_page(data, vmf->pgoff);
2597 if (!vmf->page)
2598 goto unlock;
2600 get_page(vmf->page);
2601 vmf->page->mapping = vma->vm_file->f_mapping;
2602 vmf->page->index = vmf->pgoff;
2604 ret = 0;
2605 unlock:
2606 rcu_read_unlock();
2608 return ret;
2611 static void
2612 perf_mmap_data_init(struct perf_event *event, struct perf_mmap_data *data)
2614 long max_size = perf_data_size(data);
2616 if (event->attr.watermark) {
2617 data->watermark = min_t(long, max_size,
2618 event->attr.wakeup_watermark);
2621 if (!data->watermark)
2622 data->watermark = max_size / 2;
2624 atomic_set(&data->refcount, 1);
2625 rcu_assign_pointer(event->data, data);
2628 static void perf_mmap_data_free_rcu(struct rcu_head *rcu_head)
2630 struct perf_mmap_data *data;
2632 data = container_of(rcu_head, struct perf_mmap_data, rcu_head);
2633 perf_mmap_data_free(data);
2636 static struct perf_mmap_data *perf_mmap_data_get(struct perf_event *event)
2638 struct perf_mmap_data *data;
2640 rcu_read_lock();
2641 data = rcu_dereference(event->data);
2642 if (data) {
2643 if (!atomic_inc_not_zero(&data->refcount))
2644 data = NULL;
2646 rcu_read_unlock();
2648 return data;
2651 static void perf_mmap_data_put(struct perf_mmap_data *data)
2653 if (!atomic_dec_and_test(&data->refcount))
2654 return;
2656 call_rcu(&data->rcu_head, perf_mmap_data_free_rcu);
2659 static void perf_mmap_open(struct vm_area_struct *vma)
2661 struct perf_event *event = vma->vm_file->private_data;
2663 atomic_inc(&event->mmap_count);
2666 static void perf_mmap_close(struct vm_area_struct *vma)
2668 struct perf_event *event = vma->vm_file->private_data;
2670 if (atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) {
2671 unsigned long size = perf_data_size(event->data);
2672 struct user_struct *user = event->mmap_user;
2673 struct perf_mmap_data *data = event->data;
2675 atomic_long_sub((size >> PAGE_SHIFT) + 1, &user->locked_vm);
2676 vma->vm_mm->locked_vm -= event->mmap_locked;
2677 rcu_assign_pointer(event->data, NULL);
2678 mutex_unlock(&event->mmap_mutex);
2680 perf_mmap_data_put(data);
2681 free_uid(user);
2685 static const struct vm_operations_struct perf_mmap_vmops = {
2686 .open = perf_mmap_open,
2687 .close = perf_mmap_close,
2688 .fault = perf_mmap_fault,
2689 .page_mkwrite = perf_mmap_fault,
2692 static int perf_mmap(struct file *file, struct vm_area_struct *vma)
2694 struct perf_event *event = file->private_data;
2695 unsigned long user_locked, user_lock_limit;
2696 struct user_struct *user = current_user();
2697 unsigned long locked, lock_limit;
2698 struct perf_mmap_data *data;
2699 unsigned long vma_size;
2700 unsigned long nr_pages;
2701 long user_extra, extra;
2702 int ret = 0;
2705 * Don't allow mmap() of inherited per-task counters. This would
2706 * create a performance issue due to all children writing to the
2707 * same buffer.
2709 if (event->cpu == -1 && event->attr.inherit)
2710 return -EINVAL;
2712 if (!(vma->vm_flags & VM_SHARED))
2713 return -EINVAL;
2715 vma_size = vma->vm_end - vma->vm_start;
2716 nr_pages = (vma_size / PAGE_SIZE) - 1;
2719 * If we have data pages ensure they're a power-of-two number, so we
2720 * can do bitmasks instead of modulo.
2722 if (nr_pages != 0 && !is_power_of_2(nr_pages))
2723 return -EINVAL;
2725 if (vma_size != PAGE_SIZE * (1 + nr_pages))
2726 return -EINVAL;
2728 if (vma->vm_pgoff != 0)
2729 return -EINVAL;
2731 WARN_ON_ONCE(event->ctx->parent_ctx);
2732 mutex_lock(&event->mmap_mutex);
2733 if (event->data) {
2734 if (event->data->nr_pages == nr_pages)
2735 atomic_inc(&event->data->refcount);
2736 else
2737 ret = -EINVAL;
2738 goto unlock;
2741 user_extra = nr_pages + 1;
2742 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
2745 * Increase the limit linearly with more CPUs:
2747 user_lock_limit *= num_online_cpus();
2749 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
2751 extra = 0;
2752 if (user_locked > user_lock_limit)
2753 extra = user_locked - user_lock_limit;
2755 lock_limit = rlimit(RLIMIT_MEMLOCK);
2756 lock_limit >>= PAGE_SHIFT;
2757 locked = vma->vm_mm->locked_vm + extra;
2759 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
2760 !capable(CAP_IPC_LOCK)) {
2761 ret = -EPERM;
2762 goto unlock;
2765 WARN_ON(event->data);
2767 data = perf_mmap_data_alloc(event, nr_pages);
2768 if (!data) {
2769 ret = -ENOMEM;
2770 goto unlock;
2773 perf_mmap_data_init(event, data);
2774 if (vma->vm_flags & VM_WRITE)
2775 event->data->writable = 1;
2777 atomic_long_add(user_extra, &user->locked_vm);
2778 event->mmap_locked = extra;
2779 event->mmap_user = get_current_user();
2780 vma->vm_mm->locked_vm += event->mmap_locked;
2782 unlock:
2783 if (!ret)
2784 atomic_inc(&event->mmap_count);
2785 mutex_unlock(&event->mmap_mutex);
2787 vma->vm_flags |= VM_RESERVED;
2788 vma->vm_ops = &perf_mmap_vmops;
2790 return ret;
2793 static int perf_fasync(int fd, struct file *filp, int on)
2795 struct inode *inode = filp->f_path.dentry->d_inode;
2796 struct perf_event *event = filp->private_data;
2797 int retval;
2799 mutex_lock(&inode->i_mutex);
2800 retval = fasync_helper(fd, filp, on, &event->fasync);
2801 mutex_unlock(&inode->i_mutex);
2803 if (retval < 0)
2804 return retval;
2806 return 0;
2809 static const struct file_operations perf_fops = {
2810 .llseek = no_llseek,
2811 .release = perf_release,
2812 .read = perf_read,
2813 .poll = perf_poll,
2814 .unlocked_ioctl = perf_ioctl,
2815 .compat_ioctl = perf_ioctl,
2816 .mmap = perf_mmap,
2817 .fasync = perf_fasync,
2821 * Perf event wakeup
2823 * If there's data, ensure we set the poll() state and publish everything
2824 * to user-space before waking everybody up.
2827 void perf_event_wakeup(struct perf_event *event)
2829 wake_up_all(&event->waitq);
2831 if (event->pending_kill) {
2832 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
2833 event->pending_kill = 0;
2838 * Pending wakeups
2840 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
2842 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
2843 * single linked list and use cmpxchg() to add entries lockless.
2846 static void perf_pending_event(struct perf_pending_entry *entry)
2848 struct perf_event *event = container_of(entry,
2849 struct perf_event, pending);
2851 if (event->pending_disable) {
2852 event->pending_disable = 0;
2853 __perf_event_disable(event);
2856 if (event->pending_wakeup) {
2857 event->pending_wakeup = 0;
2858 perf_event_wakeup(event);
2862 #define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
2864 static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
2865 PENDING_TAIL,
2868 static void perf_pending_queue(struct perf_pending_entry *entry,
2869 void (*func)(struct perf_pending_entry *))
2871 struct perf_pending_entry **head;
2873 if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
2874 return;
2876 entry->func = func;
2878 head = &get_cpu_var(perf_pending_head);
2880 do {
2881 entry->next = *head;
2882 } while (cmpxchg(head, entry->next, entry) != entry->next);
2884 set_perf_event_pending();
2886 put_cpu_var(perf_pending_head);
2889 static int __perf_pending_run(void)
2891 struct perf_pending_entry *list;
2892 int nr = 0;
2894 list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
2895 while (list != PENDING_TAIL) {
2896 void (*func)(struct perf_pending_entry *);
2897 struct perf_pending_entry *entry = list;
2899 list = list->next;
2901 func = entry->func;
2902 entry->next = NULL;
2904 * Ensure we observe the unqueue before we issue the wakeup,
2905 * so that we won't be waiting forever.
2906 * -- see perf_not_pending().
2908 smp_wmb();
2910 func(entry);
2911 nr++;
2914 return nr;
2917 static inline int perf_not_pending(struct perf_event *event)
2920 * If we flush on whatever cpu we run, there is a chance we don't
2921 * need to wait.
2923 get_cpu();
2924 __perf_pending_run();
2925 put_cpu();
2928 * Ensure we see the proper queue state before going to sleep
2929 * so that we do not miss the wakeup. -- see perf_pending_handle()
2931 smp_rmb();
2932 return event->pending.next == NULL;
2935 static void perf_pending_sync(struct perf_event *event)
2937 wait_event(event->waitq, perf_not_pending(event));
2940 void perf_event_do_pending(void)
2942 __perf_pending_run();
2946 * Callchain support -- arch specific
2949 __weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
2951 return NULL;
2954 __weak
2955 void perf_arch_fetch_caller_regs(struct pt_regs *regs, unsigned long ip, int skip)
2961 * We assume there is only KVM supporting the callbacks.
2962 * Later on, we might change it to a list if there is
2963 * another virtualization implementation supporting the callbacks.
2965 struct perf_guest_info_callbacks *perf_guest_cbs;
2967 int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
2969 perf_guest_cbs = cbs;
2970 return 0;
2972 EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
2974 int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
2976 perf_guest_cbs = NULL;
2977 return 0;
2979 EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
2982 * Output
2984 static bool perf_output_space(struct perf_mmap_data *data, unsigned long tail,
2985 unsigned long offset, unsigned long head)
2987 unsigned long mask;
2989 if (!data->writable)
2990 return true;
2992 mask = perf_data_size(data) - 1;
2994 offset = (offset - tail) & mask;
2995 head = (head - tail) & mask;
2997 if ((int)(head - offset) < 0)
2998 return false;
3000 return true;
3003 static void perf_output_wakeup(struct perf_output_handle *handle)
3005 atomic_set(&handle->data->poll, POLL_IN);
3007 if (handle->nmi) {
3008 handle->event->pending_wakeup = 1;
3009 perf_pending_queue(&handle->event->pending,
3010 perf_pending_event);
3011 } else
3012 perf_event_wakeup(handle->event);
3016 * We need to ensure a later event_id doesn't publish a head when a former
3017 * event isn't done writing. However since we need to deal with NMIs we
3018 * cannot fully serialize things.
3020 * We only publish the head (and generate a wakeup) when the outer-most
3021 * event completes.
3023 static void perf_output_get_handle(struct perf_output_handle *handle)
3025 struct perf_mmap_data *data = handle->data;
3027 preempt_disable();
3028 local_inc(&data->nest);
3029 handle->wakeup = local_read(&data->wakeup);
3032 static void perf_output_put_handle(struct perf_output_handle *handle)
3034 struct perf_mmap_data *data = handle->data;
3035 unsigned long head;
3037 again:
3038 head = local_read(&data->head);
3041 * IRQ/NMI can happen here, which means we can miss a head update.
3044 if (!local_dec_and_test(&data->nest))
3045 goto out;
3048 * Publish the known good head. Rely on the full barrier implied
3049 * by atomic_dec_and_test() order the data->head read and this
3050 * write.
3052 data->user_page->data_head = head;
3055 * Now check if we missed an update, rely on the (compiler)
3056 * barrier in atomic_dec_and_test() to re-read data->head.
3058 if (unlikely(head != local_read(&data->head))) {
3059 local_inc(&data->nest);
3060 goto again;
3063 if (handle->wakeup != local_read(&data->wakeup))
3064 perf_output_wakeup(handle);
3066 out:
3067 preempt_enable();
3070 __always_inline void perf_output_copy(struct perf_output_handle *handle,
3071 const void *buf, unsigned int len)
3073 do {
3074 unsigned long size = min_t(unsigned long, handle->size, len);
3076 memcpy(handle->addr, buf, size);
3078 len -= size;
3079 handle->addr += size;
3080 buf += size;
3081 handle->size -= size;
3082 if (!handle->size) {
3083 struct perf_mmap_data *data = handle->data;
3085 handle->page++;
3086 handle->page &= data->nr_pages - 1;
3087 handle->addr = data->data_pages[handle->page];
3088 handle->size = PAGE_SIZE << page_order(data);
3090 } while (len);
3093 int perf_output_begin(struct perf_output_handle *handle,
3094 struct perf_event *event, unsigned int size,
3095 int nmi, int sample)
3097 struct perf_mmap_data *data;
3098 unsigned long tail, offset, head;
3099 int have_lost;
3100 struct {
3101 struct perf_event_header header;
3102 u64 id;
3103 u64 lost;
3104 } lost_event;
3106 rcu_read_lock();
3108 * For inherited events we send all the output towards the parent.
3110 if (event->parent)
3111 event = event->parent;
3113 data = rcu_dereference(event->data);
3114 if (!data)
3115 goto out;
3117 handle->data = data;
3118 handle->event = event;
3119 handle->nmi = nmi;
3120 handle->sample = sample;
3122 if (!data->nr_pages)
3123 goto out;
3125 have_lost = local_read(&data->lost);
3126 if (have_lost)
3127 size += sizeof(lost_event);
3129 perf_output_get_handle(handle);
3131 do {
3133 * Userspace could choose to issue a mb() before updating the
3134 * tail pointer. So that all reads will be completed before the
3135 * write is issued.
3137 tail = ACCESS_ONCE(data->user_page->data_tail);
3138 smp_rmb();
3139 offset = head = local_read(&data->head);
3140 head += size;
3141 if (unlikely(!perf_output_space(data, tail, offset, head)))
3142 goto fail;
3143 } while (local_cmpxchg(&data->head, offset, head) != offset);
3145 if (head - local_read(&data->wakeup) > data->watermark)
3146 local_add(data->watermark, &data->wakeup);
3148 handle->page = offset >> (PAGE_SHIFT + page_order(data));
3149 handle->page &= data->nr_pages - 1;
3150 handle->size = offset & ((PAGE_SIZE << page_order(data)) - 1);
3151 handle->addr = data->data_pages[handle->page];
3152 handle->addr += handle->size;
3153 handle->size = (PAGE_SIZE << page_order(data)) - handle->size;
3155 if (have_lost) {
3156 lost_event.header.type = PERF_RECORD_LOST;
3157 lost_event.header.misc = 0;
3158 lost_event.header.size = sizeof(lost_event);
3159 lost_event.id = event->id;
3160 lost_event.lost = local_xchg(&data->lost, 0);
3162 perf_output_put(handle, lost_event);
3165 return 0;
3167 fail:
3168 local_inc(&data->lost);
3169 perf_output_put_handle(handle);
3170 out:
3171 rcu_read_unlock();
3173 return -ENOSPC;
3176 void perf_output_end(struct perf_output_handle *handle)
3178 struct perf_event *event = handle->event;
3179 struct perf_mmap_data *data = handle->data;
3181 int wakeup_events = event->attr.wakeup_events;
3183 if (handle->sample && wakeup_events) {
3184 int events = local_inc_return(&data->events);
3185 if (events >= wakeup_events) {
3186 local_sub(wakeup_events, &data->events);
3187 local_inc(&data->wakeup);
3191 perf_output_put_handle(handle);
3192 rcu_read_unlock();
3195 static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
3198 * only top level events have the pid namespace they were created in
3200 if (event->parent)
3201 event = event->parent;
3203 return task_tgid_nr_ns(p, event->ns);
3206 static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
3209 * only top level events have the pid namespace they were created in
3211 if (event->parent)
3212 event = event->parent;
3214 return task_pid_nr_ns(p, event->ns);
3217 static void perf_output_read_one(struct perf_output_handle *handle,
3218 struct perf_event *event)
3220 u64 read_format = event->attr.read_format;
3221 u64 values[4];
3222 int n = 0;
3224 values[n++] = atomic64_read(&event->count);
3225 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
3226 values[n++] = event->total_time_enabled +
3227 atomic64_read(&event->child_total_time_enabled);
3229 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
3230 values[n++] = event->total_time_running +
3231 atomic64_read(&event->child_total_time_running);
3233 if (read_format & PERF_FORMAT_ID)
3234 values[n++] = primary_event_id(event);
3236 perf_output_copy(handle, values, n * sizeof(u64));
3240 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
3242 static void perf_output_read_group(struct perf_output_handle *handle,
3243 struct perf_event *event)
3245 struct perf_event *leader = event->group_leader, *sub;
3246 u64 read_format = event->attr.read_format;
3247 u64 values[5];
3248 int n = 0;
3250 values[n++] = 1 + leader->nr_siblings;
3252 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3253 values[n++] = leader->total_time_enabled;
3255 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3256 values[n++] = leader->total_time_running;
3258 if (leader != event)
3259 leader->pmu->read(leader);
3261 values[n++] = atomic64_read(&leader->count);
3262 if (read_format & PERF_FORMAT_ID)
3263 values[n++] = primary_event_id(leader);
3265 perf_output_copy(handle, values, n * sizeof(u64));
3267 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
3268 n = 0;
3270 if (sub != event)
3271 sub->pmu->read(sub);
3273 values[n++] = atomic64_read(&sub->count);
3274 if (read_format & PERF_FORMAT_ID)
3275 values[n++] = primary_event_id(sub);
3277 perf_output_copy(handle, values, n * sizeof(u64));
3281 static void perf_output_read(struct perf_output_handle *handle,
3282 struct perf_event *event)
3284 if (event->attr.read_format & PERF_FORMAT_GROUP)
3285 perf_output_read_group(handle, event);
3286 else
3287 perf_output_read_one(handle, event);
3290 void perf_output_sample(struct perf_output_handle *handle,
3291 struct perf_event_header *header,
3292 struct perf_sample_data *data,
3293 struct perf_event *event)
3295 u64 sample_type = data->type;
3297 perf_output_put(handle, *header);
3299 if (sample_type & PERF_SAMPLE_IP)
3300 perf_output_put(handle, data->ip);
3302 if (sample_type & PERF_SAMPLE_TID)
3303 perf_output_put(handle, data->tid_entry);
3305 if (sample_type & PERF_SAMPLE_TIME)
3306 perf_output_put(handle, data->time);
3308 if (sample_type & PERF_SAMPLE_ADDR)
3309 perf_output_put(handle, data->addr);
3311 if (sample_type & PERF_SAMPLE_ID)
3312 perf_output_put(handle, data->id);
3314 if (sample_type & PERF_SAMPLE_STREAM_ID)
3315 perf_output_put(handle, data->stream_id);
3317 if (sample_type & PERF_SAMPLE_CPU)
3318 perf_output_put(handle, data->cpu_entry);
3320 if (sample_type & PERF_SAMPLE_PERIOD)
3321 perf_output_put(handle, data->period);
3323 if (sample_type & PERF_SAMPLE_READ)
3324 perf_output_read(handle, event);
3326 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3327 if (data->callchain) {
3328 int size = 1;
3330 if (data->callchain)
3331 size += data->callchain->nr;
3333 size *= sizeof(u64);
3335 perf_output_copy(handle, data->callchain, size);
3336 } else {
3337 u64 nr = 0;
3338 perf_output_put(handle, nr);
3342 if (sample_type & PERF_SAMPLE_RAW) {
3343 if (data->raw) {
3344 perf_output_put(handle, data->raw->size);
3345 perf_output_copy(handle, data->raw->data,
3346 data->raw->size);
3347 } else {
3348 struct {
3349 u32 size;
3350 u32 data;
3351 } raw = {
3352 .size = sizeof(u32),
3353 .data = 0,
3355 perf_output_put(handle, raw);
3360 void perf_prepare_sample(struct perf_event_header *header,
3361 struct perf_sample_data *data,
3362 struct perf_event *event,
3363 struct pt_regs *regs)
3365 u64 sample_type = event->attr.sample_type;
3367 data->type = sample_type;
3369 header->type = PERF_RECORD_SAMPLE;
3370 header->size = sizeof(*header);
3372 header->misc = 0;
3373 header->misc |= perf_misc_flags(regs);
3375 if (sample_type & PERF_SAMPLE_IP) {
3376 data->ip = perf_instruction_pointer(regs);
3378 header->size += sizeof(data->ip);
3381 if (sample_type & PERF_SAMPLE_TID) {
3382 /* namespace issues */
3383 data->tid_entry.pid = perf_event_pid(event, current);
3384 data->tid_entry.tid = perf_event_tid(event, current);
3386 header->size += sizeof(data->tid_entry);
3389 if (sample_type & PERF_SAMPLE_TIME) {
3390 data->time = perf_clock();
3392 header->size += sizeof(data->time);
3395 if (sample_type & PERF_SAMPLE_ADDR)
3396 header->size += sizeof(data->addr);
3398 if (sample_type & PERF_SAMPLE_ID) {
3399 data->id = primary_event_id(event);
3401 header->size += sizeof(data->id);
3404 if (sample_type & PERF_SAMPLE_STREAM_ID) {
3405 data->stream_id = event->id;
3407 header->size += sizeof(data->stream_id);
3410 if (sample_type & PERF_SAMPLE_CPU) {
3411 data->cpu_entry.cpu = raw_smp_processor_id();
3412 data->cpu_entry.reserved = 0;
3414 header->size += sizeof(data->cpu_entry);
3417 if (sample_type & PERF_SAMPLE_PERIOD)
3418 header->size += sizeof(data->period);
3420 if (sample_type & PERF_SAMPLE_READ)
3421 header->size += perf_event_read_size(event);
3423 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3424 int size = 1;
3426 data->callchain = perf_callchain(regs);
3428 if (data->callchain)
3429 size += data->callchain->nr;
3431 header->size += size * sizeof(u64);
3434 if (sample_type & PERF_SAMPLE_RAW) {
3435 int size = sizeof(u32);
3437 if (data->raw)
3438 size += data->raw->size;
3439 else
3440 size += sizeof(u32);
3442 WARN_ON_ONCE(size & (sizeof(u64)-1));
3443 header->size += size;
3447 static void perf_event_output(struct perf_event *event, int nmi,
3448 struct perf_sample_data *data,
3449 struct pt_regs *regs)
3451 struct perf_output_handle handle;
3452 struct perf_event_header header;
3454 perf_prepare_sample(&header, data, event, regs);
3456 if (perf_output_begin(&handle, event, header.size, nmi, 1))
3457 return;
3459 perf_output_sample(&handle, &header, data, event);
3461 perf_output_end(&handle);
3465 * read event_id
3468 struct perf_read_event {
3469 struct perf_event_header header;
3471 u32 pid;
3472 u32 tid;
3475 static void
3476 perf_event_read_event(struct perf_event *event,
3477 struct task_struct *task)
3479 struct perf_output_handle handle;
3480 struct perf_read_event read_event = {
3481 .header = {
3482 .type = PERF_RECORD_READ,
3483 .misc = 0,
3484 .size = sizeof(read_event) + perf_event_read_size(event),
3486 .pid = perf_event_pid(event, task),
3487 .tid = perf_event_tid(event, task),
3489 int ret;
3491 ret = perf_output_begin(&handle, event, read_event.header.size, 0, 0);
3492 if (ret)
3493 return;
3495 perf_output_put(&handle, read_event);
3496 perf_output_read(&handle, event);
3498 perf_output_end(&handle);
3502 * task tracking -- fork/exit
3504 * enabled by: attr.comm | attr.mmap | attr.task
3507 struct perf_task_event {
3508 struct task_struct *task;
3509 struct perf_event_context *task_ctx;
3511 struct {
3512 struct perf_event_header header;
3514 u32 pid;
3515 u32 ppid;
3516 u32 tid;
3517 u32 ptid;
3518 u64 time;
3519 } event_id;
3522 static void perf_event_task_output(struct perf_event *event,
3523 struct perf_task_event *task_event)
3525 struct perf_output_handle handle;
3526 struct task_struct *task = task_event->task;
3527 int size, ret;
3529 size = task_event->event_id.header.size;
3530 ret = perf_output_begin(&handle, event, size, 0, 0);
3532 if (ret)
3533 return;
3535 task_event->event_id.pid = perf_event_pid(event, task);
3536 task_event->event_id.ppid = perf_event_pid(event, current);
3538 task_event->event_id.tid = perf_event_tid(event, task);
3539 task_event->event_id.ptid = perf_event_tid(event, current);
3541 perf_output_put(&handle, task_event->event_id);
3543 perf_output_end(&handle);
3546 static int perf_event_task_match(struct perf_event *event)
3548 if (event->state < PERF_EVENT_STATE_INACTIVE)
3549 return 0;
3551 if (event->cpu != -1 && event->cpu != smp_processor_id())
3552 return 0;
3554 if (event->attr.comm || event->attr.mmap || event->attr.task)
3555 return 1;
3557 return 0;
3560 static void perf_event_task_ctx(struct perf_event_context *ctx,
3561 struct perf_task_event *task_event)
3563 struct perf_event *event;
3565 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3566 if (perf_event_task_match(event))
3567 perf_event_task_output(event, task_event);
3571 static void perf_event_task_event(struct perf_task_event *task_event)
3573 struct perf_cpu_context *cpuctx;
3574 struct perf_event_context *ctx = task_event->task_ctx;
3576 rcu_read_lock();
3577 cpuctx = &get_cpu_var(perf_cpu_context);
3578 perf_event_task_ctx(&cpuctx->ctx, task_event);
3579 if (!ctx)
3580 ctx = rcu_dereference(current->perf_event_ctxp);
3581 if (ctx)
3582 perf_event_task_ctx(ctx, task_event);
3583 put_cpu_var(perf_cpu_context);
3584 rcu_read_unlock();
3587 static void perf_event_task(struct task_struct *task,
3588 struct perf_event_context *task_ctx,
3589 int new)
3591 struct perf_task_event task_event;
3593 if (!atomic_read(&nr_comm_events) &&
3594 !atomic_read(&nr_mmap_events) &&
3595 !atomic_read(&nr_task_events))
3596 return;
3598 task_event = (struct perf_task_event){
3599 .task = task,
3600 .task_ctx = task_ctx,
3601 .event_id = {
3602 .header = {
3603 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
3604 .misc = 0,
3605 .size = sizeof(task_event.event_id),
3607 /* .pid */
3608 /* .ppid */
3609 /* .tid */
3610 /* .ptid */
3611 .time = perf_clock(),
3615 perf_event_task_event(&task_event);
3618 void perf_event_fork(struct task_struct *task)
3620 perf_event_task(task, NULL, 1);
3624 * comm tracking
3627 struct perf_comm_event {
3628 struct task_struct *task;
3629 char *comm;
3630 int comm_size;
3632 struct {
3633 struct perf_event_header header;
3635 u32 pid;
3636 u32 tid;
3637 } event_id;
3640 static void perf_event_comm_output(struct perf_event *event,
3641 struct perf_comm_event *comm_event)
3643 struct perf_output_handle handle;
3644 int size = comm_event->event_id.header.size;
3645 int ret = perf_output_begin(&handle, event, size, 0, 0);
3647 if (ret)
3648 return;
3650 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
3651 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
3653 perf_output_put(&handle, comm_event->event_id);
3654 perf_output_copy(&handle, comm_event->comm,
3655 comm_event->comm_size);
3656 perf_output_end(&handle);
3659 static int perf_event_comm_match(struct perf_event *event)
3661 if (event->state < PERF_EVENT_STATE_INACTIVE)
3662 return 0;
3664 if (event->cpu != -1 && event->cpu != smp_processor_id())
3665 return 0;
3667 if (event->attr.comm)
3668 return 1;
3670 return 0;
3673 static void perf_event_comm_ctx(struct perf_event_context *ctx,
3674 struct perf_comm_event *comm_event)
3676 struct perf_event *event;
3678 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3679 if (perf_event_comm_match(event))
3680 perf_event_comm_output(event, comm_event);
3684 static void perf_event_comm_event(struct perf_comm_event *comm_event)
3686 struct perf_cpu_context *cpuctx;
3687 struct perf_event_context *ctx;
3688 unsigned int size;
3689 char comm[TASK_COMM_LEN];
3691 memset(comm, 0, sizeof(comm));
3692 strlcpy(comm, comm_event->task->comm, sizeof(comm));
3693 size = ALIGN(strlen(comm)+1, sizeof(u64));
3695 comm_event->comm = comm;
3696 comm_event->comm_size = size;
3698 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
3700 rcu_read_lock();
3701 cpuctx = &get_cpu_var(perf_cpu_context);
3702 perf_event_comm_ctx(&cpuctx->ctx, comm_event);
3703 ctx = rcu_dereference(current->perf_event_ctxp);
3704 if (ctx)
3705 perf_event_comm_ctx(ctx, comm_event);
3706 put_cpu_var(perf_cpu_context);
3707 rcu_read_unlock();
3710 void perf_event_comm(struct task_struct *task)
3712 struct perf_comm_event comm_event;
3714 if (task->perf_event_ctxp)
3715 perf_event_enable_on_exec(task);
3717 if (!atomic_read(&nr_comm_events))
3718 return;
3720 comm_event = (struct perf_comm_event){
3721 .task = task,
3722 /* .comm */
3723 /* .comm_size */
3724 .event_id = {
3725 .header = {
3726 .type = PERF_RECORD_COMM,
3727 .misc = 0,
3728 /* .size */
3730 /* .pid */
3731 /* .tid */
3735 perf_event_comm_event(&comm_event);
3739 * mmap tracking
3742 struct perf_mmap_event {
3743 struct vm_area_struct *vma;
3745 const char *file_name;
3746 int file_size;
3748 struct {
3749 struct perf_event_header header;
3751 u32 pid;
3752 u32 tid;
3753 u64 start;
3754 u64 len;
3755 u64 pgoff;
3756 } event_id;
3759 static void perf_event_mmap_output(struct perf_event *event,
3760 struct perf_mmap_event *mmap_event)
3762 struct perf_output_handle handle;
3763 int size = mmap_event->event_id.header.size;
3764 int ret = perf_output_begin(&handle, event, size, 0, 0);
3766 if (ret)
3767 return;
3769 mmap_event->event_id.pid = perf_event_pid(event, current);
3770 mmap_event->event_id.tid = perf_event_tid(event, current);
3772 perf_output_put(&handle, mmap_event->event_id);
3773 perf_output_copy(&handle, mmap_event->file_name,
3774 mmap_event->file_size);
3775 perf_output_end(&handle);
3778 static int perf_event_mmap_match(struct perf_event *event,
3779 struct perf_mmap_event *mmap_event)
3781 if (event->state < PERF_EVENT_STATE_INACTIVE)
3782 return 0;
3784 if (event->cpu != -1 && event->cpu != smp_processor_id())
3785 return 0;
3787 if (event->attr.mmap)
3788 return 1;
3790 return 0;
3793 static void perf_event_mmap_ctx(struct perf_event_context *ctx,
3794 struct perf_mmap_event *mmap_event)
3796 struct perf_event *event;
3798 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3799 if (perf_event_mmap_match(event, mmap_event))
3800 perf_event_mmap_output(event, mmap_event);
3804 static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
3806 struct perf_cpu_context *cpuctx;
3807 struct perf_event_context *ctx;
3808 struct vm_area_struct *vma = mmap_event->vma;
3809 struct file *file = vma->vm_file;
3810 unsigned int size;
3811 char tmp[16];
3812 char *buf = NULL;
3813 const char *name;
3815 memset(tmp, 0, sizeof(tmp));
3817 if (file) {
3819 * d_path works from the end of the buffer backwards, so we
3820 * need to add enough zero bytes after the string to handle
3821 * the 64bit alignment we do later.
3823 buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
3824 if (!buf) {
3825 name = strncpy(tmp, "//enomem", sizeof(tmp));
3826 goto got_name;
3828 name = d_path(&file->f_path, buf, PATH_MAX);
3829 if (IS_ERR(name)) {
3830 name = strncpy(tmp, "//toolong", sizeof(tmp));
3831 goto got_name;
3833 } else {
3834 if (arch_vma_name(mmap_event->vma)) {
3835 name = strncpy(tmp, arch_vma_name(mmap_event->vma),
3836 sizeof(tmp));
3837 goto got_name;
3840 if (!vma->vm_mm) {
3841 name = strncpy(tmp, "[vdso]", sizeof(tmp));
3842 goto got_name;
3845 name = strncpy(tmp, "//anon", sizeof(tmp));
3846 goto got_name;
3849 got_name:
3850 size = ALIGN(strlen(name)+1, sizeof(u64));
3852 mmap_event->file_name = name;
3853 mmap_event->file_size = size;
3855 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
3857 rcu_read_lock();
3858 cpuctx = &get_cpu_var(perf_cpu_context);
3859 perf_event_mmap_ctx(&cpuctx->ctx, mmap_event);
3860 ctx = rcu_dereference(current->perf_event_ctxp);
3861 if (ctx)
3862 perf_event_mmap_ctx(ctx, mmap_event);
3863 put_cpu_var(perf_cpu_context);
3864 rcu_read_unlock();
3866 kfree(buf);
3869 void __perf_event_mmap(struct vm_area_struct *vma)
3871 struct perf_mmap_event mmap_event;
3873 if (!atomic_read(&nr_mmap_events))
3874 return;
3876 mmap_event = (struct perf_mmap_event){
3877 .vma = vma,
3878 /* .file_name */
3879 /* .file_size */
3880 .event_id = {
3881 .header = {
3882 .type = PERF_RECORD_MMAP,
3883 .misc = PERF_RECORD_MISC_USER,
3884 /* .size */
3886 /* .pid */
3887 /* .tid */
3888 .start = vma->vm_start,
3889 .len = vma->vm_end - vma->vm_start,
3890 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
3894 perf_event_mmap_event(&mmap_event);
3898 * IRQ throttle logging
3901 static void perf_log_throttle(struct perf_event *event, int enable)
3903 struct perf_output_handle handle;
3904 int ret;
3906 struct {
3907 struct perf_event_header header;
3908 u64 time;
3909 u64 id;
3910 u64 stream_id;
3911 } throttle_event = {
3912 .header = {
3913 .type = PERF_RECORD_THROTTLE,
3914 .misc = 0,
3915 .size = sizeof(throttle_event),
3917 .time = perf_clock(),
3918 .id = primary_event_id(event),
3919 .stream_id = event->id,
3922 if (enable)
3923 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
3925 ret = perf_output_begin(&handle, event, sizeof(throttle_event), 1, 0);
3926 if (ret)
3927 return;
3929 perf_output_put(&handle, throttle_event);
3930 perf_output_end(&handle);
3934 * Generic event overflow handling, sampling.
3937 static int __perf_event_overflow(struct perf_event *event, int nmi,
3938 int throttle, struct perf_sample_data *data,
3939 struct pt_regs *regs)
3941 int events = atomic_read(&event->event_limit);
3942 struct hw_perf_event *hwc = &event->hw;
3943 int ret = 0;
3945 throttle = (throttle && event->pmu->unthrottle != NULL);
3947 if (!throttle) {
3948 hwc->interrupts++;
3949 } else {
3950 if (hwc->interrupts != MAX_INTERRUPTS) {
3951 hwc->interrupts++;
3952 if (HZ * hwc->interrupts >
3953 (u64)sysctl_perf_event_sample_rate) {
3954 hwc->interrupts = MAX_INTERRUPTS;
3955 perf_log_throttle(event, 0);
3956 ret = 1;
3958 } else {
3960 * Keep re-disabling events even though on the previous
3961 * pass we disabled it - just in case we raced with a
3962 * sched-in and the event got enabled again:
3964 ret = 1;
3968 if (event->attr.freq) {
3969 u64 now = perf_clock();
3970 s64 delta = now - hwc->freq_time_stamp;
3972 hwc->freq_time_stamp = now;
3974 if (delta > 0 && delta < 2*TICK_NSEC)
3975 perf_adjust_period(event, delta, hwc->last_period);
3979 * XXX event_limit might not quite work as expected on inherited
3980 * events
3983 event->pending_kill = POLL_IN;
3984 if (events && atomic_dec_and_test(&event->event_limit)) {
3985 ret = 1;
3986 event->pending_kill = POLL_HUP;
3987 if (nmi) {
3988 event->pending_disable = 1;
3989 perf_pending_queue(&event->pending,
3990 perf_pending_event);
3991 } else
3992 perf_event_disable(event);
3995 if (event->overflow_handler)
3996 event->overflow_handler(event, nmi, data, regs);
3997 else
3998 perf_event_output(event, nmi, data, regs);
4000 return ret;
4003 int perf_event_overflow(struct perf_event *event, int nmi,
4004 struct perf_sample_data *data,
4005 struct pt_regs *regs)
4007 return __perf_event_overflow(event, nmi, 1, data, regs);
4011 * Generic software event infrastructure
4015 * We directly increment event->count and keep a second value in
4016 * event->hw.period_left to count intervals. This period event
4017 * is kept in the range [-sample_period, 0] so that we can use the
4018 * sign as trigger.
4021 static u64 perf_swevent_set_period(struct perf_event *event)
4023 struct hw_perf_event *hwc = &event->hw;
4024 u64 period = hwc->last_period;
4025 u64 nr, offset;
4026 s64 old, val;
4028 hwc->last_period = hwc->sample_period;
4030 again:
4031 old = val = atomic64_read(&hwc->period_left);
4032 if (val < 0)
4033 return 0;
4035 nr = div64_u64(period + val, period);
4036 offset = nr * period;
4037 val -= offset;
4038 if (atomic64_cmpxchg(&hwc->period_left, old, val) != old)
4039 goto again;
4041 return nr;
4044 static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
4045 int nmi, struct perf_sample_data *data,
4046 struct pt_regs *regs)
4048 struct hw_perf_event *hwc = &event->hw;
4049 int throttle = 0;
4051 data->period = event->hw.last_period;
4052 if (!overflow)
4053 overflow = perf_swevent_set_period(event);
4055 if (hwc->interrupts == MAX_INTERRUPTS)
4056 return;
4058 for (; overflow; overflow--) {
4059 if (__perf_event_overflow(event, nmi, throttle,
4060 data, regs)) {
4062 * We inhibit the overflow from happening when
4063 * hwc->interrupts == MAX_INTERRUPTS.
4065 break;
4067 throttle = 1;
4071 static void perf_swevent_add(struct perf_event *event, u64 nr,
4072 int nmi, struct perf_sample_data *data,
4073 struct pt_regs *regs)
4075 struct hw_perf_event *hwc = &event->hw;
4077 atomic64_add(nr, &event->count);
4079 if (!regs)
4080 return;
4082 if (!hwc->sample_period)
4083 return;
4085 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
4086 return perf_swevent_overflow(event, 1, nmi, data, regs);
4088 if (atomic64_add_negative(nr, &hwc->period_left))
4089 return;
4091 perf_swevent_overflow(event, 0, nmi, data, regs);
4094 static int perf_exclude_event(struct perf_event *event,
4095 struct pt_regs *regs)
4097 if (regs) {
4098 if (event->attr.exclude_user && user_mode(regs))
4099 return 1;
4101 if (event->attr.exclude_kernel && !user_mode(regs))
4102 return 1;
4105 return 0;
4108 static int perf_swevent_match(struct perf_event *event,
4109 enum perf_type_id type,
4110 u32 event_id,
4111 struct perf_sample_data *data,
4112 struct pt_regs *regs)
4114 if (event->attr.type != type)
4115 return 0;
4117 if (event->attr.config != event_id)
4118 return 0;
4120 if (perf_exclude_event(event, regs))
4121 return 0;
4123 return 1;
4126 static inline u64 swevent_hash(u64 type, u32 event_id)
4128 u64 val = event_id | (type << 32);
4130 return hash_64(val, SWEVENT_HLIST_BITS);
4133 static inline struct hlist_head *
4134 __find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
4136 u64 hash = swevent_hash(type, event_id);
4138 return &hlist->heads[hash];
4141 /* For the read side: events when they trigger */
4142 static inline struct hlist_head *
4143 find_swevent_head_rcu(struct perf_cpu_context *ctx, u64 type, u32 event_id)
4145 struct swevent_hlist *hlist;
4147 hlist = rcu_dereference(ctx->swevent_hlist);
4148 if (!hlist)
4149 return NULL;
4151 return __find_swevent_head(hlist, type, event_id);
4154 /* For the event head insertion and removal in the hlist */
4155 static inline struct hlist_head *
4156 find_swevent_head(struct perf_cpu_context *ctx, struct perf_event *event)
4158 struct swevent_hlist *hlist;
4159 u32 event_id = event->attr.config;
4160 u64 type = event->attr.type;
4163 * Event scheduling is always serialized against hlist allocation
4164 * and release. Which makes the protected version suitable here.
4165 * The context lock guarantees that.
4167 hlist = rcu_dereference_protected(ctx->swevent_hlist,
4168 lockdep_is_held(&event->ctx->lock));
4169 if (!hlist)
4170 return NULL;
4172 return __find_swevent_head(hlist, type, event_id);
4175 static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
4176 u64 nr, int nmi,
4177 struct perf_sample_data *data,
4178 struct pt_regs *regs)
4180 struct perf_cpu_context *cpuctx;
4181 struct perf_event *event;
4182 struct hlist_node *node;
4183 struct hlist_head *head;
4185 cpuctx = &__get_cpu_var(perf_cpu_context);
4187 rcu_read_lock();
4189 head = find_swevent_head_rcu(cpuctx, type, event_id);
4191 if (!head)
4192 goto end;
4194 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
4195 if (perf_swevent_match(event, type, event_id, data, regs))
4196 perf_swevent_add(event, nr, nmi, data, regs);
4198 end:
4199 rcu_read_unlock();
4202 int perf_swevent_get_recursion_context(void)
4204 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
4205 int rctx;
4207 if (in_nmi())
4208 rctx = 3;
4209 else if (in_irq())
4210 rctx = 2;
4211 else if (in_softirq())
4212 rctx = 1;
4213 else
4214 rctx = 0;
4216 if (cpuctx->recursion[rctx])
4217 return -1;
4219 cpuctx->recursion[rctx]++;
4220 barrier();
4222 return rctx;
4224 EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
4226 void perf_swevent_put_recursion_context(int rctx)
4228 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
4229 barrier();
4230 cpuctx->recursion[rctx]--;
4232 EXPORT_SYMBOL_GPL(perf_swevent_put_recursion_context);
4235 void __perf_sw_event(u32 event_id, u64 nr, int nmi,
4236 struct pt_regs *regs, u64 addr)
4238 struct perf_sample_data data;
4239 int rctx;
4241 preempt_disable_notrace();
4242 rctx = perf_swevent_get_recursion_context();
4243 if (rctx < 0)
4244 return;
4246 perf_sample_data_init(&data, addr);
4248 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, nmi, &data, regs);
4250 perf_swevent_put_recursion_context(rctx);
4251 preempt_enable_notrace();
4254 static void perf_swevent_read(struct perf_event *event)
4258 static int perf_swevent_enable(struct perf_event *event)
4260 struct hw_perf_event *hwc = &event->hw;
4261 struct perf_cpu_context *cpuctx;
4262 struct hlist_head *head;
4264 cpuctx = &__get_cpu_var(perf_cpu_context);
4266 if (hwc->sample_period) {
4267 hwc->last_period = hwc->sample_period;
4268 perf_swevent_set_period(event);
4271 head = find_swevent_head(cpuctx, event);
4272 if (WARN_ON_ONCE(!head))
4273 return -EINVAL;
4275 hlist_add_head_rcu(&event->hlist_entry, head);
4277 return 0;
4280 static void perf_swevent_disable(struct perf_event *event)
4282 hlist_del_rcu(&event->hlist_entry);
4285 static void perf_swevent_void(struct perf_event *event)
4289 static int perf_swevent_int(struct perf_event *event)
4291 return 0;
4294 static const struct pmu perf_ops_generic = {
4295 .enable = perf_swevent_enable,
4296 .disable = perf_swevent_disable,
4297 .start = perf_swevent_int,
4298 .stop = perf_swevent_void,
4299 .read = perf_swevent_read,
4300 .unthrottle = perf_swevent_void, /* hwc->interrupts already reset */
4304 * hrtimer based swevent callback
4307 static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
4309 enum hrtimer_restart ret = HRTIMER_RESTART;
4310 struct perf_sample_data data;
4311 struct pt_regs *regs;
4312 struct perf_event *event;
4313 u64 period;
4315 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
4316 event->pmu->read(event);
4318 perf_sample_data_init(&data, 0);
4319 data.period = event->hw.last_period;
4320 regs = get_irq_regs();
4322 if (regs && !perf_exclude_event(event, regs)) {
4323 if (!(event->attr.exclude_idle && current->pid == 0))
4324 if (perf_event_overflow(event, 0, &data, regs))
4325 ret = HRTIMER_NORESTART;
4328 period = max_t(u64, 10000, event->hw.sample_period);
4329 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
4331 return ret;
4334 static void perf_swevent_start_hrtimer(struct perf_event *event)
4336 struct hw_perf_event *hwc = &event->hw;
4338 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
4339 hwc->hrtimer.function = perf_swevent_hrtimer;
4340 if (hwc->sample_period) {
4341 u64 period;
4343 if (hwc->remaining) {
4344 if (hwc->remaining < 0)
4345 period = 10000;
4346 else
4347 period = hwc->remaining;
4348 hwc->remaining = 0;
4349 } else {
4350 period = max_t(u64, 10000, hwc->sample_period);
4352 __hrtimer_start_range_ns(&hwc->hrtimer,
4353 ns_to_ktime(period), 0,
4354 HRTIMER_MODE_REL, 0);
4358 static void perf_swevent_cancel_hrtimer(struct perf_event *event)
4360 struct hw_perf_event *hwc = &event->hw;
4362 if (hwc->sample_period) {
4363 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
4364 hwc->remaining = ktime_to_ns(remaining);
4366 hrtimer_cancel(&hwc->hrtimer);
4371 * Software event: cpu wall time clock
4374 static void cpu_clock_perf_event_update(struct perf_event *event)
4376 int cpu = raw_smp_processor_id();
4377 s64 prev;
4378 u64 now;
4380 now = cpu_clock(cpu);
4381 prev = atomic64_xchg(&event->hw.prev_count, now);
4382 atomic64_add(now - prev, &event->count);
4385 static int cpu_clock_perf_event_enable(struct perf_event *event)
4387 struct hw_perf_event *hwc = &event->hw;
4388 int cpu = raw_smp_processor_id();
4390 atomic64_set(&hwc->prev_count, cpu_clock(cpu));
4391 perf_swevent_start_hrtimer(event);
4393 return 0;
4396 static void cpu_clock_perf_event_disable(struct perf_event *event)
4398 perf_swevent_cancel_hrtimer(event);
4399 cpu_clock_perf_event_update(event);
4402 static void cpu_clock_perf_event_read(struct perf_event *event)
4404 cpu_clock_perf_event_update(event);
4407 static const struct pmu perf_ops_cpu_clock = {
4408 .enable = cpu_clock_perf_event_enable,
4409 .disable = cpu_clock_perf_event_disable,
4410 .read = cpu_clock_perf_event_read,
4414 * Software event: task time clock
4417 static void task_clock_perf_event_update(struct perf_event *event, u64 now)
4419 u64 prev;
4420 s64 delta;
4422 prev = atomic64_xchg(&event->hw.prev_count, now);
4423 delta = now - prev;
4424 atomic64_add(delta, &event->count);
4427 static int task_clock_perf_event_enable(struct perf_event *event)
4429 struct hw_perf_event *hwc = &event->hw;
4430 u64 now;
4432 now = event->ctx->time;
4434 atomic64_set(&hwc->prev_count, now);
4436 perf_swevent_start_hrtimer(event);
4438 return 0;
4441 static void task_clock_perf_event_disable(struct perf_event *event)
4443 perf_swevent_cancel_hrtimer(event);
4444 task_clock_perf_event_update(event, event->ctx->time);
4448 static void task_clock_perf_event_read(struct perf_event *event)
4450 u64 time;
4452 if (!in_nmi()) {
4453 update_context_time(event->ctx);
4454 time = event->ctx->time;
4455 } else {
4456 u64 now = perf_clock();
4457 u64 delta = now - event->ctx->timestamp;
4458 time = event->ctx->time + delta;
4461 task_clock_perf_event_update(event, time);
4464 static const struct pmu perf_ops_task_clock = {
4465 .enable = task_clock_perf_event_enable,
4466 .disable = task_clock_perf_event_disable,
4467 .read = task_clock_perf_event_read,
4470 /* Deref the hlist from the update side */
4471 static inline struct swevent_hlist *
4472 swevent_hlist_deref(struct perf_cpu_context *cpuctx)
4474 return rcu_dereference_protected(cpuctx->swevent_hlist,
4475 lockdep_is_held(&cpuctx->hlist_mutex));
4478 static void swevent_hlist_release_rcu(struct rcu_head *rcu_head)
4480 struct swevent_hlist *hlist;
4482 hlist = container_of(rcu_head, struct swevent_hlist, rcu_head);
4483 kfree(hlist);
4486 static void swevent_hlist_release(struct perf_cpu_context *cpuctx)
4488 struct swevent_hlist *hlist = swevent_hlist_deref(cpuctx);
4490 if (!hlist)
4491 return;
4493 rcu_assign_pointer(cpuctx->swevent_hlist, NULL);
4494 call_rcu(&hlist->rcu_head, swevent_hlist_release_rcu);
4497 static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
4499 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
4501 mutex_lock(&cpuctx->hlist_mutex);
4503 if (!--cpuctx->hlist_refcount)
4504 swevent_hlist_release(cpuctx);
4506 mutex_unlock(&cpuctx->hlist_mutex);
4509 static void swevent_hlist_put(struct perf_event *event)
4511 int cpu;
4513 if (event->cpu != -1) {
4514 swevent_hlist_put_cpu(event, event->cpu);
4515 return;
4518 for_each_possible_cpu(cpu)
4519 swevent_hlist_put_cpu(event, cpu);
4522 static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
4524 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
4525 int err = 0;
4527 mutex_lock(&cpuctx->hlist_mutex);
4529 if (!swevent_hlist_deref(cpuctx) && cpu_online(cpu)) {
4530 struct swevent_hlist *hlist;
4532 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
4533 if (!hlist) {
4534 err = -ENOMEM;
4535 goto exit;
4537 rcu_assign_pointer(cpuctx->swevent_hlist, hlist);
4539 cpuctx->hlist_refcount++;
4540 exit:
4541 mutex_unlock(&cpuctx->hlist_mutex);
4543 return err;
4546 static int swevent_hlist_get(struct perf_event *event)
4548 int err;
4549 int cpu, failed_cpu;
4551 if (event->cpu != -1)
4552 return swevent_hlist_get_cpu(event, event->cpu);
4554 get_online_cpus();
4555 for_each_possible_cpu(cpu) {
4556 err = swevent_hlist_get_cpu(event, cpu);
4557 if (err) {
4558 failed_cpu = cpu;
4559 goto fail;
4562 put_online_cpus();
4564 return 0;
4565 fail:
4566 for_each_possible_cpu(cpu) {
4567 if (cpu == failed_cpu)
4568 break;
4569 swevent_hlist_put_cpu(event, cpu);
4572 put_online_cpus();
4573 return err;
4576 #ifdef CONFIG_EVENT_TRACING
4578 static const struct pmu perf_ops_tracepoint = {
4579 .enable = perf_trace_enable,
4580 .disable = perf_trace_disable,
4581 .start = perf_swevent_int,
4582 .stop = perf_swevent_void,
4583 .read = perf_swevent_read,
4584 .unthrottle = perf_swevent_void,
4587 static int perf_tp_filter_match(struct perf_event *event,
4588 struct perf_sample_data *data)
4590 void *record = data->raw->data;
4592 if (likely(!event->filter) || filter_match_preds(event->filter, record))
4593 return 1;
4594 return 0;
4597 static int perf_tp_event_match(struct perf_event *event,
4598 struct perf_sample_data *data,
4599 struct pt_regs *regs)
4602 * All tracepoints are from kernel-space.
4604 if (event->attr.exclude_kernel)
4605 return 0;
4607 if (!perf_tp_filter_match(event, data))
4608 return 0;
4610 return 1;
4613 void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
4614 struct pt_regs *regs, struct hlist_head *head)
4616 struct perf_sample_data data;
4617 struct perf_event *event;
4618 struct hlist_node *node;
4620 struct perf_raw_record raw = {
4621 .size = entry_size,
4622 .data = record,
4625 perf_sample_data_init(&data, addr);
4626 data.raw = &raw;
4628 rcu_read_lock();
4629 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
4630 if (perf_tp_event_match(event, &data, regs))
4631 perf_swevent_add(event, count, 1, &data, regs);
4633 rcu_read_unlock();
4635 EXPORT_SYMBOL_GPL(perf_tp_event);
4637 static void tp_perf_event_destroy(struct perf_event *event)
4639 perf_trace_destroy(event);
4642 static const struct pmu *tp_perf_event_init(struct perf_event *event)
4644 int err;
4647 * Raw tracepoint data is a severe data leak, only allow root to
4648 * have these.
4650 if ((event->attr.sample_type & PERF_SAMPLE_RAW) &&
4651 perf_paranoid_tracepoint_raw() &&
4652 !capable(CAP_SYS_ADMIN))
4653 return ERR_PTR(-EPERM);
4655 err = perf_trace_init(event);
4656 if (err)
4657 return NULL;
4659 event->destroy = tp_perf_event_destroy;
4661 return &perf_ops_tracepoint;
4664 static int perf_event_set_filter(struct perf_event *event, void __user *arg)
4666 char *filter_str;
4667 int ret;
4669 if (event->attr.type != PERF_TYPE_TRACEPOINT)
4670 return -EINVAL;
4672 filter_str = strndup_user(arg, PAGE_SIZE);
4673 if (IS_ERR(filter_str))
4674 return PTR_ERR(filter_str);
4676 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
4678 kfree(filter_str);
4679 return ret;
4682 static void perf_event_free_filter(struct perf_event *event)
4684 ftrace_profile_free_filter(event);
4687 #else
4689 static const struct pmu *tp_perf_event_init(struct perf_event *event)
4691 return NULL;
4694 static int perf_event_set_filter(struct perf_event *event, void __user *arg)
4696 return -ENOENT;
4699 static void perf_event_free_filter(struct perf_event *event)
4703 #endif /* CONFIG_EVENT_TRACING */
4705 #ifdef CONFIG_HAVE_HW_BREAKPOINT
4706 static void bp_perf_event_destroy(struct perf_event *event)
4708 release_bp_slot(event);
4711 static const struct pmu *bp_perf_event_init(struct perf_event *bp)
4713 int err;
4715 err = register_perf_hw_breakpoint(bp);
4716 if (err)
4717 return ERR_PTR(err);
4719 bp->destroy = bp_perf_event_destroy;
4721 return &perf_ops_bp;
4724 void perf_bp_event(struct perf_event *bp, void *data)
4726 struct perf_sample_data sample;
4727 struct pt_regs *regs = data;
4729 perf_sample_data_init(&sample, bp->attr.bp_addr);
4731 if (!perf_exclude_event(bp, regs))
4732 perf_swevent_add(bp, 1, 1, &sample, regs);
4734 #else
4735 static const struct pmu *bp_perf_event_init(struct perf_event *bp)
4737 return NULL;
4740 void perf_bp_event(struct perf_event *bp, void *regs)
4743 #endif
4745 atomic_t perf_swevent_enabled[PERF_COUNT_SW_MAX];
4747 static void sw_perf_event_destroy(struct perf_event *event)
4749 u64 event_id = event->attr.config;
4751 WARN_ON(event->parent);
4753 atomic_dec(&perf_swevent_enabled[event_id]);
4754 swevent_hlist_put(event);
4757 static const struct pmu *sw_perf_event_init(struct perf_event *event)
4759 const struct pmu *pmu = NULL;
4760 u64 event_id = event->attr.config;
4763 * Software events (currently) can't in general distinguish
4764 * between user, kernel and hypervisor events.
4765 * However, context switches and cpu migrations are considered
4766 * to be kernel events, and page faults are never hypervisor
4767 * events.
4769 switch (event_id) {
4770 case PERF_COUNT_SW_CPU_CLOCK:
4771 pmu = &perf_ops_cpu_clock;
4773 break;
4774 case PERF_COUNT_SW_TASK_CLOCK:
4776 * If the user instantiates this as a per-cpu event,
4777 * use the cpu_clock event instead.
4779 if (event->ctx->task)
4780 pmu = &perf_ops_task_clock;
4781 else
4782 pmu = &perf_ops_cpu_clock;
4784 break;
4785 case PERF_COUNT_SW_PAGE_FAULTS:
4786 case PERF_COUNT_SW_PAGE_FAULTS_MIN:
4787 case PERF_COUNT_SW_PAGE_FAULTS_MAJ:
4788 case PERF_COUNT_SW_CONTEXT_SWITCHES:
4789 case PERF_COUNT_SW_CPU_MIGRATIONS:
4790 case PERF_COUNT_SW_ALIGNMENT_FAULTS:
4791 case PERF_COUNT_SW_EMULATION_FAULTS:
4792 if (!event->parent) {
4793 int err;
4795 err = swevent_hlist_get(event);
4796 if (err)
4797 return ERR_PTR(err);
4799 atomic_inc(&perf_swevent_enabled[event_id]);
4800 event->destroy = sw_perf_event_destroy;
4802 pmu = &perf_ops_generic;
4803 break;
4806 return pmu;
4810 * Allocate and initialize a event structure
4812 static struct perf_event *
4813 perf_event_alloc(struct perf_event_attr *attr,
4814 int cpu,
4815 struct perf_event_context *ctx,
4816 struct perf_event *group_leader,
4817 struct perf_event *parent_event,
4818 perf_overflow_handler_t overflow_handler,
4819 gfp_t gfpflags)
4821 const struct pmu *pmu;
4822 struct perf_event *event;
4823 struct hw_perf_event *hwc;
4824 long err;
4826 event = kzalloc(sizeof(*event), gfpflags);
4827 if (!event)
4828 return ERR_PTR(-ENOMEM);
4831 * Single events are their own group leaders, with an
4832 * empty sibling list:
4834 if (!group_leader)
4835 group_leader = event;
4837 mutex_init(&event->child_mutex);
4838 INIT_LIST_HEAD(&event->child_list);
4840 INIT_LIST_HEAD(&event->group_entry);
4841 INIT_LIST_HEAD(&event->event_entry);
4842 INIT_LIST_HEAD(&event->sibling_list);
4843 init_waitqueue_head(&event->waitq);
4845 mutex_init(&event->mmap_mutex);
4847 event->cpu = cpu;
4848 event->attr = *attr;
4849 event->group_leader = group_leader;
4850 event->pmu = NULL;
4851 event->ctx = ctx;
4852 event->oncpu = -1;
4854 event->parent = parent_event;
4856 event->ns = get_pid_ns(current->nsproxy->pid_ns);
4857 event->id = atomic64_inc_return(&perf_event_id);
4859 event->state = PERF_EVENT_STATE_INACTIVE;
4861 if (!overflow_handler && parent_event)
4862 overflow_handler = parent_event->overflow_handler;
4864 event->overflow_handler = overflow_handler;
4866 if (attr->disabled)
4867 event->state = PERF_EVENT_STATE_OFF;
4869 pmu = NULL;
4871 hwc = &event->hw;
4872 hwc->sample_period = attr->sample_period;
4873 if (attr->freq && attr->sample_freq)
4874 hwc->sample_period = 1;
4875 hwc->last_period = hwc->sample_period;
4877 atomic64_set(&hwc->period_left, hwc->sample_period);
4880 * we currently do not support PERF_FORMAT_GROUP on inherited events
4882 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
4883 goto done;
4885 switch (attr->type) {
4886 case PERF_TYPE_RAW:
4887 case PERF_TYPE_HARDWARE:
4888 case PERF_TYPE_HW_CACHE:
4889 pmu = hw_perf_event_init(event);
4890 break;
4892 case PERF_TYPE_SOFTWARE:
4893 pmu = sw_perf_event_init(event);
4894 break;
4896 case PERF_TYPE_TRACEPOINT:
4897 pmu = tp_perf_event_init(event);
4898 break;
4900 case PERF_TYPE_BREAKPOINT:
4901 pmu = bp_perf_event_init(event);
4902 break;
4905 default:
4906 break;
4908 done:
4909 err = 0;
4910 if (!pmu)
4911 err = -EINVAL;
4912 else if (IS_ERR(pmu))
4913 err = PTR_ERR(pmu);
4915 if (err) {
4916 if (event->ns)
4917 put_pid_ns(event->ns);
4918 kfree(event);
4919 return ERR_PTR(err);
4922 event->pmu = pmu;
4924 if (!event->parent) {
4925 atomic_inc(&nr_events);
4926 if (event->attr.mmap)
4927 atomic_inc(&nr_mmap_events);
4928 if (event->attr.comm)
4929 atomic_inc(&nr_comm_events);
4930 if (event->attr.task)
4931 atomic_inc(&nr_task_events);
4934 return event;
4937 static int perf_copy_attr(struct perf_event_attr __user *uattr,
4938 struct perf_event_attr *attr)
4940 u32 size;
4941 int ret;
4943 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
4944 return -EFAULT;
4947 * zero the full structure, so that a short copy will be nice.
4949 memset(attr, 0, sizeof(*attr));
4951 ret = get_user(size, &uattr->size);
4952 if (ret)
4953 return ret;
4955 if (size > PAGE_SIZE) /* silly large */
4956 goto err_size;
4958 if (!size) /* abi compat */
4959 size = PERF_ATTR_SIZE_VER0;
4961 if (size < PERF_ATTR_SIZE_VER0)
4962 goto err_size;
4965 * If we're handed a bigger struct than we know of,
4966 * ensure all the unknown bits are 0 - i.e. new
4967 * user-space does not rely on any kernel feature
4968 * extensions we dont know about yet.
4970 if (size > sizeof(*attr)) {
4971 unsigned char __user *addr;
4972 unsigned char __user *end;
4973 unsigned char val;
4975 addr = (void __user *)uattr + sizeof(*attr);
4976 end = (void __user *)uattr + size;
4978 for (; addr < end; addr++) {
4979 ret = get_user(val, addr);
4980 if (ret)
4981 return ret;
4982 if (val)
4983 goto err_size;
4985 size = sizeof(*attr);
4988 ret = copy_from_user(attr, uattr, size);
4989 if (ret)
4990 return -EFAULT;
4993 * If the type exists, the corresponding creation will verify
4994 * the attr->config.
4996 if (attr->type >= PERF_TYPE_MAX)
4997 return -EINVAL;
4999 if (attr->__reserved_1)
5000 return -EINVAL;
5002 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
5003 return -EINVAL;
5005 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
5006 return -EINVAL;
5008 out:
5009 return ret;
5011 err_size:
5012 put_user(sizeof(*attr), &uattr->size);
5013 ret = -E2BIG;
5014 goto out;
5017 static int
5018 perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
5020 struct perf_mmap_data *data = NULL, *old_data = NULL;
5021 int ret = -EINVAL;
5023 if (!output_event)
5024 goto set;
5026 /* don't allow circular references */
5027 if (event == output_event)
5028 goto out;
5031 * Don't allow cross-cpu buffers
5033 if (output_event->cpu != event->cpu)
5034 goto out;
5037 * If its not a per-cpu buffer, it must be the same task.
5039 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
5040 goto out;
5042 set:
5043 mutex_lock(&event->mmap_mutex);
5044 /* Can't redirect output if we've got an active mmap() */
5045 if (atomic_read(&event->mmap_count))
5046 goto unlock;
5048 if (output_event) {
5049 /* get the buffer we want to redirect to */
5050 data = perf_mmap_data_get(output_event);
5051 if (!data)
5052 goto unlock;
5055 old_data = event->data;
5056 rcu_assign_pointer(event->data, data);
5057 ret = 0;
5058 unlock:
5059 mutex_unlock(&event->mmap_mutex);
5061 if (old_data)
5062 perf_mmap_data_put(old_data);
5063 out:
5064 return ret;
5068 * sys_perf_event_open - open a performance event, associate it to a task/cpu
5070 * @attr_uptr: event_id type attributes for monitoring/sampling
5071 * @pid: target pid
5072 * @cpu: target cpu
5073 * @group_fd: group leader event fd
5075 SYSCALL_DEFINE5(perf_event_open,
5076 struct perf_event_attr __user *, attr_uptr,
5077 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
5079 struct perf_event *event, *group_leader = NULL, *output_event = NULL;
5080 struct perf_event_attr attr;
5081 struct perf_event_context *ctx;
5082 struct file *event_file = NULL;
5083 struct file *group_file = NULL;
5084 int event_fd;
5085 int fput_needed = 0;
5086 int err;
5088 /* for future expandability... */
5089 if (flags & ~(PERF_FLAG_FD_NO_GROUP | PERF_FLAG_FD_OUTPUT))
5090 return -EINVAL;
5092 err = perf_copy_attr(attr_uptr, &attr);
5093 if (err)
5094 return err;
5096 if (!attr.exclude_kernel) {
5097 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
5098 return -EACCES;
5101 if (attr.freq) {
5102 if (attr.sample_freq > sysctl_perf_event_sample_rate)
5103 return -EINVAL;
5106 event_fd = get_unused_fd_flags(O_RDWR);
5107 if (event_fd < 0)
5108 return event_fd;
5111 * Get the target context (task or percpu):
5113 ctx = find_get_context(pid, cpu);
5114 if (IS_ERR(ctx)) {
5115 err = PTR_ERR(ctx);
5116 goto err_fd;
5119 if (group_fd != -1) {
5120 group_leader = perf_fget_light(group_fd, &fput_needed);
5121 if (IS_ERR(group_leader)) {
5122 err = PTR_ERR(group_leader);
5123 goto err_put_context;
5125 group_file = group_leader->filp;
5126 if (flags & PERF_FLAG_FD_OUTPUT)
5127 output_event = group_leader;
5128 if (flags & PERF_FLAG_FD_NO_GROUP)
5129 group_leader = NULL;
5133 * Look up the group leader (we will attach this event to it):
5135 if (group_leader) {
5136 err = -EINVAL;
5139 * Do not allow a recursive hierarchy (this new sibling
5140 * becoming part of another group-sibling):
5142 if (group_leader->group_leader != group_leader)
5143 goto err_put_context;
5145 * Do not allow to attach to a group in a different
5146 * task or CPU context:
5148 if (group_leader->ctx != ctx)
5149 goto err_put_context;
5151 * Only a group leader can be exclusive or pinned
5153 if (attr.exclusive || attr.pinned)
5154 goto err_put_context;
5157 event = perf_event_alloc(&attr, cpu, ctx, group_leader,
5158 NULL, NULL, GFP_KERNEL);
5159 if (IS_ERR(event)) {
5160 err = PTR_ERR(event);
5161 goto err_put_context;
5164 if (output_event) {
5165 err = perf_event_set_output(event, output_event);
5166 if (err)
5167 goto err_free_put_context;
5170 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, O_RDWR);
5171 if (IS_ERR(event_file)) {
5172 err = PTR_ERR(event_file);
5173 goto err_free_put_context;
5176 event->filp = event_file;
5177 WARN_ON_ONCE(ctx->parent_ctx);
5178 mutex_lock(&ctx->mutex);
5179 perf_install_in_context(ctx, event, cpu);
5180 ++ctx->generation;
5181 mutex_unlock(&ctx->mutex);
5183 event->owner = current;
5184 get_task_struct(current);
5185 mutex_lock(&current->perf_event_mutex);
5186 list_add_tail(&event->owner_entry, &current->perf_event_list);
5187 mutex_unlock(&current->perf_event_mutex);
5190 * Drop the reference on the group_event after placing the
5191 * new event on the sibling_list. This ensures destruction
5192 * of the group leader will find the pointer to itself in
5193 * perf_group_detach().
5195 fput_light(group_file, fput_needed);
5196 fd_install(event_fd, event_file);
5197 return event_fd;
5199 err_free_put_context:
5200 free_event(event);
5201 err_put_context:
5202 fput_light(group_file, fput_needed);
5203 put_ctx(ctx);
5204 err_fd:
5205 put_unused_fd(event_fd);
5206 return err;
5210 * perf_event_create_kernel_counter
5212 * @attr: attributes of the counter to create
5213 * @cpu: cpu in which the counter is bound
5214 * @pid: task to profile
5216 struct perf_event *
5217 perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
5218 pid_t pid,
5219 perf_overflow_handler_t overflow_handler)
5221 struct perf_event *event;
5222 struct perf_event_context *ctx;
5223 int err;
5226 * Get the target context (task or percpu):
5229 ctx = find_get_context(pid, cpu);
5230 if (IS_ERR(ctx)) {
5231 err = PTR_ERR(ctx);
5232 goto err_exit;
5235 event = perf_event_alloc(attr, cpu, ctx, NULL,
5236 NULL, overflow_handler, GFP_KERNEL);
5237 if (IS_ERR(event)) {
5238 err = PTR_ERR(event);
5239 goto err_put_context;
5242 event->filp = NULL;
5243 WARN_ON_ONCE(ctx->parent_ctx);
5244 mutex_lock(&ctx->mutex);
5245 perf_install_in_context(ctx, event, cpu);
5246 ++ctx->generation;
5247 mutex_unlock(&ctx->mutex);
5249 event->owner = current;
5250 get_task_struct(current);
5251 mutex_lock(&current->perf_event_mutex);
5252 list_add_tail(&event->owner_entry, &current->perf_event_list);
5253 mutex_unlock(&current->perf_event_mutex);
5255 return event;
5257 err_put_context:
5258 put_ctx(ctx);
5259 err_exit:
5260 return ERR_PTR(err);
5262 EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
5265 * inherit a event from parent task to child task:
5267 static struct perf_event *
5268 inherit_event(struct perf_event *parent_event,
5269 struct task_struct *parent,
5270 struct perf_event_context *parent_ctx,
5271 struct task_struct *child,
5272 struct perf_event *group_leader,
5273 struct perf_event_context *child_ctx)
5275 struct perf_event *child_event;
5278 * Instead of creating recursive hierarchies of events,
5279 * we link inherited events back to the original parent,
5280 * which has a filp for sure, which we use as the reference
5281 * count:
5283 if (parent_event->parent)
5284 parent_event = parent_event->parent;
5286 child_event = perf_event_alloc(&parent_event->attr,
5287 parent_event->cpu, child_ctx,
5288 group_leader, parent_event,
5289 NULL, GFP_KERNEL);
5290 if (IS_ERR(child_event))
5291 return child_event;
5292 get_ctx(child_ctx);
5295 * Make the child state follow the state of the parent event,
5296 * not its attr.disabled bit. We hold the parent's mutex,
5297 * so we won't race with perf_event_{en, dis}able_family.
5299 if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
5300 child_event->state = PERF_EVENT_STATE_INACTIVE;
5301 else
5302 child_event->state = PERF_EVENT_STATE_OFF;
5304 if (parent_event->attr.freq) {
5305 u64 sample_period = parent_event->hw.sample_period;
5306 struct hw_perf_event *hwc = &child_event->hw;
5308 hwc->sample_period = sample_period;
5309 hwc->last_period = sample_period;
5311 atomic64_set(&hwc->period_left, sample_period);
5314 child_event->overflow_handler = parent_event->overflow_handler;
5317 * Link it up in the child's context:
5319 add_event_to_ctx(child_event, child_ctx);
5322 * Get a reference to the parent filp - we will fput it
5323 * when the child event exits. This is safe to do because
5324 * we are in the parent and we know that the filp still
5325 * exists and has a nonzero count:
5327 atomic_long_inc(&parent_event->filp->f_count);
5330 * Link this into the parent event's child list
5332 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
5333 mutex_lock(&parent_event->child_mutex);
5334 list_add_tail(&child_event->child_list, &parent_event->child_list);
5335 mutex_unlock(&parent_event->child_mutex);
5337 return child_event;
5340 static int inherit_group(struct perf_event *parent_event,
5341 struct task_struct *parent,
5342 struct perf_event_context *parent_ctx,
5343 struct task_struct *child,
5344 struct perf_event_context *child_ctx)
5346 struct perf_event *leader;
5347 struct perf_event *sub;
5348 struct perf_event *child_ctr;
5350 leader = inherit_event(parent_event, parent, parent_ctx,
5351 child, NULL, child_ctx);
5352 if (IS_ERR(leader))
5353 return PTR_ERR(leader);
5354 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
5355 child_ctr = inherit_event(sub, parent, parent_ctx,
5356 child, leader, child_ctx);
5357 if (IS_ERR(child_ctr))
5358 return PTR_ERR(child_ctr);
5360 return 0;
5363 static void sync_child_event(struct perf_event *child_event,
5364 struct task_struct *child)
5366 struct perf_event *parent_event = child_event->parent;
5367 u64 child_val;
5369 if (child_event->attr.inherit_stat)
5370 perf_event_read_event(child_event, child);
5372 child_val = atomic64_read(&child_event->count);
5375 * Add back the child's count to the parent's count:
5377 atomic64_add(child_val, &parent_event->count);
5378 atomic64_add(child_event->total_time_enabled,
5379 &parent_event->child_total_time_enabled);
5380 atomic64_add(child_event->total_time_running,
5381 &parent_event->child_total_time_running);
5384 * Remove this event from the parent's list
5386 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
5387 mutex_lock(&parent_event->child_mutex);
5388 list_del_init(&child_event->child_list);
5389 mutex_unlock(&parent_event->child_mutex);
5392 * Release the parent event, if this was the last
5393 * reference to it.
5395 fput(parent_event->filp);
5398 static void
5399 __perf_event_exit_task(struct perf_event *child_event,
5400 struct perf_event_context *child_ctx,
5401 struct task_struct *child)
5403 struct perf_event *parent_event;
5405 perf_event_remove_from_context(child_event);
5407 parent_event = child_event->parent;
5409 * It can happen that parent exits first, and has events
5410 * that are still around due to the child reference. These
5411 * events need to be zapped - but otherwise linger.
5413 if (parent_event) {
5414 sync_child_event(child_event, child);
5415 free_event(child_event);
5420 * When a child task exits, feed back event values to parent events.
5422 void perf_event_exit_task(struct task_struct *child)
5424 struct perf_event *child_event, *tmp;
5425 struct perf_event_context *child_ctx;
5426 unsigned long flags;
5428 if (likely(!child->perf_event_ctxp)) {
5429 perf_event_task(child, NULL, 0);
5430 return;
5433 local_irq_save(flags);
5435 * We can't reschedule here because interrupts are disabled,
5436 * and either child is current or it is a task that can't be
5437 * scheduled, so we are now safe from rescheduling changing
5438 * our context.
5440 child_ctx = child->perf_event_ctxp;
5441 __perf_event_task_sched_out(child_ctx);
5444 * Take the context lock here so that if find_get_context is
5445 * reading child->perf_event_ctxp, we wait until it has
5446 * incremented the context's refcount before we do put_ctx below.
5448 raw_spin_lock(&child_ctx->lock);
5449 child->perf_event_ctxp = NULL;
5451 * If this context is a clone; unclone it so it can't get
5452 * swapped to another process while we're removing all
5453 * the events from it.
5455 unclone_ctx(child_ctx);
5456 update_context_time(child_ctx);
5457 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
5460 * Report the task dead after unscheduling the events so that we
5461 * won't get any samples after PERF_RECORD_EXIT. We can however still
5462 * get a few PERF_RECORD_READ events.
5464 perf_event_task(child, child_ctx, 0);
5467 * We can recurse on the same lock type through:
5469 * __perf_event_exit_task()
5470 * sync_child_event()
5471 * fput(parent_event->filp)
5472 * perf_release()
5473 * mutex_lock(&ctx->mutex)
5475 * But since its the parent context it won't be the same instance.
5477 mutex_lock(&child_ctx->mutex);
5479 again:
5480 list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
5481 group_entry)
5482 __perf_event_exit_task(child_event, child_ctx, child);
5484 list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
5485 group_entry)
5486 __perf_event_exit_task(child_event, child_ctx, child);
5489 * If the last event was a group event, it will have appended all
5490 * its siblings to the list, but we obtained 'tmp' before that which
5491 * will still point to the list head terminating the iteration.
5493 if (!list_empty(&child_ctx->pinned_groups) ||
5494 !list_empty(&child_ctx->flexible_groups))
5495 goto again;
5497 mutex_unlock(&child_ctx->mutex);
5499 put_ctx(child_ctx);
5502 static void perf_free_event(struct perf_event *event,
5503 struct perf_event_context *ctx)
5505 struct perf_event *parent = event->parent;
5507 if (WARN_ON_ONCE(!parent))
5508 return;
5510 mutex_lock(&parent->child_mutex);
5511 list_del_init(&event->child_list);
5512 mutex_unlock(&parent->child_mutex);
5514 fput(parent->filp);
5516 perf_group_detach(event);
5517 list_del_event(event, ctx);
5518 free_event(event);
5522 * free an unexposed, unused context as created by inheritance by
5523 * init_task below, used by fork() in case of fail.
5525 void perf_event_free_task(struct task_struct *task)
5527 struct perf_event_context *ctx = task->perf_event_ctxp;
5528 struct perf_event *event, *tmp;
5530 if (!ctx)
5531 return;
5533 mutex_lock(&ctx->mutex);
5534 again:
5535 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
5536 perf_free_event(event, ctx);
5538 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
5539 group_entry)
5540 perf_free_event(event, ctx);
5542 if (!list_empty(&ctx->pinned_groups) ||
5543 !list_empty(&ctx->flexible_groups))
5544 goto again;
5546 mutex_unlock(&ctx->mutex);
5548 put_ctx(ctx);
5551 static int
5552 inherit_task_group(struct perf_event *event, struct task_struct *parent,
5553 struct perf_event_context *parent_ctx,
5554 struct task_struct *child,
5555 int *inherited_all)
5557 int ret;
5558 struct perf_event_context *child_ctx = child->perf_event_ctxp;
5560 if (!event->attr.inherit) {
5561 *inherited_all = 0;
5562 return 0;
5565 if (!child_ctx) {
5567 * This is executed from the parent task context, so
5568 * inherit events that have been marked for cloning.
5569 * First allocate and initialize a context for the
5570 * child.
5573 child_ctx = kzalloc(sizeof(struct perf_event_context),
5574 GFP_KERNEL);
5575 if (!child_ctx)
5576 return -ENOMEM;
5578 __perf_event_init_context(child_ctx, child);
5579 child->perf_event_ctxp = child_ctx;
5580 get_task_struct(child);
5583 ret = inherit_group(event, parent, parent_ctx,
5584 child, child_ctx);
5586 if (ret)
5587 *inherited_all = 0;
5589 return ret;
5594 * Initialize the perf_event context in task_struct
5596 int perf_event_init_task(struct task_struct *child)
5598 struct perf_event_context *child_ctx, *parent_ctx;
5599 struct perf_event_context *cloned_ctx;
5600 struct perf_event *event;
5601 struct task_struct *parent = current;
5602 int inherited_all = 1;
5603 unsigned long flags;
5604 int ret = 0;
5606 child->perf_event_ctxp = NULL;
5608 mutex_init(&child->perf_event_mutex);
5609 INIT_LIST_HEAD(&child->perf_event_list);
5611 if (likely(!parent->perf_event_ctxp))
5612 return 0;
5615 * If the parent's context is a clone, pin it so it won't get
5616 * swapped under us.
5618 parent_ctx = perf_pin_task_context(parent);
5621 * No need to check if parent_ctx != NULL here; since we saw
5622 * it non-NULL earlier, the only reason for it to become NULL
5623 * is if we exit, and since we're currently in the middle of
5624 * a fork we can't be exiting at the same time.
5628 * Lock the parent list. No need to lock the child - not PID
5629 * hashed yet and not running, so nobody can access it.
5631 mutex_lock(&parent_ctx->mutex);
5634 * We dont have to disable NMIs - we are only looking at
5635 * the list, not manipulating it:
5637 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
5638 ret = inherit_task_group(event, parent, parent_ctx, child,
5639 &inherited_all);
5640 if (ret)
5641 break;
5645 * We can't hold ctx->lock when iterating the ->flexible_group list due
5646 * to allocations, but we need to prevent rotation because
5647 * rotate_ctx() will change the list from interrupt context.
5649 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
5650 parent_ctx->rotate_disable = 1;
5651 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
5653 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
5654 ret = inherit_task_group(event, parent, parent_ctx, child,
5655 &inherited_all);
5656 if (ret)
5657 break;
5660 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
5661 parent_ctx->rotate_disable = 0;
5662 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
5664 child_ctx = child->perf_event_ctxp;
5666 if (child_ctx && inherited_all) {
5668 * Mark the child context as a clone of the parent
5669 * context, or of whatever the parent is a clone of.
5670 * Note that if the parent is a clone, it could get
5671 * uncloned at any point, but that doesn't matter
5672 * because the list of events and the generation
5673 * count can't have changed since we took the mutex.
5675 cloned_ctx = rcu_dereference(parent_ctx->parent_ctx);
5676 if (cloned_ctx) {
5677 child_ctx->parent_ctx = cloned_ctx;
5678 child_ctx->parent_gen = parent_ctx->parent_gen;
5679 } else {
5680 child_ctx->parent_ctx = parent_ctx;
5681 child_ctx->parent_gen = parent_ctx->generation;
5683 get_ctx(child_ctx->parent_ctx);
5686 mutex_unlock(&parent_ctx->mutex);
5688 perf_unpin_context(parent_ctx);
5690 return ret;
5693 static void __init perf_event_init_all_cpus(void)
5695 int cpu;
5696 struct perf_cpu_context *cpuctx;
5698 for_each_possible_cpu(cpu) {
5699 cpuctx = &per_cpu(perf_cpu_context, cpu);
5700 mutex_init(&cpuctx->hlist_mutex);
5701 __perf_event_init_context(&cpuctx->ctx, NULL);
5705 static void __cpuinit perf_event_init_cpu(int cpu)
5707 struct perf_cpu_context *cpuctx;
5709 cpuctx = &per_cpu(perf_cpu_context, cpu);
5711 spin_lock(&perf_resource_lock);
5712 cpuctx->max_pertask = perf_max_events - perf_reserved_percpu;
5713 spin_unlock(&perf_resource_lock);
5715 mutex_lock(&cpuctx->hlist_mutex);
5716 if (cpuctx->hlist_refcount > 0) {
5717 struct swevent_hlist *hlist;
5719 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
5720 WARN_ON_ONCE(!hlist);
5721 rcu_assign_pointer(cpuctx->swevent_hlist, hlist);
5723 mutex_unlock(&cpuctx->hlist_mutex);
5726 #ifdef CONFIG_HOTPLUG_CPU
5727 static void __perf_event_exit_cpu(void *info)
5729 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
5730 struct perf_event_context *ctx = &cpuctx->ctx;
5731 struct perf_event *event, *tmp;
5733 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
5734 __perf_event_remove_from_context(event);
5735 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, group_entry)
5736 __perf_event_remove_from_context(event);
5738 static void perf_event_exit_cpu(int cpu)
5740 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
5741 struct perf_event_context *ctx = &cpuctx->ctx;
5743 mutex_lock(&cpuctx->hlist_mutex);
5744 swevent_hlist_release(cpuctx);
5745 mutex_unlock(&cpuctx->hlist_mutex);
5747 mutex_lock(&ctx->mutex);
5748 smp_call_function_single(cpu, __perf_event_exit_cpu, NULL, 1);
5749 mutex_unlock(&ctx->mutex);
5751 #else
5752 static inline void perf_event_exit_cpu(int cpu) { }
5753 #endif
5755 static int __cpuinit
5756 perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
5758 unsigned int cpu = (long)hcpu;
5760 switch (action) {
5762 case CPU_UP_PREPARE:
5763 case CPU_UP_PREPARE_FROZEN:
5764 perf_event_init_cpu(cpu);
5765 break;
5767 case CPU_DOWN_PREPARE:
5768 case CPU_DOWN_PREPARE_FROZEN:
5769 perf_event_exit_cpu(cpu);
5770 break;
5772 default:
5773 break;
5776 return NOTIFY_OK;
5780 * This has to have a higher priority than migration_notifier in sched.c.
5782 static struct notifier_block __cpuinitdata perf_cpu_nb = {
5783 .notifier_call = perf_cpu_notify,
5784 .priority = 20,
5787 void __init perf_event_init(void)
5789 perf_event_init_all_cpus();
5790 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
5791 (void *)(long)smp_processor_id());
5792 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_ONLINE,
5793 (void *)(long)smp_processor_id());
5794 register_cpu_notifier(&perf_cpu_nb);
5797 static ssize_t perf_show_reserve_percpu(struct sysdev_class *class,
5798 struct sysdev_class_attribute *attr,
5799 char *buf)
5801 return sprintf(buf, "%d\n", perf_reserved_percpu);
5804 static ssize_t
5805 perf_set_reserve_percpu(struct sysdev_class *class,
5806 struct sysdev_class_attribute *attr,
5807 const char *buf,
5808 size_t count)
5810 struct perf_cpu_context *cpuctx;
5811 unsigned long val;
5812 int err, cpu, mpt;
5814 err = strict_strtoul(buf, 10, &val);
5815 if (err)
5816 return err;
5817 if (val > perf_max_events)
5818 return -EINVAL;
5820 spin_lock(&perf_resource_lock);
5821 perf_reserved_percpu = val;
5822 for_each_online_cpu(cpu) {
5823 cpuctx = &per_cpu(perf_cpu_context, cpu);
5824 raw_spin_lock_irq(&cpuctx->ctx.lock);
5825 mpt = min(perf_max_events - cpuctx->ctx.nr_events,
5826 perf_max_events - perf_reserved_percpu);
5827 cpuctx->max_pertask = mpt;
5828 raw_spin_unlock_irq(&cpuctx->ctx.lock);
5830 spin_unlock(&perf_resource_lock);
5832 return count;
5835 static ssize_t perf_show_overcommit(struct sysdev_class *class,
5836 struct sysdev_class_attribute *attr,
5837 char *buf)
5839 return sprintf(buf, "%d\n", perf_overcommit);
5842 static ssize_t
5843 perf_set_overcommit(struct sysdev_class *class,
5844 struct sysdev_class_attribute *attr,
5845 const char *buf, size_t count)
5847 unsigned long val;
5848 int err;
5850 err = strict_strtoul(buf, 10, &val);
5851 if (err)
5852 return err;
5853 if (val > 1)
5854 return -EINVAL;
5856 spin_lock(&perf_resource_lock);
5857 perf_overcommit = val;
5858 spin_unlock(&perf_resource_lock);
5860 return count;
5863 static SYSDEV_CLASS_ATTR(
5864 reserve_percpu,
5865 0644,
5866 perf_show_reserve_percpu,
5867 perf_set_reserve_percpu
5870 static SYSDEV_CLASS_ATTR(
5871 overcommit,
5872 0644,
5873 perf_show_overcommit,
5874 perf_set_overcommit
5877 static struct attribute *perfclass_attrs[] = {
5878 &attr_reserve_percpu.attr,
5879 &attr_overcommit.attr,
5880 NULL
5883 static struct attribute_group perfclass_attr_group = {
5884 .attrs = perfclass_attrs,
5885 .name = "perf_events",
5888 static int __init perf_event_sysfs_init(void)
5890 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
5891 &perfclass_attr_group);
5893 device_initcall(perf_event_sysfs_init);