DaVinci: EDMA: Add queue 2 and 3 for DM365 and DM6467
[linux-ginger.git] / kernel / perf_counter.c
blob36f65e2b8b5772f16e644bfa109f89393c893fd3
1 /*
2 * Performance counter core code
4 * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
5 * Copyright (C) 2008-2009 Red Hat, Inc., Ingo Molnar
6 * Copyright (C) 2008-2009 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
7 * Copyright © 2009 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
9 * For licensing details see kernel-base/COPYING
12 #include <linux/fs.h>
13 #include <linux/mm.h>
14 #include <linux/cpu.h>
15 #include <linux/smp.h>
16 #include <linux/file.h>
17 #include <linux/poll.h>
18 #include <linux/sysfs.h>
19 #include <linux/dcache.h>
20 #include <linux/percpu.h>
21 #include <linux/ptrace.h>
22 #include <linux/vmstat.h>
23 #include <linux/hardirq.h>
24 #include <linux/rculist.h>
25 #include <linux/uaccess.h>
26 #include <linux/syscalls.h>
27 #include <linux/anon_inodes.h>
28 #include <linux/kernel_stat.h>
29 #include <linux/perf_counter.h>
31 #include <asm/irq_regs.h>
34 * Each CPU has a list of per CPU counters:
36 DEFINE_PER_CPU(struct perf_cpu_context, perf_cpu_context);
38 int perf_max_counters __read_mostly = 1;
39 static int perf_reserved_percpu __read_mostly;
40 static int perf_overcommit __read_mostly = 1;
42 static atomic_t nr_counters __read_mostly;
43 static atomic_t nr_mmap_counters __read_mostly;
44 static atomic_t nr_comm_counters __read_mostly;
45 static atomic_t nr_task_counters __read_mostly;
48 * perf counter paranoia level:
49 * 0 - not paranoid
50 * 1 - disallow cpu counters to unpriv
51 * 2 - disallow kernel profiling to unpriv
53 int sysctl_perf_counter_paranoid __read_mostly;
55 static inline bool perf_paranoid_cpu(void)
57 return sysctl_perf_counter_paranoid > 0;
60 static inline bool perf_paranoid_kernel(void)
62 return sysctl_perf_counter_paranoid > 1;
65 int sysctl_perf_counter_mlock __read_mostly = 512; /* 'free' kb per user */
68 * max perf counter sample rate
70 int sysctl_perf_counter_sample_rate __read_mostly = 100000;
72 static atomic64_t perf_counter_id;
75 * Lock for (sysadmin-configurable) counter reservations:
77 static DEFINE_SPINLOCK(perf_resource_lock);
80 * Architecture provided APIs - weak aliases:
82 extern __weak const struct pmu *hw_perf_counter_init(struct perf_counter *counter)
84 return NULL;
87 void __weak hw_perf_disable(void) { barrier(); }
88 void __weak hw_perf_enable(void) { barrier(); }
90 void __weak hw_perf_counter_setup(int cpu) { barrier(); }
91 void __weak hw_perf_counter_setup_online(int cpu) { barrier(); }
93 int __weak
94 hw_perf_group_sched_in(struct perf_counter *group_leader,
95 struct perf_cpu_context *cpuctx,
96 struct perf_counter_context *ctx, int cpu)
98 return 0;
101 void __weak perf_counter_print_debug(void) { }
103 static DEFINE_PER_CPU(int, disable_count);
105 void __perf_disable(void)
107 __get_cpu_var(disable_count)++;
110 bool __perf_enable(void)
112 return !--__get_cpu_var(disable_count);
115 void perf_disable(void)
117 __perf_disable();
118 hw_perf_disable();
121 void perf_enable(void)
123 if (__perf_enable())
124 hw_perf_enable();
127 static void get_ctx(struct perf_counter_context *ctx)
129 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
132 static void free_ctx(struct rcu_head *head)
134 struct perf_counter_context *ctx;
136 ctx = container_of(head, struct perf_counter_context, rcu_head);
137 kfree(ctx);
140 static void put_ctx(struct perf_counter_context *ctx)
142 if (atomic_dec_and_test(&ctx->refcount)) {
143 if (ctx->parent_ctx)
144 put_ctx(ctx->parent_ctx);
145 if (ctx->task)
146 put_task_struct(ctx->task);
147 call_rcu(&ctx->rcu_head, free_ctx);
151 static void unclone_ctx(struct perf_counter_context *ctx)
153 if (ctx->parent_ctx) {
154 put_ctx(ctx->parent_ctx);
155 ctx->parent_ctx = NULL;
160 * If we inherit counters we want to return the parent counter id
161 * to userspace.
163 static u64 primary_counter_id(struct perf_counter *counter)
165 u64 id = counter->id;
167 if (counter->parent)
168 id = counter->parent->id;
170 return id;
174 * Get the perf_counter_context for a task and lock it.
175 * This has to cope with with the fact that until it is locked,
176 * the context could get moved to another task.
178 static struct perf_counter_context *
179 perf_lock_task_context(struct task_struct *task, unsigned long *flags)
181 struct perf_counter_context *ctx;
183 rcu_read_lock();
184 retry:
185 ctx = rcu_dereference(task->perf_counter_ctxp);
186 if (ctx) {
188 * If this context is a clone of another, it might
189 * get swapped for another underneath us by
190 * perf_counter_task_sched_out, though the
191 * rcu_read_lock() protects us from any context
192 * getting freed. Lock the context and check if it
193 * got swapped before we could get the lock, and retry
194 * if so. If we locked the right context, then it
195 * can't get swapped on us any more.
197 spin_lock_irqsave(&ctx->lock, *flags);
198 if (ctx != rcu_dereference(task->perf_counter_ctxp)) {
199 spin_unlock_irqrestore(&ctx->lock, *flags);
200 goto retry;
203 if (!atomic_inc_not_zero(&ctx->refcount)) {
204 spin_unlock_irqrestore(&ctx->lock, *flags);
205 ctx = NULL;
208 rcu_read_unlock();
209 return ctx;
213 * Get the context for a task and increment its pin_count so it
214 * can't get swapped to another task. This also increments its
215 * reference count so that the context can't get freed.
217 static struct perf_counter_context *perf_pin_task_context(struct task_struct *task)
219 struct perf_counter_context *ctx;
220 unsigned long flags;
222 ctx = perf_lock_task_context(task, &flags);
223 if (ctx) {
224 ++ctx->pin_count;
225 spin_unlock_irqrestore(&ctx->lock, flags);
227 return ctx;
230 static void perf_unpin_context(struct perf_counter_context *ctx)
232 unsigned long flags;
234 spin_lock_irqsave(&ctx->lock, flags);
235 --ctx->pin_count;
236 spin_unlock_irqrestore(&ctx->lock, flags);
237 put_ctx(ctx);
241 * Add a counter from the lists for its context.
242 * Must be called with ctx->mutex and ctx->lock held.
244 static void
245 list_add_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
247 struct perf_counter *group_leader = counter->group_leader;
250 * Depending on whether it is a standalone or sibling counter,
251 * add it straight to the context's counter list, or to the group
252 * leader's sibling list:
254 if (group_leader == counter)
255 list_add_tail(&counter->list_entry, &ctx->counter_list);
256 else {
257 list_add_tail(&counter->list_entry, &group_leader->sibling_list);
258 group_leader->nr_siblings++;
261 list_add_rcu(&counter->event_entry, &ctx->event_list);
262 ctx->nr_counters++;
263 if (counter->attr.inherit_stat)
264 ctx->nr_stat++;
268 * Remove a counter from the lists for its context.
269 * Must be called with ctx->mutex and ctx->lock held.
271 static void
272 list_del_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
274 struct perf_counter *sibling, *tmp;
276 if (list_empty(&counter->list_entry))
277 return;
278 ctx->nr_counters--;
279 if (counter->attr.inherit_stat)
280 ctx->nr_stat--;
282 list_del_init(&counter->list_entry);
283 list_del_rcu(&counter->event_entry);
285 if (counter->group_leader != counter)
286 counter->group_leader->nr_siblings--;
289 * If this was a group counter with sibling counters then
290 * upgrade the siblings to singleton counters by adding them
291 * to the context list directly:
293 list_for_each_entry_safe(sibling, tmp,
294 &counter->sibling_list, list_entry) {
296 list_move_tail(&sibling->list_entry, &ctx->counter_list);
297 sibling->group_leader = sibling;
301 static void
302 counter_sched_out(struct perf_counter *counter,
303 struct perf_cpu_context *cpuctx,
304 struct perf_counter_context *ctx)
306 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
307 return;
309 counter->state = PERF_COUNTER_STATE_INACTIVE;
310 if (counter->pending_disable) {
311 counter->pending_disable = 0;
312 counter->state = PERF_COUNTER_STATE_OFF;
314 counter->tstamp_stopped = ctx->time;
315 counter->pmu->disable(counter);
316 counter->oncpu = -1;
318 if (!is_software_counter(counter))
319 cpuctx->active_oncpu--;
320 ctx->nr_active--;
321 if (counter->attr.exclusive || !cpuctx->active_oncpu)
322 cpuctx->exclusive = 0;
325 static void
326 group_sched_out(struct perf_counter *group_counter,
327 struct perf_cpu_context *cpuctx,
328 struct perf_counter_context *ctx)
330 struct perf_counter *counter;
332 if (group_counter->state != PERF_COUNTER_STATE_ACTIVE)
333 return;
335 counter_sched_out(group_counter, cpuctx, ctx);
338 * Schedule out siblings (if any):
340 list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
341 counter_sched_out(counter, cpuctx, ctx);
343 if (group_counter->attr.exclusive)
344 cpuctx->exclusive = 0;
348 * Cross CPU call to remove a performance counter
350 * We disable the counter on the hardware level first. After that we
351 * remove it from the context list.
353 static void __perf_counter_remove_from_context(void *info)
355 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
356 struct perf_counter *counter = info;
357 struct perf_counter_context *ctx = counter->ctx;
360 * If this is a task context, we need to check whether it is
361 * the current task context of this cpu. If not it has been
362 * scheduled out before the smp call arrived.
364 if (ctx->task && cpuctx->task_ctx != ctx)
365 return;
367 spin_lock(&ctx->lock);
369 * Protect the list operation against NMI by disabling the
370 * counters on a global level.
372 perf_disable();
374 counter_sched_out(counter, cpuctx, ctx);
376 list_del_counter(counter, ctx);
378 if (!ctx->task) {
380 * Allow more per task counters with respect to the
381 * reservation:
383 cpuctx->max_pertask =
384 min(perf_max_counters - ctx->nr_counters,
385 perf_max_counters - perf_reserved_percpu);
388 perf_enable();
389 spin_unlock(&ctx->lock);
394 * Remove the counter from a task's (or a CPU's) list of counters.
396 * Must be called with ctx->mutex held.
398 * CPU counters are removed with a smp call. For task counters we only
399 * call when the task is on a CPU.
401 * If counter->ctx is a cloned context, callers must make sure that
402 * every task struct that counter->ctx->task could possibly point to
403 * remains valid. This is OK when called from perf_release since
404 * that only calls us on the top-level context, which can't be a clone.
405 * When called from perf_counter_exit_task, it's OK because the
406 * context has been detached from its task.
408 static void perf_counter_remove_from_context(struct perf_counter *counter)
410 struct perf_counter_context *ctx = counter->ctx;
411 struct task_struct *task = ctx->task;
413 if (!task) {
415 * Per cpu counters are removed via an smp call and
416 * the removal is always sucessful.
418 smp_call_function_single(counter->cpu,
419 __perf_counter_remove_from_context,
420 counter, 1);
421 return;
424 retry:
425 task_oncpu_function_call(task, __perf_counter_remove_from_context,
426 counter);
428 spin_lock_irq(&ctx->lock);
430 * If the context is active we need to retry the smp call.
432 if (ctx->nr_active && !list_empty(&counter->list_entry)) {
433 spin_unlock_irq(&ctx->lock);
434 goto retry;
438 * The lock prevents that this context is scheduled in so we
439 * can remove the counter safely, if the call above did not
440 * succeed.
442 if (!list_empty(&counter->list_entry)) {
443 list_del_counter(counter, ctx);
445 spin_unlock_irq(&ctx->lock);
448 static inline u64 perf_clock(void)
450 return cpu_clock(smp_processor_id());
454 * Update the record of the current time in a context.
456 static void update_context_time(struct perf_counter_context *ctx)
458 u64 now = perf_clock();
460 ctx->time += now - ctx->timestamp;
461 ctx->timestamp = now;
465 * Update the total_time_enabled and total_time_running fields for a counter.
467 static void update_counter_times(struct perf_counter *counter)
469 struct perf_counter_context *ctx = counter->ctx;
470 u64 run_end;
472 if (counter->state < PERF_COUNTER_STATE_INACTIVE)
473 return;
475 counter->total_time_enabled = ctx->time - counter->tstamp_enabled;
477 if (counter->state == PERF_COUNTER_STATE_INACTIVE)
478 run_end = counter->tstamp_stopped;
479 else
480 run_end = ctx->time;
482 counter->total_time_running = run_end - counter->tstamp_running;
486 * Update total_time_enabled and total_time_running for all counters in a group.
488 static void update_group_times(struct perf_counter *leader)
490 struct perf_counter *counter;
492 update_counter_times(leader);
493 list_for_each_entry(counter, &leader->sibling_list, list_entry)
494 update_counter_times(counter);
498 * Cross CPU call to disable a performance counter
500 static void __perf_counter_disable(void *info)
502 struct perf_counter *counter = info;
503 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
504 struct perf_counter_context *ctx = counter->ctx;
507 * If this is a per-task counter, need to check whether this
508 * counter's task is the current task on this cpu.
510 if (ctx->task && cpuctx->task_ctx != ctx)
511 return;
513 spin_lock(&ctx->lock);
516 * If the counter is on, turn it off.
517 * If it is in error state, leave it in error state.
519 if (counter->state >= PERF_COUNTER_STATE_INACTIVE) {
520 update_context_time(ctx);
521 update_counter_times(counter);
522 if (counter == counter->group_leader)
523 group_sched_out(counter, cpuctx, ctx);
524 else
525 counter_sched_out(counter, cpuctx, ctx);
526 counter->state = PERF_COUNTER_STATE_OFF;
529 spin_unlock(&ctx->lock);
533 * Disable a counter.
535 * If counter->ctx is a cloned context, callers must make sure that
536 * every task struct that counter->ctx->task could possibly point to
537 * remains valid. This condition is satisifed when called through
538 * perf_counter_for_each_child or perf_counter_for_each because they
539 * hold the top-level counter's child_mutex, so any descendant that
540 * goes to exit will block in sync_child_counter.
541 * When called from perf_pending_counter it's OK because counter->ctx
542 * is the current context on this CPU and preemption is disabled,
543 * hence we can't get into perf_counter_task_sched_out for this context.
545 static void perf_counter_disable(struct perf_counter *counter)
547 struct perf_counter_context *ctx = counter->ctx;
548 struct task_struct *task = ctx->task;
550 if (!task) {
552 * Disable the counter on the cpu that it's on
554 smp_call_function_single(counter->cpu, __perf_counter_disable,
555 counter, 1);
556 return;
559 retry:
560 task_oncpu_function_call(task, __perf_counter_disable, counter);
562 spin_lock_irq(&ctx->lock);
564 * If the counter is still active, we need to retry the cross-call.
566 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
567 spin_unlock_irq(&ctx->lock);
568 goto retry;
572 * Since we have the lock this context can't be scheduled
573 * in, so we can change the state safely.
575 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
576 update_counter_times(counter);
577 counter->state = PERF_COUNTER_STATE_OFF;
580 spin_unlock_irq(&ctx->lock);
583 static int
584 counter_sched_in(struct perf_counter *counter,
585 struct perf_cpu_context *cpuctx,
586 struct perf_counter_context *ctx,
587 int cpu)
589 if (counter->state <= PERF_COUNTER_STATE_OFF)
590 return 0;
592 counter->state = PERF_COUNTER_STATE_ACTIVE;
593 counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
595 * The new state must be visible before we turn it on in the hardware:
597 smp_wmb();
599 if (counter->pmu->enable(counter)) {
600 counter->state = PERF_COUNTER_STATE_INACTIVE;
601 counter->oncpu = -1;
602 return -EAGAIN;
605 counter->tstamp_running += ctx->time - counter->tstamp_stopped;
607 if (!is_software_counter(counter))
608 cpuctx->active_oncpu++;
609 ctx->nr_active++;
611 if (counter->attr.exclusive)
612 cpuctx->exclusive = 1;
614 return 0;
617 static int
618 group_sched_in(struct perf_counter *group_counter,
619 struct perf_cpu_context *cpuctx,
620 struct perf_counter_context *ctx,
621 int cpu)
623 struct perf_counter *counter, *partial_group;
624 int ret;
626 if (group_counter->state == PERF_COUNTER_STATE_OFF)
627 return 0;
629 ret = hw_perf_group_sched_in(group_counter, cpuctx, ctx, cpu);
630 if (ret)
631 return ret < 0 ? ret : 0;
633 if (counter_sched_in(group_counter, cpuctx, ctx, cpu))
634 return -EAGAIN;
637 * Schedule in siblings as one group (if any):
639 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
640 if (counter_sched_in(counter, cpuctx, ctx, cpu)) {
641 partial_group = counter;
642 goto group_error;
646 return 0;
648 group_error:
650 * Groups can be scheduled in as one unit only, so undo any
651 * partial group before returning:
653 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
654 if (counter == partial_group)
655 break;
656 counter_sched_out(counter, cpuctx, ctx);
658 counter_sched_out(group_counter, cpuctx, ctx);
660 return -EAGAIN;
664 * Return 1 for a group consisting entirely of software counters,
665 * 0 if the group contains any hardware counters.
667 static int is_software_only_group(struct perf_counter *leader)
669 struct perf_counter *counter;
671 if (!is_software_counter(leader))
672 return 0;
674 list_for_each_entry(counter, &leader->sibling_list, list_entry)
675 if (!is_software_counter(counter))
676 return 0;
678 return 1;
682 * Work out whether we can put this counter group on the CPU now.
684 static int group_can_go_on(struct perf_counter *counter,
685 struct perf_cpu_context *cpuctx,
686 int can_add_hw)
689 * Groups consisting entirely of software counters can always go on.
691 if (is_software_only_group(counter))
692 return 1;
694 * If an exclusive group is already on, no other hardware
695 * counters can go on.
697 if (cpuctx->exclusive)
698 return 0;
700 * If this group is exclusive and there are already
701 * counters on the CPU, it can't go on.
703 if (counter->attr.exclusive && cpuctx->active_oncpu)
704 return 0;
706 * Otherwise, try to add it if all previous groups were able
707 * to go on.
709 return can_add_hw;
712 static void add_counter_to_ctx(struct perf_counter *counter,
713 struct perf_counter_context *ctx)
715 list_add_counter(counter, ctx);
716 counter->tstamp_enabled = ctx->time;
717 counter->tstamp_running = ctx->time;
718 counter->tstamp_stopped = ctx->time;
722 * Cross CPU call to install and enable a performance counter
724 * Must be called with ctx->mutex held
726 static void __perf_install_in_context(void *info)
728 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
729 struct perf_counter *counter = info;
730 struct perf_counter_context *ctx = counter->ctx;
731 struct perf_counter *leader = counter->group_leader;
732 int cpu = smp_processor_id();
733 int err;
736 * If this is a task context, we need to check whether it is
737 * the current task context of this cpu. If not it has been
738 * scheduled out before the smp call arrived.
739 * Or possibly this is the right context but it isn't
740 * on this cpu because it had no counters.
742 if (ctx->task && cpuctx->task_ctx != ctx) {
743 if (cpuctx->task_ctx || ctx->task != current)
744 return;
745 cpuctx->task_ctx = ctx;
748 spin_lock(&ctx->lock);
749 ctx->is_active = 1;
750 update_context_time(ctx);
753 * Protect the list operation against NMI by disabling the
754 * counters on a global level. NOP for non NMI based counters.
756 perf_disable();
758 add_counter_to_ctx(counter, ctx);
761 * Don't put the counter on if it is disabled or if
762 * it is in a group and the group isn't on.
764 if (counter->state != PERF_COUNTER_STATE_INACTIVE ||
765 (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE))
766 goto unlock;
769 * An exclusive counter can't go on if there are already active
770 * hardware counters, and no hardware counter can go on if there
771 * is already an exclusive counter on.
773 if (!group_can_go_on(counter, cpuctx, 1))
774 err = -EEXIST;
775 else
776 err = counter_sched_in(counter, cpuctx, ctx, cpu);
778 if (err) {
780 * This counter couldn't go on. If it is in a group
781 * then we have to pull the whole group off.
782 * If the counter group is pinned then put it in error state.
784 if (leader != counter)
785 group_sched_out(leader, cpuctx, ctx);
786 if (leader->attr.pinned) {
787 update_group_times(leader);
788 leader->state = PERF_COUNTER_STATE_ERROR;
792 if (!err && !ctx->task && cpuctx->max_pertask)
793 cpuctx->max_pertask--;
795 unlock:
796 perf_enable();
798 spin_unlock(&ctx->lock);
802 * Attach a performance counter to a context
804 * First we add the counter to the list with the hardware enable bit
805 * in counter->hw_config cleared.
807 * If the counter is attached to a task which is on a CPU we use a smp
808 * call to enable it in the task context. The task might have been
809 * scheduled away, but we check this in the smp call again.
811 * Must be called with ctx->mutex held.
813 static void
814 perf_install_in_context(struct perf_counter_context *ctx,
815 struct perf_counter *counter,
816 int cpu)
818 struct task_struct *task = ctx->task;
820 if (!task) {
822 * Per cpu counters are installed via an smp call and
823 * the install is always sucessful.
825 smp_call_function_single(cpu, __perf_install_in_context,
826 counter, 1);
827 return;
830 retry:
831 task_oncpu_function_call(task, __perf_install_in_context,
832 counter);
834 spin_lock_irq(&ctx->lock);
836 * we need to retry the smp call.
838 if (ctx->is_active && list_empty(&counter->list_entry)) {
839 spin_unlock_irq(&ctx->lock);
840 goto retry;
844 * The lock prevents that this context is scheduled in so we
845 * can add the counter safely, if it the call above did not
846 * succeed.
848 if (list_empty(&counter->list_entry))
849 add_counter_to_ctx(counter, ctx);
850 spin_unlock_irq(&ctx->lock);
854 * Cross CPU call to enable a performance counter
856 static void __perf_counter_enable(void *info)
858 struct perf_counter *counter = info;
859 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
860 struct perf_counter_context *ctx = counter->ctx;
861 struct perf_counter *leader = counter->group_leader;
862 int err;
865 * If this is a per-task counter, need to check whether this
866 * counter's task is the current task on this cpu.
868 if (ctx->task && cpuctx->task_ctx != ctx) {
869 if (cpuctx->task_ctx || ctx->task != current)
870 return;
871 cpuctx->task_ctx = ctx;
874 spin_lock(&ctx->lock);
875 ctx->is_active = 1;
876 update_context_time(ctx);
878 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
879 goto unlock;
880 counter->state = PERF_COUNTER_STATE_INACTIVE;
881 counter->tstamp_enabled = ctx->time - counter->total_time_enabled;
884 * If the counter is in a group and isn't the group leader,
885 * then don't put it on unless the group is on.
887 if (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE)
888 goto unlock;
890 if (!group_can_go_on(counter, cpuctx, 1)) {
891 err = -EEXIST;
892 } else {
893 perf_disable();
894 if (counter == leader)
895 err = group_sched_in(counter, cpuctx, ctx,
896 smp_processor_id());
897 else
898 err = counter_sched_in(counter, cpuctx, ctx,
899 smp_processor_id());
900 perf_enable();
903 if (err) {
905 * If this counter can't go on and it's part of a
906 * group, then the whole group has to come off.
908 if (leader != counter)
909 group_sched_out(leader, cpuctx, ctx);
910 if (leader->attr.pinned) {
911 update_group_times(leader);
912 leader->state = PERF_COUNTER_STATE_ERROR;
916 unlock:
917 spin_unlock(&ctx->lock);
921 * Enable a counter.
923 * If counter->ctx is a cloned context, callers must make sure that
924 * every task struct that counter->ctx->task could possibly point to
925 * remains valid. This condition is satisfied when called through
926 * perf_counter_for_each_child or perf_counter_for_each as described
927 * for perf_counter_disable.
929 static void perf_counter_enable(struct perf_counter *counter)
931 struct perf_counter_context *ctx = counter->ctx;
932 struct task_struct *task = ctx->task;
934 if (!task) {
936 * Enable the counter on the cpu that it's on
938 smp_call_function_single(counter->cpu, __perf_counter_enable,
939 counter, 1);
940 return;
943 spin_lock_irq(&ctx->lock);
944 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
945 goto out;
948 * If the counter is in error state, clear that first.
949 * That way, if we see the counter in error state below, we
950 * know that it has gone back into error state, as distinct
951 * from the task having been scheduled away before the
952 * cross-call arrived.
954 if (counter->state == PERF_COUNTER_STATE_ERROR)
955 counter->state = PERF_COUNTER_STATE_OFF;
957 retry:
958 spin_unlock_irq(&ctx->lock);
959 task_oncpu_function_call(task, __perf_counter_enable, counter);
961 spin_lock_irq(&ctx->lock);
964 * If the context is active and the counter is still off,
965 * we need to retry the cross-call.
967 if (ctx->is_active && counter->state == PERF_COUNTER_STATE_OFF)
968 goto retry;
971 * Since we have the lock this context can't be scheduled
972 * in, so we can change the state safely.
974 if (counter->state == PERF_COUNTER_STATE_OFF) {
975 counter->state = PERF_COUNTER_STATE_INACTIVE;
976 counter->tstamp_enabled =
977 ctx->time - counter->total_time_enabled;
979 out:
980 spin_unlock_irq(&ctx->lock);
983 static int perf_counter_refresh(struct perf_counter *counter, int refresh)
986 * not supported on inherited counters
988 if (counter->attr.inherit)
989 return -EINVAL;
991 atomic_add(refresh, &counter->event_limit);
992 perf_counter_enable(counter);
994 return 0;
997 void __perf_counter_sched_out(struct perf_counter_context *ctx,
998 struct perf_cpu_context *cpuctx)
1000 struct perf_counter *counter;
1002 spin_lock(&ctx->lock);
1003 ctx->is_active = 0;
1004 if (likely(!ctx->nr_counters))
1005 goto out;
1006 update_context_time(ctx);
1008 perf_disable();
1009 if (ctx->nr_active) {
1010 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1011 if (counter != counter->group_leader)
1012 counter_sched_out(counter, cpuctx, ctx);
1013 else
1014 group_sched_out(counter, cpuctx, ctx);
1017 perf_enable();
1018 out:
1019 spin_unlock(&ctx->lock);
1023 * Test whether two contexts are equivalent, i.e. whether they
1024 * have both been cloned from the same version of the same context
1025 * and they both have the same number of enabled counters.
1026 * If the number of enabled counters is the same, then the set
1027 * of enabled counters should be the same, because these are both
1028 * inherited contexts, therefore we can't access individual counters
1029 * in them directly with an fd; we can only enable/disable all
1030 * counters via prctl, or enable/disable all counters in a family
1031 * via ioctl, which will have the same effect on both contexts.
1033 static int context_equiv(struct perf_counter_context *ctx1,
1034 struct perf_counter_context *ctx2)
1036 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
1037 && ctx1->parent_gen == ctx2->parent_gen
1038 && !ctx1->pin_count && !ctx2->pin_count;
1041 static void __perf_counter_read(void *counter);
1043 static void __perf_counter_sync_stat(struct perf_counter *counter,
1044 struct perf_counter *next_counter)
1046 u64 value;
1048 if (!counter->attr.inherit_stat)
1049 return;
1052 * Update the counter value, we cannot use perf_counter_read()
1053 * because we're in the middle of a context switch and have IRQs
1054 * disabled, which upsets smp_call_function_single(), however
1055 * we know the counter must be on the current CPU, therefore we
1056 * don't need to use it.
1058 switch (counter->state) {
1059 case PERF_COUNTER_STATE_ACTIVE:
1060 __perf_counter_read(counter);
1061 break;
1063 case PERF_COUNTER_STATE_INACTIVE:
1064 update_counter_times(counter);
1065 break;
1067 default:
1068 break;
1072 * In order to keep per-task stats reliable we need to flip the counter
1073 * values when we flip the contexts.
1075 value = atomic64_read(&next_counter->count);
1076 value = atomic64_xchg(&counter->count, value);
1077 atomic64_set(&next_counter->count, value);
1079 swap(counter->total_time_enabled, next_counter->total_time_enabled);
1080 swap(counter->total_time_running, next_counter->total_time_running);
1083 * Since we swizzled the values, update the user visible data too.
1085 perf_counter_update_userpage(counter);
1086 perf_counter_update_userpage(next_counter);
1089 #define list_next_entry(pos, member) \
1090 list_entry(pos->member.next, typeof(*pos), member)
1092 static void perf_counter_sync_stat(struct perf_counter_context *ctx,
1093 struct perf_counter_context *next_ctx)
1095 struct perf_counter *counter, *next_counter;
1097 if (!ctx->nr_stat)
1098 return;
1100 counter = list_first_entry(&ctx->event_list,
1101 struct perf_counter, event_entry);
1103 next_counter = list_first_entry(&next_ctx->event_list,
1104 struct perf_counter, event_entry);
1106 while (&counter->event_entry != &ctx->event_list &&
1107 &next_counter->event_entry != &next_ctx->event_list) {
1109 __perf_counter_sync_stat(counter, next_counter);
1111 counter = list_next_entry(counter, event_entry);
1112 next_counter = list_next_entry(next_counter, event_entry);
1117 * Called from scheduler to remove the counters of the current task,
1118 * with interrupts disabled.
1120 * We stop each counter and update the counter value in counter->count.
1122 * This does not protect us against NMI, but disable()
1123 * sets the disabled bit in the control field of counter _before_
1124 * accessing the counter control register. If a NMI hits, then it will
1125 * not restart the counter.
1127 void perf_counter_task_sched_out(struct task_struct *task,
1128 struct task_struct *next, int cpu)
1130 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
1131 struct perf_counter_context *ctx = task->perf_counter_ctxp;
1132 struct perf_counter_context *next_ctx;
1133 struct perf_counter_context *parent;
1134 struct pt_regs *regs;
1135 int do_switch = 1;
1137 regs = task_pt_regs(task);
1138 perf_swcounter_event(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 1, regs, 0);
1140 if (likely(!ctx || !cpuctx->task_ctx))
1141 return;
1143 update_context_time(ctx);
1145 rcu_read_lock();
1146 parent = rcu_dereference(ctx->parent_ctx);
1147 next_ctx = next->perf_counter_ctxp;
1148 if (parent && next_ctx &&
1149 rcu_dereference(next_ctx->parent_ctx) == parent) {
1151 * Looks like the two contexts are clones, so we might be
1152 * able to optimize the context switch. We lock both
1153 * contexts and check that they are clones under the
1154 * lock (including re-checking that neither has been
1155 * uncloned in the meantime). It doesn't matter which
1156 * order we take the locks because no other cpu could
1157 * be trying to lock both of these tasks.
1159 spin_lock(&ctx->lock);
1160 spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
1161 if (context_equiv(ctx, next_ctx)) {
1163 * XXX do we need a memory barrier of sorts
1164 * wrt to rcu_dereference() of perf_counter_ctxp
1166 task->perf_counter_ctxp = next_ctx;
1167 next->perf_counter_ctxp = ctx;
1168 ctx->task = next;
1169 next_ctx->task = task;
1170 do_switch = 0;
1172 perf_counter_sync_stat(ctx, next_ctx);
1174 spin_unlock(&next_ctx->lock);
1175 spin_unlock(&ctx->lock);
1177 rcu_read_unlock();
1179 if (do_switch) {
1180 __perf_counter_sched_out(ctx, cpuctx);
1181 cpuctx->task_ctx = NULL;
1186 * Called with IRQs disabled
1188 static void __perf_counter_task_sched_out(struct perf_counter_context *ctx)
1190 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1192 if (!cpuctx->task_ctx)
1193 return;
1195 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
1196 return;
1198 __perf_counter_sched_out(ctx, cpuctx);
1199 cpuctx->task_ctx = NULL;
1203 * Called with IRQs disabled
1205 static void perf_counter_cpu_sched_out(struct perf_cpu_context *cpuctx)
1207 __perf_counter_sched_out(&cpuctx->ctx, cpuctx);
1210 static void
1211 __perf_counter_sched_in(struct perf_counter_context *ctx,
1212 struct perf_cpu_context *cpuctx, int cpu)
1214 struct perf_counter *counter;
1215 int can_add_hw = 1;
1217 spin_lock(&ctx->lock);
1218 ctx->is_active = 1;
1219 if (likely(!ctx->nr_counters))
1220 goto out;
1222 ctx->timestamp = perf_clock();
1224 perf_disable();
1227 * First go through the list and put on any pinned groups
1228 * in order to give them the best chance of going on.
1230 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1231 if (counter->state <= PERF_COUNTER_STATE_OFF ||
1232 !counter->attr.pinned)
1233 continue;
1234 if (counter->cpu != -1 && counter->cpu != cpu)
1235 continue;
1237 if (counter != counter->group_leader)
1238 counter_sched_in(counter, cpuctx, ctx, cpu);
1239 else {
1240 if (group_can_go_on(counter, cpuctx, 1))
1241 group_sched_in(counter, cpuctx, ctx, cpu);
1245 * If this pinned group hasn't been scheduled,
1246 * put it in error state.
1248 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1249 update_group_times(counter);
1250 counter->state = PERF_COUNTER_STATE_ERROR;
1254 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1256 * Ignore counters in OFF or ERROR state, and
1257 * ignore pinned counters since we did them already.
1259 if (counter->state <= PERF_COUNTER_STATE_OFF ||
1260 counter->attr.pinned)
1261 continue;
1264 * Listen to the 'cpu' scheduling filter constraint
1265 * of counters:
1267 if (counter->cpu != -1 && counter->cpu != cpu)
1268 continue;
1270 if (counter != counter->group_leader) {
1271 if (counter_sched_in(counter, cpuctx, ctx, cpu))
1272 can_add_hw = 0;
1273 } else {
1274 if (group_can_go_on(counter, cpuctx, can_add_hw)) {
1275 if (group_sched_in(counter, cpuctx, ctx, cpu))
1276 can_add_hw = 0;
1280 perf_enable();
1281 out:
1282 spin_unlock(&ctx->lock);
1286 * Called from scheduler to add the counters of the current task
1287 * with interrupts disabled.
1289 * We restore the counter value and then enable it.
1291 * This does not protect us against NMI, but enable()
1292 * sets the enabled bit in the control field of counter _before_
1293 * accessing the counter control register. If a NMI hits, then it will
1294 * keep the counter running.
1296 void perf_counter_task_sched_in(struct task_struct *task, int cpu)
1298 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
1299 struct perf_counter_context *ctx = task->perf_counter_ctxp;
1301 if (likely(!ctx))
1302 return;
1303 if (cpuctx->task_ctx == ctx)
1304 return;
1305 __perf_counter_sched_in(ctx, cpuctx, cpu);
1306 cpuctx->task_ctx = ctx;
1309 static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu)
1311 struct perf_counter_context *ctx = &cpuctx->ctx;
1313 __perf_counter_sched_in(ctx, cpuctx, cpu);
1316 #define MAX_INTERRUPTS (~0ULL)
1318 static void perf_log_throttle(struct perf_counter *counter, int enable);
1320 static void perf_adjust_period(struct perf_counter *counter, u64 events)
1322 struct hw_perf_counter *hwc = &counter->hw;
1323 u64 period, sample_period;
1324 s64 delta;
1326 events *= hwc->sample_period;
1327 period = div64_u64(events, counter->attr.sample_freq);
1329 delta = (s64)(period - hwc->sample_period);
1330 delta = (delta + 7) / 8; /* low pass filter */
1332 sample_period = hwc->sample_period + delta;
1334 if (!sample_period)
1335 sample_period = 1;
1337 hwc->sample_period = sample_period;
1340 static void perf_ctx_adjust_freq(struct perf_counter_context *ctx)
1342 struct perf_counter *counter;
1343 struct hw_perf_counter *hwc;
1344 u64 interrupts, freq;
1346 spin_lock(&ctx->lock);
1347 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1348 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
1349 continue;
1351 hwc = &counter->hw;
1353 interrupts = hwc->interrupts;
1354 hwc->interrupts = 0;
1357 * unthrottle counters on the tick
1359 if (interrupts == MAX_INTERRUPTS) {
1360 perf_log_throttle(counter, 1);
1361 counter->pmu->unthrottle(counter);
1362 interrupts = 2*sysctl_perf_counter_sample_rate/HZ;
1365 if (!counter->attr.freq || !counter->attr.sample_freq)
1366 continue;
1369 * if the specified freq < HZ then we need to skip ticks
1371 if (counter->attr.sample_freq < HZ) {
1372 freq = counter->attr.sample_freq;
1374 hwc->freq_count += freq;
1375 hwc->freq_interrupts += interrupts;
1377 if (hwc->freq_count < HZ)
1378 continue;
1380 interrupts = hwc->freq_interrupts;
1381 hwc->freq_interrupts = 0;
1382 hwc->freq_count -= HZ;
1383 } else
1384 freq = HZ;
1386 perf_adjust_period(counter, freq * interrupts);
1389 * In order to avoid being stalled by an (accidental) huge
1390 * sample period, force reset the sample period if we didn't
1391 * get any events in this freq period.
1393 if (!interrupts) {
1394 perf_disable();
1395 counter->pmu->disable(counter);
1396 atomic64_set(&hwc->period_left, 0);
1397 counter->pmu->enable(counter);
1398 perf_enable();
1401 spin_unlock(&ctx->lock);
1405 * Round-robin a context's counters:
1407 static void rotate_ctx(struct perf_counter_context *ctx)
1409 struct perf_counter *counter;
1411 if (!ctx->nr_counters)
1412 return;
1414 spin_lock(&ctx->lock);
1416 * Rotate the first entry last (works just fine for group counters too):
1418 perf_disable();
1419 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1420 list_move_tail(&counter->list_entry, &ctx->counter_list);
1421 break;
1423 perf_enable();
1425 spin_unlock(&ctx->lock);
1428 void perf_counter_task_tick(struct task_struct *curr, int cpu)
1430 struct perf_cpu_context *cpuctx;
1431 struct perf_counter_context *ctx;
1433 if (!atomic_read(&nr_counters))
1434 return;
1436 cpuctx = &per_cpu(perf_cpu_context, cpu);
1437 ctx = curr->perf_counter_ctxp;
1439 perf_ctx_adjust_freq(&cpuctx->ctx);
1440 if (ctx)
1441 perf_ctx_adjust_freq(ctx);
1443 perf_counter_cpu_sched_out(cpuctx);
1444 if (ctx)
1445 __perf_counter_task_sched_out(ctx);
1447 rotate_ctx(&cpuctx->ctx);
1448 if (ctx)
1449 rotate_ctx(ctx);
1451 perf_counter_cpu_sched_in(cpuctx, cpu);
1452 if (ctx)
1453 perf_counter_task_sched_in(curr, cpu);
1457 * Enable all of a task's counters that have been marked enable-on-exec.
1458 * This expects task == current.
1460 static void perf_counter_enable_on_exec(struct task_struct *task)
1462 struct perf_counter_context *ctx;
1463 struct perf_counter *counter;
1464 unsigned long flags;
1465 int enabled = 0;
1467 local_irq_save(flags);
1468 ctx = task->perf_counter_ctxp;
1469 if (!ctx || !ctx->nr_counters)
1470 goto out;
1472 __perf_counter_task_sched_out(ctx);
1474 spin_lock(&ctx->lock);
1476 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1477 if (!counter->attr.enable_on_exec)
1478 continue;
1479 counter->attr.enable_on_exec = 0;
1480 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
1481 continue;
1482 counter->state = PERF_COUNTER_STATE_INACTIVE;
1483 counter->tstamp_enabled =
1484 ctx->time - counter->total_time_enabled;
1485 enabled = 1;
1489 * Unclone this context if we enabled any counter.
1491 if (enabled)
1492 unclone_ctx(ctx);
1494 spin_unlock(&ctx->lock);
1496 perf_counter_task_sched_in(task, smp_processor_id());
1497 out:
1498 local_irq_restore(flags);
1502 * Cross CPU call to read the hardware counter
1504 static void __perf_counter_read(void *info)
1506 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1507 struct perf_counter *counter = info;
1508 struct perf_counter_context *ctx = counter->ctx;
1509 unsigned long flags;
1512 * If this is a task context, we need to check whether it is
1513 * the current task context of this cpu. If not it has been
1514 * scheduled out before the smp call arrived. In that case
1515 * counter->count would have been updated to a recent sample
1516 * when the counter was scheduled out.
1518 if (ctx->task && cpuctx->task_ctx != ctx)
1519 return;
1521 local_irq_save(flags);
1522 if (ctx->is_active)
1523 update_context_time(ctx);
1524 counter->pmu->read(counter);
1525 update_counter_times(counter);
1526 local_irq_restore(flags);
1529 static u64 perf_counter_read(struct perf_counter *counter)
1532 * If counter is enabled and currently active on a CPU, update the
1533 * value in the counter structure:
1535 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
1536 smp_call_function_single(counter->oncpu,
1537 __perf_counter_read, counter, 1);
1538 } else if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1539 update_counter_times(counter);
1542 return atomic64_read(&counter->count);
1546 * Initialize the perf_counter context in a task_struct:
1548 static void
1549 __perf_counter_init_context(struct perf_counter_context *ctx,
1550 struct task_struct *task)
1552 memset(ctx, 0, sizeof(*ctx));
1553 spin_lock_init(&ctx->lock);
1554 mutex_init(&ctx->mutex);
1555 INIT_LIST_HEAD(&ctx->counter_list);
1556 INIT_LIST_HEAD(&ctx->event_list);
1557 atomic_set(&ctx->refcount, 1);
1558 ctx->task = task;
1561 static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
1563 struct perf_counter_context *ctx;
1564 struct perf_cpu_context *cpuctx;
1565 struct task_struct *task;
1566 unsigned long flags;
1567 int err;
1570 * If cpu is not a wildcard then this is a percpu counter:
1572 if (cpu != -1) {
1573 /* Must be root to operate on a CPU counter: */
1574 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
1575 return ERR_PTR(-EACCES);
1577 if (cpu < 0 || cpu > num_possible_cpus())
1578 return ERR_PTR(-EINVAL);
1581 * We could be clever and allow to attach a counter to an
1582 * offline CPU and activate it when the CPU comes up, but
1583 * that's for later.
1585 if (!cpu_isset(cpu, cpu_online_map))
1586 return ERR_PTR(-ENODEV);
1588 cpuctx = &per_cpu(perf_cpu_context, cpu);
1589 ctx = &cpuctx->ctx;
1590 get_ctx(ctx);
1592 return ctx;
1595 rcu_read_lock();
1596 if (!pid)
1597 task = current;
1598 else
1599 task = find_task_by_vpid(pid);
1600 if (task)
1601 get_task_struct(task);
1602 rcu_read_unlock();
1604 if (!task)
1605 return ERR_PTR(-ESRCH);
1608 * Can't attach counters to a dying task.
1610 err = -ESRCH;
1611 if (task->flags & PF_EXITING)
1612 goto errout;
1614 /* Reuse ptrace permission checks for now. */
1615 err = -EACCES;
1616 if (!ptrace_may_access(task, PTRACE_MODE_READ))
1617 goto errout;
1619 retry:
1620 ctx = perf_lock_task_context(task, &flags);
1621 if (ctx) {
1622 unclone_ctx(ctx);
1623 spin_unlock_irqrestore(&ctx->lock, flags);
1626 if (!ctx) {
1627 ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
1628 err = -ENOMEM;
1629 if (!ctx)
1630 goto errout;
1631 __perf_counter_init_context(ctx, task);
1632 get_ctx(ctx);
1633 if (cmpxchg(&task->perf_counter_ctxp, NULL, ctx)) {
1635 * We raced with some other task; use
1636 * the context they set.
1638 kfree(ctx);
1639 goto retry;
1641 get_task_struct(task);
1644 put_task_struct(task);
1645 return ctx;
1647 errout:
1648 put_task_struct(task);
1649 return ERR_PTR(err);
1652 static void free_counter_rcu(struct rcu_head *head)
1654 struct perf_counter *counter;
1656 counter = container_of(head, struct perf_counter, rcu_head);
1657 if (counter->ns)
1658 put_pid_ns(counter->ns);
1659 kfree(counter);
1662 static void perf_pending_sync(struct perf_counter *counter);
1664 static void free_counter(struct perf_counter *counter)
1666 perf_pending_sync(counter);
1668 if (!counter->parent) {
1669 atomic_dec(&nr_counters);
1670 if (counter->attr.mmap)
1671 atomic_dec(&nr_mmap_counters);
1672 if (counter->attr.comm)
1673 atomic_dec(&nr_comm_counters);
1674 if (counter->attr.task)
1675 atomic_dec(&nr_task_counters);
1678 if (counter->destroy)
1679 counter->destroy(counter);
1681 put_ctx(counter->ctx);
1682 call_rcu(&counter->rcu_head, free_counter_rcu);
1686 * Called when the last reference to the file is gone.
1688 static int perf_release(struct inode *inode, struct file *file)
1690 struct perf_counter *counter = file->private_data;
1691 struct perf_counter_context *ctx = counter->ctx;
1693 file->private_data = NULL;
1695 WARN_ON_ONCE(ctx->parent_ctx);
1696 mutex_lock(&ctx->mutex);
1697 perf_counter_remove_from_context(counter);
1698 mutex_unlock(&ctx->mutex);
1700 mutex_lock(&counter->owner->perf_counter_mutex);
1701 list_del_init(&counter->owner_entry);
1702 mutex_unlock(&counter->owner->perf_counter_mutex);
1703 put_task_struct(counter->owner);
1705 free_counter(counter);
1707 return 0;
1710 static int perf_counter_read_size(struct perf_counter *counter)
1712 int entry = sizeof(u64); /* value */
1713 int size = 0;
1714 int nr = 1;
1716 if (counter->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1717 size += sizeof(u64);
1719 if (counter->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1720 size += sizeof(u64);
1722 if (counter->attr.read_format & PERF_FORMAT_ID)
1723 entry += sizeof(u64);
1725 if (counter->attr.read_format & PERF_FORMAT_GROUP) {
1726 nr += counter->group_leader->nr_siblings;
1727 size += sizeof(u64);
1730 size += entry * nr;
1732 return size;
1735 static u64 perf_counter_read_value(struct perf_counter *counter)
1737 struct perf_counter *child;
1738 u64 total = 0;
1740 total += perf_counter_read(counter);
1741 list_for_each_entry(child, &counter->child_list, child_list)
1742 total += perf_counter_read(child);
1744 return total;
1747 static int perf_counter_read_entry(struct perf_counter *counter,
1748 u64 read_format, char __user *buf)
1750 int n = 0, count = 0;
1751 u64 values[2];
1753 values[n++] = perf_counter_read_value(counter);
1754 if (read_format & PERF_FORMAT_ID)
1755 values[n++] = primary_counter_id(counter);
1757 count = n * sizeof(u64);
1759 if (copy_to_user(buf, values, count))
1760 return -EFAULT;
1762 return count;
1765 static int perf_counter_read_group(struct perf_counter *counter,
1766 u64 read_format, char __user *buf)
1768 struct perf_counter *leader = counter->group_leader, *sub;
1769 int n = 0, size = 0, err = -EFAULT;
1770 u64 values[3];
1772 values[n++] = 1 + leader->nr_siblings;
1773 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
1774 values[n++] = leader->total_time_enabled +
1775 atomic64_read(&leader->child_total_time_enabled);
1777 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
1778 values[n++] = leader->total_time_running +
1779 atomic64_read(&leader->child_total_time_running);
1782 size = n * sizeof(u64);
1784 if (copy_to_user(buf, values, size))
1785 return -EFAULT;
1787 err = perf_counter_read_entry(leader, read_format, buf + size);
1788 if (err < 0)
1789 return err;
1791 size += err;
1793 list_for_each_entry(sub, &leader->sibling_list, list_entry) {
1794 err = perf_counter_read_entry(counter, read_format,
1795 buf + size);
1796 if (err < 0)
1797 return err;
1799 size += err;
1802 return size;
1805 static int perf_counter_read_one(struct perf_counter *counter,
1806 u64 read_format, char __user *buf)
1808 u64 values[4];
1809 int n = 0;
1811 values[n++] = perf_counter_read_value(counter);
1812 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
1813 values[n++] = counter->total_time_enabled +
1814 atomic64_read(&counter->child_total_time_enabled);
1816 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
1817 values[n++] = counter->total_time_running +
1818 atomic64_read(&counter->child_total_time_running);
1820 if (read_format & PERF_FORMAT_ID)
1821 values[n++] = primary_counter_id(counter);
1823 if (copy_to_user(buf, values, n * sizeof(u64)))
1824 return -EFAULT;
1826 return n * sizeof(u64);
1830 * Read the performance counter - simple non blocking version for now
1832 static ssize_t
1833 perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
1835 u64 read_format = counter->attr.read_format;
1836 int ret;
1839 * Return end-of-file for a read on a counter that is in
1840 * error state (i.e. because it was pinned but it couldn't be
1841 * scheduled on to the CPU at some point).
1843 if (counter->state == PERF_COUNTER_STATE_ERROR)
1844 return 0;
1846 if (count < perf_counter_read_size(counter))
1847 return -ENOSPC;
1849 WARN_ON_ONCE(counter->ctx->parent_ctx);
1850 mutex_lock(&counter->child_mutex);
1851 if (read_format & PERF_FORMAT_GROUP)
1852 ret = perf_counter_read_group(counter, read_format, buf);
1853 else
1854 ret = perf_counter_read_one(counter, read_format, buf);
1855 mutex_unlock(&counter->child_mutex);
1857 return ret;
1860 static ssize_t
1861 perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
1863 struct perf_counter *counter = file->private_data;
1865 return perf_read_hw(counter, buf, count);
1868 static unsigned int perf_poll(struct file *file, poll_table *wait)
1870 struct perf_counter *counter = file->private_data;
1871 struct perf_mmap_data *data;
1872 unsigned int events = POLL_HUP;
1874 rcu_read_lock();
1875 data = rcu_dereference(counter->data);
1876 if (data)
1877 events = atomic_xchg(&data->poll, 0);
1878 rcu_read_unlock();
1880 poll_wait(file, &counter->waitq, wait);
1882 return events;
1885 static void perf_counter_reset(struct perf_counter *counter)
1887 (void)perf_counter_read(counter);
1888 atomic64_set(&counter->count, 0);
1889 perf_counter_update_userpage(counter);
1893 * Holding the top-level counter's child_mutex means that any
1894 * descendant process that has inherited this counter will block
1895 * in sync_child_counter if it goes to exit, thus satisfying the
1896 * task existence requirements of perf_counter_enable/disable.
1898 static void perf_counter_for_each_child(struct perf_counter *counter,
1899 void (*func)(struct perf_counter *))
1901 struct perf_counter *child;
1903 WARN_ON_ONCE(counter->ctx->parent_ctx);
1904 mutex_lock(&counter->child_mutex);
1905 func(counter);
1906 list_for_each_entry(child, &counter->child_list, child_list)
1907 func(child);
1908 mutex_unlock(&counter->child_mutex);
1911 static void perf_counter_for_each(struct perf_counter *counter,
1912 void (*func)(struct perf_counter *))
1914 struct perf_counter_context *ctx = counter->ctx;
1915 struct perf_counter *sibling;
1917 WARN_ON_ONCE(ctx->parent_ctx);
1918 mutex_lock(&ctx->mutex);
1919 counter = counter->group_leader;
1921 perf_counter_for_each_child(counter, func);
1922 func(counter);
1923 list_for_each_entry(sibling, &counter->sibling_list, list_entry)
1924 perf_counter_for_each_child(counter, func);
1925 mutex_unlock(&ctx->mutex);
1928 static int perf_counter_period(struct perf_counter *counter, u64 __user *arg)
1930 struct perf_counter_context *ctx = counter->ctx;
1931 unsigned long size;
1932 int ret = 0;
1933 u64 value;
1935 if (!counter->attr.sample_period)
1936 return -EINVAL;
1938 size = copy_from_user(&value, arg, sizeof(value));
1939 if (size != sizeof(value))
1940 return -EFAULT;
1942 if (!value)
1943 return -EINVAL;
1945 spin_lock_irq(&ctx->lock);
1946 if (counter->attr.freq) {
1947 if (value > sysctl_perf_counter_sample_rate) {
1948 ret = -EINVAL;
1949 goto unlock;
1952 counter->attr.sample_freq = value;
1953 } else {
1954 counter->attr.sample_period = value;
1955 counter->hw.sample_period = value;
1957 unlock:
1958 spin_unlock_irq(&ctx->lock);
1960 return ret;
1963 static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1965 struct perf_counter *counter = file->private_data;
1966 void (*func)(struct perf_counter *);
1967 u32 flags = arg;
1969 switch (cmd) {
1970 case PERF_COUNTER_IOC_ENABLE:
1971 func = perf_counter_enable;
1972 break;
1973 case PERF_COUNTER_IOC_DISABLE:
1974 func = perf_counter_disable;
1975 break;
1976 case PERF_COUNTER_IOC_RESET:
1977 func = perf_counter_reset;
1978 break;
1980 case PERF_COUNTER_IOC_REFRESH:
1981 return perf_counter_refresh(counter, arg);
1983 case PERF_COUNTER_IOC_PERIOD:
1984 return perf_counter_period(counter, (u64 __user *)arg);
1986 default:
1987 return -ENOTTY;
1990 if (flags & PERF_IOC_FLAG_GROUP)
1991 perf_counter_for_each(counter, func);
1992 else
1993 perf_counter_for_each_child(counter, func);
1995 return 0;
1998 int perf_counter_task_enable(void)
2000 struct perf_counter *counter;
2002 mutex_lock(&current->perf_counter_mutex);
2003 list_for_each_entry(counter, &current->perf_counter_list, owner_entry)
2004 perf_counter_for_each_child(counter, perf_counter_enable);
2005 mutex_unlock(&current->perf_counter_mutex);
2007 return 0;
2010 int perf_counter_task_disable(void)
2012 struct perf_counter *counter;
2014 mutex_lock(&current->perf_counter_mutex);
2015 list_for_each_entry(counter, &current->perf_counter_list, owner_entry)
2016 perf_counter_for_each_child(counter, perf_counter_disable);
2017 mutex_unlock(&current->perf_counter_mutex);
2019 return 0;
2022 #ifndef PERF_COUNTER_INDEX_OFFSET
2023 # define PERF_COUNTER_INDEX_OFFSET 0
2024 #endif
2026 static int perf_counter_index(struct perf_counter *counter)
2028 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
2029 return 0;
2031 return counter->hw.idx + 1 - PERF_COUNTER_INDEX_OFFSET;
2035 * Callers need to ensure there can be no nesting of this function, otherwise
2036 * the seqlock logic goes bad. We can not serialize this because the arch
2037 * code calls this from NMI context.
2039 void perf_counter_update_userpage(struct perf_counter *counter)
2041 struct perf_counter_mmap_page *userpg;
2042 struct perf_mmap_data *data;
2044 rcu_read_lock();
2045 data = rcu_dereference(counter->data);
2046 if (!data)
2047 goto unlock;
2049 userpg = data->user_page;
2052 * Disable preemption so as to not let the corresponding user-space
2053 * spin too long if we get preempted.
2055 preempt_disable();
2056 ++userpg->lock;
2057 barrier();
2058 userpg->index = perf_counter_index(counter);
2059 userpg->offset = atomic64_read(&counter->count);
2060 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
2061 userpg->offset -= atomic64_read(&counter->hw.prev_count);
2063 userpg->time_enabled = counter->total_time_enabled +
2064 atomic64_read(&counter->child_total_time_enabled);
2066 userpg->time_running = counter->total_time_running +
2067 atomic64_read(&counter->child_total_time_running);
2069 barrier();
2070 ++userpg->lock;
2071 preempt_enable();
2072 unlock:
2073 rcu_read_unlock();
2076 static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
2078 struct perf_counter *counter = vma->vm_file->private_data;
2079 struct perf_mmap_data *data;
2080 int ret = VM_FAULT_SIGBUS;
2082 if (vmf->flags & FAULT_FLAG_MKWRITE) {
2083 if (vmf->pgoff == 0)
2084 ret = 0;
2085 return ret;
2088 rcu_read_lock();
2089 data = rcu_dereference(counter->data);
2090 if (!data)
2091 goto unlock;
2093 if (vmf->pgoff == 0) {
2094 vmf->page = virt_to_page(data->user_page);
2095 } else {
2096 int nr = vmf->pgoff - 1;
2098 if ((unsigned)nr > data->nr_pages)
2099 goto unlock;
2101 if (vmf->flags & FAULT_FLAG_WRITE)
2102 goto unlock;
2104 vmf->page = virt_to_page(data->data_pages[nr]);
2107 get_page(vmf->page);
2108 vmf->page->mapping = vma->vm_file->f_mapping;
2109 vmf->page->index = vmf->pgoff;
2111 ret = 0;
2112 unlock:
2113 rcu_read_unlock();
2115 return ret;
2118 static int perf_mmap_data_alloc(struct perf_counter *counter, int nr_pages)
2120 struct perf_mmap_data *data;
2121 unsigned long size;
2122 int i;
2124 WARN_ON(atomic_read(&counter->mmap_count));
2126 size = sizeof(struct perf_mmap_data);
2127 size += nr_pages * sizeof(void *);
2129 data = kzalloc(size, GFP_KERNEL);
2130 if (!data)
2131 goto fail;
2133 data->user_page = (void *)get_zeroed_page(GFP_KERNEL);
2134 if (!data->user_page)
2135 goto fail_user_page;
2137 for (i = 0; i < nr_pages; i++) {
2138 data->data_pages[i] = (void *)get_zeroed_page(GFP_KERNEL);
2139 if (!data->data_pages[i])
2140 goto fail_data_pages;
2143 data->nr_pages = nr_pages;
2144 atomic_set(&data->lock, -1);
2146 rcu_assign_pointer(counter->data, data);
2148 return 0;
2150 fail_data_pages:
2151 for (i--; i >= 0; i--)
2152 free_page((unsigned long)data->data_pages[i]);
2154 free_page((unsigned long)data->user_page);
2156 fail_user_page:
2157 kfree(data);
2159 fail:
2160 return -ENOMEM;
2163 static void perf_mmap_free_page(unsigned long addr)
2165 struct page *page = virt_to_page((void *)addr);
2167 page->mapping = NULL;
2168 __free_page(page);
2171 static void __perf_mmap_data_free(struct rcu_head *rcu_head)
2173 struct perf_mmap_data *data;
2174 int i;
2176 data = container_of(rcu_head, struct perf_mmap_data, rcu_head);
2178 perf_mmap_free_page((unsigned long)data->user_page);
2179 for (i = 0; i < data->nr_pages; i++)
2180 perf_mmap_free_page((unsigned long)data->data_pages[i]);
2182 kfree(data);
2185 static void perf_mmap_data_free(struct perf_counter *counter)
2187 struct perf_mmap_data *data = counter->data;
2189 WARN_ON(atomic_read(&counter->mmap_count));
2191 rcu_assign_pointer(counter->data, NULL);
2192 call_rcu(&data->rcu_head, __perf_mmap_data_free);
2195 static void perf_mmap_open(struct vm_area_struct *vma)
2197 struct perf_counter *counter = vma->vm_file->private_data;
2199 atomic_inc(&counter->mmap_count);
2202 static void perf_mmap_close(struct vm_area_struct *vma)
2204 struct perf_counter *counter = vma->vm_file->private_data;
2206 WARN_ON_ONCE(counter->ctx->parent_ctx);
2207 if (atomic_dec_and_mutex_lock(&counter->mmap_count, &counter->mmap_mutex)) {
2208 struct user_struct *user = current_user();
2210 atomic_long_sub(counter->data->nr_pages + 1, &user->locked_vm);
2211 vma->vm_mm->locked_vm -= counter->data->nr_locked;
2212 perf_mmap_data_free(counter);
2213 mutex_unlock(&counter->mmap_mutex);
2217 static struct vm_operations_struct perf_mmap_vmops = {
2218 .open = perf_mmap_open,
2219 .close = perf_mmap_close,
2220 .fault = perf_mmap_fault,
2221 .page_mkwrite = perf_mmap_fault,
2224 static int perf_mmap(struct file *file, struct vm_area_struct *vma)
2226 struct perf_counter *counter = file->private_data;
2227 unsigned long user_locked, user_lock_limit;
2228 struct user_struct *user = current_user();
2229 unsigned long locked, lock_limit;
2230 unsigned long vma_size;
2231 unsigned long nr_pages;
2232 long user_extra, extra;
2233 int ret = 0;
2235 if (!(vma->vm_flags & VM_SHARED))
2236 return -EINVAL;
2238 vma_size = vma->vm_end - vma->vm_start;
2239 nr_pages = (vma_size / PAGE_SIZE) - 1;
2242 * If we have data pages ensure they're a power-of-two number, so we
2243 * can do bitmasks instead of modulo.
2245 if (nr_pages != 0 && !is_power_of_2(nr_pages))
2246 return -EINVAL;
2248 if (vma_size != PAGE_SIZE * (1 + nr_pages))
2249 return -EINVAL;
2251 if (vma->vm_pgoff != 0)
2252 return -EINVAL;
2254 WARN_ON_ONCE(counter->ctx->parent_ctx);
2255 mutex_lock(&counter->mmap_mutex);
2256 if (atomic_inc_not_zero(&counter->mmap_count)) {
2257 if (nr_pages != counter->data->nr_pages)
2258 ret = -EINVAL;
2259 goto unlock;
2262 user_extra = nr_pages + 1;
2263 user_lock_limit = sysctl_perf_counter_mlock >> (PAGE_SHIFT - 10);
2266 * Increase the limit linearly with more CPUs:
2268 user_lock_limit *= num_online_cpus();
2270 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
2272 extra = 0;
2273 if (user_locked > user_lock_limit)
2274 extra = user_locked - user_lock_limit;
2276 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
2277 lock_limit >>= PAGE_SHIFT;
2278 locked = vma->vm_mm->locked_vm + extra;
2280 if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) {
2281 ret = -EPERM;
2282 goto unlock;
2285 WARN_ON(counter->data);
2286 ret = perf_mmap_data_alloc(counter, nr_pages);
2287 if (ret)
2288 goto unlock;
2290 atomic_set(&counter->mmap_count, 1);
2291 atomic_long_add(user_extra, &user->locked_vm);
2292 vma->vm_mm->locked_vm += extra;
2293 counter->data->nr_locked = extra;
2294 if (vma->vm_flags & VM_WRITE)
2295 counter->data->writable = 1;
2297 unlock:
2298 mutex_unlock(&counter->mmap_mutex);
2300 vma->vm_flags |= VM_RESERVED;
2301 vma->vm_ops = &perf_mmap_vmops;
2303 return ret;
2306 static int perf_fasync(int fd, struct file *filp, int on)
2308 struct inode *inode = filp->f_path.dentry->d_inode;
2309 struct perf_counter *counter = filp->private_data;
2310 int retval;
2312 mutex_lock(&inode->i_mutex);
2313 retval = fasync_helper(fd, filp, on, &counter->fasync);
2314 mutex_unlock(&inode->i_mutex);
2316 if (retval < 0)
2317 return retval;
2319 return 0;
2322 static const struct file_operations perf_fops = {
2323 .release = perf_release,
2324 .read = perf_read,
2325 .poll = perf_poll,
2326 .unlocked_ioctl = perf_ioctl,
2327 .compat_ioctl = perf_ioctl,
2328 .mmap = perf_mmap,
2329 .fasync = perf_fasync,
2333 * Perf counter wakeup
2335 * If there's data, ensure we set the poll() state and publish everything
2336 * to user-space before waking everybody up.
2339 void perf_counter_wakeup(struct perf_counter *counter)
2341 wake_up_all(&counter->waitq);
2343 if (counter->pending_kill) {
2344 kill_fasync(&counter->fasync, SIGIO, counter->pending_kill);
2345 counter->pending_kill = 0;
2350 * Pending wakeups
2352 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
2354 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
2355 * single linked list and use cmpxchg() to add entries lockless.
2358 static void perf_pending_counter(struct perf_pending_entry *entry)
2360 struct perf_counter *counter = container_of(entry,
2361 struct perf_counter, pending);
2363 if (counter->pending_disable) {
2364 counter->pending_disable = 0;
2365 __perf_counter_disable(counter);
2368 if (counter->pending_wakeup) {
2369 counter->pending_wakeup = 0;
2370 perf_counter_wakeup(counter);
2374 #define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
2376 static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
2377 PENDING_TAIL,
2380 static void perf_pending_queue(struct perf_pending_entry *entry,
2381 void (*func)(struct perf_pending_entry *))
2383 struct perf_pending_entry **head;
2385 if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
2386 return;
2388 entry->func = func;
2390 head = &get_cpu_var(perf_pending_head);
2392 do {
2393 entry->next = *head;
2394 } while (cmpxchg(head, entry->next, entry) != entry->next);
2396 set_perf_counter_pending();
2398 put_cpu_var(perf_pending_head);
2401 static int __perf_pending_run(void)
2403 struct perf_pending_entry *list;
2404 int nr = 0;
2406 list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
2407 while (list != PENDING_TAIL) {
2408 void (*func)(struct perf_pending_entry *);
2409 struct perf_pending_entry *entry = list;
2411 list = list->next;
2413 func = entry->func;
2414 entry->next = NULL;
2416 * Ensure we observe the unqueue before we issue the wakeup,
2417 * so that we won't be waiting forever.
2418 * -- see perf_not_pending().
2420 smp_wmb();
2422 func(entry);
2423 nr++;
2426 return nr;
2429 static inline int perf_not_pending(struct perf_counter *counter)
2432 * If we flush on whatever cpu we run, there is a chance we don't
2433 * need to wait.
2435 get_cpu();
2436 __perf_pending_run();
2437 put_cpu();
2440 * Ensure we see the proper queue state before going to sleep
2441 * so that we do not miss the wakeup. -- see perf_pending_handle()
2443 smp_rmb();
2444 return counter->pending.next == NULL;
2447 static void perf_pending_sync(struct perf_counter *counter)
2449 wait_event(counter->waitq, perf_not_pending(counter));
2452 void perf_counter_do_pending(void)
2454 __perf_pending_run();
2458 * Callchain support -- arch specific
2461 __weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
2463 return NULL;
2467 * Output
2470 struct perf_output_handle {
2471 struct perf_counter *counter;
2472 struct perf_mmap_data *data;
2473 unsigned long head;
2474 unsigned long offset;
2475 int nmi;
2476 int sample;
2477 int locked;
2478 unsigned long flags;
2481 static bool perf_output_space(struct perf_mmap_data *data,
2482 unsigned int offset, unsigned int head)
2484 unsigned long tail;
2485 unsigned long mask;
2487 if (!data->writable)
2488 return true;
2490 mask = (data->nr_pages << PAGE_SHIFT) - 1;
2492 * Userspace could choose to issue a mb() before updating the tail
2493 * pointer. So that all reads will be completed before the write is
2494 * issued.
2496 tail = ACCESS_ONCE(data->user_page->data_tail);
2497 smp_rmb();
2499 offset = (offset - tail) & mask;
2500 head = (head - tail) & mask;
2502 if ((int)(head - offset) < 0)
2503 return false;
2505 return true;
2508 static void perf_output_wakeup(struct perf_output_handle *handle)
2510 atomic_set(&handle->data->poll, POLL_IN);
2512 if (handle->nmi) {
2513 handle->counter->pending_wakeup = 1;
2514 perf_pending_queue(&handle->counter->pending,
2515 perf_pending_counter);
2516 } else
2517 perf_counter_wakeup(handle->counter);
2521 * Curious locking construct.
2523 * We need to ensure a later event doesn't publish a head when a former
2524 * event isn't done writing. However since we need to deal with NMIs we
2525 * cannot fully serialize things.
2527 * What we do is serialize between CPUs so we only have to deal with NMI
2528 * nesting on a single CPU.
2530 * We only publish the head (and generate a wakeup) when the outer-most
2531 * event completes.
2533 static void perf_output_lock(struct perf_output_handle *handle)
2535 struct perf_mmap_data *data = handle->data;
2536 int cpu;
2538 handle->locked = 0;
2540 local_irq_save(handle->flags);
2541 cpu = smp_processor_id();
2543 if (in_nmi() && atomic_read(&data->lock) == cpu)
2544 return;
2546 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
2547 cpu_relax();
2549 handle->locked = 1;
2552 static void perf_output_unlock(struct perf_output_handle *handle)
2554 struct perf_mmap_data *data = handle->data;
2555 unsigned long head;
2556 int cpu;
2558 data->done_head = data->head;
2560 if (!handle->locked)
2561 goto out;
2563 again:
2565 * The xchg implies a full barrier that ensures all writes are done
2566 * before we publish the new head, matched by a rmb() in userspace when
2567 * reading this position.
2569 while ((head = atomic_long_xchg(&data->done_head, 0)))
2570 data->user_page->data_head = head;
2573 * NMI can happen here, which means we can miss a done_head update.
2576 cpu = atomic_xchg(&data->lock, -1);
2577 WARN_ON_ONCE(cpu != smp_processor_id());
2580 * Therefore we have to validate we did not indeed do so.
2582 if (unlikely(atomic_long_read(&data->done_head))) {
2584 * Since we had it locked, we can lock it again.
2586 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
2587 cpu_relax();
2589 goto again;
2592 if (atomic_xchg(&data->wakeup, 0))
2593 perf_output_wakeup(handle);
2594 out:
2595 local_irq_restore(handle->flags);
2598 static void perf_output_copy(struct perf_output_handle *handle,
2599 const void *buf, unsigned int len)
2601 unsigned int pages_mask;
2602 unsigned int offset;
2603 unsigned int size;
2604 void **pages;
2606 offset = handle->offset;
2607 pages_mask = handle->data->nr_pages - 1;
2608 pages = handle->data->data_pages;
2610 do {
2611 unsigned int page_offset;
2612 int nr;
2614 nr = (offset >> PAGE_SHIFT) & pages_mask;
2615 page_offset = offset & (PAGE_SIZE - 1);
2616 size = min_t(unsigned int, PAGE_SIZE - page_offset, len);
2618 memcpy(pages[nr] + page_offset, buf, size);
2620 len -= size;
2621 buf += size;
2622 offset += size;
2623 } while (len);
2625 handle->offset = offset;
2628 * Check we didn't copy past our reservation window, taking the
2629 * possible unsigned int wrap into account.
2631 WARN_ON_ONCE(((long)(handle->head - handle->offset)) < 0);
2634 #define perf_output_put(handle, x) \
2635 perf_output_copy((handle), &(x), sizeof(x))
2637 static int perf_output_begin(struct perf_output_handle *handle,
2638 struct perf_counter *counter, unsigned int size,
2639 int nmi, int sample)
2641 struct perf_mmap_data *data;
2642 unsigned int offset, head;
2643 int have_lost;
2644 struct {
2645 struct perf_event_header header;
2646 u64 id;
2647 u64 lost;
2648 } lost_event;
2651 * For inherited counters we send all the output towards the parent.
2653 if (counter->parent)
2654 counter = counter->parent;
2656 rcu_read_lock();
2657 data = rcu_dereference(counter->data);
2658 if (!data)
2659 goto out;
2661 handle->data = data;
2662 handle->counter = counter;
2663 handle->nmi = nmi;
2664 handle->sample = sample;
2666 if (!data->nr_pages)
2667 goto fail;
2669 have_lost = atomic_read(&data->lost);
2670 if (have_lost)
2671 size += sizeof(lost_event);
2673 perf_output_lock(handle);
2675 do {
2676 offset = head = atomic_long_read(&data->head);
2677 head += size;
2678 if (unlikely(!perf_output_space(data, offset, head)))
2679 goto fail;
2680 } while (atomic_long_cmpxchg(&data->head, offset, head) != offset);
2682 handle->offset = offset;
2683 handle->head = head;
2685 if ((offset >> PAGE_SHIFT) != (head >> PAGE_SHIFT))
2686 atomic_set(&data->wakeup, 1);
2688 if (have_lost) {
2689 lost_event.header.type = PERF_EVENT_LOST;
2690 lost_event.header.misc = 0;
2691 lost_event.header.size = sizeof(lost_event);
2692 lost_event.id = counter->id;
2693 lost_event.lost = atomic_xchg(&data->lost, 0);
2695 perf_output_put(handle, lost_event);
2698 return 0;
2700 fail:
2701 atomic_inc(&data->lost);
2702 perf_output_unlock(handle);
2703 out:
2704 rcu_read_unlock();
2706 return -ENOSPC;
2709 static void perf_output_end(struct perf_output_handle *handle)
2711 struct perf_counter *counter = handle->counter;
2712 struct perf_mmap_data *data = handle->data;
2714 int wakeup_events = counter->attr.wakeup_events;
2716 if (handle->sample && wakeup_events) {
2717 int events = atomic_inc_return(&data->events);
2718 if (events >= wakeup_events) {
2719 atomic_sub(wakeup_events, &data->events);
2720 atomic_set(&data->wakeup, 1);
2724 perf_output_unlock(handle);
2725 rcu_read_unlock();
2728 static u32 perf_counter_pid(struct perf_counter *counter, struct task_struct *p)
2731 * only top level counters have the pid namespace they were created in
2733 if (counter->parent)
2734 counter = counter->parent;
2736 return task_tgid_nr_ns(p, counter->ns);
2739 static u32 perf_counter_tid(struct perf_counter *counter, struct task_struct *p)
2742 * only top level counters have the pid namespace they were created in
2744 if (counter->parent)
2745 counter = counter->parent;
2747 return task_pid_nr_ns(p, counter->ns);
2750 static void perf_output_read_one(struct perf_output_handle *handle,
2751 struct perf_counter *counter)
2753 u64 read_format = counter->attr.read_format;
2754 u64 values[4];
2755 int n = 0;
2757 values[n++] = atomic64_read(&counter->count);
2758 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
2759 values[n++] = counter->total_time_enabled +
2760 atomic64_read(&counter->child_total_time_enabled);
2762 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
2763 values[n++] = counter->total_time_running +
2764 atomic64_read(&counter->child_total_time_running);
2766 if (read_format & PERF_FORMAT_ID)
2767 values[n++] = primary_counter_id(counter);
2769 perf_output_copy(handle, values, n * sizeof(u64));
2773 * XXX PERF_FORMAT_GROUP vs inherited counters seems difficult.
2775 static void perf_output_read_group(struct perf_output_handle *handle,
2776 struct perf_counter *counter)
2778 struct perf_counter *leader = counter->group_leader, *sub;
2779 u64 read_format = counter->attr.read_format;
2780 u64 values[5];
2781 int n = 0;
2783 values[n++] = 1 + leader->nr_siblings;
2785 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2786 values[n++] = leader->total_time_enabled;
2788 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2789 values[n++] = leader->total_time_running;
2791 if (leader != counter)
2792 leader->pmu->read(leader);
2794 values[n++] = atomic64_read(&leader->count);
2795 if (read_format & PERF_FORMAT_ID)
2796 values[n++] = primary_counter_id(leader);
2798 perf_output_copy(handle, values, n * sizeof(u64));
2800 list_for_each_entry(sub, &leader->sibling_list, list_entry) {
2801 n = 0;
2803 if (sub != counter)
2804 sub->pmu->read(sub);
2806 values[n++] = atomic64_read(&sub->count);
2807 if (read_format & PERF_FORMAT_ID)
2808 values[n++] = primary_counter_id(sub);
2810 perf_output_copy(handle, values, n * sizeof(u64));
2814 static void perf_output_read(struct perf_output_handle *handle,
2815 struct perf_counter *counter)
2817 if (counter->attr.read_format & PERF_FORMAT_GROUP)
2818 perf_output_read_group(handle, counter);
2819 else
2820 perf_output_read_one(handle, counter);
2823 void perf_counter_output(struct perf_counter *counter, int nmi,
2824 struct perf_sample_data *data)
2826 int ret;
2827 u64 sample_type = counter->attr.sample_type;
2828 struct perf_output_handle handle;
2829 struct perf_event_header header;
2830 u64 ip;
2831 struct {
2832 u32 pid, tid;
2833 } tid_entry;
2834 struct perf_callchain_entry *callchain = NULL;
2835 int callchain_size = 0;
2836 u64 time;
2837 struct {
2838 u32 cpu, reserved;
2839 } cpu_entry;
2841 header.type = PERF_EVENT_SAMPLE;
2842 header.size = sizeof(header);
2844 header.misc = 0;
2845 header.misc |= perf_misc_flags(data->regs);
2847 if (sample_type & PERF_SAMPLE_IP) {
2848 ip = perf_instruction_pointer(data->regs);
2849 header.size += sizeof(ip);
2852 if (sample_type & PERF_SAMPLE_TID) {
2853 /* namespace issues */
2854 tid_entry.pid = perf_counter_pid(counter, current);
2855 tid_entry.tid = perf_counter_tid(counter, current);
2857 header.size += sizeof(tid_entry);
2860 if (sample_type & PERF_SAMPLE_TIME) {
2862 * Maybe do better on x86 and provide cpu_clock_nmi()
2864 time = sched_clock();
2866 header.size += sizeof(u64);
2869 if (sample_type & PERF_SAMPLE_ADDR)
2870 header.size += sizeof(u64);
2872 if (sample_type & PERF_SAMPLE_ID)
2873 header.size += sizeof(u64);
2875 if (sample_type & PERF_SAMPLE_STREAM_ID)
2876 header.size += sizeof(u64);
2878 if (sample_type & PERF_SAMPLE_CPU) {
2879 header.size += sizeof(cpu_entry);
2881 cpu_entry.cpu = raw_smp_processor_id();
2882 cpu_entry.reserved = 0;
2885 if (sample_type & PERF_SAMPLE_PERIOD)
2886 header.size += sizeof(u64);
2888 if (sample_type & PERF_SAMPLE_READ)
2889 header.size += perf_counter_read_size(counter);
2891 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
2892 callchain = perf_callchain(data->regs);
2894 if (callchain) {
2895 callchain_size = (1 + callchain->nr) * sizeof(u64);
2896 header.size += callchain_size;
2897 } else
2898 header.size += sizeof(u64);
2901 if (sample_type & PERF_SAMPLE_RAW) {
2902 int size = sizeof(u32);
2904 if (data->raw)
2905 size += data->raw->size;
2906 else
2907 size += sizeof(u32);
2909 WARN_ON_ONCE(size & (sizeof(u64)-1));
2910 header.size += size;
2913 ret = perf_output_begin(&handle, counter, header.size, nmi, 1);
2914 if (ret)
2915 return;
2917 perf_output_put(&handle, header);
2919 if (sample_type & PERF_SAMPLE_IP)
2920 perf_output_put(&handle, ip);
2922 if (sample_type & PERF_SAMPLE_TID)
2923 perf_output_put(&handle, tid_entry);
2925 if (sample_type & PERF_SAMPLE_TIME)
2926 perf_output_put(&handle, time);
2928 if (sample_type & PERF_SAMPLE_ADDR)
2929 perf_output_put(&handle, data->addr);
2931 if (sample_type & PERF_SAMPLE_ID) {
2932 u64 id = primary_counter_id(counter);
2934 perf_output_put(&handle, id);
2937 if (sample_type & PERF_SAMPLE_STREAM_ID)
2938 perf_output_put(&handle, counter->id);
2940 if (sample_type & PERF_SAMPLE_CPU)
2941 perf_output_put(&handle, cpu_entry);
2943 if (sample_type & PERF_SAMPLE_PERIOD)
2944 perf_output_put(&handle, data->period);
2946 if (sample_type & PERF_SAMPLE_READ)
2947 perf_output_read(&handle, counter);
2949 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
2950 if (callchain)
2951 perf_output_copy(&handle, callchain, callchain_size);
2952 else {
2953 u64 nr = 0;
2954 perf_output_put(&handle, nr);
2958 if (sample_type & PERF_SAMPLE_RAW) {
2959 if (data->raw) {
2960 perf_output_put(&handle, data->raw->size);
2961 perf_output_copy(&handle, data->raw->data, data->raw->size);
2962 } else {
2963 struct {
2964 u32 size;
2965 u32 data;
2966 } raw = {
2967 .size = sizeof(u32),
2968 .data = 0,
2970 perf_output_put(&handle, raw);
2974 perf_output_end(&handle);
2978 * read event
2981 struct perf_read_event {
2982 struct perf_event_header header;
2984 u32 pid;
2985 u32 tid;
2988 static void
2989 perf_counter_read_event(struct perf_counter *counter,
2990 struct task_struct *task)
2992 struct perf_output_handle handle;
2993 struct perf_read_event event = {
2994 .header = {
2995 .type = PERF_EVENT_READ,
2996 .misc = 0,
2997 .size = sizeof(event) + perf_counter_read_size(counter),
2999 .pid = perf_counter_pid(counter, task),
3000 .tid = perf_counter_tid(counter, task),
3002 int ret;
3004 ret = perf_output_begin(&handle, counter, event.header.size, 0, 0);
3005 if (ret)
3006 return;
3008 perf_output_put(&handle, event);
3009 perf_output_read(&handle, counter);
3011 perf_output_end(&handle);
3015 * task tracking -- fork/exit
3017 * enabled by: attr.comm | attr.mmap | attr.task
3020 struct perf_task_event {
3021 struct task_struct *task;
3022 struct perf_counter_context *task_ctx;
3024 struct {
3025 struct perf_event_header header;
3027 u32 pid;
3028 u32 ppid;
3029 u32 tid;
3030 u32 ptid;
3031 } event;
3034 static void perf_counter_task_output(struct perf_counter *counter,
3035 struct perf_task_event *task_event)
3037 struct perf_output_handle handle;
3038 int size = task_event->event.header.size;
3039 struct task_struct *task = task_event->task;
3040 int ret = perf_output_begin(&handle, counter, size, 0, 0);
3042 if (ret)
3043 return;
3045 task_event->event.pid = perf_counter_pid(counter, task);
3046 task_event->event.ppid = perf_counter_pid(counter, current);
3048 task_event->event.tid = perf_counter_tid(counter, task);
3049 task_event->event.ptid = perf_counter_tid(counter, current);
3051 perf_output_put(&handle, task_event->event);
3052 perf_output_end(&handle);
3055 static int perf_counter_task_match(struct perf_counter *counter)
3057 if (counter->attr.comm || counter->attr.mmap || counter->attr.task)
3058 return 1;
3060 return 0;
3063 static void perf_counter_task_ctx(struct perf_counter_context *ctx,
3064 struct perf_task_event *task_event)
3066 struct perf_counter *counter;
3068 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
3069 return;
3071 rcu_read_lock();
3072 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
3073 if (perf_counter_task_match(counter))
3074 perf_counter_task_output(counter, task_event);
3076 rcu_read_unlock();
3079 static void perf_counter_task_event(struct perf_task_event *task_event)
3081 struct perf_cpu_context *cpuctx;
3082 struct perf_counter_context *ctx = task_event->task_ctx;
3084 cpuctx = &get_cpu_var(perf_cpu_context);
3085 perf_counter_task_ctx(&cpuctx->ctx, task_event);
3086 put_cpu_var(perf_cpu_context);
3088 rcu_read_lock();
3089 if (!ctx)
3090 ctx = rcu_dereference(task_event->task->perf_counter_ctxp);
3091 if (ctx)
3092 perf_counter_task_ctx(ctx, task_event);
3093 rcu_read_unlock();
3096 static void perf_counter_task(struct task_struct *task,
3097 struct perf_counter_context *task_ctx,
3098 int new)
3100 struct perf_task_event task_event;
3102 if (!atomic_read(&nr_comm_counters) &&
3103 !atomic_read(&nr_mmap_counters) &&
3104 !atomic_read(&nr_task_counters))
3105 return;
3107 task_event = (struct perf_task_event){
3108 .task = task,
3109 .task_ctx = task_ctx,
3110 .event = {
3111 .header = {
3112 .type = new ? PERF_EVENT_FORK : PERF_EVENT_EXIT,
3113 .misc = 0,
3114 .size = sizeof(task_event.event),
3116 /* .pid */
3117 /* .ppid */
3118 /* .tid */
3119 /* .ptid */
3123 perf_counter_task_event(&task_event);
3126 void perf_counter_fork(struct task_struct *task)
3128 perf_counter_task(task, NULL, 1);
3132 * comm tracking
3135 struct perf_comm_event {
3136 struct task_struct *task;
3137 char *comm;
3138 int comm_size;
3140 struct {
3141 struct perf_event_header header;
3143 u32 pid;
3144 u32 tid;
3145 } event;
3148 static void perf_counter_comm_output(struct perf_counter *counter,
3149 struct perf_comm_event *comm_event)
3151 struct perf_output_handle handle;
3152 int size = comm_event->event.header.size;
3153 int ret = perf_output_begin(&handle, counter, size, 0, 0);
3155 if (ret)
3156 return;
3158 comm_event->event.pid = perf_counter_pid(counter, comm_event->task);
3159 comm_event->event.tid = perf_counter_tid(counter, comm_event->task);
3161 perf_output_put(&handle, comm_event->event);
3162 perf_output_copy(&handle, comm_event->comm,
3163 comm_event->comm_size);
3164 perf_output_end(&handle);
3167 static int perf_counter_comm_match(struct perf_counter *counter)
3169 if (counter->attr.comm)
3170 return 1;
3172 return 0;
3175 static void perf_counter_comm_ctx(struct perf_counter_context *ctx,
3176 struct perf_comm_event *comm_event)
3178 struct perf_counter *counter;
3180 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
3181 return;
3183 rcu_read_lock();
3184 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
3185 if (perf_counter_comm_match(counter))
3186 perf_counter_comm_output(counter, comm_event);
3188 rcu_read_unlock();
3191 static void perf_counter_comm_event(struct perf_comm_event *comm_event)
3193 struct perf_cpu_context *cpuctx;
3194 struct perf_counter_context *ctx;
3195 unsigned int size;
3196 char comm[TASK_COMM_LEN];
3198 memset(comm, 0, sizeof(comm));
3199 strncpy(comm, comm_event->task->comm, sizeof(comm));
3200 size = ALIGN(strlen(comm)+1, sizeof(u64));
3202 comm_event->comm = comm;
3203 comm_event->comm_size = size;
3205 comm_event->event.header.size = sizeof(comm_event->event) + size;
3207 cpuctx = &get_cpu_var(perf_cpu_context);
3208 perf_counter_comm_ctx(&cpuctx->ctx, comm_event);
3209 put_cpu_var(perf_cpu_context);
3211 rcu_read_lock();
3213 * doesn't really matter which of the child contexts the
3214 * events ends up in.
3216 ctx = rcu_dereference(current->perf_counter_ctxp);
3217 if (ctx)
3218 perf_counter_comm_ctx(ctx, comm_event);
3219 rcu_read_unlock();
3222 void perf_counter_comm(struct task_struct *task)
3224 struct perf_comm_event comm_event;
3226 if (task->perf_counter_ctxp)
3227 perf_counter_enable_on_exec(task);
3229 if (!atomic_read(&nr_comm_counters))
3230 return;
3232 comm_event = (struct perf_comm_event){
3233 .task = task,
3234 /* .comm */
3235 /* .comm_size */
3236 .event = {
3237 .header = {
3238 .type = PERF_EVENT_COMM,
3239 .misc = 0,
3240 /* .size */
3242 /* .pid */
3243 /* .tid */
3247 perf_counter_comm_event(&comm_event);
3251 * mmap tracking
3254 struct perf_mmap_event {
3255 struct vm_area_struct *vma;
3257 const char *file_name;
3258 int file_size;
3260 struct {
3261 struct perf_event_header header;
3263 u32 pid;
3264 u32 tid;
3265 u64 start;
3266 u64 len;
3267 u64 pgoff;
3268 } event;
3271 static void perf_counter_mmap_output(struct perf_counter *counter,
3272 struct perf_mmap_event *mmap_event)
3274 struct perf_output_handle handle;
3275 int size = mmap_event->event.header.size;
3276 int ret = perf_output_begin(&handle, counter, size, 0, 0);
3278 if (ret)
3279 return;
3281 mmap_event->event.pid = perf_counter_pid(counter, current);
3282 mmap_event->event.tid = perf_counter_tid(counter, current);
3284 perf_output_put(&handle, mmap_event->event);
3285 perf_output_copy(&handle, mmap_event->file_name,
3286 mmap_event->file_size);
3287 perf_output_end(&handle);
3290 static int perf_counter_mmap_match(struct perf_counter *counter,
3291 struct perf_mmap_event *mmap_event)
3293 if (counter->attr.mmap)
3294 return 1;
3296 return 0;
3299 static void perf_counter_mmap_ctx(struct perf_counter_context *ctx,
3300 struct perf_mmap_event *mmap_event)
3302 struct perf_counter *counter;
3304 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
3305 return;
3307 rcu_read_lock();
3308 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
3309 if (perf_counter_mmap_match(counter, mmap_event))
3310 perf_counter_mmap_output(counter, mmap_event);
3312 rcu_read_unlock();
3315 static void perf_counter_mmap_event(struct perf_mmap_event *mmap_event)
3317 struct perf_cpu_context *cpuctx;
3318 struct perf_counter_context *ctx;
3319 struct vm_area_struct *vma = mmap_event->vma;
3320 struct file *file = vma->vm_file;
3321 unsigned int size;
3322 char tmp[16];
3323 char *buf = NULL;
3324 const char *name;
3326 memset(tmp, 0, sizeof(tmp));
3328 if (file) {
3330 * d_path works from the end of the buffer backwards, so we
3331 * need to add enough zero bytes after the string to handle
3332 * the 64bit alignment we do later.
3334 buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
3335 if (!buf) {
3336 name = strncpy(tmp, "//enomem", sizeof(tmp));
3337 goto got_name;
3339 name = d_path(&file->f_path, buf, PATH_MAX);
3340 if (IS_ERR(name)) {
3341 name = strncpy(tmp, "//toolong", sizeof(tmp));
3342 goto got_name;
3344 } else {
3345 if (arch_vma_name(mmap_event->vma)) {
3346 name = strncpy(tmp, arch_vma_name(mmap_event->vma),
3347 sizeof(tmp));
3348 goto got_name;
3351 if (!vma->vm_mm) {
3352 name = strncpy(tmp, "[vdso]", sizeof(tmp));
3353 goto got_name;
3356 name = strncpy(tmp, "//anon", sizeof(tmp));
3357 goto got_name;
3360 got_name:
3361 size = ALIGN(strlen(name)+1, sizeof(u64));
3363 mmap_event->file_name = name;
3364 mmap_event->file_size = size;
3366 mmap_event->event.header.size = sizeof(mmap_event->event) + size;
3368 cpuctx = &get_cpu_var(perf_cpu_context);
3369 perf_counter_mmap_ctx(&cpuctx->ctx, mmap_event);
3370 put_cpu_var(perf_cpu_context);
3372 rcu_read_lock();
3374 * doesn't really matter which of the child contexts the
3375 * events ends up in.
3377 ctx = rcu_dereference(current->perf_counter_ctxp);
3378 if (ctx)
3379 perf_counter_mmap_ctx(ctx, mmap_event);
3380 rcu_read_unlock();
3382 kfree(buf);
3385 void __perf_counter_mmap(struct vm_area_struct *vma)
3387 struct perf_mmap_event mmap_event;
3389 if (!atomic_read(&nr_mmap_counters))
3390 return;
3392 mmap_event = (struct perf_mmap_event){
3393 .vma = vma,
3394 /* .file_name */
3395 /* .file_size */
3396 .event = {
3397 .header = {
3398 .type = PERF_EVENT_MMAP,
3399 .misc = 0,
3400 /* .size */
3402 /* .pid */
3403 /* .tid */
3404 .start = vma->vm_start,
3405 .len = vma->vm_end - vma->vm_start,
3406 .pgoff = vma->vm_pgoff,
3410 perf_counter_mmap_event(&mmap_event);
3414 * IRQ throttle logging
3417 static void perf_log_throttle(struct perf_counter *counter, int enable)
3419 struct perf_output_handle handle;
3420 int ret;
3422 struct {
3423 struct perf_event_header header;
3424 u64 time;
3425 u64 id;
3426 u64 stream_id;
3427 } throttle_event = {
3428 .header = {
3429 .type = PERF_EVENT_THROTTLE,
3430 .misc = 0,
3431 .size = sizeof(throttle_event),
3433 .time = sched_clock(),
3434 .id = primary_counter_id(counter),
3435 .stream_id = counter->id,
3438 if (enable)
3439 throttle_event.header.type = PERF_EVENT_UNTHROTTLE;
3441 ret = perf_output_begin(&handle, counter, sizeof(throttle_event), 1, 0);
3442 if (ret)
3443 return;
3445 perf_output_put(&handle, throttle_event);
3446 perf_output_end(&handle);
3450 * Generic counter overflow handling, sampling.
3453 int perf_counter_overflow(struct perf_counter *counter, int nmi,
3454 struct perf_sample_data *data)
3456 int events = atomic_read(&counter->event_limit);
3457 int throttle = counter->pmu->unthrottle != NULL;
3458 struct hw_perf_counter *hwc = &counter->hw;
3459 int ret = 0;
3461 if (!throttle) {
3462 hwc->interrupts++;
3463 } else {
3464 if (hwc->interrupts != MAX_INTERRUPTS) {
3465 hwc->interrupts++;
3466 if (HZ * hwc->interrupts >
3467 (u64)sysctl_perf_counter_sample_rate) {
3468 hwc->interrupts = MAX_INTERRUPTS;
3469 perf_log_throttle(counter, 0);
3470 ret = 1;
3472 } else {
3474 * Keep re-disabling counters even though on the previous
3475 * pass we disabled it - just in case we raced with a
3476 * sched-in and the counter got enabled again:
3478 ret = 1;
3482 if (counter->attr.freq) {
3483 u64 now = sched_clock();
3484 s64 delta = now - hwc->freq_stamp;
3486 hwc->freq_stamp = now;
3488 if (delta > 0 && delta < TICK_NSEC)
3489 perf_adjust_period(counter, NSEC_PER_SEC / (int)delta);
3493 * XXX event_limit might not quite work as expected on inherited
3494 * counters
3497 counter->pending_kill = POLL_IN;
3498 if (events && atomic_dec_and_test(&counter->event_limit)) {
3499 ret = 1;
3500 counter->pending_kill = POLL_HUP;
3501 if (nmi) {
3502 counter->pending_disable = 1;
3503 perf_pending_queue(&counter->pending,
3504 perf_pending_counter);
3505 } else
3506 perf_counter_disable(counter);
3509 perf_counter_output(counter, nmi, data);
3510 return ret;
3514 * Generic software counter infrastructure
3518 * We directly increment counter->count and keep a second value in
3519 * counter->hw.period_left to count intervals. This period counter
3520 * is kept in the range [-sample_period, 0] so that we can use the
3521 * sign as trigger.
3524 static u64 perf_swcounter_set_period(struct perf_counter *counter)
3526 struct hw_perf_counter *hwc = &counter->hw;
3527 u64 period = hwc->last_period;
3528 u64 nr, offset;
3529 s64 old, val;
3531 hwc->last_period = hwc->sample_period;
3533 again:
3534 old = val = atomic64_read(&hwc->period_left);
3535 if (val < 0)
3536 return 0;
3538 nr = div64_u64(period + val, period);
3539 offset = nr * period;
3540 val -= offset;
3541 if (atomic64_cmpxchg(&hwc->period_left, old, val) != old)
3542 goto again;
3544 return nr;
3547 static void perf_swcounter_overflow(struct perf_counter *counter,
3548 int nmi, struct perf_sample_data *data)
3550 struct hw_perf_counter *hwc = &counter->hw;
3551 u64 overflow;
3553 data->period = counter->hw.last_period;
3554 overflow = perf_swcounter_set_period(counter);
3556 if (hwc->interrupts == MAX_INTERRUPTS)
3557 return;
3559 for (; overflow; overflow--) {
3560 if (perf_counter_overflow(counter, nmi, data)) {
3562 * We inhibit the overflow from happening when
3563 * hwc->interrupts == MAX_INTERRUPTS.
3565 break;
3570 static void perf_swcounter_unthrottle(struct perf_counter *counter)
3573 * Nothing to do, we already reset hwc->interrupts.
3577 static void perf_swcounter_add(struct perf_counter *counter, u64 nr,
3578 int nmi, struct perf_sample_data *data)
3580 struct hw_perf_counter *hwc = &counter->hw;
3582 atomic64_add(nr, &counter->count);
3584 if (!hwc->sample_period)
3585 return;
3587 if (!data->regs)
3588 return;
3590 if (!atomic64_add_negative(nr, &hwc->period_left))
3591 perf_swcounter_overflow(counter, nmi, data);
3594 static int perf_swcounter_is_counting(struct perf_counter *counter)
3597 * The counter is active, we're good!
3599 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
3600 return 1;
3603 * The counter is off/error, not counting.
3605 if (counter->state != PERF_COUNTER_STATE_INACTIVE)
3606 return 0;
3609 * The counter is inactive, if the context is active
3610 * we're part of a group that didn't make it on the 'pmu',
3611 * not counting.
3613 if (counter->ctx->is_active)
3614 return 0;
3617 * We're inactive and the context is too, this means the
3618 * task is scheduled out, we're counting events that happen
3619 * to us, like migration events.
3621 return 1;
3624 static int perf_swcounter_match(struct perf_counter *counter,
3625 enum perf_type_id type,
3626 u32 event, struct pt_regs *regs)
3628 if (!perf_swcounter_is_counting(counter))
3629 return 0;
3631 if (counter->attr.type != type)
3632 return 0;
3633 if (counter->attr.config != event)
3634 return 0;
3636 if (regs) {
3637 if (counter->attr.exclude_user && user_mode(regs))
3638 return 0;
3640 if (counter->attr.exclude_kernel && !user_mode(regs))
3641 return 0;
3644 return 1;
3647 static void perf_swcounter_ctx_event(struct perf_counter_context *ctx,
3648 enum perf_type_id type,
3649 u32 event, u64 nr, int nmi,
3650 struct perf_sample_data *data)
3652 struct perf_counter *counter;
3654 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
3655 return;
3657 rcu_read_lock();
3658 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
3659 if (perf_swcounter_match(counter, type, event, data->regs))
3660 perf_swcounter_add(counter, nr, nmi, data);
3662 rcu_read_unlock();
3665 static int *perf_swcounter_recursion_context(struct perf_cpu_context *cpuctx)
3667 if (in_nmi())
3668 return &cpuctx->recursion[3];
3670 if (in_irq())
3671 return &cpuctx->recursion[2];
3673 if (in_softirq())
3674 return &cpuctx->recursion[1];
3676 return &cpuctx->recursion[0];
3679 static void do_perf_swcounter_event(enum perf_type_id type, u32 event,
3680 u64 nr, int nmi,
3681 struct perf_sample_data *data)
3683 struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
3684 int *recursion = perf_swcounter_recursion_context(cpuctx);
3685 struct perf_counter_context *ctx;
3687 if (*recursion)
3688 goto out;
3690 (*recursion)++;
3691 barrier();
3693 perf_swcounter_ctx_event(&cpuctx->ctx, type, event,
3694 nr, nmi, data);
3695 rcu_read_lock();
3697 * doesn't really matter which of the child contexts the
3698 * events ends up in.
3700 ctx = rcu_dereference(current->perf_counter_ctxp);
3701 if (ctx)
3702 perf_swcounter_ctx_event(ctx, type, event, nr, nmi, data);
3703 rcu_read_unlock();
3705 barrier();
3706 (*recursion)--;
3708 out:
3709 put_cpu_var(perf_cpu_context);
3712 void __perf_swcounter_event(u32 event, u64 nr, int nmi,
3713 struct pt_regs *regs, u64 addr)
3715 struct perf_sample_data data = {
3716 .regs = regs,
3717 .addr = addr,
3720 do_perf_swcounter_event(PERF_TYPE_SOFTWARE, event, nr, nmi, &data);
3723 static void perf_swcounter_read(struct perf_counter *counter)
3727 static int perf_swcounter_enable(struct perf_counter *counter)
3729 struct hw_perf_counter *hwc = &counter->hw;
3731 if (hwc->sample_period) {
3732 hwc->last_period = hwc->sample_period;
3733 perf_swcounter_set_period(counter);
3735 return 0;
3738 static void perf_swcounter_disable(struct perf_counter *counter)
3742 static const struct pmu perf_ops_generic = {
3743 .enable = perf_swcounter_enable,
3744 .disable = perf_swcounter_disable,
3745 .read = perf_swcounter_read,
3746 .unthrottle = perf_swcounter_unthrottle,
3750 * hrtimer based swcounter callback
3753 static enum hrtimer_restart perf_swcounter_hrtimer(struct hrtimer *hrtimer)
3755 enum hrtimer_restart ret = HRTIMER_RESTART;
3756 struct perf_sample_data data;
3757 struct perf_counter *counter;
3758 u64 period;
3760 counter = container_of(hrtimer, struct perf_counter, hw.hrtimer);
3761 counter->pmu->read(counter);
3763 data.addr = 0;
3764 data.regs = get_irq_regs();
3766 * In case we exclude kernel IPs or are somehow not in interrupt
3767 * context, provide the next best thing, the user IP.
3769 if ((counter->attr.exclude_kernel || !data.regs) &&
3770 !counter->attr.exclude_user)
3771 data.regs = task_pt_regs(current);
3773 if (data.regs) {
3774 if (perf_counter_overflow(counter, 0, &data))
3775 ret = HRTIMER_NORESTART;
3778 period = max_t(u64, 10000, counter->hw.sample_period);
3779 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
3781 return ret;
3785 * Software counter: cpu wall time clock
3788 static void cpu_clock_perf_counter_update(struct perf_counter *counter)
3790 int cpu = raw_smp_processor_id();
3791 s64 prev;
3792 u64 now;
3794 now = cpu_clock(cpu);
3795 prev = atomic64_read(&counter->hw.prev_count);
3796 atomic64_set(&counter->hw.prev_count, now);
3797 atomic64_add(now - prev, &counter->count);
3800 static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
3802 struct hw_perf_counter *hwc = &counter->hw;
3803 int cpu = raw_smp_processor_id();
3805 atomic64_set(&hwc->prev_count, cpu_clock(cpu));
3806 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3807 hwc->hrtimer.function = perf_swcounter_hrtimer;
3808 if (hwc->sample_period) {
3809 u64 period = max_t(u64, 10000, hwc->sample_period);
3810 __hrtimer_start_range_ns(&hwc->hrtimer,
3811 ns_to_ktime(period), 0,
3812 HRTIMER_MODE_REL, 0);
3815 return 0;
3818 static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
3820 if (counter->hw.sample_period)
3821 hrtimer_cancel(&counter->hw.hrtimer);
3822 cpu_clock_perf_counter_update(counter);
3825 static void cpu_clock_perf_counter_read(struct perf_counter *counter)
3827 cpu_clock_perf_counter_update(counter);
3830 static const struct pmu perf_ops_cpu_clock = {
3831 .enable = cpu_clock_perf_counter_enable,
3832 .disable = cpu_clock_perf_counter_disable,
3833 .read = cpu_clock_perf_counter_read,
3837 * Software counter: task time clock
3840 static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
3842 u64 prev;
3843 s64 delta;
3845 prev = atomic64_xchg(&counter->hw.prev_count, now);
3846 delta = now - prev;
3847 atomic64_add(delta, &counter->count);
3850 static int task_clock_perf_counter_enable(struct perf_counter *counter)
3852 struct hw_perf_counter *hwc = &counter->hw;
3853 u64 now;
3855 now = counter->ctx->time;
3857 atomic64_set(&hwc->prev_count, now);
3858 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3859 hwc->hrtimer.function = perf_swcounter_hrtimer;
3860 if (hwc->sample_period) {
3861 u64 period = max_t(u64, 10000, hwc->sample_period);
3862 __hrtimer_start_range_ns(&hwc->hrtimer,
3863 ns_to_ktime(period), 0,
3864 HRTIMER_MODE_REL, 0);
3867 return 0;
3870 static void task_clock_perf_counter_disable(struct perf_counter *counter)
3872 if (counter->hw.sample_period)
3873 hrtimer_cancel(&counter->hw.hrtimer);
3874 task_clock_perf_counter_update(counter, counter->ctx->time);
3878 static void task_clock_perf_counter_read(struct perf_counter *counter)
3880 u64 time;
3882 if (!in_nmi()) {
3883 update_context_time(counter->ctx);
3884 time = counter->ctx->time;
3885 } else {
3886 u64 now = perf_clock();
3887 u64 delta = now - counter->ctx->timestamp;
3888 time = counter->ctx->time + delta;
3891 task_clock_perf_counter_update(counter, time);
3894 static const struct pmu perf_ops_task_clock = {
3895 .enable = task_clock_perf_counter_enable,
3896 .disable = task_clock_perf_counter_disable,
3897 .read = task_clock_perf_counter_read,
3900 #ifdef CONFIG_EVENT_PROFILE
3901 void perf_tpcounter_event(int event_id, u64 addr, u64 count, void *record,
3902 int entry_size)
3904 struct perf_raw_record raw = {
3905 .size = entry_size,
3906 .data = record,
3909 struct perf_sample_data data = {
3910 .regs = get_irq_regs(),
3911 .addr = addr,
3912 .raw = &raw,
3915 if (!data.regs)
3916 data.regs = task_pt_regs(current);
3918 do_perf_swcounter_event(PERF_TYPE_TRACEPOINT, event_id, count, 1, &data);
3920 EXPORT_SYMBOL_GPL(perf_tpcounter_event);
3922 extern int ftrace_profile_enable(int);
3923 extern void ftrace_profile_disable(int);
3925 static void tp_perf_counter_destroy(struct perf_counter *counter)
3927 ftrace_profile_disable(counter->attr.config);
3930 static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
3933 * Raw tracepoint data is a severe data leak, only allow root to
3934 * have these.
3936 if ((counter->attr.sample_type & PERF_SAMPLE_RAW) &&
3937 !capable(CAP_SYS_ADMIN))
3938 return ERR_PTR(-EPERM);
3940 if (ftrace_profile_enable(counter->attr.config))
3941 return NULL;
3943 counter->destroy = tp_perf_counter_destroy;
3945 return &perf_ops_generic;
3947 #else
3948 static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
3950 return NULL;
3952 #endif
3954 atomic_t perf_swcounter_enabled[PERF_COUNT_SW_MAX];
3956 static void sw_perf_counter_destroy(struct perf_counter *counter)
3958 u64 event = counter->attr.config;
3960 WARN_ON(counter->parent);
3962 atomic_dec(&perf_swcounter_enabled[event]);
3965 static const struct pmu *sw_perf_counter_init(struct perf_counter *counter)
3967 const struct pmu *pmu = NULL;
3968 u64 event = counter->attr.config;
3971 * Software counters (currently) can't in general distinguish
3972 * between user, kernel and hypervisor events.
3973 * However, context switches and cpu migrations are considered
3974 * to be kernel events, and page faults are never hypervisor
3975 * events.
3977 switch (event) {
3978 case PERF_COUNT_SW_CPU_CLOCK:
3979 pmu = &perf_ops_cpu_clock;
3981 break;
3982 case PERF_COUNT_SW_TASK_CLOCK:
3984 * If the user instantiates this as a per-cpu counter,
3985 * use the cpu_clock counter instead.
3987 if (counter->ctx->task)
3988 pmu = &perf_ops_task_clock;
3989 else
3990 pmu = &perf_ops_cpu_clock;
3992 break;
3993 case PERF_COUNT_SW_PAGE_FAULTS:
3994 case PERF_COUNT_SW_PAGE_FAULTS_MIN:
3995 case PERF_COUNT_SW_PAGE_FAULTS_MAJ:
3996 case PERF_COUNT_SW_CONTEXT_SWITCHES:
3997 case PERF_COUNT_SW_CPU_MIGRATIONS:
3998 if (!counter->parent) {
3999 atomic_inc(&perf_swcounter_enabled[event]);
4000 counter->destroy = sw_perf_counter_destroy;
4002 pmu = &perf_ops_generic;
4003 break;
4006 return pmu;
4010 * Allocate and initialize a counter structure
4012 static struct perf_counter *
4013 perf_counter_alloc(struct perf_counter_attr *attr,
4014 int cpu,
4015 struct perf_counter_context *ctx,
4016 struct perf_counter *group_leader,
4017 struct perf_counter *parent_counter,
4018 gfp_t gfpflags)
4020 const struct pmu *pmu;
4021 struct perf_counter *counter;
4022 struct hw_perf_counter *hwc;
4023 long err;
4025 counter = kzalloc(sizeof(*counter), gfpflags);
4026 if (!counter)
4027 return ERR_PTR(-ENOMEM);
4030 * Single counters are their own group leaders, with an
4031 * empty sibling list:
4033 if (!group_leader)
4034 group_leader = counter;
4036 mutex_init(&counter->child_mutex);
4037 INIT_LIST_HEAD(&counter->child_list);
4039 INIT_LIST_HEAD(&counter->list_entry);
4040 INIT_LIST_HEAD(&counter->event_entry);
4041 INIT_LIST_HEAD(&counter->sibling_list);
4042 init_waitqueue_head(&counter->waitq);
4044 mutex_init(&counter->mmap_mutex);
4046 counter->cpu = cpu;
4047 counter->attr = *attr;
4048 counter->group_leader = group_leader;
4049 counter->pmu = NULL;
4050 counter->ctx = ctx;
4051 counter->oncpu = -1;
4053 counter->parent = parent_counter;
4055 counter->ns = get_pid_ns(current->nsproxy->pid_ns);
4056 counter->id = atomic64_inc_return(&perf_counter_id);
4058 counter->state = PERF_COUNTER_STATE_INACTIVE;
4060 if (attr->disabled)
4061 counter->state = PERF_COUNTER_STATE_OFF;
4063 pmu = NULL;
4065 hwc = &counter->hw;
4066 hwc->sample_period = attr->sample_period;
4067 if (attr->freq && attr->sample_freq)
4068 hwc->sample_period = 1;
4070 atomic64_set(&hwc->period_left, hwc->sample_period);
4073 * we currently do not support PERF_FORMAT_GROUP on inherited counters
4075 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
4076 goto done;
4078 switch (attr->type) {
4079 case PERF_TYPE_RAW:
4080 case PERF_TYPE_HARDWARE:
4081 case PERF_TYPE_HW_CACHE:
4082 pmu = hw_perf_counter_init(counter);
4083 break;
4085 case PERF_TYPE_SOFTWARE:
4086 pmu = sw_perf_counter_init(counter);
4087 break;
4089 case PERF_TYPE_TRACEPOINT:
4090 pmu = tp_perf_counter_init(counter);
4091 break;
4093 default:
4094 break;
4096 done:
4097 err = 0;
4098 if (!pmu)
4099 err = -EINVAL;
4100 else if (IS_ERR(pmu))
4101 err = PTR_ERR(pmu);
4103 if (err) {
4104 if (counter->ns)
4105 put_pid_ns(counter->ns);
4106 kfree(counter);
4107 return ERR_PTR(err);
4110 counter->pmu = pmu;
4112 if (!counter->parent) {
4113 atomic_inc(&nr_counters);
4114 if (counter->attr.mmap)
4115 atomic_inc(&nr_mmap_counters);
4116 if (counter->attr.comm)
4117 atomic_inc(&nr_comm_counters);
4118 if (counter->attr.task)
4119 atomic_inc(&nr_task_counters);
4122 return counter;
4125 static int perf_copy_attr(struct perf_counter_attr __user *uattr,
4126 struct perf_counter_attr *attr)
4128 int ret;
4129 u32 size;
4131 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
4132 return -EFAULT;
4135 * zero the full structure, so that a short copy will be nice.
4137 memset(attr, 0, sizeof(*attr));
4139 ret = get_user(size, &uattr->size);
4140 if (ret)
4141 return ret;
4143 if (size > PAGE_SIZE) /* silly large */
4144 goto err_size;
4146 if (!size) /* abi compat */
4147 size = PERF_ATTR_SIZE_VER0;
4149 if (size < PERF_ATTR_SIZE_VER0)
4150 goto err_size;
4153 * If we're handed a bigger struct than we know of,
4154 * ensure all the unknown bits are 0.
4156 if (size > sizeof(*attr)) {
4157 unsigned long val;
4158 unsigned long __user *addr;
4159 unsigned long __user *end;
4161 addr = PTR_ALIGN((void __user *)uattr + sizeof(*attr),
4162 sizeof(unsigned long));
4163 end = PTR_ALIGN((void __user *)uattr + size,
4164 sizeof(unsigned long));
4166 for (; addr < end; addr += sizeof(unsigned long)) {
4167 ret = get_user(val, addr);
4168 if (ret)
4169 return ret;
4170 if (val)
4171 goto err_size;
4175 ret = copy_from_user(attr, uattr, size);
4176 if (ret)
4177 return -EFAULT;
4180 * If the type exists, the corresponding creation will verify
4181 * the attr->config.
4183 if (attr->type >= PERF_TYPE_MAX)
4184 return -EINVAL;
4186 if (attr->__reserved_1 || attr->__reserved_2 || attr->__reserved_3)
4187 return -EINVAL;
4189 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
4190 return -EINVAL;
4192 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
4193 return -EINVAL;
4195 out:
4196 return ret;
4198 err_size:
4199 put_user(sizeof(*attr), &uattr->size);
4200 ret = -E2BIG;
4201 goto out;
4205 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
4207 * @attr_uptr: event type attributes for monitoring/sampling
4208 * @pid: target pid
4209 * @cpu: target cpu
4210 * @group_fd: group leader counter fd
4212 SYSCALL_DEFINE5(perf_counter_open,
4213 struct perf_counter_attr __user *, attr_uptr,
4214 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
4216 struct perf_counter *counter, *group_leader;
4217 struct perf_counter_attr attr;
4218 struct perf_counter_context *ctx;
4219 struct file *counter_file = NULL;
4220 struct file *group_file = NULL;
4221 int fput_needed = 0;
4222 int fput_needed2 = 0;
4223 int ret;
4225 /* for future expandability... */
4226 if (flags)
4227 return -EINVAL;
4229 ret = perf_copy_attr(attr_uptr, &attr);
4230 if (ret)
4231 return ret;
4233 if (!attr.exclude_kernel) {
4234 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
4235 return -EACCES;
4238 if (attr.freq) {
4239 if (attr.sample_freq > sysctl_perf_counter_sample_rate)
4240 return -EINVAL;
4244 * Get the target context (task or percpu):
4246 ctx = find_get_context(pid, cpu);
4247 if (IS_ERR(ctx))
4248 return PTR_ERR(ctx);
4251 * Look up the group leader (we will attach this counter to it):
4253 group_leader = NULL;
4254 if (group_fd != -1) {
4255 ret = -EINVAL;
4256 group_file = fget_light(group_fd, &fput_needed);
4257 if (!group_file)
4258 goto err_put_context;
4259 if (group_file->f_op != &perf_fops)
4260 goto err_put_context;
4262 group_leader = group_file->private_data;
4264 * Do not allow a recursive hierarchy (this new sibling
4265 * becoming part of another group-sibling):
4267 if (group_leader->group_leader != group_leader)
4268 goto err_put_context;
4270 * Do not allow to attach to a group in a different
4271 * task or CPU context:
4273 if (group_leader->ctx != ctx)
4274 goto err_put_context;
4276 * Only a group leader can be exclusive or pinned
4278 if (attr.exclusive || attr.pinned)
4279 goto err_put_context;
4282 counter = perf_counter_alloc(&attr, cpu, ctx, group_leader,
4283 NULL, GFP_KERNEL);
4284 ret = PTR_ERR(counter);
4285 if (IS_ERR(counter))
4286 goto err_put_context;
4288 ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
4289 if (ret < 0)
4290 goto err_free_put_context;
4292 counter_file = fget_light(ret, &fput_needed2);
4293 if (!counter_file)
4294 goto err_free_put_context;
4296 counter->filp = counter_file;
4297 WARN_ON_ONCE(ctx->parent_ctx);
4298 mutex_lock(&ctx->mutex);
4299 perf_install_in_context(ctx, counter, cpu);
4300 ++ctx->generation;
4301 mutex_unlock(&ctx->mutex);
4303 counter->owner = current;
4304 get_task_struct(current);
4305 mutex_lock(&current->perf_counter_mutex);
4306 list_add_tail(&counter->owner_entry, &current->perf_counter_list);
4307 mutex_unlock(&current->perf_counter_mutex);
4309 fput_light(counter_file, fput_needed2);
4311 out_fput:
4312 fput_light(group_file, fput_needed);
4314 return ret;
4316 err_free_put_context:
4317 kfree(counter);
4319 err_put_context:
4320 put_ctx(ctx);
4322 goto out_fput;
4326 * inherit a counter from parent task to child task:
4328 static struct perf_counter *
4329 inherit_counter(struct perf_counter *parent_counter,
4330 struct task_struct *parent,
4331 struct perf_counter_context *parent_ctx,
4332 struct task_struct *child,
4333 struct perf_counter *group_leader,
4334 struct perf_counter_context *child_ctx)
4336 struct perf_counter *child_counter;
4339 * Instead of creating recursive hierarchies of counters,
4340 * we link inherited counters back to the original parent,
4341 * which has a filp for sure, which we use as the reference
4342 * count:
4344 if (parent_counter->parent)
4345 parent_counter = parent_counter->parent;
4347 child_counter = perf_counter_alloc(&parent_counter->attr,
4348 parent_counter->cpu, child_ctx,
4349 group_leader, parent_counter,
4350 GFP_KERNEL);
4351 if (IS_ERR(child_counter))
4352 return child_counter;
4353 get_ctx(child_ctx);
4356 * Make the child state follow the state of the parent counter,
4357 * not its attr.disabled bit. We hold the parent's mutex,
4358 * so we won't race with perf_counter_{en, dis}able_family.
4360 if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE)
4361 child_counter->state = PERF_COUNTER_STATE_INACTIVE;
4362 else
4363 child_counter->state = PERF_COUNTER_STATE_OFF;
4365 if (parent_counter->attr.freq)
4366 child_counter->hw.sample_period = parent_counter->hw.sample_period;
4369 * Link it up in the child's context:
4371 add_counter_to_ctx(child_counter, child_ctx);
4374 * Get a reference to the parent filp - we will fput it
4375 * when the child counter exits. This is safe to do because
4376 * we are in the parent and we know that the filp still
4377 * exists and has a nonzero count:
4379 atomic_long_inc(&parent_counter->filp->f_count);
4382 * Link this into the parent counter's child list
4384 WARN_ON_ONCE(parent_counter->ctx->parent_ctx);
4385 mutex_lock(&parent_counter->child_mutex);
4386 list_add_tail(&child_counter->child_list, &parent_counter->child_list);
4387 mutex_unlock(&parent_counter->child_mutex);
4389 return child_counter;
4392 static int inherit_group(struct perf_counter *parent_counter,
4393 struct task_struct *parent,
4394 struct perf_counter_context *parent_ctx,
4395 struct task_struct *child,
4396 struct perf_counter_context *child_ctx)
4398 struct perf_counter *leader;
4399 struct perf_counter *sub;
4400 struct perf_counter *child_ctr;
4402 leader = inherit_counter(parent_counter, parent, parent_ctx,
4403 child, NULL, child_ctx);
4404 if (IS_ERR(leader))
4405 return PTR_ERR(leader);
4406 list_for_each_entry(sub, &parent_counter->sibling_list, list_entry) {
4407 child_ctr = inherit_counter(sub, parent, parent_ctx,
4408 child, leader, child_ctx);
4409 if (IS_ERR(child_ctr))
4410 return PTR_ERR(child_ctr);
4412 return 0;
4415 static void sync_child_counter(struct perf_counter *child_counter,
4416 struct task_struct *child)
4418 struct perf_counter *parent_counter = child_counter->parent;
4419 u64 child_val;
4421 if (child_counter->attr.inherit_stat)
4422 perf_counter_read_event(child_counter, child);
4424 child_val = atomic64_read(&child_counter->count);
4427 * Add back the child's count to the parent's count:
4429 atomic64_add(child_val, &parent_counter->count);
4430 atomic64_add(child_counter->total_time_enabled,
4431 &parent_counter->child_total_time_enabled);
4432 atomic64_add(child_counter->total_time_running,
4433 &parent_counter->child_total_time_running);
4436 * Remove this counter from the parent's list
4438 WARN_ON_ONCE(parent_counter->ctx->parent_ctx);
4439 mutex_lock(&parent_counter->child_mutex);
4440 list_del_init(&child_counter->child_list);
4441 mutex_unlock(&parent_counter->child_mutex);
4444 * Release the parent counter, if this was the last
4445 * reference to it.
4447 fput(parent_counter->filp);
4450 static void
4451 __perf_counter_exit_task(struct perf_counter *child_counter,
4452 struct perf_counter_context *child_ctx,
4453 struct task_struct *child)
4455 struct perf_counter *parent_counter;
4457 update_counter_times(child_counter);
4458 perf_counter_remove_from_context(child_counter);
4460 parent_counter = child_counter->parent;
4462 * It can happen that parent exits first, and has counters
4463 * that are still around due to the child reference. These
4464 * counters need to be zapped - but otherwise linger.
4466 if (parent_counter) {
4467 sync_child_counter(child_counter, child);
4468 free_counter(child_counter);
4473 * When a child task exits, feed back counter values to parent counters.
4475 void perf_counter_exit_task(struct task_struct *child)
4477 struct perf_counter *child_counter, *tmp;
4478 struct perf_counter_context *child_ctx;
4479 unsigned long flags;
4481 if (likely(!child->perf_counter_ctxp)) {
4482 perf_counter_task(child, NULL, 0);
4483 return;
4486 local_irq_save(flags);
4488 * We can't reschedule here because interrupts are disabled,
4489 * and either child is current or it is a task that can't be
4490 * scheduled, so we are now safe from rescheduling changing
4491 * our context.
4493 child_ctx = child->perf_counter_ctxp;
4494 __perf_counter_task_sched_out(child_ctx);
4497 * Take the context lock here so that if find_get_context is
4498 * reading child->perf_counter_ctxp, we wait until it has
4499 * incremented the context's refcount before we do put_ctx below.
4501 spin_lock(&child_ctx->lock);
4502 child->perf_counter_ctxp = NULL;
4504 * If this context is a clone; unclone it so it can't get
4505 * swapped to another process while we're removing all
4506 * the counters from it.
4508 unclone_ctx(child_ctx);
4509 spin_unlock_irqrestore(&child_ctx->lock, flags);
4512 * Report the task dead after unscheduling the counters so that we
4513 * won't get any samples after PERF_EVENT_EXIT. We can however still
4514 * get a few PERF_EVENT_READ events.
4516 perf_counter_task(child, child_ctx, 0);
4519 * We can recurse on the same lock type through:
4521 * __perf_counter_exit_task()
4522 * sync_child_counter()
4523 * fput(parent_counter->filp)
4524 * perf_release()
4525 * mutex_lock(&ctx->mutex)
4527 * But since its the parent context it won't be the same instance.
4529 mutex_lock_nested(&child_ctx->mutex, SINGLE_DEPTH_NESTING);
4531 again:
4532 list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
4533 list_entry)
4534 __perf_counter_exit_task(child_counter, child_ctx, child);
4537 * If the last counter was a group counter, it will have appended all
4538 * its siblings to the list, but we obtained 'tmp' before that which
4539 * will still point to the list head terminating the iteration.
4541 if (!list_empty(&child_ctx->counter_list))
4542 goto again;
4544 mutex_unlock(&child_ctx->mutex);
4546 put_ctx(child_ctx);
4550 * free an unexposed, unused context as created by inheritance by
4551 * init_task below, used by fork() in case of fail.
4553 void perf_counter_free_task(struct task_struct *task)
4555 struct perf_counter_context *ctx = task->perf_counter_ctxp;
4556 struct perf_counter *counter, *tmp;
4558 if (!ctx)
4559 return;
4561 mutex_lock(&ctx->mutex);
4562 again:
4563 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry) {
4564 struct perf_counter *parent = counter->parent;
4566 if (WARN_ON_ONCE(!parent))
4567 continue;
4569 mutex_lock(&parent->child_mutex);
4570 list_del_init(&counter->child_list);
4571 mutex_unlock(&parent->child_mutex);
4573 fput(parent->filp);
4575 list_del_counter(counter, ctx);
4576 free_counter(counter);
4579 if (!list_empty(&ctx->counter_list))
4580 goto again;
4582 mutex_unlock(&ctx->mutex);
4584 put_ctx(ctx);
4588 * Initialize the perf_counter context in task_struct
4590 int perf_counter_init_task(struct task_struct *child)
4592 struct perf_counter_context *child_ctx, *parent_ctx;
4593 struct perf_counter_context *cloned_ctx;
4594 struct perf_counter *counter;
4595 struct task_struct *parent = current;
4596 int inherited_all = 1;
4597 int ret = 0;
4599 child->perf_counter_ctxp = NULL;
4601 mutex_init(&child->perf_counter_mutex);
4602 INIT_LIST_HEAD(&child->perf_counter_list);
4604 if (likely(!parent->perf_counter_ctxp))
4605 return 0;
4608 * This is executed from the parent task context, so inherit
4609 * counters that have been marked for cloning.
4610 * First allocate and initialize a context for the child.
4613 child_ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
4614 if (!child_ctx)
4615 return -ENOMEM;
4617 __perf_counter_init_context(child_ctx, child);
4618 child->perf_counter_ctxp = child_ctx;
4619 get_task_struct(child);
4622 * If the parent's context is a clone, pin it so it won't get
4623 * swapped under us.
4625 parent_ctx = perf_pin_task_context(parent);
4628 * No need to check if parent_ctx != NULL here; since we saw
4629 * it non-NULL earlier, the only reason for it to become NULL
4630 * is if we exit, and since we're currently in the middle of
4631 * a fork we can't be exiting at the same time.
4635 * Lock the parent list. No need to lock the child - not PID
4636 * hashed yet and not running, so nobody can access it.
4638 mutex_lock(&parent_ctx->mutex);
4641 * We dont have to disable NMIs - we are only looking at
4642 * the list, not manipulating it:
4644 list_for_each_entry_rcu(counter, &parent_ctx->event_list, event_entry) {
4645 if (counter != counter->group_leader)
4646 continue;
4648 if (!counter->attr.inherit) {
4649 inherited_all = 0;
4650 continue;
4653 ret = inherit_group(counter, parent, parent_ctx,
4654 child, child_ctx);
4655 if (ret) {
4656 inherited_all = 0;
4657 break;
4661 if (inherited_all) {
4663 * Mark the child context as a clone of the parent
4664 * context, or of whatever the parent is a clone of.
4665 * Note that if the parent is a clone, it could get
4666 * uncloned at any point, but that doesn't matter
4667 * because the list of counters and the generation
4668 * count can't have changed since we took the mutex.
4670 cloned_ctx = rcu_dereference(parent_ctx->parent_ctx);
4671 if (cloned_ctx) {
4672 child_ctx->parent_ctx = cloned_ctx;
4673 child_ctx->parent_gen = parent_ctx->parent_gen;
4674 } else {
4675 child_ctx->parent_ctx = parent_ctx;
4676 child_ctx->parent_gen = parent_ctx->generation;
4678 get_ctx(child_ctx->parent_ctx);
4681 mutex_unlock(&parent_ctx->mutex);
4683 perf_unpin_context(parent_ctx);
4685 return ret;
4688 static void __cpuinit perf_counter_init_cpu(int cpu)
4690 struct perf_cpu_context *cpuctx;
4692 cpuctx = &per_cpu(perf_cpu_context, cpu);
4693 __perf_counter_init_context(&cpuctx->ctx, NULL);
4695 spin_lock(&perf_resource_lock);
4696 cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
4697 spin_unlock(&perf_resource_lock);
4699 hw_perf_counter_setup(cpu);
4702 #ifdef CONFIG_HOTPLUG_CPU
4703 static void __perf_counter_exit_cpu(void *info)
4705 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
4706 struct perf_counter_context *ctx = &cpuctx->ctx;
4707 struct perf_counter *counter, *tmp;
4709 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
4710 __perf_counter_remove_from_context(counter);
4712 static void perf_counter_exit_cpu(int cpu)
4714 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
4715 struct perf_counter_context *ctx = &cpuctx->ctx;
4717 mutex_lock(&ctx->mutex);
4718 smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
4719 mutex_unlock(&ctx->mutex);
4721 #else
4722 static inline void perf_counter_exit_cpu(int cpu) { }
4723 #endif
4725 static int __cpuinit
4726 perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
4728 unsigned int cpu = (long)hcpu;
4730 switch (action) {
4732 case CPU_UP_PREPARE:
4733 case CPU_UP_PREPARE_FROZEN:
4734 perf_counter_init_cpu(cpu);
4735 break;
4737 case CPU_ONLINE:
4738 case CPU_ONLINE_FROZEN:
4739 hw_perf_counter_setup_online(cpu);
4740 break;
4742 case CPU_DOWN_PREPARE:
4743 case CPU_DOWN_PREPARE_FROZEN:
4744 perf_counter_exit_cpu(cpu);
4745 break;
4747 default:
4748 break;
4751 return NOTIFY_OK;
4755 * This has to have a higher priority than migration_notifier in sched.c.
4757 static struct notifier_block __cpuinitdata perf_cpu_nb = {
4758 .notifier_call = perf_cpu_notify,
4759 .priority = 20,
4762 void __init perf_counter_init(void)
4764 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
4765 (void *)(long)smp_processor_id());
4766 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_ONLINE,
4767 (void *)(long)smp_processor_id());
4768 register_cpu_notifier(&perf_cpu_nb);
4771 static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
4773 return sprintf(buf, "%d\n", perf_reserved_percpu);
4776 static ssize_t
4777 perf_set_reserve_percpu(struct sysdev_class *class,
4778 const char *buf,
4779 size_t count)
4781 struct perf_cpu_context *cpuctx;
4782 unsigned long val;
4783 int err, cpu, mpt;
4785 err = strict_strtoul(buf, 10, &val);
4786 if (err)
4787 return err;
4788 if (val > perf_max_counters)
4789 return -EINVAL;
4791 spin_lock(&perf_resource_lock);
4792 perf_reserved_percpu = val;
4793 for_each_online_cpu(cpu) {
4794 cpuctx = &per_cpu(perf_cpu_context, cpu);
4795 spin_lock_irq(&cpuctx->ctx.lock);
4796 mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
4797 perf_max_counters - perf_reserved_percpu);
4798 cpuctx->max_pertask = mpt;
4799 spin_unlock_irq(&cpuctx->ctx.lock);
4801 spin_unlock(&perf_resource_lock);
4803 return count;
4806 static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
4808 return sprintf(buf, "%d\n", perf_overcommit);
4811 static ssize_t
4812 perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
4814 unsigned long val;
4815 int err;
4817 err = strict_strtoul(buf, 10, &val);
4818 if (err)
4819 return err;
4820 if (val > 1)
4821 return -EINVAL;
4823 spin_lock(&perf_resource_lock);
4824 perf_overcommit = val;
4825 spin_unlock(&perf_resource_lock);
4827 return count;
4830 static SYSDEV_CLASS_ATTR(
4831 reserve_percpu,
4832 0644,
4833 perf_show_reserve_percpu,
4834 perf_set_reserve_percpu
4837 static SYSDEV_CLASS_ATTR(
4838 overcommit,
4839 0644,
4840 perf_show_overcommit,
4841 perf_set_overcommit
4844 static struct attribute *perfclass_attrs[] = {
4845 &attr_reserve_percpu.attr,
4846 &attr_overcommit.attr,
4847 NULL
4850 static struct attribute_group perfclass_attr_group = {
4851 .attrs = perfclass_attrs,
4852 .name = "perf_counters",
4855 static int __init perf_counter_sysfs_init(void)
4857 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
4858 &perfclass_attr_group);
4860 device_initcall(perf_counter_sysfs_init);