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
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_tracking __read_mostly;
44 static atomic_t nr_munmap_tracking __read_mostly;
45 static atomic_t nr_comm_tracking __read_mostly;
47 int sysctl_perf_counter_priv __read_mostly; /* do we need to be privileged */
48 int sysctl_perf_counter_mlock __read_mostly = 512; /* 'free' kb per user */
49 int sysctl_perf_counter_limit __read_mostly = 100000; /* max NMIs per second */
52 * Lock for (sysadmin-configurable) counter reservations:
54 static DEFINE_SPINLOCK(perf_resource_lock);
57 * Architecture provided APIs - weak aliases:
59 extern __weak const struct pmu *hw_perf_counter_init(struct perf_counter *counter)
64 void __weak hw_perf_disable(void) { barrier(); }
65 void __weak hw_perf_enable(void) { barrier(); }
67 void __weak hw_perf_counter_setup(int cpu) { barrier(); }
70 hw_perf_group_sched_in(struct perf_counter *group_leader,
71 struct perf_cpu_context *cpuctx,
72 struct perf_counter_context *ctx, int cpu)
77 void __weak perf_counter_print_debug(void) { }
79 static DEFINE_PER_CPU(int, disable_count);
81 void __perf_disable(void)
83 __get_cpu_var(disable_count)++;
86 bool __perf_enable(void)
88 return !--__get_cpu_var(disable_count);
91 void perf_disable(void)
97 void perf_enable(void)
103 static void get_ctx(struct perf_counter_context *ctx)
105 atomic_inc(&ctx->refcount);
108 static void free_ctx(struct rcu_head *head)
110 struct perf_counter_context *ctx;
112 ctx = container_of(head, struct perf_counter_context, rcu_head);
116 static void put_ctx(struct perf_counter_context *ctx)
118 if (atomic_dec_and_test(&ctx->refcount)) {
120 put_ctx(ctx->parent_ctx);
122 put_task_struct(ctx->task);
123 call_rcu(&ctx->rcu_head, free_ctx);
128 * Get the perf_counter_context for a task and lock it.
129 * This has to cope with with the fact that until it is locked,
130 * the context could get moved to another task.
132 static struct perf_counter_context *
133 perf_lock_task_context(struct task_struct *task, unsigned long *flags)
135 struct perf_counter_context *ctx;
139 ctx = rcu_dereference(task->perf_counter_ctxp);
142 * If this context is a clone of another, it might
143 * get swapped for another underneath us by
144 * perf_counter_task_sched_out, though the
145 * rcu_read_lock() protects us from any context
146 * getting freed. Lock the context and check if it
147 * got swapped before we could get the lock, and retry
148 * if so. If we locked the right context, then it
149 * can't get swapped on us any more.
151 spin_lock_irqsave(&ctx->lock, *flags);
152 if (ctx != rcu_dereference(task->perf_counter_ctxp)) {
153 spin_unlock_irqrestore(&ctx->lock, *flags);
162 * Get the context for a task and increment its pin_count so it
163 * can't get swapped to another task. This also increments its
164 * reference count so that the context can't get freed.
166 static struct perf_counter_context *perf_pin_task_context(struct task_struct *task)
168 struct perf_counter_context *ctx;
171 ctx = perf_lock_task_context(task, &flags);
175 spin_unlock_irqrestore(&ctx->lock, flags);
180 static void perf_unpin_context(struct perf_counter_context *ctx)
184 spin_lock_irqsave(&ctx->lock, flags);
186 spin_unlock_irqrestore(&ctx->lock, flags);
191 * Add a counter from the lists for its context.
192 * Must be called with ctx->mutex and ctx->lock held.
195 list_add_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
197 struct perf_counter *group_leader = counter->group_leader;
200 * Depending on whether it is a standalone or sibling counter,
201 * add it straight to the context's counter list, or to the group
202 * leader's sibling list:
204 if (group_leader == counter)
205 list_add_tail(&counter->list_entry, &ctx->counter_list);
207 list_add_tail(&counter->list_entry, &group_leader->sibling_list);
208 group_leader->nr_siblings++;
211 list_add_rcu(&counter->event_entry, &ctx->event_list);
216 * Remove a counter from the lists for its context.
217 * Must be called with ctx->mutex and ctx->lock held.
220 list_del_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
222 struct perf_counter *sibling, *tmp;
224 if (list_empty(&counter->list_entry))
228 list_del_init(&counter->list_entry);
229 list_del_rcu(&counter->event_entry);
231 if (counter->group_leader != counter)
232 counter->group_leader->nr_siblings--;
235 * If this was a group counter with sibling counters then
236 * upgrade the siblings to singleton counters by adding them
237 * to the context list directly:
239 list_for_each_entry_safe(sibling, tmp,
240 &counter->sibling_list, list_entry) {
242 list_move_tail(&sibling->list_entry, &ctx->counter_list);
243 sibling->group_leader = sibling;
248 counter_sched_out(struct perf_counter *counter,
249 struct perf_cpu_context *cpuctx,
250 struct perf_counter_context *ctx)
252 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
255 counter->state = PERF_COUNTER_STATE_INACTIVE;
256 counter->tstamp_stopped = ctx->time;
257 counter->pmu->disable(counter);
260 if (!is_software_counter(counter))
261 cpuctx->active_oncpu--;
263 if (counter->hw_event.exclusive || !cpuctx->active_oncpu)
264 cpuctx->exclusive = 0;
268 group_sched_out(struct perf_counter *group_counter,
269 struct perf_cpu_context *cpuctx,
270 struct perf_counter_context *ctx)
272 struct perf_counter *counter;
274 if (group_counter->state != PERF_COUNTER_STATE_ACTIVE)
277 counter_sched_out(group_counter, cpuctx, ctx);
280 * Schedule out siblings (if any):
282 list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
283 counter_sched_out(counter, cpuctx, ctx);
285 if (group_counter->hw_event.exclusive)
286 cpuctx->exclusive = 0;
290 * Cross CPU call to remove a performance counter
292 * We disable the counter on the hardware level first. After that we
293 * remove it from the context list.
295 static void __perf_counter_remove_from_context(void *info)
297 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
298 struct perf_counter *counter = info;
299 struct perf_counter_context *ctx = counter->ctx;
302 * If this is a task context, we need to check whether it is
303 * the current task context of this cpu. If not it has been
304 * scheduled out before the smp call arrived.
306 if (ctx->task && cpuctx->task_ctx != ctx)
309 spin_lock(&ctx->lock);
311 * Protect the list operation against NMI by disabling the
312 * counters on a global level.
316 counter_sched_out(counter, cpuctx, ctx);
318 list_del_counter(counter, ctx);
322 * Allow more per task counters with respect to the
325 cpuctx->max_pertask =
326 min(perf_max_counters - ctx->nr_counters,
327 perf_max_counters - perf_reserved_percpu);
331 spin_unlock(&ctx->lock);
336 * Remove the counter from a task's (or a CPU's) list of counters.
338 * Must be called with ctx->mutex held.
340 * CPU counters are removed with a smp call. For task counters we only
341 * call when the task is on a CPU.
343 * If counter->ctx is a cloned context, callers must make sure that
344 * every task struct that counter->ctx->task could possibly point to
345 * remains valid. This is OK when called from perf_release since
346 * that only calls us on the top-level context, which can't be a clone.
347 * When called from perf_counter_exit_task, it's OK because the
348 * context has been detached from its task.
350 static void perf_counter_remove_from_context(struct perf_counter *counter)
352 struct perf_counter_context *ctx = counter->ctx;
353 struct task_struct *task = ctx->task;
357 * Per cpu counters are removed via an smp call and
358 * the removal is always sucessful.
360 smp_call_function_single(counter->cpu,
361 __perf_counter_remove_from_context,
367 task_oncpu_function_call(task, __perf_counter_remove_from_context,
370 spin_lock_irq(&ctx->lock);
372 * If the context is active we need to retry the smp call.
374 if (ctx->nr_active && !list_empty(&counter->list_entry)) {
375 spin_unlock_irq(&ctx->lock);
380 * The lock prevents that this context is scheduled in so we
381 * can remove the counter safely, if the call above did not
384 if (!list_empty(&counter->list_entry)) {
385 list_del_counter(counter, ctx);
387 spin_unlock_irq(&ctx->lock);
390 static inline u64 perf_clock(void)
392 return cpu_clock(smp_processor_id());
396 * Update the record of the current time in a context.
398 static void update_context_time(struct perf_counter_context *ctx)
400 u64 now = perf_clock();
402 ctx->time += now - ctx->timestamp;
403 ctx->timestamp = now;
407 * Update the total_time_enabled and total_time_running fields for a counter.
409 static void update_counter_times(struct perf_counter *counter)
411 struct perf_counter_context *ctx = counter->ctx;
414 if (counter->state < PERF_COUNTER_STATE_INACTIVE)
417 counter->total_time_enabled = ctx->time - counter->tstamp_enabled;
419 if (counter->state == PERF_COUNTER_STATE_INACTIVE)
420 run_end = counter->tstamp_stopped;
424 counter->total_time_running = run_end - counter->tstamp_running;
428 * Update total_time_enabled and total_time_running for all counters in a group.
430 static void update_group_times(struct perf_counter *leader)
432 struct perf_counter *counter;
434 update_counter_times(leader);
435 list_for_each_entry(counter, &leader->sibling_list, list_entry)
436 update_counter_times(counter);
440 * Cross CPU call to disable a performance counter
442 static void __perf_counter_disable(void *info)
444 struct perf_counter *counter = info;
445 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
446 struct perf_counter_context *ctx = counter->ctx;
449 * If this is a per-task counter, need to check whether this
450 * counter's task is the current task on this cpu.
452 if (ctx->task && cpuctx->task_ctx != ctx)
455 spin_lock(&ctx->lock);
458 * If the counter is on, turn it off.
459 * If it is in error state, leave it in error state.
461 if (counter->state >= PERF_COUNTER_STATE_INACTIVE) {
462 update_context_time(ctx);
463 update_counter_times(counter);
464 if (counter == counter->group_leader)
465 group_sched_out(counter, cpuctx, ctx);
467 counter_sched_out(counter, cpuctx, ctx);
468 counter->state = PERF_COUNTER_STATE_OFF;
471 spin_unlock(&ctx->lock);
477 * If counter->ctx is a cloned context, callers must make sure that
478 * every task struct that counter->ctx->task could possibly point to
479 * remains valid. This condition is satisifed when called through
480 * perf_counter_for_each_child or perf_counter_for_each because they
481 * hold the top-level counter's child_mutex, so any descendant that
482 * goes to exit will block in sync_child_counter.
483 * When called from perf_pending_counter it's OK because counter->ctx
484 * is the current context on this CPU and preemption is disabled,
485 * hence we can't get into perf_counter_task_sched_out for this context.
487 static void perf_counter_disable(struct perf_counter *counter)
489 struct perf_counter_context *ctx = counter->ctx;
490 struct task_struct *task = ctx->task;
494 * Disable the counter on the cpu that it's on
496 smp_call_function_single(counter->cpu, __perf_counter_disable,
502 task_oncpu_function_call(task, __perf_counter_disable, counter);
504 spin_lock_irq(&ctx->lock);
506 * If the counter is still active, we need to retry the cross-call.
508 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
509 spin_unlock_irq(&ctx->lock);
514 * Since we have the lock this context can't be scheduled
515 * in, so we can change the state safely.
517 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
518 update_counter_times(counter);
519 counter->state = PERF_COUNTER_STATE_OFF;
522 spin_unlock_irq(&ctx->lock);
526 counter_sched_in(struct perf_counter *counter,
527 struct perf_cpu_context *cpuctx,
528 struct perf_counter_context *ctx,
531 if (counter->state <= PERF_COUNTER_STATE_OFF)
534 counter->state = PERF_COUNTER_STATE_ACTIVE;
535 counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
537 * The new state must be visible before we turn it on in the hardware:
541 if (counter->pmu->enable(counter)) {
542 counter->state = PERF_COUNTER_STATE_INACTIVE;
547 counter->tstamp_running += ctx->time - counter->tstamp_stopped;
549 if (!is_software_counter(counter))
550 cpuctx->active_oncpu++;
553 if (counter->hw_event.exclusive)
554 cpuctx->exclusive = 1;
560 group_sched_in(struct perf_counter *group_counter,
561 struct perf_cpu_context *cpuctx,
562 struct perf_counter_context *ctx,
565 struct perf_counter *counter, *partial_group;
568 if (group_counter->state == PERF_COUNTER_STATE_OFF)
571 ret = hw_perf_group_sched_in(group_counter, cpuctx, ctx, cpu);
573 return ret < 0 ? ret : 0;
575 group_counter->prev_state = group_counter->state;
576 if (counter_sched_in(group_counter, cpuctx, ctx, cpu))
580 * Schedule in siblings as one group (if any):
582 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
583 counter->prev_state = counter->state;
584 if (counter_sched_in(counter, cpuctx, ctx, cpu)) {
585 partial_group = counter;
594 * Groups can be scheduled in as one unit only, so undo any
595 * partial group before returning:
597 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
598 if (counter == partial_group)
600 counter_sched_out(counter, cpuctx, ctx);
602 counter_sched_out(group_counter, cpuctx, ctx);
608 * Return 1 for a group consisting entirely of software counters,
609 * 0 if the group contains any hardware counters.
611 static int is_software_only_group(struct perf_counter *leader)
613 struct perf_counter *counter;
615 if (!is_software_counter(leader))
618 list_for_each_entry(counter, &leader->sibling_list, list_entry)
619 if (!is_software_counter(counter))
626 * Work out whether we can put this counter group on the CPU now.
628 static int group_can_go_on(struct perf_counter *counter,
629 struct perf_cpu_context *cpuctx,
633 * Groups consisting entirely of software counters can always go on.
635 if (is_software_only_group(counter))
638 * If an exclusive group is already on, no other hardware
639 * counters can go on.
641 if (cpuctx->exclusive)
644 * If this group is exclusive and there are already
645 * counters on the CPU, it can't go on.
647 if (counter->hw_event.exclusive && cpuctx->active_oncpu)
650 * Otherwise, try to add it if all previous groups were able
656 static void add_counter_to_ctx(struct perf_counter *counter,
657 struct perf_counter_context *ctx)
659 list_add_counter(counter, ctx);
660 counter->prev_state = PERF_COUNTER_STATE_OFF;
661 counter->tstamp_enabled = ctx->time;
662 counter->tstamp_running = ctx->time;
663 counter->tstamp_stopped = ctx->time;
667 * Cross CPU call to install and enable a performance counter
669 * Must be called with ctx->mutex held
671 static void __perf_install_in_context(void *info)
673 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
674 struct perf_counter *counter = info;
675 struct perf_counter_context *ctx = counter->ctx;
676 struct perf_counter *leader = counter->group_leader;
677 int cpu = smp_processor_id();
681 * If this is a task context, we need to check whether it is
682 * the current task context of this cpu. If not it has been
683 * scheduled out before the smp call arrived.
684 * Or possibly this is the right context but it isn't
685 * on this cpu because it had no counters.
687 if (ctx->task && cpuctx->task_ctx != ctx) {
688 if (cpuctx->task_ctx || ctx->task != current)
690 cpuctx->task_ctx = ctx;
693 spin_lock(&ctx->lock);
695 update_context_time(ctx);
698 * Protect the list operation against NMI by disabling the
699 * counters on a global level. NOP for non NMI based counters.
703 add_counter_to_ctx(counter, ctx);
706 * Don't put the counter on if it is disabled or if
707 * it is in a group and the group isn't on.
709 if (counter->state != PERF_COUNTER_STATE_INACTIVE ||
710 (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE))
714 * An exclusive counter can't go on if there are already active
715 * hardware counters, and no hardware counter can go on if there
716 * is already an exclusive counter on.
718 if (!group_can_go_on(counter, cpuctx, 1))
721 err = counter_sched_in(counter, cpuctx, ctx, cpu);
725 * This counter couldn't go on. If it is in a group
726 * then we have to pull the whole group off.
727 * If the counter group is pinned then put it in error state.
729 if (leader != counter)
730 group_sched_out(leader, cpuctx, ctx);
731 if (leader->hw_event.pinned) {
732 update_group_times(leader);
733 leader->state = PERF_COUNTER_STATE_ERROR;
737 if (!err && !ctx->task && cpuctx->max_pertask)
738 cpuctx->max_pertask--;
743 spin_unlock(&ctx->lock);
747 * Attach a performance counter to a context
749 * First we add the counter to the list with the hardware enable bit
750 * in counter->hw_config cleared.
752 * If the counter is attached to a task which is on a CPU we use a smp
753 * call to enable it in the task context. The task might have been
754 * scheduled away, but we check this in the smp call again.
756 * Must be called with ctx->mutex held.
759 perf_install_in_context(struct perf_counter_context *ctx,
760 struct perf_counter *counter,
763 struct task_struct *task = ctx->task;
767 * Per cpu counters are installed via an smp call and
768 * the install is always sucessful.
770 smp_call_function_single(cpu, __perf_install_in_context,
776 task_oncpu_function_call(task, __perf_install_in_context,
779 spin_lock_irq(&ctx->lock);
781 * we need to retry the smp call.
783 if (ctx->is_active && list_empty(&counter->list_entry)) {
784 spin_unlock_irq(&ctx->lock);
789 * The lock prevents that this context is scheduled in so we
790 * can add the counter safely, if it the call above did not
793 if (list_empty(&counter->list_entry))
794 add_counter_to_ctx(counter, ctx);
795 spin_unlock_irq(&ctx->lock);
799 * Cross CPU call to enable a performance counter
801 static void __perf_counter_enable(void *info)
803 struct perf_counter *counter = info;
804 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
805 struct perf_counter_context *ctx = counter->ctx;
806 struct perf_counter *leader = counter->group_leader;
810 * If this is a per-task counter, need to check whether this
811 * counter's task is the current task on this cpu.
813 if (ctx->task && cpuctx->task_ctx != ctx) {
814 if (cpuctx->task_ctx || ctx->task != current)
816 cpuctx->task_ctx = ctx;
819 spin_lock(&ctx->lock);
821 update_context_time(ctx);
823 counter->prev_state = counter->state;
824 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
826 counter->state = PERF_COUNTER_STATE_INACTIVE;
827 counter->tstamp_enabled = ctx->time - counter->total_time_enabled;
830 * If the counter is in a group and isn't the group leader,
831 * then don't put it on unless the group is on.
833 if (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE)
836 if (!group_can_go_on(counter, cpuctx, 1)) {
840 if (counter == leader)
841 err = group_sched_in(counter, cpuctx, ctx,
844 err = counter_sched_in(counter, cpuctx, ctx,
851 * If this counter can't go on and it's part of a
852 * group, then the whole group has to come off.
854 if (leader != counter)
855 group_sched_out(leader, cpuctx, ctx);
856 if (leader->hw_event.pinned) {
857 update_group_times(leader);
858 leader->state = PERF_COUNTER_STATE_ERROR;
863 spin_unlock(&ctx->lock);
869 * If counter->ctx is a cloned context, callers must make sure that
870 * every task struct that counter->ctx->task could possibly point to
871 * remains valid. This condition is satisfied when called through
872 * perf_counter_for_each_child or perf_counter_for_each as described
873 * for perf_counter_disable.
875 static void perf_counter_enable(struct perf_counter *counter)
877 struct perf_counter_context *ctx = counter->ctx;
878 struct task_struct *task = ctx->task;
882 * Enable the counter on the cpu that it's on
884 smp_call_function_single(counter->cpu, __perf_counter_enable,
889 spin_lock_irq(&ctx->lock);
890 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
894 * If the counter is in error state, clear that first.
895 * That way, if we see the counter in error state below, we
896 * know that it has gone back into error state, as distinct
897 * from the task having been scheduled away before the
898 * cross-call arrived.
900 if (counter->state == PERF_COUNTER_STATE_ERROR)
901 counter->state = PERF_COUNTER_STATE_OFF;
904 spin_unlock_irq(&ctx->lock);
905 task_oncpu_function_call(task, __perf_counter_enable, counter);
907 spin_lock_irq(&ctx->lock);
910 * If the context is active and the counter is still off,
911 * we need to retry the cross-call.
913 if (ctx->is_active && counter->state == PERF_COUNTER_STATE_OFF)
917 * Since we have the lock this context can't be scheduled
918 * in, so we can change the state safely.
920 if (counter->state == PERF_COUNTER_STATE_OFF) {
921 counter->state = PERF_COUNTER_STATE_INACTIVE;
922 counter->tstamp_enabled =
923 ctx->time - counter->total_time_enabled;
926 spin_unlock_irq(&ctx->lock);
929 static int perf_counter_refresh(struct perf_counter *counter, int refresh)
932 * not supported on inherited counters
934 if (counter->hw_event.inherit)
937 atomic_add(refresh, &counter->event_limit);
938 perf_counter_enable(counter);
943 void __perf_counter_sched_out(struct perf_counter_context *ctx,
944 struct perf_cpu_context *cpuctx)
946 struct perf_counter *counter;
948 spin_lock(&ctx->lock);
950 if (likely(!ctx->nr_counters))
952 update_context_time(ctx);
955 if (ctx->nr_active) {
956 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
957 if (counter != counter->group_leader)
958 counter_sched_out(counter, cpuctx, ctx);
960 group_sched_out(counter, cpuctx, ctx);
965 spin_unlock(&ctx->lock);
969 * Test whether two contexts are equivalent, i.e. whether they
970 * have both been cloned from the same version of the same context
971 * and they both have the same number of enabled counters.
972 * If the number of enabled counters is the same, then the set
973 * of enabled counters should be the same, because these are both
974 * inherited contexts, therefore we can't access individual counters
975 * in them directly with an fd; we can only enable/disable all
976 * counters via prctl, or enable/disable all counters in a family
977 * via ioctl, which will have the same effect on both contexts.
979 static int context_equiv(struct perf_counter_context *ctx1,
980 struct perf_counter_context *ctx2)
982 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
983 && ctx1->parent_gen == ctx2->parent_gen
984 && !ctx1->pin_count && !ctx2->pin_count;
988 * Called from scheduler to remove the counters of the current task,
989 * with interrupts disabled.
991 * We stop each counter and update the counter value in counter->count.
993 * This does not protect us against NMI, but disable()
994 * sets the disabled bit in the control field of counter _before_
995 * accessing the counter control register. If a NMI hits, then it will
996 * not restart the counter.
998 void perf_counter_task_sched_out(struct task_struct *task,
999 struct task_struct *next, int cpu)
1001 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
1002 struct perf_counter_context *ctx = task->perf_counter_ctxp;
1003 struct perf_counter_context *next_ctx;
1004 struct perf_counter_context *parent;
1005 struct pt_regs *regs;
1008 regs = task_pt_regs(task);
1009 perf_swcounter_event(PERF_COUNT_CONTEXT_SWITCHES, 1, 1, regs, 0);
1011 if (likely(!ctx || !cpuctx->task_ctx))
1014 update_context_time(ctx);
1017 parent = rcu_dereference(ctx->parent_ctx);
1018 next_ctx = next->perf_counter_ctxp;
1019 if (parent && next_ctx &&
1020 rcu_dereference(next_ctx->parent_ctx) == parent) {
1022 * Looks like the two contexts are clones, so we might be
1023 * able to optimize the context switch. We lock both
1024 * contexts and check that they are clones under the
1025 * lock (including re-checking that neither has been
1026 * uncloned in the meantime). It doesn't matter which
1027 * order we take the locks because no other cpu could
1028 * be trying to lock both of these tasks.
1030 spin_lock(&ctx->lock);
1031 spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
1032 if (context_equiv(ctx, next_ctx)) {
1034 * XXX do we need a memory barrier of sorts
1035 * wrt to rcu_dereference() of perf_counter_ctxp
1037 task->perf_counter_ctxp = next_ctx;
1038 next->perf_counter_ctxp = ctx;
1040 next_ctx->task = task;
1043 spin_unlock(&next_ctx->lock);
1044 spin_unlock(&ctx->lock);
1049 __perf_counter_sched_out(ctx, cpuctx);
1050 cpuctx->task_ctx = NULL;
1055 * Called with IRQs disabled
1057 static void __perf_counter_task_sched_out(struct perf_counter_context *ctx)
1059 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1061 if (!cpuctx->task_ctx)
1064 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
1067 __perf_counter_sched_out(ctx, cpuctx);
1068 cpuctx->task_ctx = NULL;
1072 * Called with IRQs disabled
1074 static void perf_counter_cpu_sched_out(struct perf_cpu_context *cpuctx)
1076 __perf_counter_sched_out(&cpuctx->ctx, cpuctx);
1080 __perf_counter_sched_in(struct perf_counter_context *ctx,
1081 struct perf_cpu_context *cpuctx, int cpu)
1083 struct perf_counter *counter;
1086 spin_lock(&ctx->lock);
1088 if (likely(!ctx->nr_counters))
1091 ctx->timestamp = perf_clock();
1096 * First go through the list and put on any pinned groups
1097 * in order to give them the best chance of going on.
1099 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1100 if (counter->state <= PERF_COUNTER_STATE_OFF ||
1101 !counter->hw_event.pinned)
1103 if (counter->cpu != -1 && counter->cpu != cpu)
1106 if (counter != counter->group_leader)
1107 counter_sched_in(counter, cpuctx, ctx, cpu);
1109 if (group_can_go_on(counter, cpuctx, 1))
1110 group_sched_in(counter, cpuctx, ctx, cpu);
1114 * If this pinned group hasn't been scheduled,
1115 * put it in error state.
1117 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1118 update_group_times(counter);
1119 counter->state = PERF_COUNTER_STATE_ERROR;
1123 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1125 * Ignore counters in OFF or ERROR state, and
1126 * ignore pinned counters since we did them already.
1128 if (counter->state <= PERF_COUNTER_STATE_OFF ||
1129 counter->hw_event.pinned)
1133 * Listen to the 'cpu' scheduling filter constraint
1136 if (counter->cpu != -1 && counter->cpu != cpu)
1139 if (counter != counter->group_leader) {
1140 if (counter_sched_in(counter, cpuctx, ctx, cpu))
1143 if (group_can_go_on(counter, cpuctx, can_add_hw)) {
1144 if (group_sched_in(counter, cpuctx, ctx, cpu))
1151 spin_unlock(&ctx->lock);
1155 * Called from scheduler to add the counters of the current task
1156 * with interrupts disabled.
1158 * We restore the counter value and then enable it.
1160 * This does not protect us against NMI, but enable()
1161 * sets the enabled bit in the control field of counter _before_
1162 * accessing the counter control register. If a NMI hits, then it will
1163 * keep the counter running.
1165 void perf_counter_task_sched_in(struct task_struct *task, int cpu)
1167 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
1168 struct perf_counter_context *ctx = task->perf_counter_ctxp;
1172 if (cpuctx->task_ctx == ctx)
1174 __perf_counter_sched_in(ctx, cpuctx, cpu);
1175 cpuctx->task_ctx = ctx;
1178 static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu)
1180 struct perf_counter_context *ctx = &cpuctx->ctx;
1182 __perf_counter_sched_in(ctx, cpuctx, cpu);
1185 #define MAX_INTERRUPTS (~0ULL)
1187 static void perf_log_throttle(struct perf_counter *counter, int enable);
1188 static void perf_log_period(struct perf_counter *counter, u64 period);
1190 static void perf_adjust_freq(struct perf_counter_context *ctx)
1192 struct perf_counter *counter;
1193 u64 interrupts, irq_period;
1197 spin_lock(&ctx->lock);
1198 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1199 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
1202 interrupts = counter->hw.interrupts;
1203 counter->hw.interrupts = 0;
1205 if (interrupts == MAX_INTERRUPTS) {
1206 perf_log_throttle(counter, 1);
1207 counter->pmu->unthrottle(counter);
1208 interrupts = 2*sysctl_perf_counter_limit/HZ;
1211 if (!counter->hw_event.freq || !counter->hw_event.irq_freq)
1214 events = HZ * interrupts * counter->hw.irq_period;
1215 period = div64_u64(events, counter->hw_event.irq_freq);
1217 delta = (s64)(1 + period - counter->hw.irq_period);
1220 irq_period = counter->hw.irq_period + delta;
1225 perf_log_period(counter, irq_period);
1227 counter->hw.irq_period = irq_period;
1229 spin_unlock(&ctx->lock);
1233 * Round-robin a context's counters:
1235 static void rotate_ctx(struct perf_counter_context *ctx)
1237 struct perf_counter *counter;
1239 if (!ctx->nr_counters)
1242 spin_lock(&ctx->lock);
1244 * Rotate the first entry last (works just fine for group counters too):
1247 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1248 list_move_tail(&counter->list_entry, &ctx->counter_list);
1253 spin_unlock(&ctx->lock);
1256 void perf_counter_task_tick(struct task_struct *curr, int cpu)
1258 struct perf_cpu_context *cpuctx;
1259 struct perf_counter_context *ctx;
1261 if (!atomic_read(&nr_counters))
1264 cpuctx = &per_cpu(perf_cpu_context, cpu);
1265 ctx = curr->perf_counter_ctxp;
1267 perf_adjust_freq(&cpuctx->ctx);
1269 perf_adjust_freq(ctx);
1271 perf_counter_cpu_sched_out(cpuctx);
1273 __perf_counter_task_sched_out(ctx);
1275 rotate_ctx(&cpuctx->ctx);
1279 perf_counter_cpu_sched_in(cpuctx, cpu);
1281 perf_counter_task_sched_in(curr, cpu);
1285 * Cross CPU call to read the hardware counter
1287 static void __read(void *info)
1289 struct perf_counter *counter = info;
1290 struct perf_counter_context *ctx = counter->ctx;
1291 unsigned long flags;
1293 local_irq_save(flags);
1295 update_context_time(ctx);
1296 counter->pmu->read(counter);
1297 update_counter_times(counter);
1298 local_irq_restore(flags);
1301 static u64 perf_counter_read(struct perf_counter *counter)
1304 * If counter is enabled and currently active on a CPU, update the
1305 * value in the counter structure:
1307 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
1308 smp_call_function_single(counter->oncpu,
1309 __read, counter, 1);
1310 } else if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1311 update_counter_times(counter);
1314 return atomic64_read(&counter->count);
1318 * Initialize the perf_counter context in a task_struct:
1321 __perf_counter_init_context(struct perf_counter_context *ctx,
1322 struct task_struct *task)
1324 memset(ctx, 0, sizeof(*ctx));
1325 spin_lock_init(&ctx->lock);
1326 mutex_init(&ctx->mutex);
1327 INIT_LIST_HEAD(&ctx->counter_list);
1328 INIT_LIST_HEAD(&ctx->event_list);
1329 atomic_set(&ctx->refcount, 1);
1333 static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
1335 struct perf_counter_context *parent_ctx;
1336 struct perf_counter_context *ctx;
1337 struct perf_cpu_context *cpuctx;
1338 struct task_struct *task;
1339 unsigned long flags;
1343 * If cpu is not a wildcard then this is a percpu counter:
1346 /* Must be root to operate on a CPU counter: */
1347 if (sysctl_perf_counter_priv && !capable(CAP_SYS_ADMIN))
1348 return ERR_PTR(-EACCES);
1350 if (cpu < 0 || cpu > num_possible_cpus())
1351 return ERR_PTR(-EINVAL);
1354 * We could be clever and allow to attach a counter to an
1355 * offline CPU and activate it when the CPU comes up, but
1358 if (!cpu_isset(cpu, cpu_online_map))
1359 return ERR_PTR(-ENODEV);
1361 cpuctx = &per_cpu(perf_cpu_context, cpu);
1372 task = find_task_by_vpid(pid);
1374 get_task_struct(task);
1378 return ERR_PTR(-ESRCH);
1381 * Can't attach counters to a dying task.
1384 if (task->flags & PF_EXITING)
1387 /* Reuse ptrace permission checks for now. */
1389 if (!ptrace_may_access(task, PTRACE_MODE_READ))
1393 ctx = perf_lock_task_context(task, &flags);
1395 parent_ctx = ctx->parent_ctx;
1397 put_ctx(parent_ctx);
1398 ctx->parent_ctx = NULL; /* no longer a clone */
1401 * Get an extra reference before dropping the lock so that
1402 * this context won't get freed if the task exits.
1405 spin_unlock_irqrestore(&ctx->lock, flags);
1409 ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
1413 __perf_counter_init_context(ctx, task);
1415 if (cmpxchg(&task->perf_counter_ctxp, NULL, ctx)) {
1417 * We raced with some other task; use
1418 * the context they set.
1423 get_task_struct(task);
1426 put_task_struct(task);
1430 put_task_struct(task);
1431 return ERR_PTR(err);
1434 static void free_counter_rcu(struct rcu_head *head)
1436 struct perf_counter *counter;
1438 counter = container_of(head, struct perf_counter, rcu_head);
1442 static void perf_pending_sync(struct perf_counter *counter);
1444 static void free_counter(struct perf_counter *counter)
1446 perf_pending_sync(counter);
1448 atomic_dec(&nr_counters);
1449 if (counter->hw_event.mmap)
1450 atomic_dec(&nr_mmap_tracking);
1451 if (counter->hw_event.munmap)
1452 atomic_dec(&nr_munmap_tracking);
1453 if (counter->hw_event.comm)
1454 atomic_dec(&nr_comm_tracking);
1456 if (counter->destroy)
1457 counter->destroy(counter);
1459 put_ctx(counter->ctx);
1460 call_rcu(&counter->rcu_head, free_counter_rcu);
1464 * Called when the last reference to the file is gone.
1466 static int perf_release(struct inode *inode, struct file *file)
1468 struct perf_counter *counter = file->private_data;
1469 struct perf_counter_context *ctx = counter->ctx;
1471 file->private_data = NULL;
1473 WARN_ON_ONCE(ctx->parent_ctx);
1474 mutex_lock(&ctx->mutex);
1475 perf_counter_remove_from_context(counter);
1476 mutex_unlock(&ctx->mutex);
1478 mutex_lock(&counter->owner->perf_counter_mutex);
1479 list_del_init(&counter->owner_entry);
1480 mutex_unlock(&counter->owner->perf_counter_mutex);
1481 put_task_struct(counter->owner);
1483 free_counter(counter);
1489 * Read the performance counter - simple non blocking version for now
1492 perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
1498 * Return end-of-file for a read on a counter that is in
1499 * error state (i.e. because it was pinned but it couldn't be
1500 * scheduled on to the CPU at some point).
1502 if (counter->state == PERF_COUNTER_STATE_ERROR)
1505 WARN_ON_ONCE(counter->ctx->parent_ctx);
1506 mutex_lock(&counter->child_mutex);
1507 values[0] = perf_counter_read(counter);
1509 if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1510 values[n++] = counter->total_time_enabled +
1511 atomic64_read(&counter->child_total_time_enabled);
1512 if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1513 values[n++] = counter->total_time_running +
1514 atomic64_read(&counter->child_total_time_running);
1515 mutex_unlock(&counter->child_mutex);
1517 if (count < n * sizeof(u64))
1519 count = n * sizeof(u64);
1521 if (copy_to_user(buf, values, count))
1528 perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
1530 struct perf_counter *counter = file->private_data;
1532 return perf_read_hw(counter, buf, count);
1535 static unsigned int perf_poll(struct file *file, poll_table *wait)
1537 struct perf_counter *counter = file->private_data;
1538 struct perf_mmap_data *data;
1539 unsigned int events = POLL_HUP;
1542 data = rcu_dereference(counter->data);
1544 events = atomic_xchg(&data->poll, 0);
1547 poll_wait(file, &counter->waitq, wait);
1552 static void perf_counter_reset(struct perf_counter *counter)
1554 (void)perf_counter_read(counter);
1555 atomic64_set(&counter->count, 0);
1556 perf_counter_update_userpage(counter);
1559 static void perf_counter_for_each_sibling(struct perf_counter *counter,
1560 void (*func)(struct perf_counter *))
1562 struct perf_counter_context *ctx = counter->ctx;
1563 struct perf_counter *sibling;
1565 WARN_ON_ONCE(ctx->parent_ctx);
1566 mutex_lock(&ctx->mutex);
1567 counter = counter->group_leader;
1570 list_for_each_entry(sibling, &counter->sibling_list, list_entry)
1572 mutex_unlock(&ctx->mutex);
1576 * Holding the top-level counter's child_mutex means that any
1577 * descendant process that has inherited this counter will block
1578 * in sync_child_counter if it goes to exit, thus satisfying the
1579 * task existence requirements of perf_counter_enable/disable.
1581 static void perf_counter_for_each_child(struct perf_counter *counter,
1582 void (*func)(struct perf_counter *))
1584 struct perf_counter *child;
1586 WARN_ON_ONCE(counter->ctx->parent_ctx);
1587 mutex_lock(&counter->child_mutex);
1589 list_for_each_entry(child, &counter->child_list, child_list)
1591 mutex_unlock(&counter->child_mutex);
1594 static void perf_counter_for_each(struct perf_counter *counter,
1595 void (*func)(struct perf_counter *))
1597 struct perf_counter *child;
1599 WARN_ON_ONCE(counter->ctx->parent_ctx);
1600 mutex_lock(&counter->child_mutex);
1601 perf_counter_for_each_sibling(counter, func);
1602 list_for_each_entry(child, &counter->child_list, child_list)
1603 perf_counter_for_each_sibling(child, func);
1604 mutex_unlock(&counter->child_mutex);
1607 static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1609 struct perf_counter *counter = file->private_data;
1610 void (*func)(struct perf_counter *);
1614 case PERF_COUNTER_IOC_ENABLE:
1615 func = perf_counter_enable;
1617 case PERF_COUNTER_IOC_DISABLE:
1618 func = perf_counter_disable;
1620 case PERF_COUNTER_IOC_RESET:
1621 func = perf_counter_reset;
1624 case PERF_COUNTER_IOC_REFRESH:
1625 return perf_counter_refresh(counter, arg);
1630 if (flags & PERF_IOC_FLAG_GROUP)
1631 perf_counter_for_each(counter, func);
1633 perf_counter_for_each_child(counter, func);
1638 int perf_counter_task_enable(void)
1640 struct perf_counter *counter;
1642 mutex_lock(¤t->perf_counter_mutex);
1643 list_for_each_entry(counter, ¤t->perf_counter_list, owner_entry)
1644 perf_counter_for_each_child(counter, perf_counter_enable);
1645 mutex_unlock(¤t->perf_counter_mutex);
1650 int perf_counter_task_disable(void)
1652 struct perf_counter *counter;
1654 mutex_lock(¤t->perf_counter_mutex);
1655 list_for_each_entry(counter, ¤t->perf_counter_list, owner_entry)
1656 perf_counter_for_each_child(counter, perf_counter_disable);
1657 mutex_unlock(¤t->perf_counter_mutex);
1663 * Callers need to ensure there can be no nesting of this function, otherwise
1664 * the seqlock logic goes bad. We can not serialize this because the arch
1665 * code calls this from NMI context.
1667 void perf_counter_update_userpage(struct perf_counter *counter)
1669 struct perf_counter_mmap_page *userpg;
1670 struct perf_mmap_data *data;
1673 data = rcu_dereference(counter->data);
1677 userpg = data->user_page;
1680 * Disable preemption so as to not let the corresponding user-space
1681 * spin too long if we get preempted.
1686 userpg->index = counter->hw.idx;
1687 userpg->offset = atomic64_read(&counter->count);
1688 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
1689 userpg->offset -= atomic64_read(&counter->hw.prev_count);
1698 static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1700 struct perf_counter *counter = vma->vm_file->private_data;
1701 struct perf_mmap_data *data;
1702 int ret = VM_FAULT_SIGBUS;
1705 data = rcu_dereference(counter->data);
1709 if (vmf->pgoff == 0) {
1710 vmf->page = virt_to_page(data->user_page);
1712 int nr = vmf->pgoff - 1;
1714 if ((unsigned)nr > data->nr_pages)
1717 vmf->page = virt_to_page(data->data_pages[nr]);
1719 get_page(vmf->page);
1727 static int perf_mmap_data_alloc(struct perf_counter *counter, int nr_pages)
1729 struct perf_mmap_data *data;
1733 WARN_ON(atomic_read(&counter->mmap_count));
1735 size = sizeof(struct perf_mmap_data);
1736 size += nr_pages * sizeof(void *);
1738 data = kzalloc(size, GFP_KERNEL);
1742 data->user_page = (void *)get_zeroed_page(GFP_KERNEL);
1743 if (!data->user_page)
1744 goto fail_user_page;
1746 for (i = 0; i < nr_pages; i++) {
1747 data->data_pages[i] = (void *)get_zeroed_page(GFP_KERNEL);
1748 if (!data->data_pages[i])
1749 goto fail_data_pages;
1752 data->nr_pages = nr_pages;
1753 atomic_set(&data->lock, -1);
1755 rcu_assign_pointer(counter->data, data);
1760 for (i--; i >= 0; i--)
1761 free_page((unsigned long)data->data_pages[i]);
1763 free_page((unsigned long)data->user_page);
1772 static void __perf_mmap_data_free(struct rcu_head *rcu_head)
1774 struct perf_mmap_data *data;
1777 data = container_of(rcu_head, struct perf_mmap_data, rcu_head);
1779 free_page((unsigned long)data->user_page);
1780 for (i = 0; i < data->nr_pages; i++)
1781 free_page((unsigned long)data->data_pages[i]);
1785 static void perf_mmap_data_free(struct perf_counter *counter)
1787 struct perf_mmap_data *data = counter->data;
1789 WARN_ON(atomic_read(&counter->mmap_count));
1791 rcu_assign_pointer(counter->data, NULL);
1792 call_rcu(&data->rcu_head, __perf_mmap_data_free);
1795 static void perf_mmap_open(struct vm_area_struct *vma)
1797 struct perf_counter *counter = vma->vm_file->private_data;
1799 atomic_inc(&counter->mmap_count);
1802 static void perf_mmap_close(struct vm_area_struct *vma)
1804 struct perf_counter *counter = vma->vm_file->private_data;
1806 WARN_ON_ONCE(counter->ctx->parent_ctx);
1807 if (atomic_dec_and_mutex_lock(&counter->mmap_count, &counter->mmap_mutex)) {
1808 struct user_struct *user = current_user();
1810 atomic_long_sub(counter->data->nr_pages + 1, &user->locked_vm);
1811 vma->vm_mm->locked_vm -= counter->data->nr_locked;
1812 perf_mmap_data_free(counter);
1813 mutex_unlock(&counter->mmap_mutex);
1817 static struct vm_operations_struct perf_mmap_vmops = {
1818 .open = perf_mmap_open,
1819 .close = perf_mmap_close,
1820 .fault = perf_mmap_fault,
1823 static int perf_mmap(struct file *file, struct vm_area_struct *vma)
1825 struct perf_counter *counter = file->private_data;
1826 unsigned long user_locked, user_lock_limit;
1827 struct user_struct *user = current_user();
1828 unsigned long locked, lock_limit;
1829 unsigned long vma_size;
1830 unsigned long nr_pages;
1831 long user_extra, extra;
1834 if (!(vma->vm_flags & VM_SHARED) || (vma->vm_flags & VM_WRITE))
1837 vma_size = vma->vm_end - vma->vm_start;
1838 nr_pages = (vma_size / PAGE_SIZE) - 1;
1841 * If we have data pages ensure they're a power-of-two number, so we
1842 * can do bitmasks instead of modulo.
1844 if (nr_pages != 0 && !is_power_of_2(nr_pages))
1847 if (vma_size != PAGE_SIZE * (1 + nr_pages))
1850 if (vma->vm_pgoff != 0)
1853 WARN_ON_ONCE(counter->ctx->parent_ctx);
1854 mutex_lock(&counter->mmap_mutex);
1855 if (atomic_inc_not_zero(&counter->mmap_count)) {
1856 if (nr_pages != counter->data->nr_pages)
1861 user_extra = nr_pages + 1;
1862 user_lock_limit = sysctl_perf_counter_mlock >> (PAGE_SHIFT - 10);
1865 * Increase the limit linearly with more CPUs:
1867 user_lock_limit *= num_online_cpus();
1869 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
1872 if (user_locked > user_lock_limit)
1873 extra = user_locked - user_lock_limit;
1875 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
1876 lock_limit >>= PAGE_SHIFT;
1877 locked = vma->vm_mm->locked_vm + extra;
1879 if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) {
1884 WARN_ON(counter->data);
1885 ret = perf_mmap_data_alloc(counter, nr_pages);
1889 atomic_set(&counter->mmap_count, 1);
1890 atomic_long_add(user_extra, &user->locked_vm);
1891 vma->vm_mm->locked_vm += extra;
1892 counter->data->nr_locked = extra;
1894 mutex_unlock(&counter->mmap_mutex);
1896 vma->vm_flags &= ~VM_MAYWRITE;
1897 vma->vm_flags |= VM_RESERVED;
1898 vma->vm_ops = &perf_mmap_vmops;
1903 static int perf_fasync(int fd, struct file *filp, int on)
1905 struct inode *inode = filp->f_path.dentry->d_inode;
1906 struct perf_counter *counter = filp->private_data;
1909 mutex_lock(&inode->i_mutex);
1910 retval = fasync_helper(fd, filp, on, &counter->fasync);
1911 mutex_unlock(&inode->i_mutex);
1919 static const struct file_operations perf_fops = {
1920 .release = perf_release,
1923 .unlocked_ioctl = perf_ioctl,
1924 .compat_ioctl = perf_ioctl,
1926 .fasync = perf_fasync,
1930 * Perf counter wakeup
1932 * If there's data, ensure we set the poll() state and publish everything
1933 * to user-space before waking everybody up.
1936 void perf_counter_wakeup(struct perf_counter *counter)
1938 wake_up_all(&counter->waitq);
1940 if (counter->pending_kill) {
1941 kill_fasync(&counter->fasync, SIGIO, counter->pending_kill);
1942 counter->pending_kill = 0;
1949 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
1951 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
1952 * single linked list and use cmpxchg() to add entries lockless.
1955 static void perf_pending_counter(struct perf_pending_entry *entry)
1957 struct perf_counter *counter = container_of(entry,
1958 struct perf_counter, pending);
1960 if (counter->pending_disable) {
1961 counter->pending_disable = 0;
1962 perf_counter_disable(counter);
1965 if (counter->pending_wakeup) {
1966 counter->pending_wakeup = 0;
1967 perf_counter_wakeup(counter);
1971 #define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
1973 static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
1977 static void perf_pending_queue(struct perf_pending_entry *entry,
1978 void (*func)(struct perf_pending_entry *))
1980 struct perf_pending_entry **head;
1982 if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
1987 head = &get_cpu_var(perf_pending_head);
1990 entry->next = *head;
1991 } while (cmpxchg(head, entry->next, entry) != entry->next);
1993 set_perf_counter_pending();
1995 put_cpu_var(perf_pending_head);
1998 static int __perf_pending_run(void)
2000 struct perf_pending_entry *list;
2003 list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
2004 while (list != PENDING_TAIL) {
2005 void (*func)(struct perf_pending_entry *);
2006 struct perf_pending_entry *entry = list;
2013 * Ensure we observe the unqueue before we issue the wakeup,
2014 * so that we won't be waiting forever.
2015 * -- see perf_not_pending().
2026 static inline int perf_not_pending(struct perf_counter *counter)
2029 * If we flush on whatever cpu we run, there is a chance we don't
2033 __perf_pending_run();
2037 * Ensure we see the proper queue state before going to sleep
2038 * so that we do not miss the wakeup. -- see perf_pending_handle()
2041 return counter->pending.next == NULL;
2044 static void perf_pending_sync(struct perf_counter *counter)
2046 wait_event(counter->waitq, perf_not_pending(counter));
2049 void perf_counter_do_pending(void)
2051 __perf_pending_run();
2055 * Callchain support -- arch specific
2058 __weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
2067 struct perf_output_handle {
2068 struct perf_counter *counter;
2069 struct perf_mmap_data *data;
2070 unsigned int offset;
2075 unsigned long flags;
2078 static void perf_output_wakeup(struct perf_output_handle *handle)
2080 atomic_set(&handle->data->poll, POLL_IN);
2083 handle->counter->pending_wakeup = 1;
2084 perf_pending_queue(&handle->counter->pending,
2085 perf_pending_counter);
2087 perf_counter_wakeup(handle->counter);
2091 * Curious locking construct.
2093 * We need to ensure a later event doesn't publish a head when a former
2094 * event isn't done writing. However since we need to deal with NMIs we
2095 * cannot fully serialize things.
2097 * What we do is serialize between CPUs so we only have to deal with NMI
2098 * nesting on a single CPU.
2100 * We only publish the head (and generate a wakeup) when the outer-most
2103 static void perf_output_lock(struct perf_output_handle *handle)
2105 struct perf_mmap_data *data = handle->data;
2110 local_irq_save(handle->flags);
2111 cpu = smp_processor_id();
2113 if (in_nmi() && atomic_read(&data->lock) == cpu)
2116 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
2122 static void perf_output_unlock(struct perf_output_handle *handle)
2124 struct perf_mmap_data *data = handle->data;
2127 data->done_head = data->head;
2129 if (!handle->locked)
2134 * The xchg implies a full barrier that ensures all writes are done
2135 * before we publish the new head, matched by a rmb() in userspace when
2136 * reading this position.
2138 while ((head = atomic_xchg(&data->done_head, 0)))
2139 data->user_page->data_head = head;
2142 * NMI can happen here, which means we can miss a done_head update.
2145 cpu = atomic_xchg(&data->lock, -1);
2146 WARN_ON_ONCE(cpu != smp_processor_id());
2149 * Therefore we have to validate we did not indeed do so.
2151 if (unlikely(atomic_read(&data->done_head))) {
2153 * Since we had it locked, we can lock it again.
2155 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
2161 if (atomic_xchg(&data->wakeup, 0))
2162 perf_output_wakeup(handle);
2164 local_irq_restore(handle->flags);
2167 static int perf_output_begin(struct perf_output_handle *handle,
2168 struct perf_counter *counter, unsigned int size,
2169 int nmi, int overflow)
2171 struct perf_mmap_data *data;
2172 unsigned int offset, head;
2175 * For inherited counters we send all the output towards the parent.
2177 if (counter->parent)
2178 counter = counter->parent;
2181 data = rcu_dereference(counter->data);
2185 handle->data = data;
2186 handle->counter = counter;
2188 handle->overflow = overflow;
2190 if (!data->nr_pages)
2193 perf_output_lock(handle);
2196 offset = head = atomic_read(&data->head);
2198 } while (atomic_cmpxchg(&data->head, offset, head) != offset);
2200 handle->offset = offset;
2201 handle->head = head;
2203 if ((offset >> PAGE_SHIFT) != (head >> PAGE_SHIFT))
2204 atomic_set(&data->wakeup, 1);
2209 perf_output_wakeup(handle);
2216 static void perf_output_copy(struct perf_output_handle *handle,
2217 void *buf, unsigned int len)
2219 unsigned int pages_mask;
2220 unsigned int offset;
2224 offset = handle->offset;
2225 pages_mask = handle->data->nr_pages - 1;
2226 pages = handle->data->data_pages;
2229 unsigned int page_offset;
2232 nr = (offset >> PAGE_SHIFT) & pages_mask;
2233 page_offset = offset & (PAGE_SIZE - 1);
2234 size = min_t(unsigned int, PAGE_SIZE - page_offset, len);
2236 memcpy(pages[nr] + page_offset, buf, size);
2243 handle->offset = offset;
2246 * Check we didn't copy past our reservation window, taking the
2247 * possible unsigned int wrap into account.
2249 WARN_ON_ONCE(((int)(handle->head - handle->offset)) < 0);
2252 #define perf_output_put(handle, x) \
2253 perf_output_copy((handle), &(x), sizeof(x))
2255 static void perf_output_end(struct perf_output_handle *handle)
2257 struct perf_counter *counter = handle->counter;
2258 struct perf_mmap_data *data = handle->data;
2260 int wakeup_events = counter->hw_event.wakeup_events;
2262 if (handle->overflow && wakeup_events) {
2263 int events = atomic_inc_return(&data->events);
2264 if (events >= wakeup_events) {
2265 atomic_sub(wakeup_events, &data->events);
2266 atomic_set(&data->wakeup, 1);
2270 perf_output_unlock(handle);
2274 static void perf_counter_output(struct perf_counter *counter,
2275 int nmi, struct pt_regs *regs, u64 addr)
2278 u64 record_type = counter->hw_event.record_type;
2279 struct perf_output_handle handle;
2280 struct perf_event_header header;
2289 struct perf_callchain_entry *callchain = NULL;
2290 int callchain_size = 0;
2297 header.size = sizeof(header);
2299 header.misc = PERF_EVENT_MISC_OVERFLOW;
2300 header.misc |= perf_misc_flags(regs);
2302 if (record_type & PERF_RECORD_IP) {
2303 ip = perf_instruction_pointer(regs);
2304 header.type |= PERF_RECORD_IP;
2305 header.size += sizeof(ip);
2308 if (record_type & PERF_RECORD_TID) {
2309 /* namespace issues */
2310 tid_entry.pid = current->group_leader->pid;
2311 tid_entry.tid = current->pid;
2313 header.type |= PERF_RECORD_TID;
2314 header.size += sizeof(tid_entry);
2317 if (record_type & PERF_RECORD_TIME) {
2319 * Maybe do better on x86 and provide cpu_clock_nmi()
2321 time = sched_clock();
2323 header.type |= PERF_RECORD_TIME;
2324 header.size += sizeof(u64);
2327 if (record_type & PERF_RECORD_ADDR) {
2328 header.type |= PERF_RECORD_ADDR;
2329 header.size += sizeof(u64);
2332 if (record_type & PERF_RECORD_CONFIG) {
2333 header.type |= PERF_RECORD_CONFIG;
2334 header.size += sizeof(u64);
2337 if (record_type & PERF_RECORD_CPU) {
2338 header.type |= PERF_RECORD_CPU;
2339 header.size += sizeof(cpu_entry);
2341 cpu_entry.cpu = raw_smp_processor_id();
2344 if (record_type & PERF_RECORD_GROUP) {
2345 header.type |= PERF_RECORD_GROUP;
2346 header.size += sizeof(u64) +
2347 counter->nr_siblings * sizeof(group_entry);
2350 if (record_type & PERF_RECORD_CALLCHAIN) {
2351 callchain = perf_callchain(regs);
2354 callchain_size = (1 + callchain->nr) * sizeof(u64);
2356 header.type |= PERF_RECORD_CALLCHAIN;
2357 header.size += callchain_size;
2361 ret = perf_output_begin(&handle, counter, header.size, nmi, 1);
2365 perf_output_put(&handle, header);
2367 if (record_type & PERF_RECORD_IP)
2368 perf_output_put(&handle, ip);
2370 if (record_type & PERF_RECORD_TID)
2371 perf_output_put(&handle, tid_entry);
2373 if (record_type & PERF_RECORD_TIME)
2374 perf_output_put(&handle, time);
2376 if (record_type & PERF_RECORD_ADDR)
2377 perf_output_put(&handle, addr);
2379 if (record_type & PERF_RECORD_CONFIG)
2380 perf_output_put(&handle, counter->hw_event.config);
2382 if (record_type & PERF_RECORD_CPU)
2383 perf_output_put(&handle, cpu_entry);
2386 * XXX PERF_RECORD_GROUP vs inherited counters seems difficult.
2388 if (record_type & PERF_RECORD_GROUP) {
2389 struct perf_counter *leader, *sub;
2390 u64 nr = counter->nr_siblings;
2392 perf_output_put(&handle, nr);
2394 leader = counter->group_leader;
2395 list_for_each_entry(sub, &leader->sibling_list, list_entry) {
2397 sub->pmu->read(sub);
2399 group_entry.event = sub->hw_event.config;
2400 group_entry.counter = atomic64_read(&sub->count);
2402 perf_output_put(&handle, group_entry);
2407 perf_output_copy(&handle, callchain, callchain_size);
2409 perf_output_end(&handle);
2416 struct perf_comm_event {
2417 struct task_struct *task;
2422 struct perf_event_header header;
2429 static void perf_counter_comm_output(struct perf_counter *counter,
2430 struct perf_comm_event *comm_event)
2432 struct perf_output_handle handle;
2433 int size = comm_event->event.header.size;
2434 int ret = perf_output_begin(&handle, counter, size, 0, 0);
2439 perf_output_put(&handle, comm_event->event);
2440 perf_output_copy(&handle, comm_event->comm,
2441 comm_event->comm_size);
2442 perf_output_end(&handle);
2445 static int perf_counter_comm_match(struct perf_counter *counter,
2446 struct perf_comm_event *comm_event)
2448 if (counter->hw_event.comm &&
2449 comm_event->event.header.type == PERF_EVENT_COMM)
2455 static void perf_counter_comm_ctx(struct perf_counter_context *ctx,
2456 struct perf_comm_event *comm_event)
2458 struct perf_counter *counter;
2460 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2464 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2465 if (perf_counter_comm_match(counter, comm_event))
2466 perf_counter_comm_output(counter, comm_event);
2471 static void perf_counter_comm_event(struct perf_comm_event *comm_event)
2473 struct perf_cpu_context *cpuctx;
2474 struct perf_counter_context *ctx;
2476 char *comm = comm_event->task->comm;
2478 size = ALIGN(strlen(comm)+1, sizeof(u64));
2480 comm_event->comm = comm;
2481 comm_event->comm_size = size;
2483 comm_event->event.header.size = sizeof(comm_event->event) + size;
2485 cpuctx = &get_cpu_var(perf_cpu_context);
2486 perf_counter_comm_ctx(&cpuctx->ctx, comm_event);
2487 put_cpu_var(perf_cpu_context);
2491 * doesn't really matter which of the child contexts the
2492 * events ends up in.
2494 ctx = rcu_dereference(current->perf_counter_ctxp);
2496 perf_counter_comm_ctx(ctx, comm_event);
2500 void perf_counter_comm(struct task_struct *task)
2502 struct perf_comm_event comm_event;
2504 if (!atomic_read(&nr_comm_tracking))
2507 comm_event = (struct perf_comm_event){
2510 .header = { .type = PERF_EVENT_COMM, },
2511 .pid = task->group_leader->pid,
2516 perf_counter_comm_event(&comm_event);
2523 struct perf_mmap_event {
2529 struct perf_event_header header;
2539 static void perf_counter_mmap_output(struct perf_counter *counter,
2540 struct perf_mmap_event *mmap_event)
2542 struct perf_output_handle handle;
2543 int size = mmap_event->event.header.size;
2544 int ret = perf_output_begin(&handle, counter, size, 0, 0);
2549 perf_output_put(&handle, mmap_event->event);
2550 perf_output_copy(&handle, mmap_event->file_name,
2551 mmap_event->file_size);
2552 perf_output_end(&handle);
2555 static int perf_counter_mmap_match(struct perf_counter *counter,
2556 struct perf_mmap_event *mmap_event)
2558 if (counter->hw_event.mmap &&
2559 mmap_event->event.header.type == PERF_EVENT_MMAP)
2562 if (counter->hw_event.munmap &&
2563 mmap_event->event.header.type == PERF_EVENT_MUNMAP)
2569 static void perf_counter_mmap_ctx(struct perf_counter_context *ctx,
2570 struct perf_mmap_event *mmap_event)
2572 struct perf_counter *counter;
2574 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2578 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2579 if (perf_counter_mmap_match(counter, mmap_event))
2580 perf_counter_mmap_output(counter, mmap_event);
2585 static void perf_counter_mmap_event(struct perf_mmap_event *mmap_event)
2587 struct perf_cpu_context *cpuctx;
2588 struct perf_counter_context *ctx;
2589 struct file *file = mmap_event->file;
2596 buf = kzalloc(PATH_MAX, GFP_KERNEL);
2598 name = strncpy(tmp, "//enomem", sizeof(tmp));
2601 name = d_path(&file->f_path, buf, PATH_MAX);
2603 name = strncpy(tmp, "//toolong", sizeof(tmp));
2607 name = strncpy(tmp, "//anon", sizeof(tmp));
2612 size = ALIGN(strlen(name)+1, sizeof(u64));
2614 mmap_event->file_name = name;
2615 mmap_event->file_size = size;
2617 mmap_event->event.header.size = sizeof(mmap_event->event) + size;
2619 cpuctx = &get_cpu_var(perf_cpu_context);
2620 perf_counter_mmap_ctx(&cpuctx->ctx, mmap_event);
2621 put_cpu_var(perf_cpu_context);
2625 * doesn't really matter which of the child contexts the
2626 * events ends up in.
2628 ctx = rcu_dereference(current->perf_counter_ctxp);
2630 perf_counter_mmap_ctx(ctx, mmap_event);
2636 void perf_counter_mmap(unsigned long addr, unsigned long len,
2637 unsigned long pgoff, struct file *file)
2639 struct perf_mmap_event mmap_event;
2641 if (!atomic_read(&nr_mmap_tracking))
2644 mmap_event = (struct perf_mmap_event){
2647 .header = { .type = PERF_EVENT_MMAP, },
2648 .pid = current->group_leader->pid,
2649 .tid = current->pid,
2656 perf_counter_mmap_event(&mmap_event);
2659 void perf_counter_munmap(unsigned long addr, unsigned long len,
2660 unsigned long pgoff, struct file *file)
2662 struct perf_mmap_event mmap_event;
2664 if (!atomic_read(&nr_munmap_tracking))
2667 mmap_event = (struct perf_mmap_event){
2670 .header = { .type = PERF_EVENT_MUNMAP, },
2671 .pid = current->group_leader->pid,
2672 .tid = current->pid,
2679 perf_counter_mmap_event(&mmap_event);
2683 * Log irq_period changes so that analyzing tools can re-normalize the
2687 static void perf_log_period(struct perf_counter *counter, u64 period)
2689 struct perf_output_handle handle;
2693 struct perf_event_header header;
2698 .type = PERF_EVENT_PERIOD,
2700 .size = sizeof(freq_event),
2702 .time = sched_clock(),
2706 if (counter->hw.irq_period == period)
2709 ret = perf_output_begin(&handle, counter, sizeof(freq_event), 0, 0);
2713 perf_output_put(&handle, freq_event);
2714 perf_output_end(&handle);
2718 * IRQ throttle logging
2721 static void perf_log_throttle(struct perf_counter *counter, int enable)
2723 struct perf_output_handle handle;
2727 struct perf_event_header header;
2729 } throttle_event = {
2731 .type = PERF_EVENT_THROTTLE + 1,
2733 .size = sizeof(throttle_event),
2735 .time = sched_clock(),
2738 ret = perf_output_begin(&handle, counter, sizeof(throttle_event), 1, 0);
2742 perf_output_put(&handle, throttle_event);
2743 perf_output_end(&handle);
2747 * Generic counter overflow handling.
2750 int perf_counter_overflow(struct perf_counter *counter,
2751 int nmi, struct pt_regs *regs, u64 addr)
2753 int events = atomic_read(&counter->event_limit);
2754 int throttle = counter->pmu->unthrottle != NULL;
2758 counter->hw.interrupts++;
2759 } else if (counter->hw.interrupts != MAX_INTERRUPTS) {
2760 counter->hw.interrupts++;
2761 if (HZ*counter->hw.interrupts > (u64)sysctl_perf_counter_limit) {
2762 counter->hw.interrupts = MAX_INTERRUPTS;
2763 perf_log_throttle(counter, 0);
2769 * XXX event_limit might not quite work as expected on inherited
2773 counter->pending_kill = POLL_IN;
2774 if (events && atomic_dec_and_test(&counter->event_limit)) {
2776 counter->pending_kill = POLL_HUP;
2778 counter->pending_disable = 1;
2779 perf_pending_queue(&counter->pending,
2780 perf_pending_counter);
2782 perf_counter_disable(counter);
2785 perf_counter_output(counter, nmi, regs, addr);
2790 * Generic software counter infrastructure
2793 static void perf_swcounter_update(struct perf_counter *counter)
2795 struct hw_perf_counter *hwc = &counter->hw;
2800 prev = atomic64_read(&hwc->prev_count);
2801 now = atomic64_read(&hwc->count);
2802 if (atomic64_cmpxchg(&hwc->prev_count, prev, now) != prev)
2807 atomic64_add(delta, &counter->count);
2808 atomic64_sub(delta, &hwc->period_left);
2811 static void perf_swcounter_set_period(struct perf_counter *counter)
2813 struct hw_perf_counter *hwc = &counter->hw;
2814 s64 left = atomic64_read(&hwc->period_left);
2815 s64 period = hwc->irq_period;
2817 if (unlikely(left <= -period)) {
2819 atomic64_set(&hwc->period_left, left);
2822 if (unlikely(left <= 0)) {
2824 atomic64_add(period, &hwc->period_left);
2827 atomic64_set(&hwc->prev_count, -left);
2828 atomic64_set(&hwc->count, -left);
2831 static enum hrtimer_restart perf_swcounter_hrtimer(struct hrtimer *hrtimer)
2833 enum hrtimer_restart ret = HRTIMER_RESTART;
2834 struct perf_counter *counter;
2835 struct pt_regs *regs;
2838 counter = container_of(hrtimer, struct perf_counter, hw.hrtimer);
2839 counter->pmu->read(counter);
2841 regs = get_irq_regs();
2843 * In case we exclude kernel IPs or are somehow not in interrupt
2844 * context, provide the next best thing, the user IP.
2846 if ((counter->hw_event.exclude_kernel || !regs) &&
2847 !counter->hw_event.exclude_user)
2848 regs = task_pt_regs(current);
2851 if (perf_counter_overflow(counter, 0, regs, 0))
2852 ret = HRTIMER_NORESTART;
2855 period = max_t(u64, 10000, counter->hw.irq_period);
2856 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
2861 static void perf_swcounter_overflow(struct perf_counter *counter,
2862 int nmi, struct pt_regs *regs, u64 addr)
2864 perf_swcounter_update(counter);
2865 perf_swcounter_set_period(counter);
2866 if (perf_counter_overflow(counter, nmi, regs, addr))
2867 /* soft-disable the counter */
2872 static int perf_swcounter_is_counting(struct perf_counter *counter)
2874 struct perf_counter_context *ctx;
2875 unsigned long flags;
2878 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
2881 if (counter->state != PERF_COUNTER_STATE_INACTIVE)
2885 * If the counter is inactive, it could be just because
2886 * its task is scheduled out, or because it's in a group
2887 * which could not go on the PMU. We want to count in
2888 * the first case but not the second. If the context is
2889 * currently active then an inactive software counter must
2890 * be the second case. If it's not currently active then
2891 * we need to know whether the counter was active when the
2892 * context was last active, which we can determine by
2893 * comparing counter->tstamp_stopped with ctx->time.
2895 * We are within an RCU read-side critical section,
2896 * which protects the existence of *ctx.
2899 spin_lock_irqsave(&ctx->lock, flags);
2901 /* Re-check state now we have the lock */
2902 if (counter->state < PERF_COUNTER_STATE_INACTIVE ||
2903 counter->ctx->is_active ||
2904 counter->tstamp_stopped < ctx->time)
2906 spin_unlock_irqrestore(&ctx->lock, flags);
2910 static int perf_swcounter_match(struct perf_counter *counter,
2911 enum perf_event_types type,
2912 u32 event, struct pt_regs *regs)
2916 event_config = ((u64) type << PERF_COUNTER_TYPE_SHIFT) | event;
2918 if (!perf_swcounter_is_counting(counter))
2921 if (counter->hw_event.config != event_config)
2924 if (counter->hw_event.exclude_user && user_mode(regs))
2927 if (counter->hw_event.exclude_kernel && !user_mode(regs))
2933 static void perf_swcounter_add(struct perf_counter *counter, u64 nr,
2934 int nmi, struct pt_regs *regs, u64 addr)
2936 int neg = atomic64_add_negative(nr, &counter->hw.count);
2938 if (counter->hw.irq_period && !neg)
2939 perf_swcounter_overflow(counter, nmi, regs, addr);
2942 static void perf_swcounter_ctx_event(struct perf_counter_context *ctx,
2943 enum perf_event_types type, u32 event,
2944 u64 nr, int nmi, struct pt_regs *regs,
2947 struct perf_counter *counter;
2949 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2953 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2954 if (perf_swcounter_match(counter, type, event, regs))
2955 perf_swcounter_add(counter, nr, nmi, regs, addr);
2960 static int *perf_swcounter_recursion_context(struct perf_cpu_context *cpuctx)
2963 return &cpuctx->recursion[3];
2966 return &cpuctx->recursion[2];
2969 return &cpuctx->recursion[1];
2971 return &cpuctx->recursion[0];
2974 static void __perf_swcounter_event(enum perf_event_types type, u32 event,
2975 u64 nr, int nmi, struct pt_regs *regs,
2978 struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
2979 int *recursion = perf_swcounter_recursion_context(cpuctx);
2980 struct perf_counter_context *ctx;
2988 perf_swcounter_ctx_event(&cpuctx->ctx, type, event,
2989 nr, nmi, regs, addr);
2992 * doesn't really matter which of the child contexts the
2993 * events ends up in.
2995 ctx = rcu_dereference(current->perf_counter_ctxp);
2997 perf_swcounter_ctx_event(ctx, type, event, nr, nmi, regs, addr);
3004 put_cpu_var(perf_cpu_context);
3008 perf_swcounter_event(u32 event, u64 nr, int nmi, struct pt_regs *regs, u64 addr)
3010 __perf_swcounter_event(PERF_TYPE_SOFTWARE, event, nr, nmi, regs, addr);
3013 static void perf_swcounter_read(struct perf_counter *counter)
3015 perf_swcounter_update(counter);
3018 static int perf_swcounter_enable(struct perf_counter *counter)
3020 perf_swcounter_set_period(counter);
3024 static void perf_swcounter_disable(struct perf_counter *counter)
3026 perf_swcounter_update(counter);
3029 static const struct pmu perf_ops_generic = {
3030 .enable = perf_swcounter_enable,
3031 .disable = perf_swcounter_disable,
3032 .read = perf_swcounter_read,
3036 * Software counter: cpu wall time clock
3039 static void cpu_clock_perf_counter_update(struct perf_counter *counter)
3041 int cpu = raw_smp_processor_id();
3045 now = cpu_clock(cpu);
3046 prev = atomic64_read(&counter->hw.prev_count);
3047 atomic64_set(&counter->hw.prev_count, now);
3048 atomic64_add(now - prev, &counter->count);
3051 static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
3053 struct hw_perf_counter *hwc = &counter->hw;
3054 int cpu = raw_smp_processor_id();
3056 atomic64_set(&hwc->prev_count, cpu_clock(cpu));
3057 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3058 hwc->hrtimer.function = perf_swcounter_hrtimer;
3059 if (hwc->irq_period) {
3060 u64 period = max_t(u64, 10000, hwc->irq_period);
3061 __hrtimer_start_range_ns(&hwc->hrtimer,
3062 ns_to_ktime(period), 0,
3063 HRTIMER_MODE_REL, 0);
3069 static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
3071 if (counter->hw.irq_period)
3072 hrtimer_cancel(&counter->hw.hrtimer);
3073 cpu_clock_perf_counter_update(counter);
3076 static void cpu_clock_perf_counter_read(struct perf_counter *counter)
3078 cpu_clock_perf_counter_update(counter);
3081 static const struct pmu perf_ops_cpu_clock = {
3082 .enable = cpu_clock_perf_counter_enable,
3083 .disable = cpu_clock_perf_counter_disable,
3084 .read = cpu_clock_perf_counter_read,
3088 * Software counter: task time clock
3091 static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
3096 prev = atomic64_xchg(&counter->hw.prev_count, now);
3098 atomic64_add(delta, &counter->count);
3101 static int task_clock_perf_counter_enable(struct perf_counter *counter)
3103 struct hw_perf_counter *hwc = &counter->hw;
3106 now = counter->ctx->time;
3108 atomic64_set(&hwc->prev_count, now);
3109 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3110 hwc->hrtimer.function = perf_swcounter_hrtimer;
3111 if (hwc->irq_period) {
3112 u64 period = max_t(u64, 10000, hwc->irq_period);
3113 __hrtimer_start_range_ns(&hwc->hrtimer,
3114 ns_to_ktime(period), 0,
3115 HRTIMER_MODE_REL, 0);
3121 static void task_clock_perf_counter_disable(struct perf_counter *counter)
3123 if (counter->hw.irq_period)
3124 hrtimer_cancel(&counter->hw.hrtimer);
3125 task_clock_perf_counter_update(counter, counter->ctx->time);
3129 static void task_clock_perf_counter_read(struct perf_counter *counter)
3134 update_context_time(counter->ctx);
3135 time = counter->ctx->time;
3137 u64 now = perf_clock();
3138 u64 delta = now - counter->ctx->timestamp;
3139 time = counter->ctx->time + delta;
3142 task_clock_perf_counter_update(counter, time);
3145 static const struct pmu perf_ops_task_clock = {
3146 .enable = task_clock_perf_counter_enable,
3147 .disable = task_clock_perf_counter_disable,
3148 .read = task_clock_perf_counter_read,
3152 * Software counter: cpu migrations
3155 static inline u64 get_cpu_migrations(struct perf_counter *counter)
3157 struct task_struct *curr = counter->ctx->task;
3160 return curr->se.nr_migrations;
3161 return cpu_nr_migrations(smp_processor_id());
3164 static void cpu_migrations_perf_counter_update(struct perf_counter *counter)
3169 prev = atomic64_read(&counter->hw.prev_count);
3170 now = get_cpu_migrations(counter);
3172 atomic64_set(&counter->hw.prev_count, now);
3176 atomic64_add(delta, &counter->count);
3179 static void cpu_migrations_perf_counter_read(struct perf_counter *counter)
3181 cpu_migrations_perf_counter_update(counter);
3184 static int cpu_migrations_perf_counter_enable(struct perf_counter *counter)
3186 if (counter->prev_state <= PERF_COUNTER_STATE_OFF)
3187 atomic64_set(&counter->hw.prev_count,
3188 get_cpu_migrations(counter));
3192 static void cpu_migrations_perf_counter_disable(struct perf_counter *counter)
3194 cpu_migrations_perf_counter_update(counter);
3197 static const struct pmu perf_ops_cpu_migrations = {
3198 .enable = cpu_migrations_perf_counter_enable,
3199 .disable = cpu_migrations_perf_counter_disable,
3200 .read = cpu_migrations_perf_counter_read,
3203 #ifdef CONFIG_EVENT_PROFILE
3204 void perf_tpcounter_event(int event_id)
3206 struct pt_regs *regs = get_irq_regs();
3209 regs = task_pt_regs(current);
3211 __perf_swcounter_event(PERF_TYPE_TRACEPOINT, event_id, 1, 1, regs, 0);
3213 EXPORT_SYMBOL_GPL(perf_tpcounter_event);
3215 extern int ftrace_profile_enable(int);
3216 extern void ftrace_profile_disable(int);
3218 static void tp_perf_counter_destroy(struct perf_counter *counter)
3220 ftrace_profile_disable(perf_event_id(&counter->hw_event));
3223 static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
3225 int event_id = perf_event_id(&counter->hw_event);
3228 ret = ftrace_profile_enable(event_id);
3232 counter->destroy = tp_perf_counter_destroy;
3233 counter->hw.irq_period = counter->hw_event.irq_period;
3235 return &perf_ops_generic;
3238 static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
3244 static const struct pmu *sw_perf_counter_init(struct perf_counter *counter)
3246 const struct pmu *pmu = NULL;
3249 * Software counters (currently) can't in general distinguish
3250 * between user, kernel and hypervisor events.
3251 * However, context switches and cpu migrations are considered
3252 * to be kernel events, and page faults are never hypervisor
3255 switch (perf_event_id(&counter->hw_event)) {
3256 case PERF_COUNT_CPU_CLOCK:
3257 pmu = &perf_ops_cpu_clock;
3260 case PERF_COUNT_TASK_CLOCK:
3262 * If the user instantiates this as a per-cpu counter,
3263 * use the cpu_clock counter instead.
3265 if (counter->ctx->task)
3266 pmu = &perf_ops_task_clock;
3268 pmu = &perf_ops_cpu_clock;
3271 case PERF_COUNT_PAGE_FAULTS:
3272 case PERF_COUNT_PAGE_FAULTS_MIN:
3273 case PERF_COUNT_PAGE_FAULTS_MAJ:
3274 case PERF_COUNT_CONTEXT_SWITCHES:
3275 pmu = &perf_ops_generic;
3277 case PERF_COUNT_CPU_MIGRATIONS:
3278 if (!counter->hw_event.exclude_kernel)
3279 pmu = &perf_ops_cpu_migrations;
3287 * Allocate and initialize a counter structure
3289 static struct perf_counter *
3290 perf_counter_alloc(struct perf_counter_hw_event *hw_event,
3292 struct perf_counter_context *ctx,
3293 struct perf_counter *group_leader,
3296 const struct pmu *pmu;
3297 struct perf_counter *counter;
3298 struct hw_perf_counter *hwc;
3301 counter = kzalloc(sizeof(*counter), gfpflags);
3303 return ERR_PTR(-ENOMEM);
3306 * Single counters are their own group leaders, with an
3307 * empty sibling list:
3310 group_leader = counter;
3312 mutex_init(&counter->child_mutex);
3313 INIT_LIST_HEAD(&counter->child_list);
3315 INIT_LIST_HEAD(&counter->list_entry);
3316 INIT_LIST_HEAD(&counter->event_entry);
3317 INIT_LIST_HEAD(&counter->sibling_list);
3318 init_waitqueue_head(&counter->waitq);
3320 mutex_init(&counter->mmap_mutex);
3323 counter->hw_event = *hw_event;
3324 counter->group_leader = group_leader;
3325 counter->pmu = NULL;
3327 counter->oncpu = -1;
3329 counter->state = PERF_COUNTER_STATE_INACTIVE;
3330 if (hw_event->disabled)
3331 counter->state = PERF_COUNTER_STATE_OFF;
3336 if (hw_event->freq && hw_event->irq_freq)
3337 hwc->irq_period = div64_u64(TICK_NSEC, hw_event->irq_freq);
3339 hwc->irq_period = hw_event->irq_period;
3342 * we currently do not support PERF_RECORD_GROUP on inherited counters
3344 if (hw_event->inherit && (hw_event->record_type & PERF_RECORD_GROUP))
3347 if (perf_event_raw(hw_event)) {
3348 pmu = hw_perf_counter_init(counter);
3352 switch (perf_event_type(hw_event)) {
3353 case PERF_TYPE_HARDWARE:
3354 pmu = hw_perf_counter_init(counter);
3357 case PERF_TYPE_SOFTWARE:
3358 pmu = sw_perf_counter_init(counter);
3361 case PERF_TYPE_TRACEPOINT:
3362 pmu = tp_perf_counter_init(counter);
3369 else if (IS_ERR(pmu))
3374 return ERR_PTR(err);
3379 atomic_inc(&nr_counters);
3380 if (counter->hw_event.mmap)
3381 atomic_inc(&nr_mmap_tracking);
3382 if (counter->hw_event.munmap)
3383 atomic_inc(&nr_munmap_tracking);
3384 if (counter->hw_event.comm)
3385 atomic_inc(&nr_comm_tracking);
3391 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
3393 * @hw_event_uptr: event type attributes for monitoring/sampling
3396 * @group_fd: group leader counter fd
3398 SYSCALL_DEFINE5(perf_counter_open,
3399 const struct perf_counter_hw_event __user *, hw_event_uptr,
3400 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
3402 struct perf_counter *counter, *group_leader;
3403 struct perf_counter_hw_event hw_event;
3404 struct perf_counter_context *ctx;
3405 struct file *counter_file = NULL;
3406 struct file *group_file = NULL;
3407 int fput_needed = 0;
3408 int fput_needed2 = 0;
3411 /* for future expandability... */
3415 if (copy_from_user(&hw_event, hw_event_uptr, sizeof(hw_event)) != 0)
3419 * Get the target context (task or percpu):
3421 ctx = find_get_context(pid, cpu);
3423 return PTR_ERR(ctx);
3426 * Look up the group leader (we will attach this counter to it):
3428 group_leader = NULL;
3429 if (group_fd != -1) {
3431 group_file = fget_light(group_fd, &fput_needed);
3433 goto err_put_context;
3434 if (group_file->f_op != &perf_fops)
3435 goto err_put_context;
3437 group_leader = group_file->private_data;
3439 * Do not allow a recursive hierarchy (this new sibling
3440 * becoming part of another group-sibling):
3442 if (group_leader->group_leader != group_leader)
3443 goto err_put_context;
3445 * Do not allow to attach to a group in a different
3446 * task or CPU context:
3448 if (group_leader->ctx != ctx)
3449 goto err_put_context;
3451 * Only a group leader can be exclusive or pinned
3453 if (hw_event.exclusive || hw_event.pinned)
3454 goto err_put_context;
3457 counter = perf_counter_alloc(&hw_event, cpu, ctx, group_leader,
3459 ret = PTR_ERR(counter);
3460 if (IS_ERR(counter))
3461 goto err_put_context;
3463 ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
3465 goto err_free_put_context;
3467 counter_file = fget_light(ret, &fput_needed2);
3469 goto err_free_put_context;
3471 counter->filp = counter_file;
3472 WARN_ON_ONCE(ctx->parent_ctx);
3473 mutex_lock(&ctx->mutex);
3474 perf_install_in_context(ctx, counter, cpu);
3476 mutex_unlock(&ctx->mutex);
3478 counter->owner = current;
3479 get_task_struct(current);
3480 mutex_lock(¤t->perf_counter_mutex);
3481 list_add_tail(&counter->owner_entry, ¤t->perf_counter_list);
3482 mutex_unlock(¤t->perf_counter_mutex);
3484 fput_light(counter_file, fput_needed2);
3487 fput_light(group_file, fput_needed);
3491 err_free_put_context:
3501 * inherit a counter from parent task to child task:
3503 static struct perf_counter *
3504 inherit_counter(struct perf_counter *parent_counter,
3505 struct task_struct *parent,
3506 struct perf_counter_context *parent_ctx,
3507 struct task_struct *child,
3508 struct perf_counter *group_leader,
3509 struct perf_counter_context *child_ctx)
3511 struct perf_counter *child_counter;
3514 * Instead of creating recursive hierarchies of counters,
3515 * we link inherited counters back to the original parent,
3516 * which has a filp for sure, which we use as the reference
3519 if (parent_counter->parent)
3520 parent_counter = parent_counter->parent;
3522 child_counter = perf_counter_alloc(&parent_counter->hw_event,
3523 parent_counter->cpu, child_ctx,
3524 group_leader, GFP_KERNEL);
3525 if (IS_ERR(child_counter))
3526 return child_counter;
3530 * Make the child state follow the state of the parent counter,
3531 * not its hw_event.disabled bit. We hold the parent's mutex,
3532 * so we won't race with perf_counter_{en, dis}able_family.
3534 if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE)
3535 child_counter->state = PERF_COUNTER_STATE_INACTIVE;
3537 child_counter->state = PERF_COUNTER_STATE_OFF;
3540 * Link it up in the child's context:
3542 add_counter_to_ctx(child_counter, child_ctx);
3544 child_counter->parent = parent_counter;
3546 * inherit into child's child as well:
3548 child_counter->hw_event.inherit = 1;
3551 * Get a reference to the parent filp - we will fput it
3552 * when the child counter exits. This is safe to do because
3553 * we are in the parent and we know that the filp still
3554 * exists and has a nonzero count:
3556 atomic_long_inc(&parent_counter->filp->f_count);
3559 * Link this into the parent counter's child list
3561 WARN_ON_ONCE(parent_counter->ctx->parent_ctx);
3562 mutex_lock(&parent_counter->child_mutex);
3563 list_add_tail(&child_counter->child_list, &parent_counter->child_list);
3564 mutex_unlock(&parent_counter->child_mutex);
3566 return child_counter;
3569 static int inherit_group(struct perf_counter *parent_counter,
3570 struct task_struct *parent,
3571 struct perf_counter_context *parent_ctx,
3572 struct task_struct *child,
3573 struct perf_counter_context *child_ctx)
3575 struct perf_counter *leader;
3576 struct perf_counter *sub;
3577 struct perf_counter *child_ctr;
3579 leader = inherit_counter(parent_counter, parent, parent_ctx,
3580 child, NULL, child_ctx);
3582 return PTR_ERR(leader);
3583 list_for_each_entry(sub, &parent_counter->sibling_list, list_entry) {
3584 child_ctr = inherit_counter(sub, parent, parent_ctx,
3585 child, leader, child_ctx);
3586 if (IS_ERR(child_ctr))
3587 return PTR_ERR(child_ctr);
3592 static void sync_child_counter(struct perf_counter *child_counter,
3593 struct perf_counter *parent_counter)
3597 child_val = atomic64_read(&child_counter->count);
3600 * Add back the child's count to the parent's count:
3602 atomic64_add(child_val, &parent_counter->count);
3603 atomic64_add(child_counter->total_time_enabled,
3604 &parent_counter->child_total_time_enabled);
3605 atomic64_add(child_counter->total_time_running,
3606 &parent_counter->child_total_time_running);
3609 * Remove this counter from the parent's list
3611 WARN_ON_ONCE(parent_counter->ctx->parent_ctx);
3612 mutex_lock(&parent_counter->child_mutex);
3613 list_del_init(&child_counter->child_list);
3614 mutex_unlock(&parent_counter->child_mutex);
3617 * Release the parent counter, if this was the last
3620 fput(parent_counter->filp);
3624 __perf_counter_exit_task(struct perf_counter *child_counter,
3625 struct perf_counter_context *child_ctx)
3627 struct perf_counter *parent_counter;
3629 update_counter_times(child_counter);
3630 perf_counter_remove_from_context(child_counter);
3632 parent_counter = child_counter->parent;
3634 * It can happen that parent exits first, and has counters
3635 * that are still around due to the child reference. These
3636 * counters need to be zapped - but otherwise linger.
3638 if (parent_counter) {
3639 sync_child_counter(child_counter, parent_counter);
3640 free_counter(child_counter);
3645 * When a child task exits, feed back counter values to parent counters.
3647 void perf_counter_exit_task(struct task_struct *child)
3649 struct perf_counter *child_counter, *tmp;
3650 struct perf_counter_context *child_ctx;
3651 unsigned long flags;
3653 if (likely(!child->perf_counter_ctxp))
3656 local_irq_save(flags);
3658 * We can't reschedule here because interrupts are disabled,
3659 * and either child is current or it is a task that can't be
3660 * scheduled, so we are now safe from rescheduling changing
3663 child_ctx = child->perf_counter_ctxp;
3664 __perf_counter_task_sched_out(child_ctx);
3667 * Take the context lock here so that if find_get_context is
3668 * reading child->perf_counter_ctxp, we wait until it has
3669 * incremented the context's refcount before we do put_ctx below.
3671 spin_lock(&child_ctx->lock);
3672 child->perf_counter_ctxp = NULL;
3673 if (child_ctx->parent_ctx) {
3675 * This context is a clone; unclone it so it can't get
3676 * swapped to another process while we're removing all
3677 * the counters from it.
3679 put_ctx(child_ctx->parent_ctx);
3680 child_ctx->parent_ctx = NULL;
3682 spin_unlock(&child_ctx->lock);
3683 local_irq_restore(flags);
3685 mutex_lock(&child_ctx->mutex);
3688 list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
3690 __perf_counter_exit_task(child_counter, child_ctx);
3693 * If the last counter was a group counter, it will have appended all
3694 * its siblings to the list, but we obtained 'tmp' before that which
3695 * will still point to the list head terminating the iteration.
3697 if (!list_empty(&child_ctx->counter_list))
3700 mutex_unlock(&child_ctx->mutex);
3706 * free an unexposed, unused context as created by inheritance by
3707 * init_task below, used by fork() in case of fail.
3709 void perf_counter_free_task(struct task_struct *task)
3711 struct perf_counter_context *ctx = task->perf_counter_ctxp;
3712 struct perf_counter *counter, *tmp;
3717 mutex_lock(&ctx->mutex);
3719 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry) {
3720 struct perf_counter *parent = counter->parent;
3722 if (WARN_ON_ONCE(!parent))
3725 mutex_lock(&parent->child_mutex);
3726 list_del_init(&counter->child_list);
3727 mutex_unlock(&parent->child_mutex);
3731 list_del_counter(counter, ctx);
3732 free_counter(counter);
3735 if (!list_empty(&ctx->counter_list))
3738 mutex_unlock(&ctx->mutex);
3744 * Initialize the perf_counter context in task_struct
3746 int perf_counter_init_task(struct task_struct *child)
3748 struct perf_counter_context *child_ctx, *parent_ctx;
3749 struct perf_counter_context *cloned_ctx;
3750 struct perf_counter *counter;
3751 struct task_struct *parent = current;
3752 int inherited_all = 1;
3755 child->perf_counter_ctxp = NULL;
3757 mutex_init(&child->perf_counter_mutex);
3758 INIT_LIST_HEAD(&child->perf_counter_list);
3760 if (likely(!parent->perf_counter_ctxp))
3764 * This is executed from the parent task context, so inherit
3765 * counters that have been marked for cloning.
3766 * First allocate and initialize a context for the child.
3769 child_ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
3773 __perf_counter_init_context(child_ctx, child);
3774 child->perf_counter_ctxp = child_ctx;
3775 get_task_struct(child);
3778 * If the parent's context is a clone, pin it so it won't get
3781 parent_ctx = perf_pin_task_context(parent);
3784 * No need to check if parent_ctx != NULL here; since we saw
3785 * it non-NULL earlier, the only reason for it to become NULL
3786 * is if we exit, and since we're currently in the middle of
3787 * a fork we can't be exiting at the same time.
3791 * Lock the parent list. No need to lock the child - not PID
3792 * hashed yet and not running, so nobody can access it.
3794 mutex_lock(&parent_ctx->mutex);
3797 * We dont have to disable NMIs - we are only looking at
3798 * the list, not manipulating it:
3800 list_for_each_entry_rcu(counter, &parent_ctx->event_list, event_entry) {
3801 if (counter != counter->group_leader)
3804 if (!counter->hw_event.inherit) {
3809 ret = inherit_group(counter, parent, parent_ctx,
3817 if (inherited_all) {
3819 * Mark the child context as a clone of the parent
3820 * context, or of whatever the parent is a clone of.
3821 * Note that if the parent is a clone, it could get
3822 * uncloned at any point, but that doesn't matter
3823 * because the list of counters and the generation
3824 * count can't have changed since we took the mutex.
3826 cloned_ctx = rcu_dereference(parent_ctx->parent_ctx);
3828 child_ctx->parent_ctx = cloned_ctx;
3829 child_ctx->parent_gen = parent_ctx->parent_gen;
3831 child_ctx->parent_ctx = parent_ctx;
3832 child_ctx->parent_gen = parent_ctx->generation;
3834 get_ctx(child_ctx->parent_ctx);
3837 mutex_unlock(&parent_ctx->mutex);
3839 perf_unpin_context(parent_ctx);
3844 static void __cpuinit perf_counter_init_cpu(int cpu)
3846 struct perf_cpu_context *cpuctx;
3848 cpuctx = &per_cpu(perf_cpu_context, cpu);
3849 __perf_counter_init_context(&cpuctx->ctx, NULL);
3851 spin_lock(&perf_resource_lock);
3852 cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
3853 spin_unlock(&perf_resource_lock);
3855 hw_perf_counter_setup(cpu);
3858 #ifdef CONFIG_HOTPLUG_CPU
3859 static void __perf_counter_exit_cpu(void *info)
3861 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
3862 struct perf_counter_context *ctx = &cpuctx->ctx;
3863 struct perf_counter *counter, *tmp;
3865 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
3866 __perf_counter_remove_from_context(counter);
3868 static void perf_counter_exit_cpu(int cpu)
3870 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
3871 struct perf_counter_context *ctx = &cpuctx->ctx;
3873 mutex_lock(&ctx->mutex);
3874 smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
3875 mutex_unlock(&ctx->mutex);
3878 static inline void perf_counter_exit_cpu(int cpu) { }
3881 static int __cpuinit
3882 perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
3884 unsigned int cpu = (long)hcpu;
3888 case CPU_UP_PREPARE:
3889 case CPU_UP_PREPARE_FROZEN:
3890 perf_counter_init_cpu(cpu);
3893 case CPU_DOWN_PREPARE:
3894 case CPU_DOWN_PREPARE_FROZEN:
3895 perf_counter_exit_cpu(cpu);
3905 static struct notifier_block __cpuinitdata perf_cpu_nb = {
3906 .notifier_call = perf_cpu_notify,
3909 void __init perf_counter_init(void)
3911 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
3912 (void *)(long)smp_processor_id());
3913 register_cpu_notifier(&perf_cpu_nb);
3916 static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
3918 return sprintf(buf, "%d\n", perf_reserved_percpu);
3922 perf_set_reserve_percpu(struct sysdev_class *class,
3926 struct perf_cpu_context *cpuctx;
3930 err = strict_strtoul(buf, 10, &val);
3933 if (val > perf_max_counters)
3936 spin_lock(&perf_resource_lock);
3937 perf_reserved_percpu = val;
3938 for_each_online_cpu(cpu) {
3939 cpuctx = &per_cpu(perf_cpu_context, cpu);
3940 spin_lock_irq(&cpuctx->ctx.lock);
3941 mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
3942 perf_max_counters - perf_reserved_percpu);
3943 cpuctx->max_pertask = mpt;
3944 spin_unlock_irq(&cpuctx->ctx.lock);
3946 spin_unlock(&perf_resource_lock);
3951 static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
3953 return sprintf(buf, "%d\n", perf_overcommit);
3957 perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
3962 err = strict_strtoul(buf, 10, &val);
3968 spin_lock(&perf_resource_lock);
3969 perf_overcommit = val;
3970 spin_unlock(&perf_resource_lock);
3975 static SYSDEV_CLASS_ATTR(
3978 perf_show_reserve_percpu,
3979 perf_set_reserve_percpu
3982 static SYSDEV_CLASS_ATTR(
3985 perf_show_overcommit,
3989 static struct attribute *perfclass_attrs[] = {
3990 &attr_reserve_percpu.attr,
3991 &attr_overcommit.attr,
3995 static struct attribute_group perfclass_attr_group = {
3996 .attrs = perfclass_attrs,
3997 .name = "perf_counters",
4000 static int __init perf_counter_sysfs_init(void)
4002 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
4003 &perfclass_attr_group);
4005 device_initcall(perf_counter_sysfs_init);