perf_counter: Simplify and fix task migration counting
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / kernel / perf_counter.c
blob8d4f0dd41c2281606fa5aab2f2debd4f53a892f6
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;
47 * perf counter paranoia level:
48 * 0 - not paranoid
49 * 1 - disallow cpu counters to unpriv
50 * 2 - disallow kernel profiling to unpriv
52 int sysctl_perf_counter_paranoid __read_mostly;
54 static inline bool perf_paranoid_cpu(void)
56 return sysctl_perf_counter_paranoid > 0;
59 static inline bool perf_paranoid_kernel(void)
61 return sysctl_perf_counter_paranoid > 1;
64 int sysctl_perf_counter_mlock __read_mostly = 512; /* 'free' kb per user */
67 * max perf counter sample rate
69 int sysctl_perf_counter_sample_rate __read_mostly = 100000;
71 static atomic64_t perf_counter_id;
74 * Lock for (sysadmin-configurable) counter reservations:
76 static DEFINE_SPINLOCK(perf_resource_lock);
79 * Architecture provided APIs - weak aliases:
81 extern __weak const struct pmu *hw_perf_counter_init(struct perf_counter *counter)
83 return NULL;
86 void __weak hw_perf_disable(void) { barrier(); }
87 void __weak hw_perf_enable(void) { barrier(); }
89 void __weak hw_perf_counter_setup(int cpu) { barrier(); }
91 int __weak
92 hw_perf_group_sched_in(struct perf_counter *group_leader,
93 struct perf_cpu_context *cpuctx,
94 struct perf_counter_context *ctx, int cpu)
96 return 0;
99 void __weak perf_counter_print_debug(void) { }
101 static DEFINE_PER_CPU(int, disable_count);
103 void __perf_disable(void)
105 __get_cpu_var(disable_count)++;
108 bool __perf_enable(void)
110 return !--__get_cpu_var(disable_count);
113 void perf_disable(void)
115 __perf_disable();
116 hw_perf_disable();
119 void perf_enable(void)
121 if (__perf_enable())
122 hw_perf_enable();
125 static void get_ctx(struct perf_counter_context *ctx)
127 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
130 static void free_ctx(struct rcu_head *head)
132 struct perf_counter_context *ctx;
134 ctx = container_of(head, struct perf_counter_context, rcu_head);
135 kfree(ctx);
138 static void put_ctx(struct perf_counter_context *ctx)
140 if (atomic_dec_and_test(&ctx->refcount)) {
141 if (ctx->parent_ctx)
142 put_ctx(ctx->parent_ctx);
143 if (ctx->task)
144 put_task_struct(ctx->task);
145 call_rcu(&ctx->rcu_head, free_ctx);
150 * Get the perf_counter_context for a task and lock it.
151 * This has to cope with with the fact that until it is locked,
152 * the context could get moved to another task.
154 static struct perf_counter_context *
155 perf_lock_task_context(struct task_struct *task, unsigned long *flags)
157 struct perf_counter_context *ctx;
159 rcu_read_lock();
160 retry:
161 ctx = rcu_dereference(task->perf_counter_ctxp);
162 if (ctx) {
164 * If this context is a clone of another, it might
165 * get swapped for another underneath us by
166 * perf_counter_task_sched_out, though the
167 * rcu_read_lock() protects us from any context
168 * getting freed. Lock the context and check if it
169 * got swapped before we could get the lock, and retry
170 * if so. If we locked the right context, then it
171 * can't get swapped on us any more.
173 spin_lock_irqsave(&ctx->lock, *flags);
174 if (ctx != rcu_dereference(task->perf_counter_ctxp)) {
175 spin_unlock_irqrestore(&ctx->lock, *flags);
176 goto retry;
179 rcu_read_unlock();
180 return ctx;
184 * Get the context for a task and increment its pin_count so it
185 * can't get swapped to another task. This also increments its
186 * reference count so that the context can't get freed.
188 static struct perf_counter_context *perf_pin_task_context(struct task_struct *task)
190 struct perf_counter_context *ctx;
191 unsigned long flags;
193 ctx = perf_lock_task_context(task, &flags);
194 if (ctx) {
195 ++ctx->pin_count;
196 get_ctx(ctx);
197 spin_unlock_irqrestore(&ctx->lock, flags);
199 return ctx;
202 static void perf_unpin_context(struct perf_counter_context *ctx)
204 unsigned long flags;
206 spin_lock_irqsave(&ctx->lock, flags);
207 --ctx->pin_count;
208 spin_unlock_irqrestore(&ctx->lock, flags);
209 put_ctx(ctx);
213 * Add a counter from the lists for its context.
214 * Must be called with ctx->mutex and ctx->lock held.
216 static void
217 list_add_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
219 struct perf_counter *group_leader = counter->group_leader;
222 * Depending on whether it is a standalone or sibling counter,
223 * add it straight to the context's counter list, or to the group
224 * leader's sibling list:
226 if (group_leader == counter)
227 list_add_tail(&counter->list_entry, &ctx->counter_list);
228 else {
229 list_add_tail(&counter->list_entry, &group_leader->sibling_list);
230 group_leader->nr_siblings++;
233 list_add_rcu(&counter->event_entry, &ctx->event_list);
234 ctx->nr_counters++;
238 * Remove a counter from the lists for its context.
239 * Must be called with ctx->mutex and ctx->lock held.
241 static void
242 list_del_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
244 struct perf_counter *sibling, *tmp;
246 if (list_empty(&counter->list_entry))
247 return;
248 ctx->nr_counters--;
250 list_del_init(&counter->list_entry);
251 list_del_rcu(&counter->event_entry);
253 if (counter->group_leader != counter)
254 counter->group_leader->nr_siblings--;
257 * If this was a group counter with sibling counters then
258 * upgrade the siblings to singleton counters by adding them
259 * to the context list directly:
261 list_for_each_entry_safe(sibling, tmp,
262 &counter->sibling_list, list_entry) {
264 list_move_tail(&sibling->list_entry, &ctx->counter_list);
265 sibling->group_leader = sibling;
269 static void
270 counter_sched_out(struct perf_counter *counter,
271 struct perf_cpu_context *cpuctx,
272 struct perf_counter_context *ctx)
274 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
275 return;
277 counter->state = PERF_COUNTER_STATE_INACTIVE;
278 counter->tstamp_stopped = ctx->time;
279 counter->pmu->disable(counter);
280 counter->oncpu = -1;
282 if (!is_software_counter(counter))
283 cpuctx->active_oncpu--;
284 ctx->nr_active--;
285 if (counter->attr.exclusive || !cpuctx->active_oncpu)
286 cpuctx->exclusive = 0;
289 static void
290 group_sched_out(struct perf_counter *group_counter,
291 struct perf_cpu_context *cpuctx,
292 struct perf_counter_context *ctx)
294 struct perf_counter *counter;
296 if (group_counter->state != PERF_COUNTER_STATE_ACTIVE)
297 return;
299 counter_sched_out(group_counter, cpuctx, ctx);
302 * Schedule out siblings (if any):
304 list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
305 counter_sched_out(counter, cpuctx, ctx);
307 if (group_counter->attr.exclusive)
308 cpuctx->exclusive = 0;
312 * Cross CPU call to remove a performance counter
314 * We disable the counter on the hardware level first. After that we
315 * remove it from the context list.
317 static void __perf_counter_remove_from_context(void *info)
319 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
320 struct perf_counter *counter = info;
321 struct perf_counter_context *ctx = counter->ctx;
324 * If this is a task context, we need to check whether it is
325 * the current task context of this cpu. If not it has been
326 * scheduled out before the smp call arrived.
328 if (ctx->task && cpuctx->task_ctx != ctx)
329 return;
331 spin_lock(&ctx->lock);
333 * Protect the list operation against NMI by disabling the
334 * counters on a global level.
336 perf_disable();
338 counter_sched_out(counter, cpuctx, ctx);
340 list_del_counter(counter, ctx);
342 if (!ctx->task) {
344 * Allow more per task counters with respect to the
345 * reservation:
347 cpuctx->max_pertask =
348 min(perf_max_counters - ctx->nr_counters,
349 perf_max_counters - perf_reserved_percpu);
352 perf_enable();
353 spin_unlock(&ctx->lock);
358 * Remove the counter from a task's (or a CPU's) list of counters.
360 * Must be called with ctx->mutex held.
362 * CPU counters are removed with a smp call. For task counters we only
363 * call when the task is on a CPU.
365 * If counter->ctx is a cloned context, callers must make sure that
366 * every task struct that counter->ctx->task could possibly point to
367 * remains valid. This is OK when called from perf_release since
368 * that only calls us on the top-level context, which can't be a clone.
369 * When called from perf_counter_exit_task, it's OK because the
370 * context has been detached from its task.
372 static void perf_counter_remove_from_context(struct perf_counter *counter)
374 struct perf_counter_context *ctx = counter->ctx;
375 struct task_struct *task = ctx->task;
377 if (!task) {
379 * Per cpu counters are removed via an smp call and
380 * the removal is always sucessful.
382 smp_call_function_single(counter->cpu,
383 __perf_counter_remove_from_context,
384 counter, 1);
385 return;
388 retry:
389 task_oncpu_function_call(task, __perf_counter_remove_from_context,
390 counter);
392 spin_lock_irq(&ctx->lock);
394 * If the context is active we need to retry the smp call.
396 if (ctx->nr_active && !list_empty(&counter->list_entry)) {
397 spin_unlock_irq(&ctx->lock);
398 goto retry;
402 * The lock prevents that this context is scheduled in so we
403 * can remove the counter safely, if the call above did not
404 * succeed.
406 if (!list_empty(&counter->list_entry)) {
407 list_del_counter(counter, ctx);
409 spin_unlock_irq(&ctx->lock);
412 static inline u64 perf_clock(void)
414 return cpu_clock(smp_processor_id());
418 * Update the record of the current time in a context.
420 static void update_context_time(struct perf_counter_context *ctx)
422 u64 now = perf_clock();
424 ctx->time += now - ctx->timestamp;
425 ctx->timestamp = now;
429 * Update the total_time_enabled and total_time_running fields for a counter.
431 static void update_counter_times(struct perf_counter *counter)
433 struct perf_counter_context *ctx = counter->ctx;
434 u64 run_end;
436 if (counter->state < PERF_COUNTER_STATE_INACTIVE)
437 return;
439 counter->total_time_enabled = ctx->time - counter->tstamp_enabled;
441 if (counter->state == PERF_COUNTER_STATE_INACTIVE)
442 run_end = counter->tstamp_stopped;
443 else
444 run_end = ctx->time;
446 counter->total_time_running = run_end - counter->tstamp_running;
450 * Update total_time_enabled and total_time_running for all counters in a group.
452 static void update_group_times(struct perf_counter *leader)
454 struct perf_counter *counter;
456 update_counter_times(leader);
457 list_for_each_entry(counter, &leader->sibling_list, list_entry)
458 update_counter_times(counter);
462 * Cross CPU call to disable a performance counter
464 static void __perf_counter_disable(void *info)
466 struct perf_counter *counter = info;
467 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
468 struct perf_counter_context *ctx = counter->ctx;
471 * If this is a per-task counter, need to check whether this
472 * counter's task is the current task on this cpu.
474 if (ctx->task && cpuctx->task_ctx != ctx)
475 return;
477 spin_lock(&ctx->lock);
480 * If the counter is on, turn it off.
481 * If it is in error state, leave it in error state.
483 if (counter->state >= PERF_COUNTER_STATE_INACTIVE) {
484 update_context_time(ctx);
485 update_counter_times(counter);
486 if (counter == counter->group_leader)
487 group_sched_out(counter, cpuctx, ctx);
488 else
489 counter_sched_out(counter, cpuctx, ctx);
490 counter->state = PERF_COUNTER_STATE_OFF;
493 spin_unlock(&ctx->lock);
497 * Disable a counter.
499 * If counter->ctx is a cloned context, callers must make sure that
500 * every task struct that counter->ctx->task could possibly point to
501 * remains valid. This condition is satisifed when called through
502 * perf_counter_for_each_child or perf_counter_for_each because they
503 * hold the top-level counter's child_mutex, so any descendant that
504 * goes to exit will block in sync_child_counter.
505 * When called from perf_pending_counter it's OK because counter->ctx
506 * is the current context on this CPU and preemption is disabled,
507 * hence we can't get into perf_counter_task_sched_out for this context.
509 static void perf_counter_disable(struct perf_counter *counter)
511 struct perf_counter_context *ctx = counter->ctx;
512 struct task_struct *task = ctx->task;
514 if (!task) {
516 * Disable the counter on the cpu that it's on
518 smp_call_function_single(counter->cpu, __perf_counter_disable,
519 counter, 1);
520 return;
523 retry:
524 task_oncpu_function_call(task, __perf_counter_disable, counter);
526 spin_lock_irq(&ctx->lock);
528 * If the counter is still active, we need to retry the cross-call.
530 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
531 spin_unlock_irq(&ctx->lock);
532 goto retry;
536 * Since we have the lock this context can't be scheduled
537 * in, so we can change the state safely.
539 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
540 update_counter_times(counter);
541 counter->state = PERF_COUNTER_STATE_OFF;
544 spin_unlock_irq(&ctx->lock);
547 static int
548 counter_sched_in(struct perf_counter *counter,
549 struct perf_cpu_context *cpuctx,
550 struct perf_counter_context *ctx,
551 int cpu)
553 if (counter->state <= PERF_COUNTER_STATE_OFF)
554 return 0;
556 counter->state = PERF_COUNTER_STATE_ACTIVE;
557 counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
559 * The new state must be visible before we turn it on in the hardware:
561 smp_wmb();
563 if (counter->pmu->enable(counter)) {
564 counter->state = PERF_COUNTER_STATE_INACTIVE;
565 counter->oncpu = -1;
566 return -EAGAIN;
569 counter->tstamp_running += ctx->time - counter->tstamp_stopped;
571 if (!is_software_counter(counter))
572 cpuctx->active_oncpu++;
573 ctx->nr_active++;
575 if (counter->attr.exclusive)
576 cpuctx->exclusive = 1;
578 return 0;
581 static int
582 group_sched_in(struct perf_counter *group_counter,
583 struct perf_cpu_context *cpuctx,
584 struct perf_counter_context *ctx,
585 int cpu)
587 struct perf_counter *counter, *partial_group;
588 int ret;
590 if (group_counter->state == PERF_COUNTER_STATE_OFF)
591 return 0;
593 ret = hw_perf_group_sched_in(group_counter, cpuctx, ctx, cpu);
594 if (ret)
595 return ret < 0 ? ret : 0;
597 if (counter_sched_in(group_counter, cpuctx, ctx, cpu))
598 return -EAGAIN;
601 * Schedule in siblings as one group (if any):
603 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
604 if (counter_sched_in(counter, cpuctx, ctx, cpu)) {
605 partial_group = counter;
606 goto group_error;
610 return 0;
612 group_error:
614 * Groups can be scheduled in as one unit only, so undo any
615 * partial group before returning:
617 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
618 if (counter == partial_group)
619 break;
620 counter_sched_out(counter, cpuctx, ctx);
622 counter_sched_out(group_counter, cpuctx, ctx);
624 return -EAGAIN;
628 * Return 1 for a group consisting entirely of software counters,
629 * 0 if the group contains any hardware counters.
631 static int is_software_only_group(struct perf_counter *leader)
633 struct perf_counter *counter;
635 if (!is_software_counter(leader))
636 return 0;
638 list_for_each_entry(counter, &leader->sibling_list, list_entry)
639 if (!is_software_counter(counter))
640 return 0;
642 return 1;
646 * Work out whether we can put this counter group on the CPU now.
648 static int group_can_go_on(struct perf_counter *counter,
649 struct perf_cpu_context *cpuctx,
650 int can_add_hw)
653 * Groups consisting entirely of software counters can always go on.
655 if (is_software_only_group(counter))
656 return 1;
658 * If an exclusive group is already on, no other hardware
659 * counters can go on.
661 if (cpuctx->exclusive)
662 return 0;
664 * If this group is exclusive and there are already
665 * counters on the CPU, it can't go on.
667 if (counter->attr.exclusive && cpuctx->active_oncpu)
668 return 0;
670 * Otherwise, try to add it if all previous groups were able
671 * to go on.
673 return can_add_hw;
676 static void add_counter_to_ctx(struct perf_counter *counter,
677 struct perf_counter_context *ctx)
679 list_add_counter(counter, ctx);
680 counter->tstamp_enabled = ctx->time;
681 counter->tstamp_running = ctx->time;
682 counter->tstamp_stopped = ctx->time;
686 * Cross CPU call to install and enable a performance counter
688 * Must be called with ctx->mutex held
690 static void __perf_install_in_context(void *info)
692 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
693 struct perf_counter *counter = info;
694 struct perf_counter_context *ctx = counter->ctx;
695 struct perf_counter *leader = counter->group_leader;
696 int cpu = smp_processor_id();
697 int err;
700 * If this is a task context, we need to check whether it is
701 * the current task context of this cpu. If not it has been
702 * scheduled out before the smp call arrived.
703 * Or possibly this is the right context but it isn't
704 * on this cpu because it had no counters.
706 if (ctx->task && cpuctx->task_ctx != ctx) {
707 if (cpuctx->task_ctx || ctx->task != current)
708 return;
709 cpuctx->task_ctx = ctx;
712 spin_lock(&ctx->lock);
713 ctx->is_active = 1;
714 update_context_time(ctx);
717 * Protect the list operation against NMI by disabling the
718 * counters on a global level. NOP for non NMI based counters.
720 perf_disable();
722 add_counter_to_ctx(counter, ctx);
725 * Don't put the counter on if it is disabled or if
726 * it is in a group and the group isn't on.
728 if (counter->state != PERF_COUNTER_STATE_INACTIVE ||
729 (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE))
730 goto unlock;
733 * An exclusive counter can't go on if there are already active
734 * hardware counters, and no hardware counter can go on if there
735 * is already an exclusive counter on.
737 if (!group_can_go_on(counter, cpuctx, 1))
738 err = -EEXIST;
739 else
740 err = counter_sched_in(counter, cpuctx, ctx, cpu);
742 if (err) {
744 * This counter couldn't go on. If it is in a group
745 * then we have to pull the whole group off.
746 * If the counter group is pinned then put it in error state.
748 if (leader != counter)
749 group_sched_out(leader, cpuctx, ctx);
750 if (leader->attr.pinned) {
751 update_group_times(leader);
752 leader->state = PERF_COUNTER_STATE_ERROR;
756 if (!err && !ctx->task && cpuctx->max_pertask)
757 cpuctx->max_pertask--;
759 unlock:
760 perf_enable();
762 spin_unlock(&ctx->lock);
766 * Attach a performance counter to a context
768 * First we add the counter to the list with the hardware enable bit
769 * in counter->hw_config cleared.
771 * If the counter is attached to a task which is on a CPU we use a smp
772 * call to enable it in the task context. The task might have been
773 * scheduled away, but we check this in the smp call again.
775 * Must be called with ctx->mutex held.
777 static void
778 perf_install_in_context(struct perf_counter_context *ctx,
779 struct perf_counter *counter,
780 int cpu)
782 struct task_struct *task = ctx->task;
784 if (!task) {
786 * Per cpu counters are installed via an smp call and
787 * the install is always sucessful.
789 smp_call_function_single(cpu, __perf_install_in_context,
790 counter, 1);
791 return;
794 retry:
795 task_oncpu_function_call(task, __perf_install_in_context,
796 counter);
798 spin_lock_irq(&ctx->lock);
800 * we need to retry the smp call.
802 if (ctx->is_active && list_empty(&counter->list_entry)) {
803 spin_unlock_irq(&ctx->lock);
804 goto retry;
808 * The lock prevents that this context is scheduled in so we
809 * can add the counter safely, if it the call above did not
810 * succeed.
812 if (list_empty(&counter->list_entry))
813 add_counter_to_ctx(counter, ctx);
814 spin_unlock_irq(&ctx->lock);
818 * Cross CPU call to enable a performance counter
820 static void __perf_counter_enable(void *info)
822 struct perf_counter *counter = info;
823 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
824 struct perf_counter_context *ctx = counter->ctx;
825 struct perf_counter *leader = counter->group_leader;
826 int err;
829 * If this is a per-task counter, need to check whether this
830 * counter's task is the current task on this cpu.
832 if (ctx->task && cpuctx->task_ctx != ctx) {
833 if (cpuctx->task_ctx || ctx->task != current)
834 return;
835 cpuctx->task_ctx = ctx;
838 spin_lock(&ctx->lock);
839 ctx->is_active = 1;
840 update_context_time(ctx);
842 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
843 goto unlock;
844 counter->state = PERF_COUNTER_STATE_INACTIVE;
845 counter->tstamp_enabled = ctx->time - counter->total_time_enabled;
848 * If the counter is in a group and isn't the group leader,
849 * then don't put it on unless the group is on.
851 if (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE)
852 goto unlock;
854 if (!group_can_go_on(counter, cpuctx, 1)) {
855 err = -EEXIST;
856 } else {
857 perf_disable();
858 if (counter == leader)
859 err = group_sched_in(counter, cpuctx, ctx,
860 smp_processor_id());
861 else
862 err = counter_sched_in(counter, cpuctx, ctx,
863 smp_processor_id());
864 perf_enable();
867 if (err) {
869 * If this counter can't go on and it's part of a
870 * group, then the whole group has to come off.
872 if (leader != counter)
873 group_sched_out(leader, cpuctx, ctx);
874 if (leader->attr.pinned) {
875 update_group_times(leader);
876 leader->state = PERF_COUNTER_STATE_ERROR;
880 unlock:
881 spin_unlock(&ctx->lock);
885 * Enable a counter.
887 * If counter->ctx is a cloned context, callers must make sure that
888 * every task struct that counter->ctx->task could possibly point to
889 * remains valid. This condition is satisfied when called through
890 * perf_counter_for_each_child or perf_counter_for_each as described
891 * for perf_counter_disable.
893 static void perf_counter_enable(struct perf_counter *counter)
895 struct perf_counter_context *ctx = counter->ctx;
896 struct task_struct *task = ctx->task;
898 if (!task) {
900 * Enable the counter on the cpu that it's on
902 smp_call_function_single(counter->cpu, __perf_counter_enable,
903 counter, 1);
904 return;
907 spin_lock_irq(&ctx->lock);
908 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
909 goto out;
912 * If the counter is in error state, clear that first.
913 * That way, if we see the counter in error state below, we
914 * know that it has gone back into error state, as distinct
915 * from the task having been scheduled away before the
916 * cross-call arrived.
918 if (counter->state == PERF_COUNTER_STATE_ERROR)
919 counter->state = PERF_COUNTER_STATE_OFF;
921 retry:
922 spin_unlock_irq(&ctx->lock);
923 task_oncpu_function_call(task, __perf_counter_enable, counter);
925 spin_lock_irq(&ctx->lock);
928 * If the context is active and the counter is still off,
929 * we need to retry the cross-call.
931 if (ctx->is_active && counter->state == PERF_COUNTER_STATE_OFF)
932 goto retry;
935 * Since we have the lock this context can't be scheduled
936 * in, so we can change the state safely.
938 if (counter->state == PERF_COUNTER_STATE_OFF) {
939 counter->state = PERF_COUNTER_STATE_INACTIVE;
940 counter->tstamp_enabled =
941 ctx->time - counter->total_time_enabled;
943 out:
944 spin_unlock_irq(&ctx->lock);
947 static int perf_counter_refresh(struct perf_counter *counter, int refresh)
950 * not supported on inherited counters
952 if (counter->attr.inherit)
953 return -EINVAL;
955 atomic_add(refresh, &counter->event_limit);
956 perf_counter_enable(counter);
958 return 0;
961 void __perf_counter_sched_out(struct perf_counter_context *ctx,
962 struct perf_cpu_context *cpuctx)
964 struct perf_counter *counter;
966 spin_lock(&ctx->lock);
967 ctx->is_active = 0;
968 if (likely(!ctx->nr_counters))
969 goto out;
970 update_context_time(ctx);
972 perf_disable();
973 if (ctx->nr_active) {
974 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
975 if (counter != counter->group_leader)
976 counter_sched_out(counter, cpuctx, ctx);
977 else
978 group_sched_out(counter, cpuctx, ctx);
981 perf_enable();
982 out:
983 spin_unlock(&ctx->lock);
987 * Test whether two contexts are equivalent, i.e. whether they
988 * have both been cloned from the same version of the same context
989 * and they both have the same number of enabled counters.
990 * If the number of enabled counters is the same, then the set
991 * of enabled counters should be the same, because these are both
992 * inherited contexts, therefore we can't access individual counters
993 * in them directly with an fd; we can only enable/disable all
994 * counters via prctl, or enable/disable all counters in a family
995 * via ioctl, which will have the same effect on both contexts.
997 static int context_equiv(struct perf_counter_context *ctx1,
998 struct perf_counter_context *ctx2)
1000 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
1001 && ctx1->parent_gen == ctx2->parent_gen
1002 && !ctx1->pin_count && !ctx2->pin_count;
1006 * Called from scheduler to remove the counters of the current task,
1007 * with interrupts disabled.
1009 * We stop each counter and update the counter value in counter->count.
1011 * This does not protect us against NMI, but disable()
1012 * sets the disabled bit in the control field of counter _before_
1013 * accessing the counter control register. If a NMI hits, then it will
1014 * not restart the counter.
1016 void perf_counter_task_sched_out(struct task_struct *task,
1017 struct task_struct *next, int cpu)
1019 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
1020 struct perf_counter_context *ctx = task->perf_counter_ctxp;
1021 struct perf_counter_context *next_ctx;
1022 struct perf_counter_context *parent;
1023 struct pt_regs *regs;
1024 int do_switch = 1;
1026 regs = task_pt_regs(task);
1027 perf_swcounter_event(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 1, regs, 0);
1029 if (likely(!ctx || !cpuctx->task_ctx))
1030 return;
1032 update_context_time(ctx);
1034 rcu_read_lock();
1035 parent = rcu_dereference(ctx->parent_ctx);
1036 next_ctx = next->perf_counter_ctxp;
1037 if (parent && next_ctx &&
1038 rcu_dereference(next_ctx->parent_ctx) == parent) {
1040 * Looks like the two contexts are clones, so we might be
1041 * able to optimize the context switch. We lock both
1042 * contexts and check that they are clones under the
1043 * lock (including re-checking that neither has been
1044 * uncloned in the meantime). It doesn't matter which
1045 * order we take the locks because no other cpu could
1046 * be trying to lock both of these tasks.
1048 spin_lock(&ctx->lock);
1049 spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
1050 if (context_equiv(ctx, next_ctx)) {
1052 * XXX do we need a memory barrier of sorts
1053 * wrt to rcu_dereference() of perf_counter_ctxp
1055 task->perf_counter_ctxp = next_ctx;
1056 next->perf_counter_ctxp = ctx;
1057 ctx->task = next;
1058 next_ctx->task = task;
1059 do_switch = 0;
1061 spin_unlock(&next_ctx->lock);
1062 spin_unlock(&ctx->lock);
1064 rcu_read_unlock();
1066 if (do_switch) {
1067 __perf_counter_sched_out(ctx, cpuctx);
1068 cpuctx->task_ctx = NULL;
1073 * Called with IRQs disabled
1075 static void __perf_counter_task_sched_out(struct perf_counter_context *ctx)
1077 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1079 if (!cpuctx->task_ctx)
1080 return;
1082 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
1083 return;
1085 __perf_counter_sched_out(ctx, cpuctx);
1086 cpuctx->task_ctx = NULL;
1090 * Called with IRQs disabled
1092 static void perf_counter_cpu_sched_out(struct perf_cpu_context *cpuctx)
1094 __perf_counter_sched_out(&cpuctx->ctx, cpuctx);
1097 static void
1098 __perf_counter_sched_in(struct perf_counter_context *ctx,
1099 struct perf_cpu_context *cpuctx, int cpu)
1101 struct perf_counter *counter;
1102 int can_add_hw = 1;
1104 spin_lock(&ctx->lock);
1105 ctx->is_active = 1;
1106 if (likely(!ctx->nr_counters))
1107 goto out;
1109 ctx->timestamp = perf_clock();
1111 perf_disable();
1114 * First go through the list and put on any pinned groups
1115 * in order to give them the best chance of going on.
1117 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1118 if (counter->state <= PERF_COUNTER_STATE_OFF ||
1119 !counter->attr.pinned)
1120 continue;
1121 if (counter->cpu != -1 && counter->cpu != cpu)
1122 continue;
1124 if (counter != counter->group_leader)
1125 counter_sched_in(counter, cpuctx, ctx, cpu);
1126 else {
1127 if (group_can_go_on(counter, cpuctx, 1))
1128 group_sched_in(counter, cpuctx, ctx, cpu);
1132 * If this pinned group hasn't been scheduled,
1133 * put it in error state.
1135 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1136 update_group_times(counter);
1137 counter->state = PERF_COUNTER_STATE_ERROR;
1141 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1143 * Ignore counters in OFF or ERROR state, and
1144 * ignore pinned counters since we did them already.
1146 if (counter->state <= PERF_COUNTER_STATE_OFF ||
1147 counter->attr.pinned)
1148 continue;
1151 * Listen to the 'cpu' scheduling filter constraint
1152 * of counters:
1154 if (counter->cpu != -1 && counter->cpu != cpu)
1155 continue;
1157 if (counter != counter->group_leader) {
1158 if (counter_sched_in(counter, cpuctx, ctx, cpu))
1159 can_add_hw = 0;
1160 } else {
1161 if (group_can_go_on(counter, cpuctx, can_add_hw)) {
1162 if (group_sched_in(counter, cpuctx, ctx, cpu))
1163 can_add_hw = 0;
1167 perf_enable();
1168 out:
1169 spin_unlock(&ctx->lock);
1173 * Called from scheduler to add the counters of the current task
1174 * with interrupts disabled.
1176 * We restore the counter value and then enable it.
1178 * This does not protect us against NMI, but enable()
1179 * sets the enabled bit in the control field of counter _before_
1180 * accessing the counter control register. If a NMI hits, then it will
1181 * keep the counter running.
1183 void perf_counter_task_sched_in(struct task_struct *task, int cpu)
1185 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
1186 struct perf_counter_context *ctx = task->perf_counter_ctxp;
1188 if (likely(!ctx))
1189 return;
1190 if (cpuctx->task_ctx == ctx)
1191 return;
1192 __perf_counter_sched_in(ctx, cpuctx, cpu);
1193 cpuctx->task_ctx = ctx;
1196 static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu)
1198 struct perf_counter_context *ctx = &cpuctx->ctx;
1200 __perf_counter_sched_in(ctx, cpuctx, cpu);
1203 #define MAX_INTERRUPTS (~0ULL)
1205 static void perf_log_throttle(struct perf_counter *counter, int enable);
1206 static void perf_log_period(struct perf_counter *counter, u64 period);
1208 static void perf_adjust_period(struct perf_counter *counter, u64 events)
1210 struct hw_perf_counter *hwc = &counter->hw;
1211 u64 period, sample_period;
1212 s64 delta;
1214 events *= hwc->sample_period;
1215 period = div64_u64(events, counter->attr.sample_freq);
1217 delta = (s64)(period - hwc->sample_period);
1218 delta = (delta + 7) / 8; /* low pass filter */
1220 sample_period = hwc->sample_period + delta;
1222 if (!sample_period)
1223 sample_period = 1;
1225 perf_log_period(counter, sample_period);
1227 hwc->sample_period = sample_period;
1230 static void perf_ctx_adjust_freq(struct perf_counter_context *ctx)
1232 struct perf_counter *counter;
1233 struct hw_perf_counter *hwc;
1234 u64 interrupts, freq;
1236 spin_lock(&ctx->lock);
1237 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1238 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
1239 continue;
1241 hwc = &counter->hw;
1243 interrupts = hwc->interrupts;
1244 hwc->interrupts = 0;
1247 * unthrottle counters on the tick
1249 if (interrupts == MAX_INTERRUPTS) {
1250 perf_log_throttle(counter, 1);
1251 counter->pmu->unthrottle(counter);
1252 interrupts = 2*sysctl_perf_counter_sample_rate/HZ;
1255 if (!counter->attr.freq || !counter->attr.sample_freq)
1256 continue;
1259 * if the specified freq < HZ then we need to skip ticks
1261 if (counter->attr.sample_freq < HZ) {
1262 freq = counter->attr.sample_freq;
1264 hwc->freq_count += freq;
1265 hwc->freq_interrupts += interrupts;
1267 if (hwc->freq_count < HZ)
1268 continue;
1270 interrupts = hwc->freq_interrupts;
1271 hwc->freq_interrupts = 0;
1272 hwc->freq_count -= HZ;
1273 } else
1274 freq = HZ;
1276 perf_adjust_period(counter, freq * interrupts);
1279 * In order to avoid being stalled by an (accidental) huge
1280 * sample period, force reset the sample period if we didn't
1281 * get any events in this freq period.
1283 if (!interrupts) {
1284 perf_disable();
1285 counter->pmu->disable(counter);
1286 atomic64_set(&hwc->period_left, 0);
1287 counter->pmu->enable(counter);
1288 perf_enable();
1291 spin_unlock(&ctx->lock);
1295 * Round-robin a context's counters:
1297 static void rotate_ctx(struct perf_counter_context *ctx)
1299 struct perf_counter *counter;
1301 if (!ctx->nr_counters)
1302 return;
1304 spin_lock(&ctx->lock);
1306 * Rotate the first entry last (works just fine for group counters too):
1308 perf_disable();
1309 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1310 list_move_tail(&counter->list_entry, &ctx->counter_list);
1311 break;
1313 perf_enable();
1315 spin_unlock(&ctx->lock);
1318 void perf_counter_task_tick(struct task_struct *curr, int cpu)
1320 struct perf_cpu_context *cpuctx;
1321 struct perf_counter_context *ctx;
1323 if (!atomic_read(&nr_counters))
1324 return;
1326 cpuctx = &per_cpu(perf_cpu_context, cpu);
1327 ctx = curr->perf_counter_ctxp;
1329 perf_ctx_adjust_freq(&cpuctx->ctx);
1330 if (ctx)
1331 perf_ctx_adjust_freq(ctx);
1333 perf_counter_cpu_sched_out(cpuctx);
1334 if (ctx)
1335 __perf_counter_task_sched_out(ctx);
1337 rotate_ctx(&cpuctx->ctx);
1338 if (ctx)
1339 rotate_ctx(ctx);
1341 perf_counter_cpu_sched_in(cpuctx, cpu);
1342 if (ctx)
1343 perf_counter_task_sched_in(curr, cpu);
1347 * Cross CPU call to read the hardware counter
1349 static void __read(void *info)
1351 struct perf_counter *counter = info;
1352 struct perf_counter_context *ctx = counter->ctx;
1353 unsigned long flags;
1355 local_irq_save(flags);
1356 if (ctx->is_active)
1357 update_context_time(ctx);
1358 counter->pmu->read(counter);
1359 update_counter_times(counter);
1360 local_irq_restore(flags);
1363 static u64 perf_counter_read(struct perf_counter *counter)
1366 * If counter is enabled and currently active on a CPU, update the
1367 * value in the counter structure:
1369 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
1370 smp_call_function_single(counter->oncpu,
1371 __read, counter, 1);
1372 } else if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1373 update_counter_times(counter);
1376 return atomic64_read(&counter->count);
1380 * Initialize the perf_counter context in a task_struct:
1382 static void
1383 __perf_counter_init_context(struct perf_counter_context *ctx,
1384 struct task_struct *task)
1386 memset(ctx, 0, sizeof(*ctx));
1387 spin_lock_init(&ctx->lock);
1388 mutex_init(&ctx->mutex);
1389 INIT_LIST_HEAD(&ctx->counter_list);
1390 INIT_LIST_HEAD(&ctx->event_list);
1391 atomic_set(&ctx->refcount, 1);
1392 ctx->task = task;
1395 static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
1397 struct perf_counter_context *parent_ctx;
1398 struct perf_counter_context *ctx;
1399 struct perf_cpu_context *cpuctx;
1400 struct task_struct *task;
1401 unsigned long flags;
1402 int err;
1405 * If cpu is not a wildcard then this is a percpu counter:
1407 if (cpu != -1) {
1408 /* Must be root to operate on a CPU counter: */
1409 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
1410 return ERR_PTR(-EACCES);
1412 if (cpu < 0 || cpu > num_possible_cpus())
1413 return ERR_PTR(-EINVAL);
1416 * We could be clever and allow to attach a counter to an
1417 * offline CPU and activate it when the CPU comes up, but
1418 * that's for later.
1420 if (!cpu_isset(cpu, cpu_online_map))
1421 return ERR_PTR(-ENODEV);
1423 cpuctx = &per_cpu(perf_cpu_context, cpu);
1424 ctx = &cpuctx->ctx;
1425 get_ctx(ctx);
1427 return ctx;
1430 rcu_read_lock();
1431 if (!pid)
1432 task = current;
1433 else
1434 task = find_task_by_vpid(pid);
1435 if (task)
1436 get_task_struct(task);
1437 rcu_read_unlock();
1439 if (!task)
1440 return ERR_PTR(-ESRCH);
1443 * Can't attach counters to a dying task.
1445 err = -ESRCH;
1446 if (task->flags & PF_EXITING)
1447 goto errout;
1449 /* Reuse ptrace permission checks for now. */
1450 err = -EACCES;
1451 if (!ptrace_may_access(task, PTRACE_MODE_READ))
1452 goto errout;
1454 retry:
1455 ctx = perf_lock_task_context(task, &flags);
1456 if (ctx) {
1457 parent_ctx = ctx->parent_ctx;
1458 if (parent_ctx) {
1459 put_ctx(parent_ctx);
1460 ctx->parent_ctx = NULL; /* no longer a clone */
1463 * Get an extra reference before dropping the lock so that
1464 * this context won't get freed if the task exits.
1466 get_ctx(ctx);
1467 spin_unlock_irqrestore(&ctx->lock, flags);
1470 if (!ctx) {
1471 ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
1472 err = -ENOMEM;
1473 if (!ctx)
1474 goto errout;
1475 __perf_counter_init_context(ctx, task);
1476 get_ctx(ctx);
1477 if (cmpxchg(&task->perf_counter_ctxp, NULL, ctx)) {
1479 * We raced with some other task; use
1480 * the context they set.
1482 kfree(ctx);
1483 goto retry;
1485 get_task_struct(task);
1488 put_task_struct(task);
1489 return ctx;
1491 errout:
1492 put_task_struct(task);
1493 return ERR_PTR(err);
1496 static void free_counter_rcu(struct rcu_head *head)
1498 struct perf_counter *counter;
1500 counter = container_of(head, struct perf_counter, rcu_head);
1501 if (counter->ns)
1502 put_pid_ns(counter->ns);
1503 kfree(counter);
1506 static void perf_pending_sync(struct perf_counter *counter);
1508 static void free_counter(struct perf_counter *counter)
1510 perf_pending_sync(counter);
1512 atomic_dec(&nr_counters);
1513 if (counter->attr.mmap)
1514 atomic_dec(&nr_mmap_counters);
1515 if (counter->attr.comm)
1516 atomic_dec(&nr_comm_counters);
1518 if (counter->destroy)
1519 counter->destroy(counter);
1521 put_ctx(counter->ctx);
1522 call_rcu(&counter->rcu_head, free_counter_rcu);
1526 * Called when the last reference to the file is gone.
1528 static int perf_release(struct inode *inode, struct file *file)
1530 struct perf_counter *counter = file->private_data;
1531 struct perf_counter_context *ctx = counter->ctx;
1533 file->private_data = NULL;
1535 WARN_ON_ONCE(ctx->parent_ctx);
1536 mutex_lock(&ctx->mutex);
1537 perf_counter_remove_from_context(counter);
1538 mutex_unlock(&ctx->mutex);
1540 mutex_lock(&counter->owner->perf_counter_mutex);
1541 list_del_init(&counter->owner_entry);
1542 mutex_unlock(&counter->owner->perf_counter_mutex);
1543 put_task_struct(counter->owner);
1545 free_counter(counter);
1547 return 0;
1551 * Read the performance counter - simple non blocking version for now
1553 static ssize_t
1554 perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
1556 u64 values[4];
1557 int n;
1560 * Return end-of-file for a read on a counter that is in
1561 * error state (i.e. because it was pinned but it couldn't be
1562 * scheduled on to the CPU at some point).
1564 if (counter->state == PERF_COUNTER_STATE_ERROR)
1565 return 0;
1567 WARN_ON_ONCE(counter->ctx->parent_ctx);
1568 mutex_lock(&counter->child_mutex);
1569 values[0] = perf_counter_read(counter);
1570 n = 1;
1571 if (counter->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1572 values[n++] = counter->total_time_enabled +
1573 atomic64_read(&counter->child_total_time_enabled);
1574 if (counter->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1575 values[n++] = counter->total_time_running +
1576 atomic64_read(&counter->child_total_time_running);
1577 if (counter->attr.read_format & PERF_FORMAT_ID)
1578 values[n++] = counter->id;
1579 mutex_unlock(&counter->child_mutex);
1581 if (count < n * sizeof(u64))
1582 return -EINVAL;
1583 count = n * sizeof(u64);
1585 if (copy_to_user(buf, values, count))
1586 return -EFAULT;
1588 return count;
1591 static ssize_t
1592 perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
1594 struct perf_counter *counter = file->private_data;
1596 return perf_read_hw(counter, buf, count);
1599 static unsigned int perf_poll(struct file *file, poll_table *wait)
1601 struct perf_counter *counter = file->private_data;
1602 struct perf_mmap_data *data;
1603 unsigned int events = POLL_HUP;
1605 rcu_read_lock();
1606 data = rcu_dereference(counter->data);
1607 if (data)
1608 events = atomic_xchg(&data->poll, 0);
1609 rcu_read_unlock();
1611 poll_wait(file, &counter->waitq, wait);
1613 return events;
1616 static void perf_counter_reset(struct perf_counter *counter)
1618 (void)perf_counter_read(counter);
1619 atomic64_set(&counter->count, 0);
1620 perf_counter_update_userpage(counter);
1624 * Holding the top-level counter's child_mutex means that any
1625 * descendant process that has inherited this counter will block
1626 * in sync_child_counter if it goes to exit, thus satisfying the
1627 * task existence requirements of perf_counter_enable/disable.
1629 static void perf_counter_for_each_child(struct perf_counter *counter,
1630 void (*func)(struct perf_counter *))
1632 struct perf_counter *child;
1634 WARN_ON_ONCE(counter->ctx->parent_ctx);
1635 mutex_lock(&counter->child_mutex);
1636 func(counter);
1637 list_for_each_entry(child, &counter->child_list, child_list)
1638 func(child);
1639 mutex_unlock(&counter->child_mutex);
1642 static void perf_counter_for_each(struct perf_counter *counter,
1643 void (*func)(struct perf_counter *))
1645 struct perf_counter_context *ctx = counter->ctx;
1646 struct perf_counter *sibling;
1648 WARN_ON_ONCE(ctx->parent_ctx);
1649 mutex_lock(&ctx->mutex);
1650 counter = counter->group_leader;
1652 perf_counter_for_each_child(counter, func);
1653 func(counter);
1654 list_for_each_entry(sibling, &counter->sibling_list, list_entry)
1655 perf_counter_for_each_child(counter, func);
1656 mutex_unlock(&ctx->mutex);
1659 static int perf_counter_period(struct perf_counter *counter, u64 __user *arg)
1661 struct perf_counter_context *ctx = counter->ctx;
1662 unsigned long size;
1663 int ret = 0;
1664 u64 value;
1666 if (!counter->attr.sample_period)
1667 return -EINVAL;
1669 size = copy_from_user(&value, arg, sizeof(value));
1670 if (size != sizeof(value))
1671 return -EFAULT;
1673 if (!value)
1674 return -EINVAL;
1676 spin_lock_irq(&ctx->lock);
1677 if (counter->attr.freq) {
1678 if (value > sysctl_perf_counter_sample_rate) {
1679 ret = -EINVAL;
1680 goto unlock;
1683 counter->attr.sample_freq = value;
1684 } else {
1685 perf_log_period(counter, value);
1687 counter->attr.sample_period = value;
1688 counter->hw.sample_period = value;
1690 unlock:
1691 spin_unlock_irq(&ctx->lock);
1693 return ret;
1696 static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1698 struct perf_counter *counter = file->private_data;
1699 void (*func)(struct perf_counter *);
1700 u32 flags = arg;
1702 switch (cmd) {
1703 case PERF_COUNTER_IOC_ENABLE:
1704 func = perf_counter_enable;
1705 break;
1706 case PERF_COUNTER_IOC_DISABLE:
1707 func = perf_counter_disable;
1708 break;
1709 case PERF_COUNTER_IOC_RESET:
1710 func = perf_counter_reset;
1711 break;
1713 case PERF_COUNTER_IOC_REFRESH:
1714 return perf_counter_refresh(counter, arg);
1716 case PERF_COUNTER_IOC_PERIOD:
1717 return perf_counter_period(counter, (u64 __user *)arg);
1719 default:
1720 return -ENOTTY;
1723 if (flags & PERF_IOC_FLAG_GROUP)
1724 perf_counter_for_each(counter, func);
1725 else
1726 perf_counter_for_each_child(counter, func);
1728 return 0;
1731 int perf_counter_task_enable(void)
1733 struct perf_counter *counter;
1735 mutex_lock(&current->perf_counter_mutex);
1736 list_for_each_entry(counter, &current->perf_counter_list, owner_entry)
1737 perf_counter_for_each_child(counter, perf_counter_enable);
1738 mutex_unlock(&current->perf_counter_mutex);
1740 return 0;
1743 int perf_counter_task_disable(void)
1745 struct perf_counter *counter;
1747 mutex_lock(&current->perf_counter_mutex);
1748 list_for_each_entry(counter, &current->perf_counter_list, owner_entry)
1749 perf_counter_for_each_child(counter, perf_counter_disable);
1750 mutex_unlock(&current->perf_counter_mutex);
1752 return 0;
1756 * Callers need to ensure there can be no nesting of this function, otherwise
1757 * the seqlock logic goes bad. We can not serialize this because the arch
1758 * code calls this from NMI context.
1760 void perf_counter_update_userpage(struct perf_counter *counter)
1762 struct perf_counter_mmap_page *userpg;
1763 struct perf_mmap_data *data;
1765 rcu_read_lock();
1766 data = rcu_dereference(counter->data);
1767 if (!data)
1768 goto unlock;
1770 userpg = data->user_page;
1773 * Disable preemption so as to not let the corresponding user-space
1774 * spin too long if we get preempted.
1776 preempt_disable();
1777 ++userpg->lock;
1778 barrier();
1779 userpg->index = counter->hw.idx;
1780 userpg->offset = atomic64_read(&counter->count);
1781 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
1782 userpg->offset -= atomic64_read(&counter->hw.prev_count);
1784 barrier();
1785 ++userpg->lock;
1786 preempt_enable();
1787 unlock:
1788 rcu_read_unlock();
1791 static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1793 struct perf_counter *counter = vma->vm_file->private_data;
1794 struct perf_mmap_data *data;
1795 int ret = VM_FAULT_SIGBUS;
1797 if (vmf->flags & FAULT_FLAG_MKWRITE) {
1798 if (vmf->pgoff == 0)
1799 ret = 0;
1800 return ret;
1803 rcu_read_lock();
1804 data = rcu_dereference(counter->data);
1805 if (!data)
1806 goto unlock;
1808 if (vmf->pgoff == 0) {
1809 vmf->page = virt_to_page(data->user_page);
1810 } else {
1811 int nr = vmf->pgoff - 1;
1813 if ((unsigned)nr > data->nr_pages)
1814 goto unlock;
1816 if (vmf->flags & FAULT_FLAG_WRITE)
1817 goto unlock;
1819 vmf->page = virt_to_page(data->data_pages[nr]);
1822 get_page(vmf->page);
1823 vmf->page->mapping = vma->vm_file->f_mapping;
1824 vmf->page->index = vmf->pgoff;
1826 ret = 0;
1827 unlock:
1828 rcu_read_unlock();
1830 return ret;
1833 static int perf_mmap_data_alloc(struct perf_counter *counter, int nr_pages)
1835 struct perf_mmap_data *data;
1836 unsigned long size;
1837 int i;
1839 WARN_ON(atomic_read(&counter->mmap_count));
1841 size = sizeof(struct perf_mmap_data);
1842 size += nr_pages * sizeof(void *);
1844 data = kzalloc(size, GFP_KERNEL);
1845 if (!data)
1846 goto fail;
1848 data->user_page = (void *)get_zeroed_page(GFP_KERNEL);
1849 if (!data->user_page)
1850 goto fail_user_page;
1852 for (i = 0; i < nr_pages; i++) {
1853 data->data_pages[i] = (void *)get_zeroed_page(GFP_KERNEL);
1854 if (!data->data_pages[i])
1855 goto fail_data_pages;
1858 data->nr_pages = nr_pages;
1859 atomic_set(&data->lock, -1);
1861 rcu_assign_pointer(counter->data, data);
1863 return 0;
1865 fail_data_pages:
1866 for (i--; i >= 0; i--)
1867 free_page((unsigned long)data->data_pages[i]);
1869 free_page((unsigned long)data->user_page);
1871 fail_user_page:
1872 kfree(data);
1874 fail:
1875 return -ENOMEM;
1878 static void perf_mmap_free_page(unsigned long addr)
1880 struct page *page = virt_to_page(addr);
1882 page->mapping = NULL;
1883 __free_page(page);
1886 static void __perf_mmap_data_free(struct rcu_head *rcu_head)
1888 struct perf_mmap_data *data;
1889 int i;
1891 data = container_of(rcu_head, struct perf_mmap_data, rcu_head);
1893 perf_mmap_free_page((unsigned long)data->user_page);
1894 for (i = 0; i < data->nr_pages; i++)
1895 perf_mmap_free_page((unsigned long)data->data_pages[i]);
1897 kfree(data);
1900 static void perf_mmap_data_free(struct perf_counter *counter)
1902 struct perf_mmap_data *data = counter->data;
1904 WARN_ON(atomic_read(&counter->mmap_count));
1906 rcu_assign_pointer(counter->data, NULL);
1907 call_rcu(&data->rcu_head, __perf_mmap_data_free);
1910 static void perf_mmap_open(struct vm_area_struct *vma)
1912 struct perf_counter *counter = vma->vm_file->private_data;
1914 atomic_inc(&counter->mmap_count);
1917 static void perf_mmap_close(struct vm_area_struct *vma)
1919 struct perf_counter *counter = vma->vm_file->private_data;
1921 WARN_ON_ONCE(counter->ctx->parent_ctx);
1922 if (atomic_dec_and_mutex_lock(&counter->mmap_count, &counter->mmap_mutex)) {
1923 struct user_struct *user = current_user();
1925 atomic_long_sub(counter->data->nr_pages + 1, &user->locked_vm);
1926 vma->vm_mm->locked_vm -= counter->data->nr_locked;
1927 perf_mmap_data_free(counter);
1928 mutex_unlock(&counter->mmap_mutex);
1932 static struct vm_operations_struct perf_mmap_vmops = {
1933 .open = perf_mmap_open,
1934 .close = perf_mmap_close,
1935 .fault = perf_mmap_fault,
1936 .page_mkwrite = perf_mmap_fault,
1939 static int perf_mmap(struct file *file, struct vm_area_struct *vma)
1941 struct perf_counter *counter = file->private_data;
1942 unsigned long user_locked, user_lock_limit;
1943 struct user_struct *user = current_user();
1944 unsigned long locked, lock_limit;
1945 unsigned long vma_size;
1946 unsigned long nr_pages;
1947 long user_extra, extra;
1948 int ret = 0;
1950 if (!(vma->vm_flags & VM_SHARED))
1951 return -EINVAL;
1953 vma_size = vma->vm_end - vma->vm_start;
1954 nr_pages = (vma_size / PAGE_SIZE) - 1;
1957 * If we have data pages ensure they're a power-of-two number, so we
1958 * can do bitmasks instead of modulo.
1960 if (nr_pages != 0 && !is_power_of_2(nr_pages))
1961 return -EINVAL;
1963 if (vma_size != PAGE_SIZE * (1 + nr_pages))
1964 return -EINVAL;
1966 if (vma->vm_pgoff != 0)
1967 return -EINVAL;
1969 WARN_ON_ONCE(counter->ctx->parent_ctx);
1970 mutex_lock(&counter->mmap_mutex);
1971 if (atomic_inc_not_zero(&counter->mmap_count)) {
1972 if (nr_pages != counter->data->nr_pages)
1973 ret = -EINVAL;
1974 goto unlock;
1977 user_extra = nr_pages + 1;
1978 user_lock_limit = sysctl_perf_counter_mlock >> (PAGE_SHIFT - 10);
1981 * Increase the limit linearly with more CPUs:
1983 user_lock_limit *= num_online_cpus();
1985 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
1987 extra = 0;
1988 if (user_locked > user_lock_limit)
1989 extra = user_locked - user_lock_limit;
1991 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
1992 lock_limit >>= PAGE_SHIFT;
1993 locked = vma->vm_mm->locked_vm + extra;
1995 if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) {
1996 ret = -EPERM;
1997 goto unlock;
2000 WARN_ON(counter->data);
2001 ret = perf_mmap_data_alloc(counter, nr_pages);
2002 if (ret)
2003 goto unlock;
2005 atomic_set(&counter->mmap_count, 1);
2006 atomic_long_add(user_extra, &user->locked_vm);
2007 vma->vm_mm->locked_vm += extra;
2008 counter->data->nr_locked = extra;
2009 if (vma->vm_flags & VM_WRITE)
2010 counter->data->writable = 1;
2012 unlock:
2013 mutex_unlock(&counter->mmap_mutex);
2015 vma->vm_flags |= VM_RESERVED;
2016 vma->vm_ops = &perf_mmap_vmops;
2018 return ret;
2021 static int perf_fasync(int fd, struct file *filp, int on)
2023 struct inode *inode = filp->f_path.dentry->d_inode;
2024 struct perf_counter *counter = filp->private_data;
2025 int retval;
2027 mutex_lock(&inode->i_mutex);
2028 retval = fasync_helper(fd, filp, on, &counter->fasync);
2029 mutex_unlock(&inode->i_mutex);
2031 if (retval < 0)
2032 return retval;
2034 return 0;
2037 static const struct file_operations perf_fops = {
2038 .release = perf_release,
2039 .read = perf_read,
2040 .poll = perf_poll,
2041 .unlocked_ioctl = perf_ioctl,
2042 .compat_ioctl = perf_ioctl,
2043 .mmap = perf_mmap,
2044 .fasync = perf_fasync,
2048 * Perf counter wakeup
2050 * If there's data, ensure we set the poll() state and publish everything
2051 * to user-space before waking everybody up.
2054 void perf_counter_wakeup(struct perf_counter *counter)
2056 wake_up_all(&counter->waitq);
2058 if (counter->pending_kill) {
2059 kill_fasync(&counter->fasync, SIGIO, counter->pending_kill);
2060 counter->pending_kill = 0;
2065 * Pending wakeups
2067 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
2069 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
2070 * single linked list and use cmpxchg() to add entries lockless.
2073 static void perf_pending_counter(struct perf_pending_entry *entry)
2075 struct perf_counter *counter = container_of(entry,
2076 struct perf_counter, pending);
2078 if (counter->pending_disable) {
2079 counter->pending_disable = 0;
2080 perf_counter_disable(counter);
2083 if (counter->pending_wakeup) {
2084 counter->pending_wakeup = 0;
2085 perf_counter_wakeup(counter);
2089 #define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
2091 static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
2092 PENDING_TAIL,
2095 static void perf_pending_queue(struct perf_pending_entry *entry,
2096 void (*func)(struct perf_pending_entry *))
2098 struct perf_pending_entry **head;
2100 if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
2101 return;
2103 entry->func = func;
2105 head = &get_cpu_var(perf_pending_head);
2107 do {
2108 entry->next = *head;
2109 } while (cmpxchg(head, entry->next, entry) != entry->next);
2111 set_perf_counter_pending();
2113 put_cpu_var(perf_pending_head);
2116 static int __perf_pending_run(void)
2118 struct perf_pending_entry *list;
2119 int nr = 0;
2121 list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
2122 while (list != PENDING_TAIL) {
2123 void (*func)(struct perf_pending_entry *);
2124 struct perf_pending_entry *entry = list;
2126 list = list->next;
2128 func = entry->func;
2129 entry->next = NULL;
2131 * Ensure we observe the unqueue before we issue the wakeup,
2132 * so that we won't be waiting forever.
2133 * -- see perf_not_pending().
2135 smp_wmb();
2137 func(entry);
2138 nr++;
2141 return nr;
2144 static inline int perf_not_pending(struct perf_counter *counter)
2147 * If we flush on whatever cpu we run, there is a chance we don't
2148 * need to wait.
2150 get_cpu();
2151 __perf_pending_run();
2152 put_cpu();
2155 * Ensure we see the proper queue state before going to sleep
2156 * so that we do not miss the wakeup. -- see perf_pending_handle()
2158 smp_rmb();
2159 return counter->pending.next == NULL;
2162 static void perf_pending_sync(struct perf_counter *counter)
2164 wait_event(counter->waitq, perf_not_pending(counter));
2167 void perf_counter_do_pending(void)
2169 __perf_pending_run();
2173 * Callchain support -- arch specific
2176 __weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
2178 return NULL;
2182 * Output
2185 struct perf_output_handle {
2186 struct perf_counter *counter;
2187 struct perf_mmap_data *data;
2188 unsigned long head;
2189 unsigned long offset;
2190 int nmi;
2191 int sample;
2192 int locked;
2193 unsigned long flags;
2196 static bool perf_output_space(struct perf_mmap_data *data,
2197 unsigned int offset, unsigned int head)
2199 unsigned long tail;
2200 unsigned long mask;
2202 if (!data->writable)
2203 return true;
2205 mask = (data->nr_pages << PAGE_SHIFT) - 1;
2207 * Userspace could choose to issue a mb() before updating the tail
2208 * pointer. So that all reads will be completed before the write is
2209 * issued.
2211 tail = ACCESS_ONCE(data->user_page->data_tail);
2212 smp_rmb();
2214 offset = (offset - tail) & mask;
2215 head = (head - tail) & mask;
2217 if ((int)(head - offset) < 0)
2218 return false;
2220 return true;
2223 static void perf_output_wakeup(struct perf_output_handle *handle)
2225 atomic_set(&handle->data->poll, POLL_IN);
2227 if (handle->nmi) {
2228 handle->counter->pending_wakeup = 1;
2229 perf_pending_queue(&handle->counter->pending,
2230 perf_pending_counter);
2231 } else
2232 perf_counter_wakeup(handle->counter);
2236 * Curious locking construct.
2238 * We need to ensure a later event doesn't publish a head when a former
2239 * event isn't done writing. However since we need to deal with NMIs we
2240 * cannot fully serialize things.
2242 * What we do is serialize between CPUs so we only have to deal with NMI
2243 * nesting on a single CPU.
2245 * We only publish the head (and generate a wakeup) when the outer-most
2246 * event completes.
2248 static void perf_output_lock(struct perf_output_handle *handle)
2250 struct perf_mmap_data *data = handle->data;
2251 int cpu;
2253 handle->locked = 0;
2255 local_irq_save(handle->flags);
2256 cpu = smp_processor_id();
2258 if (in_nmi() && atomic_read(&data->lock) == cpu)
2259 return;
2261 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
2262 cpu_relax();
2264 handle->locked = 1;
2267 static void perf_output_unlock(struct perf_output_handle *handle)
2269 struct perf_mmap_data *data = handle->data;
2270 unsigned long head;
2271 int cpu;
2273 data->done_head = data->head;
2275 if (!handle->locked)
2276 goto out;
2278 again:
2280 * The xchg implies a full barrier that ensures all writes are done
2281 * before we publish the new head, matched by a rmb() in userspace when
2282 * reading this position.
2284 while ((head = atomic_long_xchg(&data->done_head, 0)))
2285 data->user_page->data_head = head;
2288 * NMI can happen here, which means we can miss a done_head update.
2291 cpu = atomic_xchg(&data->lock, -1);
2292 WARN_ON_ONCE(cpu != smp_processor_id());
2295 * Therefore we have to validate we did not indeed do so.
2297 if (unlikely(atomic_long_read(&data->done_head))) {
2299 * Since we had it locked, we can lock it again.
2301 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
2302 cpu_relax();
2304 goto again;
2307 if (atomic_xchg(&data->wakeup, 0))
2308 perf_output_wakeup(handle);
2309 out:
2310 local_irq_restore(handle->flags);
2313 static void perf_output_copy(struct perf_output_handle *handle,
2314 const void *buf, unsigned int len)
2316 unsigned int pages_mask;
2317 unsigned int offset;
2318 unsigned int size;
2319 void **pages;
2321 offset = handle->offset;
2322 pages_mask = handle->data->nr_pages - 1;
2323 pages = handle->data->data_pages;
2325 do {
2326 unsigned int page_offset;
2327 int nr;
2329 nr = (offset >> PAGE_SHIFT) & pages_mask;
2330 page_offset = offset & (PAGE_SIZE - 1);
2331 size = min_t(unsigned int, PAGE_SIZE - page_offset, len);
2333 memcpy(pages[nr] + page_offset, buf, size);
2335 len -= size;
2336 buf += size;
2337 offset += size;
2338 } while (len);
2340 handle->offset = offset;
2343 * Check we didn't copy past our reservation window, taking the
2344 * possible unsigned int wrap into account.
2346 WARN_ON_ONCE(((long)(handle->head - handle->offset)) < 0);
2349 #define perf_output_put(handle, x) \
2350 perf_output_copy((handle), &(x), sizeof(x))
2352 static int perf_output_begin(struct perf_output_handle *handle,
2353 struct perf_counter *counter, unsigned int size,
2354 int nmi, int sample)
2356 struct perf_mmap_data *data;
2357 unsigned int offset, head;
2358 int have_lost;
2359 struct {
2360 struct perf_event_header header;
2361 u64 id;
2362 u64 lost;
2363 } lost_event;
2366 * For inherited counters we send all the output towards the parent.
2368 if (counter->parent)
2369 counter = counter->parent;
2371 rcu_read_lock();
2372 data = rcu_dereference(counter->data);
2373 if (!data)
2374 goto out;
2376 handle->data = data;
2377 handle->counter = counter;
2378 handle->nmi = nmi;
2379 handle->sample = sample;
2381 if (!data->nr_pages)
2382 goto fail;
2384 have_lost = atomic_read(&data->lost);
2385 if (have_lost)
2386 size += sizeof(lost_event);
2388 perf_output_lock(handle);
2390 do {
2391 offset = head = atomic_long_read(&data->head);
2392 head += size;
2393 if (unlikely(!perf_output_space(data, offset, head)))
2394 goto fail;
2395 } while (atomic_long_cmpxchg(&data->head, offset, head) != offset);
2397 handle->offset = offset;
2398 handle->head = head;
2400 if ((offset >> PAGE_SHIFT) != (head >> PAGE_SHIFT))
2401 atomic_set(&data->wakeup, 1);
2403 if (have_lost) {
2404 lost_event.header.type = PERF_EVENT_LOST;
2405 lost_event.header.misc = 0;
2406 lost_event.header.size = sizeof(lost_event);
2407 lost_event.id = counter->id;
2408 lost_event.lost = atomic_xchg(&data->lost, 0);
2410 perf_output_put(handle, lost_event);
2413 return 0;
2415 fail:
2416 atomic_inc(&data->lost);
2417 perf_output_unlock(handle);
2418 out:
2419 rcu_read_unlock();
2421 return -ENOSPC;
2424 static void perf_output_end(struct perf_output_handle *handle)
2426 struct perf_counter *counter = handle->counter;
2427 struct perf_mmap_data *data = handle->data;
2429 int wakeup_events = counter->attr.wakeup_events;
2431 if (handle->sample && wakeup_events) {
2432 int events = atomic_inc_return(&data->events);
2433 if (events >= wakeup_events) {
2434 atomic_sub(wakeup_events, &data->events);
2435 atomic_set(&data->wakeup, 1);
2439 perf_output_unlock(handle);
2440 rcu_read_unlock();
2443 static u32 perf_counter_pid(struct perf_counter *counter, struct task_struct *p)
2446 * only top level counters have the pid namespace they were created in
2448 if (counter->parent)
2449 counter = counter->parent;
2451 return task_tgid_nr_ns(p, counter->ns);
2454 static u32 perf_counter_tid(struct perf_counter *counter, struct task_struct *p)
2457 * only top level counters have the pid namespace they were created in
2459 if (counter->parent)
2460 counter = counter->parent;
2462 return task_pid_nr_ns(p, counter->ns);
2465 static void perf_counter_output(struct perf_counter *counter, int nmi,
2466 struct perf_sample_data *data)
2468 int ret;
2469 u64 sample_type = counter->attr.sample_type;
2470 struct perf_output_handle handle;
2471 struct perf_event_header header;
2472 u64 ip;
2473 struct {
2474 u32 pid, tid;
2475 } tid_entry;
2476 struct {
2477 u64 id;
2478 u64 counter;
2479 } group_entry;
2480 struct perf_callchain_entry *callchain = NULL;
2481 int callchain_size = 0;
2482 u64 time;
2483 struct {
2484 u32 cpu, reserved;
2485 } cpu_entry;
2487 header.type = 0;
2488 header.size = sizeof(header);
2490 header.misc = PERF_EVENT_MISC_OVERFLOW;
2491 header.misc |= perf_misc_flags(data->regs);
2493 if (sample_type & PERF_SAMPLE_IP) {
2494 ip = perf_instruction_pointer(data->regs);
2495 header.type |= PERF_SAMPLE_IP;
2496 header.size += sizeof(ip);
2499 if (sample_type & PERF_SAMPLE_TID) {
2500 /* namespace issues */
2501 tid_entry.pid = perf_counter_pid(counter, current);
2502 tid_entry.tid = perf_counter_tid(counter, current);
2504 header.type |= PERF_SAMPLE_TID;
2505 header.size += sizeof(tid_entry);
2508 if (sample_type & PERF_SAMPLE_TIME) {
2510 * Maybe do better on x86 and provide cpu_clock_nmi()
2512 time = sched_clock();
2514 header.type |= PERF_SAMPLE_TIME;
2515 header.size += sizeof(u64);
2518 if (sample_type & PERF_SAMPLE_ADDR) {
2519 header.type |= PERF_SAMPLE_ADDR;
2520 header.size += sizeof(u64);
2523 if (sample_type & PERF_SAMPLE_ID) {
2524 header.type |= PERF_SAMPLE_ID;
2525 header.size += sizeof(u64);
2528 if (sample_type & PERF_SAMPLE_CPU) {
2529 header.type |= PERF_SAMPLE_CPU;
2530 header.size += sizeof(cpu_entry);
2532 cpu_entry.cpu = raw_smp_processor_id();
2535 if (sample_type & PERF_SAMPLE_PERIOD) {
2536 header.type |= PERF_SAMPLE_PERIOD;
2537 header.size += sizeof(u64);
2540 if (sample_type & PERF_SAMPLE_GROUP) {
2541 header.type |= PERF_SAMPLE_GROUP;
2542 header.size += sizeof(u64) +
2543 counter->nr_siblings * sizeof(group_entry);
2546 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
2547 callchain = perf_callchain(data->regs);
2549 if (callchain) {
2550 callchain_size = (1 + callchain->nr) * sizeof(u64);
2552 header.type |= PERF_SAMPLE_CALLCHAIN;
2553 header.size += callchain_size;
2557 ret = perf_output_begin(&handle, counter, header.size, nmi, 1);
2558 if (ret)
2559 return;
2561 perf_output_put(&handle, header);
2563 if (sample_type & PERF_SAMPLE_IP)
2564 perf_output_put(&handle, ip);
2566 if (sample_type & PERF_SAMPLE_TID)
2567 perf_output_put(&handle, tid_entry);
2569 if (sample_type & PERF_SAMPLE_TIME)
2570 perf_output_put(&handle, time);
2572 if (sample_type & PERF_SAMPLE_ADDR)
2573 perf_output_put(&handle, data->addr);
2575 if (sample_type & PERF_SAMPLE_ID)
2576 perf_output_put(&handle, counter->id);
2578 if (sample_type & PERF_SAMPLE_CPU)
2579 perf_output_put(&handle, cpu_entry);
2581 if (sample_type & PERF_SAMPLE_PERIOD)
2582 perf_output_put(&handle, data->period);
2585 * XXX PERF_SAMPLE_GROUP vs inherited counters seems difficult.
2587 if (sample_type & PERF_SAMPLE_GROUP) {
2588 struct perf_counter *leader, *sub;
2589 u64 nr = counter->nr_siblings;
2591 perf_output_put(&handle, nr);
2593 leader = counter->group_leader;
2594 list_for_each_entry(sub, &leader->sibling_list, list_entry) {
2595 if (sub != counter)
2596 sub->pmu->read(sub);
2598 group_entry.id = sub->id;
2599 group_entry.counter = atomic64_read(&sub->count);
2601 perf_output_put(&handle, group_entry);
2605 if (callchain)
2606 perf_output_copy(&handle, callchain, callchain_size);
2608 perf_output_end(&handle);
2612 * fork tracking
2615 struct perf_fork_event {
2616 struct task_struct *task;
2618 struct {
2619 struct perf_event_header header;
2621 u32 pid;
2622 u32 ppid;
2623 } event;
2626 static void perf_counter_fork_output(struct perf_counter *counter,
2627 struct perf_fork_event *fork_event)
2629 struct perf_output_handle handle;
2630 int size = fork_event->event.header.size;
2631 struct task_struct *task = fork_event->task;
2632 int ret = perf_output_begin(&handle, counter, size, 0, 0);
2634 if (ret)
2635 return;
2637 fork_event->event.pid = perf_counter_pid(counter, task);
2638 fork_event->event.ppid = perf_counter_pid(counter, task->real_parent);
2640 perf_output_put(&handle, fork_event->event);
2641 perf_output_end(&handle);
2644 static int perf_counter_fork_match(struct perf_counter *counter)
2646 if (counter->attr.comm || counter->attr.mmap)
2647 return 1;
2649 return 0;
2652 static void perf_counter_fork_ctx(struct perf_counter_context *ctx,
2653 struct perf_fork_event *fork_event)
2655 struct perf_counter *counter;
2657 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2658 return;
2660 rcu_read_lock();
2661 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2662 if (perf_counter_fork_match(counter))
2663 perf_counter_fork_output(counter, fork_event);
2665 rcu_read_unlock();
2668 static void perf_counter_fork_event(struct perf_fork_event *fork_event)
2670 struct perf_cpu_context *cpuctx;
2671 struct perf_counter_context *ctx;
2673 cpuctx = &get_cpu_var(perf_cpu_context);
2674 perf_counter_fork_ctx(&cpuctx->ctx, fork_event);
2675 put_cpu_var(perf_cpu_context);
2677 rcu_read_lock();
2679 * doesn't really matter which of the child contexts the
2680 * events ends up in.
2682 ctx = rcu_dereference(current->perf_counter_ctxp);
2683 if (ctx)
2684 perf_counter_fork_ctx(ctx, fork_event);
2685 rcu_read_unlock();
2688 void perf_counter_fork(struct task_struct *task)
2690 struct perf_fork_event fork_event;
2692 if (!atomic_read(&nr_comm_counters) &&
2693 !atomic_read(&nr_mmap_counters))
2694 return;
2696 fork_event = (struct perf_fork_event){
2697 .task = task,
2698 .event = {
2699 .header = {
2700 .type = PERF_EVENT_FORK,
2701 .size = sizeof(fork_event.event),
2706 perf_counter_fork_event(&fork_event);
2710 * comm tracking
2713 struct perf_comm_event {
2714 struct task_struct *task;
2715 char *comm;
2716 int comm_size;
2718 struct {
2719 struct perf_event_header header;
2721 u32 pid;
2722 u32 tid;
2723 } event;
2726 static void perf_counter_comm_output(struct perf_counter *counter,
2727 struct perf_comm_event *comm_event)
2729 struct perf_output_handle handle;
2730 int size = comm_event->event.header.size;
2731 int ret = perf_output_begin(&handle, counter, size, 0, 0);
2733 if (ret)
2734 return;
2736 comm_event->event.pid = perf_counter_pid(counter, comm_event->task);
2737 comm_event->event.tid = perf_counter_tid(counter, comm_event->task);
2739 perf_output_put(&handle, comm_event->event);
2740 perf_output_copy(&handle, comm_event->comm,
2741 comm_event->comm_size);
2742 perf_output_end(&handle);
2745 static int perf_counter_comm_match(struct perf_counter *counter)
2747 if (counter->attr.comm)
2748 return 1;
2750 return 0;
2753 static void perf_counter_comm_ctx(struct perf_counter_context *ctx,
2754 struct perf_comm_event *comm_event)
2756 struct perf_counter *counter;
2758 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2759 return;
2761 rcu_read_lock();
2762 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2763 if (perf_counter_comm_match(counter))
2764 perf_counter_comm_output(counter, comm_event);
2766 rcu_read_unlock();
2769 static void perf_counter_comm_event(struct perf_comm_event *comm_event)
2771 struct perf_cpu_context *cpuctx;
2772 struct perf_counter_context *ctx;
2773 unsigned int size;
2774 char *comm = comm_event->task->comm;
2776 size = ALIGN(strlen(comm)+1, sizeof(u64));
2778 comm_event->comm = comm;
2779 comm_event->comm_size = size;
2781 comm_event->event.header.size = sizeof(comm_event->event) + size;
2783 cpuctx = &get_cpu_var(perf_cpu_context);
2784 perf_counter_comm_ctx(&cpuctx->ctx, comm_event);
2785 put_cpu_var(perf_cpu_context);
2787 rcu_read_lock();
2789 * doesn't really matter which of the child contexts the
2790 * events ends up in.
2792 ctx = rcu_dereference(current->perf_counter_ctxp);
2793 if (ctx)
2794 perf_counter_comm_ctx(ctx, comm_event);
2795 rcu_read_unlock();
2798 void perf_counter_comm(struct task_struct *task)
2800 struct perf_comm_event comm_event;
2802 if (!atomic_read(&nr_comm_counters))
2803 return;
2805 comm_event = (struct perf_comm_event){
2806 .task = task,
2807 .event = {
2808 .header = { .type = PERF_EVENT_COMM, },
2812 perf_counter_comm_event(&comm_event);
2816 * mmap tracking
2819 struct perf_mmap_event {
2820 struct vm_area_struct *vma;
2822 const char *file_name;
2823 int file_size;
2825 struct {
2826 struct perf_event_header header;
2828 u32 pid;
2829 u32 tid;
2830 u64 start;
2831 u64 len;
2832 u64 pgoff;
2833 } event;
2836 static void perf_counter_mmap_output(struct perf_counter *counter,
2837 struct perf_mmap_event *mmap_event)
2839 struct perf_output_handle handle;
2840 int size = mmap_event->event.header.size;
2841 int ret = perf_output_begin(&handle, counter, size, 0, 0);
2843 if (ret)
2844 return;
2846 mmap_event->event.pid = perf_counter_pid(counter, current);
2847 mmap_event->event.tid = perf_counter_tid(counter, current);
2849 perf_output_put(&handle, mmap_event->event);
2850 perf_output_copy(&handle, mmap_event->file_name,
2851 mmap_event->file_size);
2852 perf_output_end(&handle);
2855 static int perf_counter_mmap_match(struct perf_counter *counter,
2856 struct perf_mmap_event *mmap_event)
2858 if (counter->attr.mmap)
2859 return 1;
2861 return 0;
2864 static void perf_counter_mmap_ctx(struct perf_counter_context *ctx,
2865 struct perf_mmap_event *mmap_event)
2867 struct perf_counter *counter;
2869 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2870 return;
2872 rcu_read_lock();
2873 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2874 if (perf_counter_mmap_match(counter, mmap_event))
2875 perf_counter_mmap_output(counter, mmap_event);
2877 rcu_read_unlock();
2880 static void perf_counter_mmap_event(struct perf_mmap_event *mmap_event)
2882 struct perf_cpu_context *cpuctx;
2883 struct perf_counter_context *ctx;
2884 struct vm_area_struct *vma = mmap_event->vma;
2885 struct file *file = vma->vm_file;
2886 unsigned int size;
2887 char tmp[16];
2888 char *buf = NULL;
2889 const char *name;
2891 if (file) {
2892 buf = kzalloc(PATH_MAX, GFP_KERNEL);
2893 if (!buf) {
2894 name = strncpy(tmp, "//enomem", sizeof(tmp));
2895 goto got_name;
2897 name = d_path(&file->f_path, buf, PATH_MAX);
2898 if (IS_ERR(name)) {
2899 name = strncpy(tmp, "//toolong", sizeof(tmp));
2900 goto got_name;
2902 } else {
2903 name = arch_vma_name(mmap_event->vma);
2904 if (name)
2905 goto got_name;
2907 if (!vma->vm_mm) {
2908 name = strncpy(tmp, "[vdso]", sizeof(tmp));
2909 goto got_name;
2912 name = strncpy(tmp, "//anon", sizeof(tmp));
2913 goto got_name;
2916 got_name:
2917 size = ALIGN(strlen(name)+1, sizeof(u64));
2919 mmap_event->file_name = name;
2920 mmap_event->file_size = size;
2922 mmap_event->event.header.size = sizeof(mmap_event->event) + size;
2924 cpuctx = &get_cpu_var(perf_cpu_context);
2925 perf_counter_mmap_ctx(&cpuctx->ctx, mmap_event);
2926 put_cpu_var(perf_cpu_context);
2928 rcu_read_lock();
2930 * doesn't really matter which of the child contexts the
2931 * events ends up in.
2933 ctx = rcu_dereference(current->perf_counter_ctxp);
2934 if (ctx)
2935 perf_counter_mmap_ctx(ctx, mmap_event);
2936 rcu_read_unlock();
2938 kfree(buf);
2941 void __perf_counter_mmap(struct vm_area_struct *vma)
2943 struct perf_mmap_event mmap_event;
2945 if (!atomic_read(&nr_mmap_counters))
2946 return;
2948 mmap_event = (struct perf_mmap_event){
2949 .vma = vma,
2950 .event = {
2951 .header = { .type = PERF_EVENT_MMAP, },
2952 .start = vma->vm_start,
2953 .len = vma->vm_end - vma->vm_start,
2954 .pgoff = vma->vm_pgoff,
2958 perf_counter_mmap_event(&mmap_event);
2962 * Log sample_period changes so that analyzing tools can re-normalize the
2963 * event flow.
2966 struct freq_event {
2967 struct perf_event_header header;
2968 u64 time;
2969 u64 id;
2970 u64 period;
2973 static void perf_log_period(struct perf_counter *counter, u64 period)
2975 struct perf_output_handle handle;
2976 struct freq_event event;
2977 int ret;
2979 if (counter->hw.sample_period == period)
2980 return;
2982 if (counter->attr.sample_type & PERF_SAMPLE_PERIOD)
2983 return;
2985 event = (struct freq_event) {
2986 .header = {
2987 .type = PERF_EVENT_PERIOD,
2988 .misc = 0,
2989 .size = sizeof(event),
2991 .time = sched_clock(),
2992 .id = counter->id,
2993 .period = period,
2996 ret = perf_output_begin(&handle, counter, sizeof(event), 1, 0);
2997 if (ret)
2998 return;
3000 perf_output_put(&handle, event);
3001 perf_output_end(&handle);
3005 * IRQ throttle logging
3008 static void perf_log_throttle(struct perf_counter *counter, int enable)
3010 struct perf_output_handle handle;
3011 int ret;
3013 struct {
3014 struct perf_event_header header;
3015 u64 time;
3016 u64 id;
3017 } throttle_event = {
3018 .header = {
3019 .type = PERF_EVENT_THROTTLE + 1,
3020 .misc = 0,
3021 .size = sizeof(throttle_event),
3023 .time = sched_clock(),
3024 .id = counter->id,
3027 ret = perf_output_begin(&handle, counter, sizeof(throttle_event), 1, 0);
3028 if (ret)
3029 return;
3031 perf_output_put(&handle, throttle_event);
3032 perf_output_end(&handle);
3036 * Generic counter overflow handling, sampling.
3039 int perf_counter_overflow(struct perf_counter *counter, int nmi,
3040 struct perf_sample_data *data)
3042 int events = atomic_read(&counter->event_limit);
3043 int throttle = counter->pmu->unthrottle != NULL;
3044 struct hw_perf_counter *hwc = &counter->hw;
3045 int ret = 0;
3047 if (!throttle) {
3048 hwc->interrupts++;
3049 } else {
3050 if (hwc->interrupts != MAX_INTERRUPTS) {
3051 hwc->interrupts++;
3052 if (HZ * hwc->interrupts >
3053 (u64)sysctl_perf_counter_sample_rate) {
3054 hwc->interrupts = MAX_INTERRUPTS;
3055 perf_log_throttle(counter, 0);
3056 ret = 1;
3058 } else {
3060 * Keep re-disabling counters even though on the previous
3061 * pass we disabled it - just in case we raced with a
3062 * sched-in and the counter got enabled again:
3064 ret = 1;
3068 if (counter->attr.freq) {
3069 u64 now = sched_clock();
3070 s64 delta = now - hwc->freq_stamp;
3072 hwc->freq_stamp = now;
3074 if (delta > 0 && delta < TICK_NSEC)
3075 perf_adjust_period(counter, NSEC_PER_SEC / (int)delta);
3079 * XXX event_limit might not quite work as expected on inherited
3080 * counters
3083 counter->pending_kill = POLL_IN;
3084 if (events && atomic_dec_and_test(&counter->event_limit)) {
3085 ret = 1;
3086 counter->pending_kill = POLL_HUP;
3087 if (nmi) {
3088 counter->pending_disable = 1;
3089 perf_pending_queue(&counter->pending,
3090 perf_pending_counter);
3091 } else
3092 perf_counter_disable(counter);
3095 perf_counter_output(counter, nmi, data);
3096 return ret;
3100 * Generic software counter infrastructure
3103 static void perf_swcounter_update(struct perf_counter *counter)
3105 struct hw_perf_counter *hwc = &counter->hw;
3106 u64 prev, now;
3107 s64 delta;
3109 again:
3110 prev = atomic64_read(&hwc->prev_count);
3111 now = atomic64_read(&hwc->count);
3112 if (atomic64_cmpxchg(&hwc->prev_count, prev, now) != prev)
3113 goto again;
3115 delta = now - prev;
3117 atomic64_add(delta, &counter->count);
3118 atomic64_sub(delta, &hwc->period_left);
3121 static void perf_swcounter_set_period(struct perf_counter *counter)
3123 struct hw_perf_counter *hwc = &counter->hw;
3124 s64 left = atomic64_read(&hwc->period_left);
3125 s64 period = hwc->sample_period;
3127 if (unlikely(left <= -period)) {
3128 left = period;
3129 atomic64_set(&hwc->period_left, left);
3130 hwc->last_period = period;
3133 if (unlikely(left <= 0)) {
3134 left += period;
3135 atomic64_add(period, &hwc->period_left);
3136 hwc->last_period = period;
3139 atomic64_set(&hwc->prev_count, -left);
3140 atomic64_set(&hwc->count, -left);
3143 static enum hrtimer_restart perf_swcounter_hrtimer(struct hrtimer *hrtimer)
3145 enum hrtimer_restart ret = HRTIMER_RESTART;
3146 struct perf_sample_data data;
3147 struct perf_counter *counter;
3148 u64 period;
3150 counter = container_of(hrtimer, struct perf_counter, hw.hrtimer);
3151 counter->pmu->read(counter);
3153 data.addr = 0;
3154 data.regs = get_irq_regs();
3156 * In case we exclude kernel IPs or are somehow not in interrupt
3157 * context, provide the next best thing, the user IP.
3159 if ((counter->attr.exclude_kernel || !data.regs) &&
3160 !counter->attr.exclude_user)
3161 data.regs = task_pt_regs(current);
3163 if (data.regs) {
3164 if (perf_counter_overflow(counter, 0, &data))
3165 ret = HRTIMER_NORESTART;
3168 period = max_t(u64, 10000, counter->hw.sample_period);
3169 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
3171 return ret;
3174 static void perf_swcounter_overflow(struct perf_counter *counter,
3175 int nmi, struct pt_regs *regs, u64 addr)
3177 struct perf_sample_data data = {
3178 .regs = regs,
3179 .addr = addr,
3180 .period = counter->hw.last_period,
3183 perf_swcounter_update(counter);
3184 perf_swcounter_set_period(counter);
3185 if (perf_counter_overflow(counter, nmi, &data))
3186 /* soft-disable the counter */
3191 static int perf_swcounter_is_counting(struct perf_counter *counter)
3193 struct perf_counter_context *ctx;
3194 unsigned long flags;
3195 int count;
3197 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
3198 return 1;
3200 if (counter->state != PERF_COUNTER_STATE_INACTIVE)
3201 return 0;
3204 * If the counter is inactive, it could be just because
3205 * its task is scheduled out, or because it's in a group
3206 * which could not go on the PMU. We want to count in
3207 * the first case but not the second. If the context is
3208 * currently active then an inactive software counter must
3209 * be the second case. If it's not currently active then
3210 * we need to know whether the counter was active when the
3211 * context was last active, which we can determine by
3212 * comparing counter->tstamp_stopped with ctx->time.
3214 * We are within an RCU read-side critical section,
3215 * which protects the existence of *ctx.
3217 ctx = counter->ctx;
3218 spin_lock_irqsave(&ctx->lock, flags);
3219 count = 1;
3220 /* Re-check state now we have the lock */
3221 if (counter->state < PERF_COUNTER_STATE_INACTIVE ||
3222 counter->ctx->is_active ||
3223 counter->tstamp_stopped < ctx->time)
3224 count = 0;
3225 spin_unlock_irqrestore(&ctx->lock, flags);
3226 return count;
3229 static int perf_swcounter_match(struct perf_counter *counter,
3230 enum perf_type_id type,
3231 u32 event, struct pt_regs *regs)
3233 if (!perf_swcounter_is_counting(counter))
3234 return 0;
3236 if (counter->attr.type != type)
3237 return 0;
3238 if (counter->attr.config != event)
3239 return 0;
3241 if (regs) {
3242 if (counter->attr.exclude_user && user_mode(regs))
3243 return 0;
3245 if (counter->attr.exclude_kernel && !user_mode(regs))
3246 return 0;
3249 return 1;
3252 static void perf_swcounter_add(struct perf_counter *counter, u64 nr,
3253 int nmi, struct pt_regs *regs, u64 addr)
3255 int neg = atomic64_add_negative(nr, &counter->hw.count);
3257 if (counter->hw.sample_period && !neg && regs)
3258 perf_swcounter_overflow(counter, nmi, regs, addr);
3261 static void perf_swcounter_ctx_event(struct perf_counter_context *ctx,
3262 enum perf_type_id type, u32 event,
3263 u64 nr, int nmi, struct pt_regs *regs,
3264 u64 addr)
3266 struct perf_counter *counter;
3268 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
3269 return;
3271 rcu_read_lock();
3272 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
3273 if (perf_swcounter_match(counter, type, event, regs))
3274 perf_swcounter_add(counter, nr, nmi, regs, addr);
3276 rcu_read_unlock();
3279 static int *perf_swcounter_recursion_context(struct perf_cpu_context *cpuctx)
3281 if (in_nmi())
3282 return &cpuctx->recursion[3];
3284 if (in_irq())
3285 return &cpuctx->recursion[2];
3287 if (in_softirq())
3288 return &cpuctx->recursion[1];
3290 return &cpuctx->recursion[0];
3293 static void __perf_swcounter_event(enum perf_type_id type, u32 event,
3294 u64 nr, int nmi, struct pt_regs *regs,
3295 u64 addr)
3297 struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
3298 int *recursion = perf_swcounter_recursion_context(cpuctx);
3299 struct perf_counter_context *ctx;
3301 if (*recursion)
3302 goto out;
3304 (*recursion)++;
3305 barrier();
3307 perf_swcounter_ctx_event(&cpuctx->ctx, type, event,
3308 nr, nmi, regs, addr);
3309 rcu_read_lock();
3311 * doesn't really matter which of the child contexts the
3312 * events ends up in.
3314 ctx = rcu_dereference(current->perf_counter_ctxp);
3315 if (ctx)
3316 perf_swcounter_ctx_event(ctx, type, event, nr, nmi, regs, addr);
3317 rcu_read_unlock();
3319 barrier();
3320 (*recursion)--;
3322 out:
3323 put_cpu_var(perf_cpu_context);
3326 void
3327 perf_swcounter_event(u32 event, u64 nr, int nmi, struct pt_regs *regs, u64 addr)
3329 __perf_swcounter_event(PERF_TYPE_SOFTWARE, event, nr, nmi, regs, addr);
3332 static void perf_swcounter_read(struct perf_counter *counter)
3334 perf_swcounter_update(counter);
3337 static int perf_swcounter_enable(struct perf_counter *counter)
3339 perf_swcounter_set_period(counter);
3340 return 0;
3343 static void perf_swcounter_disable(struct perf_counter *counter)
3345 perf_swcounter_update(counter);
3348 static const struct pmu perf_ops_generic = {
3349 .enable = perf_swcounter_enable,
3350 .disable = perf_swcounter_disable,
3351 .read = perf_swcounter_read,
3355 * Software counter: cpu wall time clock
3358 static void cpu_clock_perf_counter_update(struct perf_counter *counter)
3360 int cpu = raw_smp_processor_id();
3361 s64 prev;
3362 u64 now;
3364 now = cpu_clock(cpu);
3365 prev = atomic64_read(&counter->hw.prev_count);
3366 atomic64_set(&counter->hw.prev_count, now);
3367 atomic64_add(now - prev, &counter->count);
3370 static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
3372 struct hw_perf_counter *hwc = &counter->hw;
3373 int cpu = raw_smp_processor_id();
3375 atomic64_set(&hwc->prev_count, cpu_clock(cpu));
3376 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3377 hwc->hrtimer.function = perf_swcounter_hrtimer;
3378 if (hwc->sample_period) {
3379 u64 period = max_t(u64, 10000, hwc->sample_period);
3380 __hrtimer_start_range_ns(&hwc->hrtimer,
3381 ns_to_ktime(period), 0,
3382 HRTIMER_MODE_REL, 0);
3385 return 0;
3388 static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
3390 if (counter->hw.sample_period)
3391 hrtimer_cancel(&counter->hw.hrtimer);
3392 cpu_clock_perf_counter_update(counter);
3395 static void cpu_clock_perf_counter_read(struct perf_counter *counter)
3397 cpu_clock_perf_counter_update(counter);
3400 static const struct pmu perf_ops_cpu_clock = {
3401 .enable = cpu_clock_perf_counter_enable,
3402 .disable = cpu_clock_perf_counter_disable,
3403 .read = cpu_clock_perf_counter_read,
3407 * Software counter: task time clock
3410 static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
3412 u64 prev;
3413 s64 delta;
3415 prev = atomic64_xchg(&counter->hw.prev_count, now);
3416 delta = now - prev;
3417 atomic64_add(delta, &counter->count);
3420 static int task_clock_perf_counter_enable(struct perf_counter *counter)
3422 struct hw_perf_counter *hwc = &counter->hw;
3423 u64 now;
3425 now = counter->ctx->time;
3427 atomic64_set(&hwc->prev_count, now);
3428 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3429 hwc->hrtimer.function = perf_swcounter_hrtimer;
3430 if (hwc->sample_period) {
3431 u64 period = max_t(u64, 10000, hwc->sample_period);
3432 __hrtimer_start_range_ns(&hwc->hrtimer,
3433 ns_to_ktime(period), 0,
3434 HRTIMER_MODE_REL, 0);
3437 return 0;
3440 static void task_clock_perf_counter_disable(struct perf_counter *counter)
3442 if (counter->hw.sample_period)
3443 hrtimer_cancel(&counter->hw.hrtimer);
3444 task_clock_perf_counter_update(counter, counter->ctx->time);
3448 static void task_clock_perf_counter_read(struct perf_counter *counter)
3450 u64 time;
3452 if (!in_nmi()) {
3453 update_context_time(counter->ctx);
3454 time = counter->ctx->time;
3455 } else {
3456 u64 now = perf_clock();
3457 u64 delta = now - counter->ctx->timestamp;
3458 time = counter->ctx->time + delta;
3461 task_clock_perf_counter_update(counter, time);
3464 static const struct pmu perf_ops_task_clock = {
3465 .enable = task_clock_perf_counter_enable,
3466 .disable = task_clock_perf_counter_disable,
3467 .read = task_clock_perf_counter_read,
3470 #ifdef CONFIG_EVENT_PROFILE
3471 void perf_tpcounter_event(int event_id)
3473 struct pt_regs *regs = get_irq_regs();
3475 if (!regs)
3476 regs = task_pt_regs(current);
3478 __perf_swcounter_event(PERF_TYPE_TRACEPOINT, event_id, 1, 1, regs, 0);
3480 EXPORT_SYMBOL_GPL(perf_tpcounter_event);
3482 extern int ftrace_profile_enable(int);
3483 extern void ftrace_profile_disable(int);
3485 static void tp_perf_counter_destroy(struct perf_counter *counter)
3487 ftrace_profile_disable(perf_event_id(&counter->attr));
3490 static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
3492 int event_id = perf_event_id(&counter->attr);
3493 int ret;
3495 ret = ftrace_profile_enable(event_id);
3496 if (ret)
3497 return NULL;
3499 counter->destroy = tp_perf_counter_destroy;
3501 return &perf_ops_generic;
3503 #else
3504 static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
3506 return NULL;
3508 #endif
3510 static const struct pmu *sw_perf_counter_init(struct perf_counter *counter)
3512 const struct pmu *pmu = NULL;
3515 * Software counters (currently) can't in general distinguish
3516 * between user, kernel and hypervisor events.
3517 * However, context switches and cpu migrations are considered
3518 * to be kernel events, and page faults are never hypervisor
3519 * events.
3521 switch (counter->attr.config) {
3522 case PERF_COUNT_SW_CPU_CLOCK:
3523 pmu = &perf_ops_cpu_clock;
3525 break;
3526 case PERF_COUNT_SW_TASK_CLOCK:
3528 * If the user instantiates this as a per-cpu counter,
3529 * use the cpu_clock counter instead.
3531 if (counter->ctx->task)
3532 pmu = &perf_ops_task_clock;
3533 else
3534 pmu = &perf_ops_cpu_clock;
3536 break;
3537 case PERF_COUNT_SW_PAGE_FAULTS:
3538 case PERF_COUNT_SW_PAGE_FAULTS_MIN:
3539 case PERF_COUNT_SW_PAGE_FAULTS_MAJ:
3540 case PERF_COUNT_SW_CONTEXT_SWITCHES:
3541 case PERF_COUNT_SW_CPU_MIGRATIONS:
3542 pmu = &perf_ops_generic;
3543 break;
3546 return pmu;
3550 * Allocate and initialize a counter structure
3552 static struct perf_counter *
3553 perf_counter_alloc(struct perf_counter_attr *attr,
3554 int cpu,
3555 struct perf_counter_context *ctx,
3556 struct perf_counter *group_leader,
3557 gfp_t gfpflags)
3559 const struct pmu *pmu;
3560 struct perf_counter *counter;
3561 struct hw_perf_counter *hwc;
3562 long err;
3564 counter = kzalloc(sizeof(*counter), gfpflags);
3565 if (!counter)
3566 return ERR_PTR(-ENOMEM);
3569 * Single counters are their own group leaders, with an
3570 * empty sibling list:
3572 if (!group_leader)
3573 group_leader = counter;
3575 mutex_init(&counter->child_mutex);
3576 INIT_LIST_HEAD(&counter->child_list);
3578 INIT_LIST_HEAD(&counter->list_entry);
3579 INIT_LIST_HEAD(&counter->event_entry);
3580 INIT_LIST_HEAD(&counter->sibling_list);
3581 init_waitqueue_head(&counter->waitq);
3583 mutex_init(&counter->mmap_mutex);
3585 counter->cpu = cpu;
3586 counter->attr = *attr;
3587 counter->group_leader = group_leader;
3588 counter->pmu = NULL;
3589 counter->ctx = ctx;
3590 counter->oncpu = -1;
3592 counter->ns = get_pid_ns(current->nsproxy->pid_ns);
3593 counter->id = atomic64_inc_return(&perf_counter_id);
3595 counter->state = PERF_COUNTER_STATE_INACTIVE;
3597 if (attr->disabled)
3598 counter->state = PERF_COUNTER_STATE_OFF;
3600 pmu = NULL;
3602 hwc = &counter->hw;
3603 hwc->sample_period = attr->sample_period;
3604 if (attr->freq && attr->sample_freq)
3605 hwc->sample_period = 1;
3607 atomic64_set(&hwc->period_left, hwc->sample_period);
3610 * we currently do not support PERF_SAMPLE_GROUP on inherited counters
3612 if (attr->inherit && (attr->sample_type & PERF_SAMPLE_GROUP))
3613 goto done;
3615 switch (attr->type) {
3616 case PERF_TYPE_RAW:
3617 case PERF_TYPE_HARDWARE:
3618 case PERF_TYPE_HW_CACHE:
3619 pmu = hw_perf_counter_init(counter);
3620 break;
3622 case PERF_TYPE_SOFTWARE:
3623 pmu = sw_perf_counter_init(counter);
3624 break;
3626 case PERF_TYPE_TRACEPOINT:
3627 pmu = tp_perf_counter_init(counter);
3628 break;
3630 default:
3631 break;
3633 done:
3634 err = 0;
3635 if (!pmu)
3636 err = -EINVAL;
3637 else if (IS_ERR(pmu))
3638 err = PTR_ERR(pmu);
3640 if (err) {
3641 if (counter->ns)
3642 put_pid_ns(counter->ns);
3643 kfree(counter);
3644 return ERR_PTR(err);
3647 counter->pmu = pmu;
3649 atomic_inc(&nr_counters);
3650 if (counter->attr.mmap)
3651 atomic_inc(&nr_mmap_counters);
3652 if (counter->attr.comm)
3653 atomic_inc(&nr_comm_counters);
3655 return counter;
3658 static int perf_copy_attr(struct perf_counter_attr __user *uattr,
3659 struct perf_counter_attr *attr)
3661 int ret;
3662 u32 size;
3664 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
3665 return -EFAULT;
3668 * zero the full structure, so that a short copy will be nice.
3670 memset(attr, 0, sizeof(*attr));
3672 ret = get_user(size, &uattr->size);
3673 if (ret)
3674 return ret;
3676 if (size > PAGE_SIZE) /* silly large */
3677 goto err_size;
3679 if (!size) /* abi compat */
3680 size = PERF_ATTR_SIZE_VER0;
3682 if (size < PERF_ATTR_SIZE_VER0)
3683 goto err_size;
3686 * If we're handed a bigger struct than we know of,
3687 * ensure all the unknown bits are 0.
3689 if (size > sizeof(*attr)) {
3690 unsigned long val;
3691 unsigned long __user *addr;
3692 unsigned long __user *end;
3694 addr = PTR_ALIGN((void __user *)uattr + sizeof(*attr),
3695 sizeof(unsigned long));
3696 end = PTR_ALIGN((void __user *)uattr + size,
3697 sizeof(unsigned long));
3699 for (; addr < end; addr += sizeof(unsigned long)) {
3700 ret = get_user(val, addr);
3701 if (ret)
3702 return ret;
3703 if (val)
3704 goto err_size;
3708 ret = copy_from_user(attr, uattr, size);
3709 if (ret)
3710 return -EFAULT;
3713 * If the type exists, the corresponding creation will verify
3714 * the attr->config.
3716 if (attr->type >= PERF_TYPE_MAX)
3717 return -EINVAL;
3719 if (attr->__reserved_1 || attr->__reserved_2 || attr->__reserved_3)
3720 return -EINVAL;
3722 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
3723 return -EINVAL;
3725 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
3726 return -EINVAL;
3728 out:
3729 return ret;
3731 err_size:
3732 put_user(sizeof(*attr), &uattr->size);
3733 ret = -E2BIG;
3734 goto out;
3738 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
3740 * @attr_uptr: event type attributes for monitoring/sampling
3741 * @pid: target pid
3742 * @cpu: target cpu
3743 * @group_fd: group leader counter fd
3745 SYSCALL_DEFINE5(perf_counter_open,
3746 struct perf_counter_attr __user *, attr_uptr,
3747 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
3749 struct perf_counter *counter, *group_leader;
3750 struct perf_counter_attr attr;
3751 struct perf_counter_context *ctx;
3752 struct file *counter_file = NULL;
3753 struct file *group_file = NULL;
3754 int fput_needed = 0;
3755 int fput_needed2 = 0;
3756 int ret;
3758 /* for future expandability... */
3759 if (flags)
3760 return -EINVAL;
3762 ret = perf_copy_attr(attr_uptr, &attr);
3763 if (ret)
3764 return ret;
3766 if (!attr.exclude_kernel) {
3767 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
3768 return -EACCES;
3771 if (attr.freq) {
3772 if (attr.sample_freq > sysctl_perf_counter_sample_rate)
3773 return -EINVAL;
3777 * Get the target context (task or percpu):
3779 ctx = find_get_context(pid, cpu);
3780 if (IS_ERR(ctx))
3781 return PTR_ERR(ctx);
3784 * Look up the group leader (we will attach this counter to it):
3786 group_leader = NULL;
3787 if (group_fd != -1) {
3788 ret = -EINVAL;
3789 group_file = fget_light(group_fd, &fput_needed);
3790 if (!group_file)
3791 goto err_put_context;
3792 if (group_file->f_op != &perf_fops)
3793 goto err_put_context;
3795 group_leader = group_file->private_data;
3797 * Do not allow a recursive hierarchy (this new sibling
3798 * becoming part of another group-sibling):
3800 if (group_leader->group_leader != group_leader)
3801 goto err_put_context;
3803 * Do not allow to attach to a group in a different
3804 * task or CPU context:
3806 if (group_leader->ctx != ctx)
3807 goto err_put_context;
3809 * Only a group leader can be exclusive or pinned
3811 if (attr.exclusive || attr.pinned)
3812 goto err_put_context;
3815 counter = perf_counter_alloc(&attr, cpu, ctx, group_leader,
3816 GFP_KERNEL);
3817 ret = PTR_ERR(counter);
3818 if (IS_ERR(counter))
3819 goto err_put_context;
3821 ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
3822 if (ret < 0)
3823 goto err_free_put_context;
3825 counter_file = fget_light(ret, &fput_needed2);
3826 if (!counter_file)
3827 goto err_free_put_context;
3829 counter->filp = counter_file;
3830 WARN_ON_ONCE(ctx->parent_ctx);
3831 mutex_lock(&ctx->mutex);
3832 perf_install_in_context(ctx, counter, cpu);
3833 ++ctx->generation;
3834 mutex_unlock(&ctx->mutex);
3836 counter->owner = current;
3837 get_task_struct(current);
3838 mutex_lock(&current->perf_counter_mutex);
3839 list_add_tail(&counter->owner_entry, &current->perf_counter_list);
3840 mutex_unlock(&current->perf_counter_mutex);
3842 fput_light(counter_file, fput_needed2);
3844 out_fput:
3845 fput_light(group_file, fput_needed);
3847 return ret;
3849 err_free_put_context:
3850 kfree(counter);
3852 err_put_context:
3853 put_ctx(ctx);
3855 goto out_fput;
3859 * inherit a counter from parent task to child task:
3861 static struct perf_counter *
3862 inherit_counter(struct perf_counter *parent_counter,
3863 struct task_struct *parent,
3864 struct perf_counter_context *parent_ctx,
3865 struct task_struct *child,
3866 struct perf_counter *group_leader,
3867 struct perf_counter_context *child_ctx)
3869 struct perf_counter *child_counter;
3872 * Instead of creating recursive hierarchies of counters,
3873 * we link inherited counters back to the original parent,
3874 * which has a filp for sure, which we use as the reference
3875 * count:
3877 if (parent_counter->parent)
3878 parent_counter = parent_counter->parent;
3880 child_counter = perf_counter_alloc(&parent_counter->attr,
3881 parent_counter->cpu, child_ctx,
3882 group_leader, GFP_KERNEL);
3883 if (IS_ERR(child_counter))
3884 return child_counter;
3885 get_ctx(child_ctx);
3888 * Make the child state follow the state of the parent counter,
3889 * not its attr.disabled bit. We hold the parent's mutex,
3890 * so we won't race with perf_counter_{en, dis}able_family.
3892 if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE)
3893 child_counter->state = PERF_COUNTER_STATE_INACTIVE;
3894 else
3895 child_counter->state = PERF_COUNTER_STATE_OFF;
3897 if (parent_counter->attr.freq)
3898 child_counter->hw.sample_period = parent_counter->hw.sample_period;
3901 * Link it up in the child's context:
3903 add_counter_to_ctx(child_counter, child_ctx);
3905 child_counter->parent = parent_counter;
3907 * inherit into child's child as well:
3909 child_counter->attr.inherit = 1;
3912 * Get a reference to the parent filp - we will fput it
3913 * when the child counter exits. This is safe to do because
3914 * we are in the parent and we know that the filp still
3915 * exists and has a nonzero count:
3917 atomic_long_inc(&parent_counter->filp->f_count);
3920 * Link this into the parent counter's child list
3922 WARN_ON_ONCE(parent_counter->ctx->parent_ctx);
3923 mutex_lock(&parent_counter->child_mutex);
3924 list_add_tail(&child_counter->child_list, &parent_counter->child_list);
3925 mutex_unlock(&parent_counter->child_mutex);
3927 return child_counter;
3930 static int inherit_group(struct perf_counter *parent_counter,
3931 struct task_struct *parent,
3932 struct perf_counter_context *parent_ctx,
3933 struct task_struct *child,
3934 struct perf_counter_context *child_ctx)
3936 struct perf_counter *leader;
3937 struct perf_counter *sub;
3938 struct perf_counter *child_ctr;
3940 leader = inherit_counter(parent_counter, parent, parent_ctx,
3941 child, NULL, child_ctx);
3942 if (IS_ERR(leader))
3943 return PTR_ERR(leader);
3944 list_for_each_entry(sub, &parent_counter->sibling_list, list_entry) {
3945 child_ctr = inherit_counter(sub, parent, parent_ctx,
3946 child, leader, child_ctx);
3947 if (IS_ERR(child_ctr))
3948 return PTR_ERR(child_ctr);
3950 return 0;
3953 static void sync_child_counter(struct perf_counter *child_counter,
3954 struct perf_counter *parent_counter)
3956 u64 child_val;
3958 child_val = atomic64_read(&child_counter->count);
3961 * Add back the child's count to the parent's count:
3963 atomic64_add(child_val, &parent_counter->count);
3964 atomic64_add(child_counter->total_time_enabled,
3965 &parent_counter->child_total_time_enabled);
3966 atomic64_add(child_counter->total_time_running,
3967 &parent_counter->child_total_time_running);
3970 * Remove this counter from the parent's list
3972 WARN_ON_ONCE(parent_counter->ctx->parent_ctx);
3973 mutex_lock(&parent_counter->child_mutex);
3974 list_del_init(&child_counter->child_list);
3975 mutex_unlock(&parent_counter->child_mutex);
3978 * Release the parent counter, if this was the last
3979 * reference to it.
3981 fput(parent_counter->filp);
3984 static void
3985 __perf_counter_exit_task(struct perf_counter *child_counter,
3986 struct perf_counter_context *child_ctx)
3988 struct perf_counter *parent_counter;
3990 update_counter_times(child_counter);
3991 perf_counter_remove_from_context(child_counter);
3993 parent_counter = child_counter->parent;
3995 * It can happen that parent exits first, and has counters
3996 * that are still around due to the child reference. These
3997 * counters need to be zapped - but otherwise linger.
3999 if (parent_counter) {
4000 sync_child_counter(child_counter, parent_counter);
4001 free_counter(child_counter);
4006 * When a child task exits, feed back counter values to parent counters.
4008 void perf_counter_exit_task(struct task_struct *child)
4010 struct perf_counter *child_counter, *tmp;
4011 struct perf_counter_context *child_ctx;
4012 unsigned long flags;
4014 if (likely(!child->perf_counter_ctxp))
4015 return;
4017 local_irq_save(flags);
4019 * We can't reschedule here because interrupts are disabled,
4020 * and either child is current or it is a task that can't be
4021 * scheduled, so we are now safe from rescheduling changing
4022 * our context.
4024 child_ctx = child->perf_counter_ctxp;
4025 __perf_counter_task_sched_out(child_ctx);
4028 * Take the context lock here so that if find_get_context is
4029 * reading child->perf_counter_ctxp, we wait until it has
4030 * incremented the context's refcount before we do put_ctx below.
4032 spin_lock(&child_ctx->lock);
4033 child->perf_counter_ctxp = NULL;
4034 if (child_ctx->parent_ctx) {
4036 * This context is a clone; unclone it so it can't get
4037 * swapped to another process while we're removing all
4038 * the counters from it.
4040 put_ctx(child_ctx->parent_ctx);
4041 child_ctx->parent_ctx = NULL;
4043 spin_unlock(&child_ctx->lock);
4044 local_irq_restore(flags);
4047 * We can recurse on the same lock type through:
4049 * __perf_counter_exit_task()
4050 * sync_child_counter()
4051 * fput(parent_counter->filp)
4052 * perf_release()
4053 * mutex_lock(&ctx->mutex)
4055 * But since its the parent context it won't be the same instance.
4057 mutex_lock_nested(&child_ctx->mutex, SINGLE_DEPTH_NESTING);
4059 again:
4060 list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
4061 list_entry)
4062 __perf_counter_exit_task(child_counter, child_ctx);
4065 * If the last counter was a group counter, it will have appended all
4066 * its siblings to the list, but we obtained 'tmp' before that which
4067 * will still point to the list head terminating the iteration.
4069 if (!list_empty(&child_ctx->counter_list))
4070 goto again;
4072 mutex_unlock(&child_ctx->mutex);
4074 put_ctx(child_ctx);
4078 * free an unexposed, unused context as created by inheritance by
4079 * init_task below, used by fork() in case of fail.
4081 void perf_counter_free_task(struct task_struct *task)
4083 struct perf_counter_context *ctx = task->perf_counter_ctxp;
4084 struct perf_counter *counter, *tmp;
4086 if (!ctx)
4087 return;
4089 mutex_lock(&ctx->mutex);
4090 again:
4091 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry) {
4092 struct perf_counter *parent = counter->parent;
4094 if (WARN_ON_ONCE(!parent))
4095 continue;
4097 mutex_lock(&parent->child_mutex);
4098 list_del_init(&counter->child_list);
4099 mutex_unlock(&parent->child_mutex);
4101 fput(parent->filp);
4103 list_del_counter(counter, ctx);
4104 free_counter(counter);
4107 if (!list_empty(&ctx->counter_list))
4108 goto again;
4110 mutex_unlock(&ctx->mutex);
4112 put_ctx(ctx);
4116 * Initialize the perf_counter context in task_struct
4118 int perf_counter_init_task(struct task_struct *child)
4120 struct perf_counter_context *child_ctx, *parent_ctx;
4121 struct perf_counter_context *cloned_ctx;
4122 struct perf_counter *counter;
4123 struct task_struct *parent = current;
4124 int inherited_all = 1;
4125 int ret = 0;
4127 child->perf_counter_ctxp = NULL;
4129 mutex_init(&child->perf_counter_mutex);
4130 INIT_LIST_HEAD(&child->perf_counter_list);
4132 if (likely(!parent->perf_counter_ctxp))
4133 return 0;
4136 * This is executed from the parent task context, so inherit
4137 * counters that have been marked for cloning.
4138 * First allocate and initialize a context for the child.
4141 child_ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
4142 if (!child_ctx)
4143 return -ENOMEM;
4145 __perf_counter_init_context(child_ctx, child);
4146 child->perf_counter_ctxp = child_ctx;
4147 get_task_struct(child);
4150 * If the parent's context is a clone, pin it so it won't get
4151 * swapped under us.
4153 parent_ctx = perf_pin_task_context(parent);
4156 * No need to check if parent_ctx != NULL here; since we saw
4157 * it non-NULL earlier, the only reason for it to become NULL
4158 * is if we exit, and since we're currently in the middle of
4159 * a fork we can't be exiting at the same time.
4163 * Lock the parent list. No need to lock the child - not PID
4164 * hashed yet and not running, so nobody can access it.
4166 mutex_lock(&parent_ctx->mutex);
4169 * We dont have to disable NMIs - we are only looking at
4170 * the list, not manipulating it:
4172 list_for_each_entry_rcu(counter, &parent_ctx->event_list, event_entry) {
4173 if (counter != counter->group_leader)
4174 continue;
4176 if (!counter->attr.inherit) {
4177 inherited_all = 0;
4178 continue;
4181 ret = inherit_group(counter, parent, parent_ctx,
4182 child, child_ctx);
4183 if (ret) {
4184 inherited_all = 0;
4185 break;
4189 if (inherited_all) {
4191 * Mark the child context as a clone of the parent
4192 * context, or of whatever the parent is a clone of.
4193 * Note that if the parent is a clone, it could get
4194 * uncloned at any point, but that doesn't matter
4195 * because the list of counters and the generation
4196 * count can't have changed since we took the mutex.
4198 cloned_ctx = rcu_dereference(parent_ctx->parent_ctx);
4199 if (cloned_ctx) {
4200 child_ctx->parent_ctx = cloned_ctx;
4201 child_ctx->parent_gen = parent_ctx->parent_gen;
4202 } else {
4203 child_ctx->parent_ctx = parent_ctx;
4204 child_ctx->parent_gen = parent_ctx->generation;
4206 get_ctx(child_ctx->parent_ctx);
4209 mutex_unlock(&parent_ctx->mutex);
4211 perf_unpin_context(parent_ctx);
4213 return ret;
4216 static void __cpuinit perf_counter_init_cpu(int cpu)
4218 struct perf_cpu_context *cpuctx;
4220 cpuctx = &per_cpu(perf_cpu_context, cpu);
4221 __perf_counter_init_context(&cpuctx->ctx, NULL);
4223 spin_lock(&perf_resource_lock);
4224 cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
4225 spin_unlock(&perf_resource_lock);
4227 hw_perf_counter_setup(cpu);
4230 #ifdef CONFIG_HOTPLUG_CPU
4231 static void __perf_counter_exit_cpu(void *info)
4233 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
4234 struct perf_counter_context *ctx = &cpuctx->ctx;
4235 struct perf_counter *counter, *tmp;
4237 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
4238 __perf_counter_remove_from_context(counter);
4240 static void perf_counter_exit_cpu(int cpu)
4242 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
4243 struct perf_counter_context *ctx = &cpuctx->ctx;
4245 mutex_lock(&ctx->mutex);
4246 smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
4247 mutex_unlock(&ctx->mutex);
4249 #else
4250 static inline void perf_counter_exit_cpu(int cpu) { }
4251 #endif
4253 static int __cpuinit
4254 perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
4256 unsigned int cpu = (long)hcpu;
4258 switch (action) {
4260 case CPU_UP_PREPARE:
4261 case CPU_UP_PREPARE_FROZEN:
4262 perf_counter_init_cpu(cpu);
4263 break;
4265 case CPU_DOWN_PREPARE:
4266 case CPU_DOWN_PREPARE_FROZEN:
4267 perf_counter_exit_cpu(cpu);
4268 break;
4270 default:
4271 break;
4274 return NOTIFY_OK;
4278 * This has to have a higher priority than migration_notifier in sched.c.
4280 static struct notifier_block __cpuinitdata perf_cpu_nb = {
4281 .notifier_call = perf_cpu_notify,
4282 .priority = 20,
4285 void __init perf_counter_init(void)
4287 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
4288 (void *)(long)smp_processor_id());
4289 register_cpu_notifier(&perf_cpu_nb);
4292 static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
4294 return sprintf(buf, "%d\n", perf_reserved_percpu);
4297 static ssize_t
4298 perf_set_reserve_percpu(struct sysdev_class *class,
4299 const char *buf,
4300 size_t count)
4302 struct perf_cpu_context *cpuctx;
4303 unsigned long val;
4304 int err, cpu, mpt;
4306 err = strict_strtoul(buf, 10, &val);
4307 if (err)
4308 return err;
4309 if (val > perf_max_counters)
4310 return -EINVAL;
4312 spin_lock(&perf_resource_lock);
4313 perf_reserved_percpu = val;
4314 for_each_online_cpu(cpu) {
4315 cpuctx = &per_cpu(perf_cpu_context, cpu);
4316 spin_lock_irq(&cpuctx->ctx.lock);
4317 mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
4318 perf_max_counters - perf_reserved_percpu);
4319 cpuctx->max_pertask = mpt;
4320 spin_unlock_irq(&cpuctx->ctx.lock);
4322 spin_unlock(&perf_resource_lock);
4324 return count;
4327 static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
4329 return sprintf(buf, "%d\n", perf_overcommit);
4332 static ssize_t
4333 perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
4335 unsigned long val;
4336 int err;
4338 err = strict_strtoul(buf, 10, &val);
4339 if (err)
4340 return err;
4341 if (val > 1)
4342 return -EINVAL;
4344 spin_lock(&perf_resource_lock);
4345 perf_overcommit = val;
4346 spin_unlock(&perf_resource_lock);
4348 return count;
4351 static SYSDEV_CLASS_ATTR(
4352 reserve_percpu,
4353 0644,
4354 perf_show_reserve_percpu,
4355 perf_set_reserve_percpu
4358 static SYSDEV_CLASS_ATTR(
4359 overcommit,
4360 0644,
4361 perf_show_overcommit,
4362 perf_set_overcommit
4365 static struct attribute *perfclass_attrs[] = {
4366 &attr_reserve_percpu.attr,
4367 &attr_overcommit.attr,
4368 NULL
4371 static struct attribute_group perfclass_attr_group = {
4372 .attrs = perfclass_attrs,
4373 .name = "perf_counters",
4376 static int __init perf_counter_sysfs_init(void)
4378 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
4379 &perfclass_attr_group);
4381 device_initcall(perf_counter_sysfs_init);