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_counters __read_mostly;
44 static atomic_t nr_comm_counters __read_mostly;
46 int sysctl_perf_counter_priv __read_mostly; /* do we need to be privileged */
47 int sysctl_perf_counter_mlock __read_mostly = 512; /* 'free' kb per user */
48 int sysctl_perf_counter_limit __read_mostly = 100000; /* max NMIs per second */
50 static atomic64_t perf_counter_id;
53 * Lock for (sysadmin-configurable) counter reservations:
55 static DEFINE_SPINLOCK(perf_resource_lock);
58 * Architecture provided APIs - weak aliases:
60 extern __weak const struct pmu *hw_perf_counter_init(struct perf_counter *counter)
65 void __weak hw_perf_disable(void) { barrier(); }
66 void __weak hw_perf_enable(void) { barrier(); }
68 void __weak hw_perf_counter_setup(int cpu) { barrier(); }
71 hw_perf_group_sched_in(struct perf_counter *group_leader,
72 struct perf_cpu_context *cpuctx,
73 struct perf_counter_context *ctx, int cpu)
78 void __weak perf_counter_print_debug(void) { }
80 static DEFINE_PER_CPU(int, disable_count);
82 void __perf_disable(void)
84 __get_cpu_var(disable_count)++;
87 bool __perf_enable(void)
89 return !--__get_cpu_var(disable_count);
92 void perf_disable(void)
98 void perf_enable(void)
104 static void get_ctx(struct perf_counter_context *ctx)
106 atomic_inc(&ctx->refcount);
109 static void free_ctx(struct rcu_head *head)
111 struct perf_counter_context *ctx;
113 ctx = container_of(head, struct perf_counter_context, rcu_head);
117 static void put_ctx(struct perf_counter_context *ctx)
119 if (atomic_dec_and_test(&ctx->refcount)) {
121 put_ctx(ctx->parent_ctx);
123 put_task_struct(ctx->task);
124 call_rcu(&ctx->rcu_head, free_ctx);
129 * Get the perf_counter_context for a task and lock it.
130 * This has to cope with with the fact that until it is locked,
131 * the context could get moved to another task.
133 static struct perf_counter_context *
134 perf_lock_task_context(struct task_struct *task, unsigned long *flags)
136 struct perf_counter_context *ctx;
140 ctx = rcu_dereference(task->perf_counter_ctxp);
143 * If this context is a clone of another, it might
144 * get swapped for another underneath us by
145 * perf_counter_task_sched_out, though the
146 * rcu_read_lock() protects us from any context
147 * getting freed. Lock the context and check if it
148 * got swapped before we could get the lock, and retry
149 * if so. If we locked the right context, then it
150 * can't get swapped on us any more.
152 spin_lock_irqsave(&ctx->lock, *flags);
153 if (ctx != rcu_dereference(task->perf_counter_ctxp)) {
154 spin_unlock_irqrestore(&ctx->lock, *flags);
163 * Get the context for a task and increment its pin_count so it
164 * can't get swapped to another task. This also increments its
165 * reference count so that the context can't get freed.
167 static struct perf_counter_context *perf_pin_task_context(struct task_struct *task)
169 struct perf_counter_context *ctx;
172 ctx = perf_lock_task_context(task, &flags);
176 spin_unlock_irqrestore(&ctx->lock, flags);
181 static void perf_unpin_context(struct perf_counter_context *ctx)
185 spin_lock_irqsave(&ctx->lock, flags);
187 spin_unlock_irqrestore(&ctx->lock, flags);
192 * Add a counter from the lists for its context.
193 * Must be called with ctx->mutex and ctx->lock held.
196 list_add_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
198 struct perf_counter *group_leader = counter->group_leader;
201 * Depending on whether it is a standalone or sibling counter,
202 * add it straight to the context's counter list, or to the group
203 * leader's sibling list:
205 if (group_leader == counter)
206 list_add_tail(&counter->list_entry, &ctx->counter_list);
208 list_add_tail(&counter->list_entry, &group_leader->sibling_list);
209 group_leader->nr_siblings++;
212 list_add_rcu(&counter->event_entry, &ctx->event_list);
217 * Remove a counter from the lists for its context.
218 * Must be called with ctx->mutex and ctx->lock held.
221 list_del_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
223 struct perf_counter *sibling, *tmp;
225 if (list_empty(&counter->list_entry))
229 list_del_init(&counter->list_entry);
230 list_del_rcu(&counter->event_entry);
232 if (counter->group_leader != counter)
233 counter->group_leader->nr_siblings--;
236 * If this was a group counter with sibling counters then
237 * upgrade the siblings to singleton counters by adding them
238 * to the context list directly:
240 list_for_each_entry_safe(sibling, tmp,
241 &counter->sibling_list, list_entry) {
243 list_move_tail(&sibling->list_entry, &ctx->counter_list);
244 sibling->group_leader = sibling;
249 counter_sched_out(struct perf_counter *counter,
250 struct perf_cpu_context *cpuctx,
251 struct perf_counter_context *ctx)
253 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
256 counter->state = PERF_COUNTER_STATE_INACTIVE;
257 counter->tstamp_stopped = ctx->time;
258 counter->pmu->disable(counter);
261 if (!is_software_counter(counter))
262 cpuctx->active_oncpu--;
264 if (counter->attr.exclusive || !cpuctx->active_oncpu)
265 cpuctx->exclusive = 0;
269 group_sched_out(struct perf_counter *group_counter,
270 struct perf_cpu_context *cpuctx,
271 struct perf_counter_context *ctx)
273 struct perf_counter *counter;
275 if (group_counter->state != PERF_COUNTER_STATE_ACTIVE)
278 counter_sched_out(group_counter, cpuctx, ctx);
281 * Schedule out siblings (if any):
283 list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
284 counter_sched_out(counter, cpuctx, ctx);
286 if (group_counter->attr.exclusive)
287 cpuctx->exclusive = 0;
291 * Cross CPU call to remove a performance counter
293 * We disable the counter on the hardware level first. After that we
294 * remove it from the context list.
296 static void __perf_counter_remove_from_context(void *info)
298 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
299 struct perf_counter *counter = info;
300 struct perf_counter_context *ctx = counter->ctx;
303 * If this is a task context, we need to check whether it is
304 * the current task context of this cpu. If not it has been
305 * scheduled out before the smp call arrived.
307 if (ctx->task && cpuctx->task_ctx != ctx)
310 spin_lock(&ctx->lock);
312 * Protect the list operation against NMI by disabling the
313 * counters on a global level.
317 counter_sched_out(counter, cpuctx, ctx);
319 list_del_counter(counter, ctx);
323 * Allow more per task counters with respect to the
326 cpuctx->max_pertask =
327 min(perf_max_counters - ctx->nr_counters,
328 perf_max_counters - perf_reserved_percpu);
332 spin_unlock(&ctx->lock);
337 * Remove the counter from a task's (or a CPU's) list of counters.
339 * Must be called with ctx->mutex held.
341 * CPU counters are removed with a smp call. For task counters we only
342 * call when the task is on a CPU.
344 * If counter->ctx is a cloned context, callers must make sure that
345 * every task struct that counter->ctx->task could possibly point to
346 * remains valid. This is OK when called from perf_release since
347 * that only calls us on the top-level context, which can't be a clone.
348 * When called from perf_counter_exit_task, it's OK because the
349 * context has been detached from its task.
351 static void perf_counter_remove_from_context(struct perf_counter *counter)
353 struct perf_counter_context *ctx = counter->ctx;
354 struct task_struct *task = ctx->task;
358 * Per cpu counters are removed via an smp call and
359 * the removal is always sucessful.
361 smp_call_function_single(counter->cpu,
362 __perf_counter_remove_from_context,
368 task_oncpu_function_call(task, __perf_counter_remove_from_context,
371 spin_lock_irq(&ctx->lock);
373 * If the context is active we need to retry the smp call.
375 if (ctx->nr_active && !list_empty(&counter->list_entry)) {
376 spin_unlock_irq(&ctx->lock);
381 * The lock prevents that this context is scheduled in so we
382 * can remove the counter safely, if the call above did not
385 if (!list_empty(&counter->list_entry)) {
386 list_del_counter(counter, ctx);
388 spin_unlock_irq(&ctx->lock);
391 static inline u64 perf_clock(void)
393 return cpu_clock(smp_processor_id());
397 * Update the record of the current time in a context.
399 static void update_context_time(struct perf_counter_context *ctx)
401 u64 now = perf_clock();
403 ctx->time += now - ctx->timestamp;
404 ctx->timestamp = now;
408 * Update the total_time_enabled and total_time_running fields for a counter.
410 static void update_counter_times(struct perf_counter *counter)
412 struct perf_counter_context *ctx = counter->ctx;
415 if (counter->state < PERF_COUNTER_STATE_INACTIVE)
418 counter->total_time_enabled = ctx->time - counter->tstamp_enabled;
420 if (counter->state == PERF_COUNTER_STATE_INACTIVE)
421 run_end = counter->tstamp_stopped;
425 counter->total_time_running = run_end - counter->tstamp_running;
429 * Update total_time_enabled and total_time_running for all counters in a group.
431 static void update_group_times(struct perf_counter *leader)
433 struct perf_counter *counter;
435 update_counter_times(leader);
436 list_for_each_entry(counter, &leader->sibling_list, list_entry)
437 update_counter_times(counter);
441 * Cross CPU call to disable a performance counter
443 static void __perf_counter_disable(void *info)
445 struct perf_counter *counter = info;
446 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
447 struct perf_counter_context *ctx = counter->ctx;
450 * If this is a per-task counter, need to check whether this
451 * counter's task is the current task on this cpu.
453 if (ctx->task && cpuctx->task_ctx != ctx)
456 spin_lock(&ctx->lock);
459 * If the counter is on, turn it off.
460 * If it is in error state, leave it in error state.
462 if (counter->state >= PERF_COUNTER_STATE_INACTIVE) {
463 update_context_time(ctx);
464 update_counter_times(counter);
465 if (counter == counter->group_leader)
466 group_sched_out(counter, cpuctx, ctx);
468 counter_sched_out(counter, cpuctx, ctx);
469 counter->state = PERF_COUNTER_STATE_OFF;
472 spin_unlock(&ctx->lock);
478 * If counter->ctx is a cloned context, callers must make sure that
479 * every task struct that counter->ctx->task could possibly point to
480 * remains valid. This condition is satisifed when called through
481 * perf_counter_for_each_child or perf_counter_for_each because they
482 * hold the top-level counter's child_mutex, so any descendant that
483 * goes to exit will block in sync_child_counter.
484 * When called from perf_pending_counter it's OK because counter->ctx
485 * is the current context on this CPU and preemption is disabled,
486 * hence we can't get into perf_counter_task_sched_out for this context.
488 static void perf_counter_disable(struct perf_counter *counter)
490 struct perf_counter_context *ctx = counter->ctx;
491 struct task_struct *task = ctx->task;
495 * Disable the counter on the cpu that it's on
497 smp_call_function_single(counter->cpu, __perf_counter_disable,
503 task_oncpu_function_call(task, __perf_counter_disable, counter);
505 spin_lock_irq(&ctx->lock);
507 * If the counter is still active, we need to retry the cross-call.
509 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
510 spin_unlock_irq(&ctx->lock);
515 * Since we have the lock this context can't be scheduled
516 * in, so we can change the state safely.
518 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
519 update_counter_times(counter);
520 counter->state = PERF_COUNTER_STATE_OFF;
523 spin_unlock_irq(&ctx->lock);
527 counter_sched_in(struct perf_counter *counter,
528 struct perf_cpu_context *cpuctx,
529 struct perf_counter_context *ctx,
532 if (counter->state <= PERF_COUNTER_STATE_OFF)
535 counter->state = PERF_COUNTER_STATE_ACTIVE;
536 counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
538 * The new state must be visible before we turn it on in the hardware:
542 if (counter->pmu->enable(counter)) {
543 counter->state = PERF_COUNTER_STATE_INACTIVE;
548 counter->tstamp_running += ctx->time - counter->tstamp_stopped;
550 if (!is_software_counter(counter))
551 cpuctx->active_oncpu++;
554 if (counter->attr.exclusive)
555 cpuctx->exclusive = 1;
561 group_sched_in(struct perf_counter *group_counter,
562 struct perf_cpu_context *cpuctx,
563 struct perf_counter_context *ctx,
566 struct perf_counter *counter, *partial_group;
569 if (group_counter->state == PERF_COUNTER_STATE_OFF)
572 ret = hw_perf_group_sched_in(group_counter, cpuctx, ctx, cpu);
574 return ret < 0 ? ret : 0;
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 if (counter_sched_in(counter, cpuctx, ctx, cpu)) {
584 partial_group = counter;
593 * Groups can be scheduled in as one unit only, so undo any
594 * partial group before returning:
596 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
597 if (counter == partial_group)
599 counter_sched_out(counter, cpuctx, ctx);
601 counter_sched_out(group_counter, cpuctx, ctx);
607 * Return 1 for a group consisting entirely of software counters,
608 * 0 if the group contains any hardware counters.
610 static int is_software_only_group(struct perf_counter *leader)
612 struct perf_counter *counter;
614 if (!is_software_counter(leader))
617 list_for_each_entry(counter, &leader->sibling_list, list_entry)
618 if (!is_software_counter(counter))
625 * Work out whether we can put this counter group on the CPU now.
627 static int group_can_go_on(struct perf_counter *counter,
628 struct perf_cpu_context *cpuctx,
632 * Groups consisting entirely of software counters can always go on.
634 if (is_software_only_group(counter))
637 * If an exclusive group is already on, no other hardware
638 * counters can go on.
640 if (cpuctx->exclusive)
643 * If this group is exclusive and there are already
644 * counters on the CPU, it can't go on.
646 if (counter->attr.exclusive && cpuctx->active_oncpu)
649 * Otherwise, try to add it if all previous groups were able
655 static void add_counter_to_ctx(struct perf_counter *counter,
656 struct perf_counter_context *ctx)
658 list_add_counter(counter, ctx);
659 counter->tstamp_enabled = ctx->time;
660 counter->tstamp_running = ctx->time;
661 counter->tstamp_stopped = ctx->time;
665 * Cross CPU call to install and enable a performance counter
667 * Must be called with ctx->mutex held
669 static void __perf_install_in_context(void *info)
671 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
672 struct perf_counter *counter = info;
673 struct perf_counter_context *ctx = counter->ctx;
674 struct perf_counter *leader = counter->group_leader;
675 int cpu = smp_processor_id();
679 * If this is a task context, we need to check whether it is
680 * the current task context of this cpu. If not it has been
681 * scheduled out before the smp call arrived.
682 * Or possibly this is the right context but it isn't
683 * on this cpu because it had no counters.
685 if (ctx->task && cpuctx->task_ctx != ctx) {
686 if (cpuctx->task_ctx || ctx->task != current)
688 cpuctx->task_ctx = ctx;
691 spin_lock(&ctx->lock);
693 update_context_time(ctx);
696 * Protect the list operation against NMI by disabling the
697 * counters on a global level. NOP for non NMI based counters.
701 add_counter_to_ctx(counter, ctx);
704 * Don't put the counter on if it is disabled or if
705 * it is in a group and the group isn't on.
707 if (counter->state != PERF_COUNTER_STATE_INACTIVE ||
708 (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE))
712 * An exclusive counter can't go on if there are already active
713 * hardware counters, and no hardware counter can go on if there
714 * is already an exclusive counter on.
716 if (!group_can_go_on(counter, cpuctx, 1))
719 err = counter_sched_in(counter, cpuctx, ctx, cpu);
723 * This counter couldn't go on. If it is in a group
724 * then we have to pull the whole group off.
725 * If the counter group is pinned then put it in error state.
727 if (leader != counter)
728 group_sched_out(leader, cpuctx, ctx);
729 if (leader->attr.pinned) {
730 update_group_times(leader);
731 leader->state = PERF_COUNTER_STATE_ERROR;
735 if (!err && !ctx->task && cpuctx->max_pertask)
736 cpuctx->max_pertask--;
741 spin_unlock(&ctx->lock);
745 * Attach a performance counter to a context
747 * First we add the counter to the list with the hardware enable bit
748 * in counter->hw_config cleared.
750 * If the counter is attached to a task which is on a CPU we use a smp
751 * call to enable it in the task context. The task might have been
752 * scheduled away, but we check this in the smp call again.
754 * Must be called with ctx->mutex held.
757 perf_install_in_context(struct perf_counter_context *ctx,
758 struct perf_counter *counter,
761 struct task_struct *task = ctx->task;
765 * Per cpu counters are installed via an smp call and
766 * the install is always sucessful.
768 smp_call_function_single(cpu, __perf_install_in_context,
774 task_oncpu_function_call(task, __perf_install_in_context,
777 spin_lock_irq(&ctx->lock);
779 * we need to retry the smp call.
781 if (ctx->is_active && list_empty(&counter->list_entry)) {
782 spin_unlock_irq(&ctx->lock);
787 * The lock prevents that this context is scheduled in so we
788 * can add the counter safely, if it the call above did not
791 if (list_empty(&counter->list_entry))
792 add_counter_to_ctx(counter, ctx);
793 spin_unlock_irq(&ctx->lock);
797 * Cross CPU call to enable a performance counter
799 static void __perf_counter_enable(void *info)
801 struct perf_counter *counter = info;
802 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
803 struct perf_counter_context *ctx = counter->ctx;
804 struct perf_counter *leader = counter->group_leader;
808 * If this is a per-task counter, need to check whether this
809 * counter's task is the current task on this cpu.
811 if (ctx->task && cpuctx->task_ctx != ctx) {
812 if (cpuctx->task_ctx || ctx->task != current)
814 cpuctx->task_ctx = ctx;
817 spin_lock(&ctx->lock);
819 update_context_time(ctx);
821 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
823 counter->state = PERF_COUNTER_STATE_INACTIVE;
824 counter->tstamp_enabled = ctx->time - counter->total_time_enabled;
827 * If the counter is in a group and isn't the group leader,
828 * then don't put it on unless the group is on.
830 if (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE)
833 if (!group_can_go_on(counter, cpuctx, 1)) {
837 if (counter == leader)
838 err = group_sched_in(counter, cpuctx, ctx,
841 err = counter_sched_in(counter, cpuctx, ctx,
848 * If this counter can't go on and it's part of a
849 * group, then the whole group has to come off.
851 if (leader != counter)
852 group_sched_out(leader, cpuctx, ctx);
853 if (leader->attr.pinned) {
854 update_group_times(leader);
855 leader->state = PERF_COUNTER_STATE_ERROR;
860 spin_unlock(&ctx->lock);
866 * If counter->ctx is a cloned context, callers must make sure that
867 * every task struct that counter->ctx->task could possibly point to
868 * remains valid. This condition is satisfied when called through
869 * perf_counter_for_each_child or perf_counter_for_each as described
870 * for perf_counter_disable.
872 static void perf_counter_enable(struct perf_counter *counter)
874 struct perf_counter_context *ctx = counter->ctx;
875 struct task_struct *task = ctx->task;
879 * Enable the counter on the cpu that it's on
881 smp_call_function_single(counter->cpu, __perf_counter_enable,
886 spin_lock_irq(&ctx->lock);
887 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
891 * If the counter is in error state, clear that first.
892 * That way, if we see the counter in error state below, we
893 * know that it has gone back into error state, as distinct
894 * from the task having been scheduled away before the
895 * cross-call arrived.
897 if (counter->state == PERF_COUNTER_STATE_ERROR)
898 counter->state = PERF_COUNTER_STATE_OFF;
901 spin_unlock_irq(&ctx->lock);
902 task_oncpu_function_call(task, __perf_counter_enable, counter);
904 spin_lock_irq(&ctx->lock);
907 * If the context is active and the counter is still off,
908 * we need to retry the cross-call.
910 if (ctx->is_active && counter->state == PERF_COUNTER_STATE_OFF)
914 * Since we have the lock this context can't be scheduled
915 * in, so we can change the state safely.
917 if (counter->state == PERF_COUNTER_STATE_OFF) {
918 counter->state = PERF_COUNTER_STATE_INACTIVE;
919 counter->tstamp_enabled =
920 ctx->time - counter->total_time_enabled;
923 spin_unlock_irq(&ctx->lock);
926 static int perf_counter_refresh(struct perf_counter *counter, int refresh)
929 * not supported on inherited counters
931 if (counter->attr.inherit)
934 atomic_add(refresh, &counter->event_limit);
935 perf_counter_enable(counter);
940 void __perf_counter_sched_out(struct perf_counter_context *ctx,
941 struct perf_cpu_context *cpuctx)
943 struct perf_counter *counter;
945 spin_lock(&ctx->lock);
947 if (likely(!ctx->nr_counters))
949 update_context_time(ctx);
952 if (ctx->nr_active) {
953 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
954 if (counter != counter->group_leader)
955 counter_sched_out(counter, cpuctx, ctx);
957 group_sched_out(counter, cpuctx, ctx);
962 spin_unlock(&ctx->lock);
966 * Test whether two contexts are equivalent, i.e. whether they
967 * have both been cloned from the same version of the same context
968 * and they both have the same number of enabled counters.
969 * If the number of enabled counters is the same, then the set
970 * of enabled counters should be the same, because these are both
971 * inherited contexts, therefore we can't access individual counters
972 * in them directly with an fd; we can only enable/disable all
973 * counters via prctl, or enable/disable all counters in a family
974 * via ioctl, which will have the same effect on both contexts.
976 static int context_equiv(struct perf_counter_context *ctx1,
977 struct perf_counter_context *ctx2)
979 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
980 && ctx1->parent_gen == ctx2->parent_gen
981 && !ctx1->pin_count && !ctx2->pin_count;
985 * Called from scheduler to remove the counters of the current task,
986 * with interrupts disabled.
988 * We stop each counter and update the counter value in counter->count.
990 * This does not protect us against NMI, but disable()
991 * sets the disabled bit in the control field of counter _before_
992 * accessing the counter control register. If a NMI hits, then it will
993 * not restart the counter.
995 void perf_counter_task_sched_out(struct task_struct *task,
996 struct task_struct *next, int cpu)
998 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
999 struct perf_counter_context *ctx = task->perf_counter_ctxp;
1000 struct perf_counter_context *next_ctx;
1001 struct perf_counter_context *parent;
1002 struct pt_regs *regs;
1005 regs = task_pt_regs(task);
1006 perf_swcounter_event(PERF_COUNT_CONTEXT_SWITCHES, 1, 1, regs, 0);
1008 if (likely(!ctx || !cpuctx->task_ctx))
1011 update_context_time(ctx);
1014 parent = rcu_dereference(ctx->parent_ctx);
1015 next_ctx = next->perf_counter_ctxp;
1016 if (parent && next_ctx &&
1017 rcu_dereference(next_ctx->parent_ctx) == parent) {
1019 * Looks like the two contexts are clones, so we might be
1020 * able to optimize the context switch. We lock both
1021 * contexts and check that they are clones under the
1022 * lock (including re-checking that neither has been
1023 * uncloned in the meantime). It doesn't matter which
1024 * order we take the locks because no other cpu could
1025 * be trying to lock both of these tasks.
1027 spin_lock(&ctx->lock);
1028 spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
1029 if (context_equiv(ctx, next_ctx)) {
1031 * XXX do we need a memory barrier of sorts
1032 * wrt to rcu_dereference() of perf_counter_ctxp
1034 task->perf_counter_ctxp = next_ctx;
1035 next->perf_counter_ctxp = ctx;
1037 next_ctx->task = task;
1040 spin_unlock(&next_ctx->lock);
1041 spin_unlock(&ctx->lock);
1046 __perf_counter_sched_out(ctx, cpuctx);
1047 cpuctx->task_ctx = NULL;
1052 * Called with IRQs disabled
1054 static void __perf_counter_task_sched_out(struct perf_counter_context *ctx)
1056 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1058 if (!cpuctx->task_ctx)
1061 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
1064 __perf_counter_sched_out(ctx, cpuctx);
1065 cpuctx->task_ctx = NULL;
1069 * Called with IRQs disabled
1071 static void perf_counter_cpu_sched_out(struct perf_cpu_context *cpuctx)
1073 __perf_counter_sched_out(&cpuctx->ctx, cpuctx);
1077 __perf_counter_sched_in(struct perf_counter_context *ctx,
1078 struct perf_cpu_context *cpuctx, int cpu)
1080 struct perf_counter *counter;
1083 spin_lock(&ctx->lock);
1085 if (likely(!ctx->nr_counters))
1088 ctx->timestamp = perf_clock();
1093 * First go through the list and put on any pinned groups
1094 * in order to give them the best chance of going on.
1096 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1097 if (counter->state <= PERF_COUNTER_STATE_OFF ||
1098 !counter->attr.pinned)
1100 if (counter->cpu != -1 && counter->cpu != cpu)
1103 if (counter != counter->group_leader)
1104 counter_sched_in(counter, cpuctx, ctx, cpu);
1106 if (group_can_go_on(counter, cpuctx, 1))
1107 group_sched_in(counter, cpuctx, ctx, cpu);
1111 * If this pinned group hasn't been scheduled,
1112 * put it in error state.
1114 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1115 update_group_times(counter);
1116 counter->state = PERF_COUNTER_STATE_ERROR;
1120 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1122 * Ignore counters in OFF or ERROR state, and
1123 * ignore pinned counters since we did them already.
1125 if (counter->state <= PERF_COUNTER_STATE_OFF ||
1126 counter->attr.pinned)
1130 * Listen to the 'cpu' scheduling filter constraint
1133 if (counter->cpu != -1 && counter->cpu != cpu)
1136 if (counter != counter->group_leader) {
1137 if (counter_sched_in(counter, cpuctx, ctx, cpu))
1140 if (group_can_go_on(counter, cpuctx, can_add_hw)) {
1141 if (group_sched_in(counter, cpuctx, ctx, cpu))
1148 spin_unlock(&ctx->lock);
1152 * Called from scheduler to add the counters of the current task
1153 * with interrupts disabled.
1155 * We restore the counter value and then enable it.
1157 * This does not protect us against NMI, but enable()
1158 * sets the enabled bit in the control field of counter _before_
1159 * accessing the counter control register. If a NMI hits, then it will
1160 * keep the counter running.
1162 void perf_counter_task_sched_in(struct task_struct *task, int cpu)
1164 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
1165 struct perf_counter_context *ctx = task->perf_counter_ctxp;
1169 if (cpuctx->task_ctx == ctx)
1171 __perf_counter_sched_in(ctx, cpuctx, cpu);
1172 cpuctx->task_ctx = ctx;
1175 static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu)
1177 struct perf_counter_context *ctx = &cpuctx->ctx;
1179 __perf_counter_sched_in(ctx, cpuctx, cpu);
1182 #define MAX_INTERRUPTS (~0ULL)
1184 static void perf_log_throttle(struct perf_counter *counter, int enable);
1185 static void perf_log_period(struct perf_counter *counter, u64 period);
1187 static void perf_adjust_period(struct perf_counter *counter, u64 events)
1189 struct hw_perf_counter *hwc = &counter->hw;
1190 u64 period, sample_period;
1193 events *= hwc->sample_period;
1194 period = div64_u64(events, counter->attr.sample_freq);
1196 delta = (s64)(period - hwc->sample_period);
1197 delta = (delta + 7) / 8; /* low pass filter */
1199 sample_period = hwc->sample_period + delta;
1204 perf_log_period(counter, sample_period);
1206 hwc->sample_period = sample_period;
1209 static void perf_ctx_adjust_freq(struct perf_counter_context *ctx)
1211 struct perf_counter *counter;
1212 struct hw_perf_counter *hwc;
1213 u64 interrupts, freq;
1215 spin_lock(&ctx->lock);
1216 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1217 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
1222 interrupts = hwc->interrupts;
1223 hwc->interrupts = 0;
1226 * unthrottle counters on the tick
1228 if (interrupts == MAX_INTERRUPTS) {
1229 perf_log_throttle(counter, 1);
1230 counter->pmu->unthrottle(counter);
1231 interrupts = 2*sysctl_perf_counter_limit/HZ;
1234 if (!counter->attr.freq || !counter->attr.sample_freq)
1238 * if the specified freq < HZ then we need to skip ticks
1240 if (counter->attr.sample_freq < HZ) {
1241 freq = counter->attr.sample_freq;
1243 hwc->freq_count += freq;
1244 hwc->freq_interrupts += interrupts;
1246 if (hwc->freq_count < HZ)
1249 interrupts = hwc->freq_interrupts;
1250 hwc->freq_interrupts = 0;
1251 hwc->freq_count -= HZ;
1255 perf_adjust_period(counter, freq * interrupts);
1258 * In order to avoid being stalled by an (accidental) huge
1259 * sample period, force reset the sample period if we didn't
1260 * get any events in this freq period.
1264 counter->pmu->disable(counter);
1265 atomic_set(&hwc->period_left, 0);
1266 counter->pmu->enable(counter);
1270 spin_unlock(&ctx->lock);
1274 * Round-robin a context's counters:
1276 static void rotate_ctx(struct perf_counter_context *ctx)
1278 struct perf_counter *counter;
1280 if (!ctx->nr_counters)
1283 spin_lock(&ctx->lock);
1285 * Rotate the first entry last (works just fine for group counters too):
1288 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1289 list_move_tail(&counter->list_entry, &ctx->counter_list);
1294 spin_unlock(&ctx->lock);
1297 void perf_counter_task_tick(struct task_struct *curr, int cpu)
1299 struct perf_cpu_context *cpuctx;
1300 struct perf_counter_context *ctx;
1302 if (!atomic_read(&nr_counters))
1305 cpuctx = &per_cpu(perf_cpu_context, cpu);
1306 ctx = curr->perf_counter_ctxp;
1308 perf_ctx_adjust_freq(&cpuctx->ctx);
1310 perf_ctx_adjust_freq(ctx);
1312 perf_counter_cpu_sched_out(cpuctx);
1314 __perf_counter_task_sched_out(ctx);
1316 rotate_ctx(&cpuctx->ctx);
1320 perf_counter_cpu_sched_in(cpuctx, cpu);
1322 perf_counter_task_sched_in(curr, cpu);
1326 * Cross CPU call to read the hardware counter
1328 static void __read(void *info)
1330 struct perf_counter *counter = info;
1331 struct perf_counter_context *ctx = counter->ctx;
1332 unsigned long flags;
1334 local_irq_save(flags);
1336 update_context_time(ctx);
1337 counter->pmu->read(counter);
1338 update_counter_times(counter);
1339 local_irq_restore(flags);
1342 static u64 perf_counter_read(struct perf_counter *counter)
1345 * If counter is enabled and currently active on a CPU, update the
1346 * value in the counter structure:
1348 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
1349 smp_call_function_single(counter->oncpu,
1350 __read, counter, 1);
1351 } else if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1352 update_counter_times(counter);
1355 return atomic64_read(&counter->count);
1359 * Initialize the perf_counter context in a task_struct:
1362 __perf_counter_init_context(struct perf_counter_context *ctx,
1363 struct task_struct *task)
1365 memset(ctx, 0, sizeof(*ctx));
1366 spin_lock_init(&ctx->lock);
1367 mutex_init(&ctx->mutex);
1368 INIT_LIST_HEAD(&ctx->counter_list);
1369 INIT_LIST_HEAD(&ctx->event_list);
1370 atomic_set(&ctx->refcount, 1);
1374 static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
1376 struct perf_counter_context *parent_ctx;
1377 struct perf_counter_context *ctx;
1378 struct perf_cpu_context *cpuctx;
1379 struct task_struct *task;
1380 unsigned long flags;
1384 * If cpu is not a wildcard then this is a percpu counter:
1387 /* Must be root to operate on a CPU counter: */
1388 if (sysctl_perf_counter_priv && !capable(CAP_SYS_ADMIN))
1389 return ERR_PTR(-EACCES);
1391 if (cpu < 0 || cpu > num_possible_cpus())
1392 return ERR_PTR(-EINVAL);
1395 * We could be clever and allow to attach a counter to an
1396 * offline CPU and activate it when the CPU comes up, but
1399 if (!cpu_isset(cpu, cpu_online_map))
1400 return ERR_PTR(-ENODEV);
1402 cpuctx = &per_cpu(perf_cpu_context, cpu);
1413 task = find_task_by_vpid(pid);
1415 get_task_struct(task);
1419 return ERR_PTR(-ESRCH);
1422 * Can't attach counters to a dying task.
1425 if (task->flags & PF_EXITING)
1428 /* Reuse ptrace permission checks for now. */
1430 if (!ptrace_may_access(task, PTRACE_MODE_READ))
1434 ctx = perf_lock_task_context(task, &flags);
1436 parent_ctx = ctx->parent_ctx;
1438 put_ctx(parent_ctx);
1439 ctx->parent_ctx = NULL; /* no longer a clone */
1442 * Get an extra reference before dropping the lock so that
1443 * this context won't get freed if the task exits.
1446 spin_unlock_irqrestore(&ctx->lock, flags);
1450 ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
1454 __perf_counter_init_context(ctx, task);
1456 if (cmpxchg(&task->perf_counter_ctxp, NULL, ctx)) {
1458 * We raced with some other task; use
1459 * the context they set.
1464 get_task_struct(task);
1467 put_task_struct(task);
1471 put_task_struct(task);
1472 return ERR_PTR(err);
1475 static void free_counter_rcu(struct rcu_head *head)
1477 struct perf_counter *counter;
1479 counter = container_of(head, struct perf_counter, rcu_head);
1481 put_pid_ns(counter->ns);
1485 static void perf_pending_sync(struct perf_counter *counter);
1487 static void free_counter(struct perf_counter *counter)
1489 perf_pending_sync(counter);
1491 atomic_dec(&nr_counters);
1492 if (counter->attr.mmap)
1493 atomic_dec(&nr_mmap_counters);
1494 if (counter->attr.comm)
1495 atomic_dec(&nr_comm_counters);
1497 if (counter->destroy)
1498 counter->destroy(counter);
1500 put_ctx(counter->ctx);
1501 call_rcu(&counter->rcu_head, free_counter_rcu);
1505 * Called when the last reference to the file is gone.
1507 static int perf_release(struct inode *inode, struct file *file)
1509 struct perf_counter *counter = file->private_data;
1510 struct perf_counter_context *ctx = counter->ctx;
1512 file->private_data = NULL;
1514 WARN_ON_ONCE(ctx->parent_ctx);
1515 mutex_lock(&ctx->mutex);
1516 perf_counter_remove_from_context(counter);
1517 mutex_unlock(&ctx->mutex);
1519 mutex_lock(&counter->owner->perf_counter_mutex);
1520 list_del_init(&counter->owner_entry);
1521 mutex_unlock(&counter->owner->perf_counter_mutex);
1522 put_task_struct(counter->owner);
1524 free_counter(counter);
1530 * Read the performance counter - simple non blocking version for now
1533 perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
1539 * Return end-of-file for a read on a counter that is in
1540 * error state (i.e. because it was pinned but it couldn't be
1541 * scheduled on to the CPU at some point).
1543 if (counter->state == PERF_COUNTER_STATE_ERROR)
1546 WARN_ON_ONCE(counter->ctx->parent_ctx);
1547 mutex_lock(&counter->child_mutex);
1548 values[0] = perf_counter_read(counter);
1550 if (counter->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1551 values[n++] = counter->total_time_enabled +
1552 atomic64_read(&counter->child_total_time_enabled);
1553 if (counter->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1554 values[n++] = counter->total_time_running +
1555 atomic64_read(&counter->child_total_time_running);
1556 if (counter->attr.read_format & PERF_FORMAT_ID)
1557 values[n++] = counter->id;
1558 mutex_unlock(&counter->child_mutex);
1560 if (count < n * sizeof(u64))
1562 count = n * sizeof(u64);
1564 if (copy_to_user(buf, values, count))
1571 perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
1573 struct perf_counter *counter = file->private_data;
1575 return perf_read_hw(counter, buf, count);
1578 static unsigned int perf_poll(struct file *file, poll_table *wait)
1580 struct perf_counter *counter = file->private_data;
1581 struct perf_mmap_data *data;
1582 unsigned int events = POLL_HUP;
1585 data = rcu_dereference(counter->data);
1587 events = atomic_xchg(&data->poll, 0);
1590 poll_wait(file, &counter->waitq, wait);
1595 static void perf_counter_reset(struct perf_counter *counter)
1597 (void)perf_counter_read(counter);
1598 atomic64_set(&counter->count, 0);
1599 perf_counter_update_userpage(counter);
1602 static void perf_counter_for_each_sibling(struct perf_counter *counter,
1603 void (*func)(struct perf_counter *))
1605 struct perf_counter_context *ctx = counter->ctx;
1606 struct perf_counter *sibling;
1608 WARN_ON_ONCE(ctx->parent_ctx);
1609 mutex_lock(&ctx->mutex);
1610 counter = counter->group_leader;
1613 list_for_each_entry(sibling, &counter->sibling_list, list_entry)
1615 mutex_unlock(&ctx->mutex);
1619 * Holding the top-level counter's child_mutex means that any
1620 * descendant process that has inherited this counter will block
1621 * in sync_child_counter if it goes to exit, thus satisfying the
1622 * task existence requirements of perf_counter_enable/disable.
1624 static void perf_counter_for_each_child(struct perf_counter *counter,
1625 void (*func)(struct perf_counter *))
1627 struct perf_counter *child;
1629 WARN_ON_ONCE(counter->ctx->parent_ctx);
1630 mutex_lock(&counter->child_mutex);
1632 list_for_each_entry(child, &counter->child_list, child_list)
1634 mutex_unlock(&counter->child_mutex);
1637 static void perf_counter_for_each(struct perf_counter *counter,
1638 void (*func)(struct perf_counter *))
1640 struct perf_counter *child;
1642 WARN_ON_ONCE(counter->ctx->parent_ctx);
1643 mutex_lock(&counter->child_mutex);
1644 perf_counter_for_each_sibling(counter, func);
1645 list_for_each_entry(child, &counter->child_list, child_list)
1646 perf_counter_for_each_sibling(child, func);
1647 mutex_unlock(&counter->child_mutex);
1650 static int perf_counter_period(struct perf_counter *counter, u64 __user *arg)
1652 struct perf_counter_context *ctx = counter->ctx;
1657 if (!counter->attr.sample_period)
1660 size = copy_from_user(&value, arg, sizeof(value));
1661 if (size != sizeof(value))
1667 spin_lock_irq(&ctx->lock);
1668 if (counter->attr.freq) {
1669 if (value > sysctl_perf_counter_limit) {
1674 counter->attr.sample_freq = value;
1676 perf_log_period(counter, value);
1678 counter->attr.sample_period = value;
1679 counter->hw.sample_period = value;
1682 spin_unlock_irq(&ctx->lock);
1687 static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1689 struct perf_counter *counter = file->private_data;
1690 void (*func)(struct perf_counter *);
1694 case PERF_COUNTER_IOC_ENABLE:
1695 func = perf_counter_enable;
1697 case PERF_COUNTER_IOC_DISABLE:
1698 func = perf_counter_disable;
1700 case PERF_COUNTER_IOC_RESET:
1701 func = perf_counter_reset;
1704 case PERF_COUNTER_IOC_REFRESH:
1705 return perf_counter_refresh(counter, arg);
1707 case PERF_COUNTER_IOC_PERIOD:
1708 return perf_counter_period(counter, (u64 __user *)arg);
1714 if (flags & PERF_IOC_FLAG_GROUP)
1715 perf_counter_for_each(counter, func);
1717 perf_counter_for_each_child(counter, func);
1722 int perf_counter_task_enable(void)
1724 struct perf_counter *counter;
1726 mutex_lock(¤t->perf_counter_mutex);
1727 list_for_each_entry(counter, ¤t->perf_counter_list, owner_entry)
1728 perf_counter_for_each_child(counter, perf_counter_enable);
1729 mutex_unlock(¤t->perf_counter_mutex);
1734 int perf_counter_task_disable(void)
1736 struct perf_counter *counter;
1738 mutex_lock(¤t->perf_counter_mutex);
1739 list_for_each_entry(counter, ¤t->perf_counter_list, owner_entry)
1740 perf_counter_for_each_child(counter, perf_counter_disable);
1741 mutex_unlock(¤t->perf_counter_mutex);
1747 * Callers need to ensure there can be no nesting of this function, otherwise
1748 * the seqlock logic goes bad. We can not serialize this because the arch
1749 * code calls this from NMI context.
1751 void perf_counter_update_userpage(struct perf_counter *counter)
1753 struct perf_counter_mmap_page *userpg;
1754 struct perf_mmap_data *data;
1757 data = rcu_dereference(counter->data);
1761 userpg = data->user_page;
1764 * Disable preemption so as to not let the corresponding user-space
1765 * spin too long if we get preempted.
1770 userpg->index = counter->hw.idx;
1771 userpg->offset = atomic64_read(&counter->count);
1772 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
1773 userpg->offset -= atomic64_read(&counter->hw.prev_count);
1782 static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1784 struct perf_counter *counter = vma->vm_file->private_data;
1785 struct perf_mmap_data *data;
1786 int ret = VM_FAULT_SIGBUS;
1789 data = rcu_dereference(counter->data);
1793 if (vmf->pgoff == 0) {
1794 vmf->page = virt_to_page(data->user_page);
1796 int nr = vmf->pgoff - 1;
1798 if ((unsigned)nr > data->nr_pages)
1801 vmf->page = virt_to_page(data->data_pages[nr]);
1803 get_page(vmf->page);
1811 static int perf_mmap_data_alloc(struct perf_counter *counter, int nr_pages)
1813 struct perf_mmap_data *data;
1817 WARN_ON(atomic_read(&counter->mmap_count));
1819 size = sizeof(struct perf_mmap_data);
1820 size += nr_pages * sizeof(void *);
1822 data = kzalloc(size, GFP_KERNEL);
1826 data->user_page = (void *)get_zeroed_page(GFP_KERNEL);
1827 if (!data->user_page)
1828 goto fail_user_page;
1830 for (i = 0; i < nr_pages; i++) {
1831 data->data_pages[i] = (void *)get_zeroed_page(GFP_KERNEL);
1832 if (!data->data_pages[i])
1833 goto fail_data_pages;
1836 data->nr_pages = nr_pages;
1837 atomic_set(&data->lock, -1);
1839 rcu_assign_pointer(counter->data, data);
1844 for (i--; i >= 0; i--)
1845 free_page((unsigned long)data->data_pages[i]);
1847 free_page((unsigned long)data->user_page);
1856 static void __perf_mmap_data_free(struct rcu_head *rcu_head)
1858 struct perf_mmap_data *data;
1861 data = container_of(rcu_head, struct perf_mmap_data, rcu_head);
1863 free_page((unsigned long)data->user_page);
1864 for (i = 0; i < data->nr_pages; i++)
1865 free_page((unsigned long)data->data_pages[i]);
1869 static void perf_mmap_data_free(struct perf_counter *counter)
1871 struct perf_mmap_data *data = counter->data;
1873 WARN_ON(atomic_read(&counter->mmap_count));
1875 rcu_assign_pointer(counter->data, NULL);
1876 call_rcu(&data->rcu_head, __perf_mmap_data_free);
1879 static void perf_mmap_open(struct vm_area_struct *vma)
1881 struct perf_counter *counter = vma->vm_file->private_data;
1883 atomic_inc(&counter->mmap_count);
1886 static void perf_mmap_close(struct vm_area_struct *vma)
1888 struct perf_counter *counter = vma->vm_file->private_data;
1890 WARN_ON_ONCE(counter->ctx->parent_ctx);
1891 if (atomic_dec_and_mutex_lock(&counter->mmap_count, &counter->mmap_mutex)) {
1892 struct user_struct *user = current_user();
1894 atomic_long_sub(counter->data->nr_pages + 1, &user->locked_vm);
1895 vma->vm_mm->locked_vm -= counter->data->nr_locked;
1896 perf_mmap_data_free(counter);
1897 mutex_unlock(&counter->mmap_mutex);
1901 static struct vm_operations_struct perf_mmap_vmops = {
1902 .open = perf_mmap_open,
1903 .close = perf_mmap_close,
1904 .fault = perf_mmap_fault,
1907 static int perf_mmap(struct file *file, struct vm_area_struct *vma)
1909 struct perf_counter *counter = file->private_data;
1910 unsigned long user_locked, user_lock_limit;
1911 struct user_struct *user = current_user();
1912 unsigned long locked, lock_limit;
1913 unsigned long vma_size;
1914 unsigned long nr_pages;
1915 long user_extra, extra;
1918 if (!(vma->vm_flags & VM_SHARED) || (vma->vm_flags & VM_WRITE))
1921 vma_size = vma->vm_end - vma->vm_start;
1922 nr_pages = (vma_size / PAGE_SIZE) - 1;
1925 * If we have data pages ensure they're a power-of-two number, so we
1926 * can do bitmasks instead of modulo.
1928 if (nr_pages != 0 && !is_power_of_2(nr_pages))
1931 if (vma_size != PAGE_SIZE * (1 + nr_pages))
1934 if (vma->vm_pgoff != 0)
1937 WARN_ON_ONCE(counter->ctx->parent_ctx);
1938 mutex_lock(&counter->mmap_mutex);
1939 if (atomic_inc_not_zero(&counter->mmap_count)) {
1940 if (nr_pages != counter->data->nr_pages)
1945 user_extra = nr_pages + 1;
1946 user_lock_limit = sysctl_perf_counter_mlock >> (PAGE_SHIFT - 10);
1949 * Increase the limit linearly with more CPUs:
1951 user_lock_limit *= num_online_cpus();
1953 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
1956 if (user_locked > user_lock_limit)
1957 extra = user_locked - user_lock_limit;
1959 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
1960 lock_limit >>= PAGE_SHIFT;
1961 locked = vma->vm_mm->locked_vm + extra;
1963 if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) {
1968 WARN_ON(counter->data);
1969 ret = perf_mmap_data_alloc(counter, nr_pages);
1973 atomic_set(&counter->mmap_count, 1);
1974 atomic_long_add(user_extra, &user->locked_vm);
1975 vma->vm_mm->locked_vm += extra;
1976 counter->data->nr_locked = extra;
1978 mutex_unlock(&counter->mmap_mutex);
1980 vma->vm_flags &= ~VM_MAYWRITE;
1981 vma->vm_flags |= VM_RESERVED;
1982 vma->vm_ops = &perf_mmap_vmops;
1987 static int perf_fasync(int fd, struct file *filp, int on)
1989 struct inode *inode = filp->f_path.dentry->d_inode;
1990 struct perf_counter *counter = filp->private_data;
1993 mutex_lock(&inode->i_mutex);
1994 retval = fasync_helper(fd, filp, on, &counter->fasync);
1995 mutex_unlock(&inode->i_mutex);
2003 static const struct file_operations perf_fops = {
2004 .release = perf_release,
2007 .unlocked_ioctl = perf_ioctl,
2008 .compat_ioctl = perf_ioctl,
2010 .fasync = perf_fasync,
2014 * Perf counter wakeup
2016 * If there's data, ensure we set the poll() state and publish everything
2017 * to user-space before waking everybody up.
2020 void perf_counter_wakeup(struct perf_counter *counter)
2022 wake_up_all(&counter->waitq);
2024 if (counter->pending_kill) {
2025 kill_fasync(&counter->fasync, SIGIO, counter->pending_kill);
2026 counter->pending_kill = 0;
2033 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
2035 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
2036 * single linked list and use cmpxchg() to add entries lockless.
2039 static void perf_pending_counter(struct perf_pending_entry *entry)
2041 struct perf_counter *counter = container_of(entry,
2042 struct perf_counter, pending);
2044 if (counter->pending_disable) {
2045 counter->pending_disable = 0;
2046 perf_counter_disable(counter);
2049 if (counter->pending_wakeup) {
2050 counter->pending_wakeup = 0;
2051 perf_counter_wakeup(counter);
2055 #define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
2057 static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
2061 static void perf_pending_queue(struct perf_pending_entry *entry,
2062 void (*func)(struct perf_pending_entry *))
2064 struct perf_pending_entry **head;
2066 if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
2071 head = &get_cpu_var(perf_pending_head);
2074 entry->next = *head;
2075 } while (cmpxchg(head, entry->next, entry) != entry->next);
2077 set_perf_counter_pending();
2079 put_cpu_var(perf_pending_head);
2082 static int __perf_pending_run(void)
2084 struct perf_pending_entry *list;
2087 list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
2088 while (list != PENDING_TAIL) {
2089 void (*func)(struct perf_pending_entry *);
2090 struct perf_pending_entry *entry = list;
2097 * Ensure we observe the unqueue before we issue the wakeup,
2098 * so that we won't be waiting forever.
2099 * -- see perf_not_pending().
2110 static inline int perf_not_pending(struct perf_counter *counter)
2113 * If we flush on whatever cpu we run, there is a chance we don't
2117 __perf_pending_run();
2121 * Ensure we see the proper queue state before going to sleep
2122 * so that we do not miss the wakeup. -- see perf_pending_handle()
2125 return counter->pending.next == NULL;
2128 static void perf_pending_sync(struct perf_counter *counter)
2130 wait_event(counter->waitq, perf_not_pending(counter));
2133 void perf_counter_do_pending(void)
2135 __perf_pending_run();
2139 * Callchain support -- arch specific
2142 __weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
2151 struct perf_output_handle {
2152 struct perf_counter *counter;
2153 struct perf_mmap_data *data;
2155 unsigned long offset;
2159 unsigned long flags;
2162 static void perf_output_wakeup(struct perf_output_handle *handle)
2164 atomic_set(&handle->data->poll, POLL_IN);
2167 handle->counter->pending_wakeup = 1;
2168 perf_pending_queue(&handle->counter->pending,
2169 perf_pending_counter);
2171 perf_counter_wakeup(handle->counter);
2175 * Curious locking construct.
2177 * We need to ensure a later event doesn't publish a head when a former
2178 * event isn't done writing. However since we need to deal with NMIs we
2179 * cannot fully serialize things.
2181 * What we do is serialize between CPUs so we only have to deal with NMI
2182 * nesting on a single CPU.
2184 * We only publish the head (and generate a wakeup) when the outer-most
2187 static void perf_output_lock(struct perf_output_handle *handle)
2189 struct perf_mmap_data *data = handle->data;
2194 local_irq_save(handle->flags);
2195 cpu = smp_processor_id();
2197 if (in_nmi() && atomic_read(&data->lock) == cpu)
2200 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
2206 static void perf_output_unlock(struct perf_output_handle *handle)
2208 struct perf_mmap_data *data = handle->data;
2212 data->done_head = data->head;
2214 if (!handle->locked)
2219 * The xchg implies a full barrier that ensures all writes are done
2220 * before we publish the new head, matched by a rmb() in userspace when
2221 * reading this position.
2223 while ((head = atomic_long_xchg(&data->done_head, 0)))
2224 data->user_page->data_head = head;
2227 * NMI can happen here, which means we can miss a done_head update.
2230 cpu = atomic_xchg(&data->lock, -1);
2231 WARN_ON_ONCE(cpu != smp_processor_id());
2234 * Therefore we have to validate we did not indeed do so.
2236 if (unlikely(atomic_long_read(&data->done_head))) {
2238 * Since we had it locked, we can lock it again.
2240 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
2246 if (atomic_xchg(&data->wakeup, 0))
2247 perf_output_wakeup(handle);
2249 local_irq_restore(handle->flags);
2252 static int perf_output_begin(struct perf_output_handle *handle,
2253 struct perf_counter *counter, unsigned int size,
2254 int nmi, int overflow)
2256 struct perf_mmap_data *data;
2257 unsigned int offset, head;
2260 * For inherited counters we send all the output towards the parent.
2262 if (counter->parent)
2263 counter = counter->parent;
2266 data = rcu_dereference(counter->data);
2270 handle->data = data;
2271 handle->counter = counter;
2273 handle->overflow = overflow;
2275 if (!data->nr_pages)
2278 perf_output_lock(handle);
2281 offset = head = atomic_long_read(&data->head);
2283 } while (atomic_long_cmpxchg(&data->head, offset, head) != offset);
2285 handle->offset = offset;
2286 handle->head = head;
2288 if ((offset >> PAGE_SHIFT) != (head >> PAGE_SHIFT))
2289 atomic_set(&data->wakeup, 1);
2294 perf_output_wakeup(handle);
2301 static void perf_output_copy(struct perf_output_handle *handle,
2302 const void *buf, unsigned int len)
2304 unsigned int pages_mask;
2305 unsigned int offset;
2309 offset = handle->offset;
2310 pages_mask = handle->data->nr_pages - 1;
2311 pages = handle->data->data_pages;
2314 unsigned int page_offset;
2317 nr = (offset >> PAGE_SHIFT) & pages_mask;
2318 page_offset = offset & (PAGE_SIZE - 1);
2319 size = min_t(unsigned int, PAGE_SIZE - page_offset, len);
2321 memcpy(pages[nr] + page_offset, buf, size);
2328 handle->offset = offset;
2331 * Check we didn't copy past our reservation window, taking the
2332 * possible unsigned int wrap into account.
2334 WARN_ON_ONCE(((long)(handle->head - handle->offset)) < 0);
2337 #define perf_output_put(handle, x) \
2338 perf_output_copy((handle), &(x), sizeof(x))
2340 static void perf_output_end(struct perf_output_handle *handle)
2342 struct perf_counter *counter = handle->counter;
2343 struct perf_mmap_data *data = handle->data;
2345 int wakeup_events = counter->attr.wakeup_events;
2347 if (handle->overflow && wakeup_events) {
2348 int events = atomic_inc_return(&data->events);
2349 if (events >= wakeup_events) {
2350 atomic_sub(wakeup_events, &data->events);
2351 atomic_set(&data->wakeup, 1);
2355 perf_output_unlock(handle);
2359 static u32 perf_counter_pid(struct perf_counter *counter, struct task_struct *p)
2362 * only top level counters have the pid namespace they were created in
2364 if (counter->parent)
2365 counter = counter->parent;
2367 return task_tgid_nr_ns(p, counter->ns);
2370 static u32 perf_counter_tid(struct perf_counter *counter, struct task_struct *p)
2373 * only top level counters have the pid namespace they were created in
2375 if (counter->parent)
2376 counter = counter->parent;
2378 return task_pid_nr_ns(p, counter->ns);
2381 static void perf_counter_output(struct perf_counter *counter,
2382 int nmi, struct pt_regs *regs, u64 addr)
2385 u64 sample_type = counter->attr.sample_type;
2386 struct perf_output_handle handle;
2387 struct perf_event_header header;
2396 struct perf_callchain_entry *callchain = NULL;
2397 int callchain_size = 0;
2404 header.size = sizeof(header);
2406 header.misc = PERF_EVENT_MISC_OVERFLOW;
2407 header.misc |= perf_misc_flags(regs);
2409 if (sample_type & PERF_SAMPLE_IP) {
2410 ip = perf_instruction_pointer(regs);
2411 header.type |= PERF_SAMPLE_IP;
2412 header.size += sizeof(ip);
2415 if (sample_type & PERF_SAMPLE_TID) {
2416 /* namespace issues */
2417 tid_entry.pid = perf_counter_pid(counter, current);
2418 tid_entry.tid = perf_counter_tid(counter, current);
2420 header.type |= PERF_SAMPLE_TID;
2421 header.size += sizeof(tid_entry);
2424 if (sample_type & PERF_SAMPLE_TIME) {
2426 * Maybe do better on x86 and provide cpu_clock_nmi()
2428 time = sched_clock();
2430 header.type |= PERF_SAMPLE_TIME;
2431 header.size += sizeof(u64);
2434 if (sample_type & PERF_SAMPLE_ADDR) {
2435 header.type |= PERF_SAMPLE_ADDR;
2436 header.size += sizeof(u64);
2439 if (sample_type & PERF_SAMPLE_ID) {
2440 header.type |= PERF_SAMPLE_ID;
2441 header.size += sizeof(u64);
2444 if (sample_type & PERF_SAMPLE_CPU) {
2445 header.type |= PERF_SAMPLE_CPU;
2446 header.size += sizeof(cpu_entry);
2448 cpu_entry.cpu = raw_smp_processor_id();
2451 if (sample_type & PERF_SAMPLE_PERIOD) {
2452 header.type |= PERF_SAMPLE_PERIOD;
2453 header.size += sizeof(u64);
2456 if (sample_type & PERF_SAMPLE_GROUP) {
2457 header.type |= PERF_SAMPLE_GROUP;
2458 header.size += sizeof(u64) +
2459 counter->nr_siblings * sizeof(group_entry);
2462 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
2463 callchain = perf_callchain(regs);
2466 callchain_size = (1 + callchain->nr) * sizeof(u64);
2468 header.type |= PERF_SAMPLE_CALLCHAIN;
2469 header.size += callchain_size;
2473 ret = perf_output_begin(&handle, counter, header.size, nmi, 1);
2477 perf_output_put(&handle, header);
2479 if (sample_type & PERF_SAMPLE_IP)
2480 perf_output_put(&handle, ip);
2482 if (sample_type & PERF_SAMPLE_TID)
2483 perf_output_put(&handle, tid_entry);
2485 if (sample_type & PERF_SAMPLE_TIME)
2486 perf_output_put(&handle, time);
2488 if (sample_type & PERF_SAMPLE_ADDR)
2489 perf_output_put(&handle, addr);
2491 if (sample_type & PERF_SAMPLE_ID)
2492 perf_output_put(&handle, counter->id);
2494 if (sample_type & PERF_SAMPLE_CPU)
2495 perf_output_put(&handle, cpu_entry);
2497 if (sample_type & PERF_SAMPLE_PERIOD)
2498 perf_output_put(&handle, counter->hw.sample_period);
2501 * XXX PERF_SAMPLE_GROUP vs inherited counters seems difficult.
2503 if (sample_type & PERF_SAMPLE_GROUP) {
2504 struct perf_counter *leader, *sub;
2505 u64 nr = counter->nr_siblings;
2507 perf_output_put(&handle, nr);
2509 leader = counter->group_leader;
2510 list_for_each_entry(sub, &leader->sibling_list, list_entry) {
2512 sub->pmu->read(sub);
2514 group_entry.id = sub->id;
2515 group_entry.counter = atomic64_read(&sub->count);
2517 perf_output_put(&handle, group_entry);
2522 perf_output_copy(&handle, callchain, callchain_size);
2524 perf_output_end(&handle);
2531 struct perf_fork_event {
2532 struct task_struct *task;
2535 struct perf_event_header header;
2542 static void perf_counter_fork_output(struct perf_counter *counter,
2543 struct perf_fork_event *fork_event)
2545 struct perf_output_handle handle;
2546 int size = fork_event->event.header.size;
2547 struct task_struct *task = fork_event->task;
2548 int ret = perf_output_begin(&handle, counter, size, 0, 0);
2553 fork_event->event.pid = perf_counter_pid(counter, task);
2554 fork_event->event.ppid = perf_counter_pid(counter, task->real_parent);
2556 perf_output_put(&handle, fork_event->event);
2557 perf_output_end(&handle);
2560 static int perf_counter_fork_match(struct perf_counter *counter)
2562 if (counter->attr.comm || counter->attr.mmap)
2568 static void perf_counter_fork_ctx(struct perf_counter_context *ctx,
2569 struct perf_fork_event *fork_event)
2571 struct perf_counter *counter;
2573 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2577 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2578 if (perf_counter_fork_match(counter))
2579 perf_counter_fork_output(counter, fork_event);
2584 static void perf_counter_fork_event(struct perf_fork_event *fork_event)
2586 struct perf_cpu_context *cpuctx;
2587 struct perf_counter_context *ctx;
2589 cpuctx = &get_cpu_var(perf_cpu_context);
2590 perf_counter_fork_ctx(&cpuctx->ctx, fork_event);
2591 put_cpu_var(perf_cpu_context);
2595 * doesn't really matter which of the child contexts the
2596 * events ends up in.
2598 ctx = rcu_dereference(current->perf_counter_ctxp);
2600 perf_counter_fork_ctx(ctx, fork_event);
2604 void perf_counter_fork(struct task_struct *task)
2606 struct perf_fork_event fork_event;
2608 if (!atomic_read(&nr_comm_counters) &&
2609 !atomic_read(&nr_mmap_counters))
2612 fork_event = (struct perf_fork_event){
2616 .type = PERF_EVENT_FORK,
2617 .size = sizeof(fork_event.event),
2622 perf_counter_fork_event(&fork_event);
2629 struct perf_comm_event {
2630 struct task_struct *task;
2635 struct perf_event_header header;
2642 static void perf_counter_comm_output(struct perf_counter *counter,
2643 struct perf_comm_event *comm_event)
2645 struct perf_output_handle handle;
2646 int size = comm_event->event.header.size;
2647 int ret = perf_output_begin(&handle, counter, size, 0, 0);
2652 comm_event->event.pid = perf_counter_pid(counter, comm_event->task);
2653 comm_event->event.tid = perf_counter_tid(counter, comm_event->task);
2655 perf_output_put(&handle, comm_event->event);
2656 perf_output_copy(&handle, comm_event->comm,
2657 comm_event->comm_size);
2658 perf_output_end(&handle);
2661 static int perf_counter_comm_match(struct perf_counter *counter)
2663 if (counter->attr.comm)
2669 static void perf_counter_comm_ctx(struct perf_counter_context *ctx,
2670 struct perf_comm_event *comm_event)
2672 struct perf_counter *counter;
2674 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2678 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2679 if (perf_counter_comm_match(counter))
2680 perf_counter_comm_output(counter, comm_event);
2685 static void perf_counter_comm_event(struct perf_comm_event *comm_event)
2687 struct perf_cpu_context *cpuctx;
2688 struct perf_counter_context *ctx;
2690 char *comm = comm_event->task->comm;
2692 size = ALIGN(strlen(comm)+1, sizeof(u64));
2694 comm_event->comm = comm;
2695 comm_event->comm_size = size;
2697 comm_event->event.header.size = sizeof(comm_event->event) + size;
2699 cpuctx = &get_cpu_var(perf_cpu_context);
2700 perf_counter_comm_ctx(&cpuctx->ctx, comm_event);
2701 put_cpu_var(perf_cpu_context);
2705 * doesn't really matter which of the child contexts the
2706 * events ends up in.
2708 ctx = rcu_dereference(current->perf_counter_ctxp);
2710 perf_counter_comm_ctx(ctx, comm_event);
2714 void perf_counter_comm(struct task_struct *task)
2716 struct perf_comm_event comm_event;
2718 if (!atomic_read(&nr_comm_counters))
2721 comm_event = (struct perf_comm_event){
2724 .header = { .type = PERF_EVENT_COMM, },
2728 perf_counter_comm_event(&comm_event);
2735 struct perf_mmap_event {
2736 struct vm_area_struct *vma;
2738 const char *file_name;
2742 struct perf_event_header header;
2752 static void perf_counter_mmap_output(struct perf_counter *counter,
2753 struct perf_mmap_event *mmap_event)
2755 struct perf_output_handle handle;
2756 int size = mmap_event->event.header.size;
2757 int ret = perf_output_begin(&handle, counter, size, 0, 0);
2762 mmap_event->event.pid = perf_counter_pid(counter, current);
2763 mmap_event->event.tid = perf_counter_tid(counter, current);
2765 perf_output_put(&handle, mmap_event->event);
2766 perf_output_copy(&handle, mmap_event->file_name,
2767 mmap_event->file_size);
2768 perf_output_end(&handle);
2771 static int perf_counter_mmap_match(struct perf_counter *counter,
2772 struct perf_mmap_event *mmap_event)
2774 if (counter->attr.mmap)
2780 static void perf_counter_mmap_ctx(struct perf_counter_context *ctx,
2781 struct perf_mmap_event *mmap_event)
2783 struct perf_counter *counter;
2785 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2789 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2790 if (perf_counter_mmap_match(counter, mmap_event))
2791 perf_counter_mmap_output(counter, mmap_event);
2796 static void perf_counter_mmap_event(struct perf_mmap_event *mmap_event)
2798 struct perf_cpu_context *cpuctx;
2799 struct perf_counter_context *ctx;
2800 struct vm_area_struct *vma = mmap_event->vma;
2801 struct file *file = vma->vm_file;
2808 buf = kzalloc(PATH_MAX, GFP_KERNEL);
2810 name = strncpy(tmp, "//enomem", sizeof(tmp));
2813 name = d_path(&file->f_path, buf, PATH_MAX);
2815 name = strncpy(tmp, "//toolong", sizeof(tmp));
2819 name = arch_vma_name(mmap_event->vma);
2824 name = strncpy(tmp, "[vdso]", sizeof(tmp));
2828 name = strncpy(tmp, "//anon", sizeof(tmp));
2833 size = ALIGN(strlen(name)+1, sizeof(u64));
2835 mmap_event->file_name = name;
2836 mmap_event->file_size = size;
2838 mmap_event->event.header.size = sizeof(mmap_event->event) + size;
2840 cpuctx = &get_cpu_var(perf_cpu_context);
2841 perf_counter_mmap_ctx(&cpuctx->ctx, mmap_event);
2842 put_cpu_var(perf_cpu_context);
2846 * doesn't really matter which of the child contexts the
2847 * events ends up in.
2849 ctx = rcu_dereference(current->perf_counter_ctxp);
2851 perf_counter_mmap_ctx(ctx, mmap_event);
2857 void __perf_counter_mmap(struct vm_area_struct *vma)
2859 struct perf_mmap_event mmap_event;
2861 if (!atomic_read(&nr_mmap_counters))
2864 mmap_event = (struct perf_mmap_event){
2867 .header = { .type = PERF_EVENT_MMAP, },
2868 .start = vma->vm_start,
2869 .len = vma->vm_end - vma->vm_start,
2870 .pgoff = vma->vm_pgoff,
2874 perf_counter_mmap_event(&mmap_event);
2878 * Log sample_period changes so that analyzing tools can re-normalize the
2883 struct perf_event_header header;
2889 static void perf_log_period(struct perf_counter *counter, u64 period)
2891 struct perf_output_handle handle;
2892 struct freq_event event;
2895 if (counter->hw.sample_period == period)
2898 if (counter->attr.sample_type & PERF_SAMPLE_PERIOD)
2901 event = (struct freq_event) {
2903 .type = PERF_EVENT_PERIOD,
2905 .size = sizeof(event),
2907 .time = sched_clock(),
2912 ret = perf_output_begin(&handle, counter, sizeof(event), 1, 0);
2916 perf_output_put(&handle, event);
2917 perf_output_end(&handle);
2921 * IRQ throttle logging
2924 static void perf_log_throttle(struct perf_counter *counter, int enable)
2926 struct perf_output_handle handle;
2930 struct perf_event_header header;
2932 } throttle_event = {
2934 .type = PERF_EVENT_THROTTLE + 1,
2936 .size = sizeof(throttle_event),
2938 .time = sched_clock(),
2941 ret = perf_output_begin(&handle, counter, sizeof(throttle_event), 1, 0);
2945 perf_output_put(&handle, throttle_event);
2946 perf_output_end(&handle);
2950 * Generic counter overflow handling.
2953 int perf_counter_overflow(struct perf_counter *counter,
2954 int nmi, struct pt_regs *regs, u64 addr)
2956 int events = atomic_read(&counter->event_limit);
2957 int throttle = counter->pmu->unthrottle != NULL;
2958 struct hw_perf_counter *hwc = &counter->hw;
2964 if (hwc->interrupts != MAX_INTERRUPTS) {
2966 if (HZ * hwc->interrupts > (u64)sysctl_perf_counter_limit) {
2967 hwc->interrupts = MAX_INTERRUPTS;
2968 perf_log_throttle(counter, 0);
2973 * Keep re-disabling counters even though on the previous
2974 * pass we disabled it - just in case we raced with a
2975 * sched-in and the counter got enabled again:
2981 if (counter->attr.freq) {
2982 u64 now = sched_clock();
2983 s64 delta = now - hwc->freq_stamp;
2985 hwc->freq_stamp = now;
2987 if (delta > 0 && delta < TICK_NSEC)
2988 perf_adjust_period(counter, NSEC_PER_SEC / (int)delta);
2992 * XXX event_limit might not quite work as expected on inherited
2996 counter->pending_kill = POLL_IN;
2997 if (events && atomic_dec_and_test(&counter->event_limit)) {
2999 counter->pending_kill = POLL_HUP;
3001 counter->pending_disable = 1;
3002 perf_pending_queue(&counter->pending,
3003 perf_pending_counter);
3005 perf_counter_disable(counter);
3008 perf_counter_output(counter, nmi, regs, addr);
3013 * Generic software counter infrastructure
3016 static void perf_swcounter_update(struct perf_counter *counter)
3018 struct hw_perf_counter *hwc = &counter->hw;
3023 prev = atomic64_read(&hwc->prev_count);
3024 now = atomic64_read(&hwc->count);
3025 if (atomic64_cmpxchg(&hwc->prev_count, prev, now) != prev)
3030 atomic64_add(delta, &counter->count);
3031 atomic64_sub(delta, &hwc->period_left);
3034 static void perf_swcounter_set_period(struct perf_counter *counter)
3036 struct hw_perf_counter *hwc = &counter->hw;
3037 s64 left = atomic64_read(&hwc->period_left);
3038 s64 period = hwc->sample_period;
3040 if (unlikely(left <= -period)) {
3042 atomic64_set(&hwc->period_left, left);
3045 if (unlikely(left <= 0)) {
3047 atomic64_add(period, &hwc->period_left);
3050 atomic64_set(&hwc->prev_count, -left);
3051 atomic64_set(&hwc->count, -left);
3054 static enum hrtimer_restart perf_swcounter_hrtimer(struct hrtimer *hrtimer)
3056 enum hrtimer_restart ret = HRTIMER_RESTART;
3057 struct perf_counter *counter;
3058 struct pt_regs *regs;
3061 counter = container_of(hrtimer, struct perf_counter, hw.hrtimer);
3062 counter->pmu->read(counter);
3064 regs = get_irq_regs();
3066 * In case we exclude kernel IPs or are somehow not in interrupt
3067 * context, provide the next best thing, the user IP.
3069 if ((counter->attr.exclude_kernel || !regs) &&
3070 !counter->attr.exclude_user)
3071 regs = task_pt_regs(current);
3074 if (perf_counter_overflow(counter, 0, regs, 0))
3075 ret = HRTIMER_NORESTART;
3078 period = max_t(u64, 10000, counter->hw.sample_period);
3079 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
3084 static void perf_swcounter_overflow(struct perf_counter *counter,
3085 int nmi, struct pt_regs *regs, u64 addr)
3087 perf_swcounter_update(counter);
3088 perf_swcounter_set_period(counter);
3089 if (perf_counter_overflow(counter, nmi, regs, addr))
3090 /* soft-disable the counter */
3095 static int perf_swcounter_is_counting(struct perf_counter *counter)
3097 struct perf_counter_context *ctx;
3098 unsigned long flags;
3101 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
3104 if (counter->state != PERF_COUNTER_STATE_INACTIVE)
3108 * If the counter is inactive, it could be just because
3109 * its task is scheduled out, or because it's in a group
3110 * which could not go on the PMU. We want to count in
3111 * the first case but not the second. If the context is
3112 * currently active then an inactive software counter must
3113 * be the second case. If it's not currently active then
3114 * we need to know whether the counter was active when the
3115 * context was last active, which we can determine by
3116 * comparing counter->tstamp_stopped with ctx->time.
3118 * We are within an RCU read-side critical section,
3119 * which protects the existence of *ctx.
3122 spin_lock_irqsave(&ctx->lock, flags);
3124 /* Re-check state now we have the lock */
3125 if (counter->state < PERF_COUNTER_STATE_INACTIVE ||
3126 counter->ctx->is_active ||
3127 counter->tstamp_stopped < ctx->time)
3129 spin_unlock_irqrestore(&ctx->lock, flags);
3133 static int perf_swcounter_match(struct perf_counter *counter,
3134 enum perf_event_types type,
3135 u32 event, struct pt_regs *regs)
3137 if (!perf_swcounter_is_counting(counter))
3140 if (counter->attr.type != type)
3142 if (counter->attr.config != event)
3146 if (counter->attr.exclude_user && user_mode(regs))
3149 if (counter->attr.exclude_kernel && !user_mode(regs))
3156 static void perf_swcounter_add(struct perf_counter *counter, u64 nr,
3157 int nmi, struct pt_regs *regs, u64 addr)
3159 int neg = atomic64_add_negative(nr, &counter->hw.count);
3161 if (counter->hw.sample_period && !neg && regs)
3162 perf_swcounter_overflow(counter, nmi, regs, addr);
3165 static void perf_swcounter_ctx_event(struct perf_counter_context *ctx,
3166 enum perf_event_types type, u32 event,
3167 u64 nr, int nmi, struct pt_regs *regs,
3170 struct perf_counter *counter;
3172 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
3176 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
3177 if (perf_swcounter_match(counter, type, event, regs))
3178 perf_swcounter_add(counter, nr, nmi, regs, addr);
3183 static int *perf_swcounter_recursion_context(struct perf_cpu_context *cpuctx)
3186 return &cpuctx->recursion[3];
3189 return &cpuctx->recursion[2];
3192 return &cpuctx->recursion[1];
3194 return &cpuctx->recursion[0];
3197 static void __perf_swcounter_event(enum perf_event_types type, u32 event,
3198 u64 nr, int nmi, struct pt_regs *regs,
3201 struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
3202 int *recursion = perf_swcounter_recursion_context(cpuctx);
3203 struct perf_counter_context *ctx;
3211 perf_swcounter_ctx_event(&cpuctx->ctx, type, event,
3212 nr, nmi, regs, addr);
3215 * doesn't really matter which of the child contexts the
3216 * events ends up in.
3218 ctx = rcu_dereference(current->perf_counter_ctxp);
3220 perf_swcounter_ctx_event(ctx, type, event, nr, nmi, regs, addr);
3227 put_cpu_var(perf_cpu_context);
3231 perf_swcounter_event(u32 event, u64 nr, int nmi, struct pt_regs *regs, u64 addr)
3233 __perf_swcounter_event(PERF_TYPE_SOFTWARE, event, nr, nmi, regs, addr);
3236 static void perf_swcounter_read(struct perf_counter *counter)
3238 perf_swcounter_update(counter);
3241 static int perf_swcounter_enable(struct perf_counter *counter)
3243 perf_swcounter_set_period(counter);
3247 static void perf_swcounter_disable(struct perf_counter *counter)
3249 perf_swcounter_update(counter);
3252 static const struct pmu perf_ops_generic = {
3253 .enable = perf_swcounter_enable,
3254 .disable = perf_swcounter_disable,
3255 .read = perf_swcounter_read,
3259 * Software counter: cpu wall time clock
3262 static void cpu_clock_perf_counter_update(struct perf_counter *counter)
3264 int cpu = raw_smp_processor_id();
3268 now = cpu_clock(cpu);
3269 prev = atomic64_read(&counter->hw.prev_count);
3270 atomic64_set(&counter->hw.prev_count, now);
3271 atomic64_add(now - prev, &counter->count);
3274 static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
3276 struct hw_perf_counter *hwc = &counter->hw;
3277 int cpu = raw_smp_processor_id();
3279 atomic64_set(&hwc->prev_count, cpu_clock(cpu));
3280 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3281 hwc->hrtimer.function = perf_swcounter_hrtimer;
3282 if (hwc->sample_period) {
3283 u64 period = max_t(u64, 10000, hwc->sample_period);
3284 __hrtimer_start_range_ns(&hwc->hrtimer,
3285 ns_to_ktime(period), 0,
3286 HRTIMER_MODE_REL, 0);
3292 static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
3294 if (counter->hw.sample_period)
3295 hrtimer_cancel(&counter->hw.hrtimer);
3296 cpu_clock_perf_counter_update(counter);
3299 static void cpu_clock_perf_counter_read(struct perf_counter *counter)
3301 cpu_clock_perf_counter_update(counter);
3304 static const struct pmu perf_ops_cpu_clock = {
3305 .enable = cpu_clock_perf_counter_enable,
3306 .disable = cpu_clock_perf_counter_disable,
3307 .read = cpu_clock_perf_counter_read,
3311 * Software counter: task time clock
3314 static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
3319 prev = atomic64_xchg(&counter->hw.prev_count, now);
3321 atomic64_add(delta, &counter->count);
3324 static int task_clock_perf_counter_enable(struct perf_counter *counter)
3326 struct hw_perf_counter *hwc = &counter->hw;
3329 now = counter->ctx->time;
3331 atomic64_set(&hwc->prev_count, now);
3332 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3333 hwc->hrtimer.function = perf_swcounter_hrtimer;
3334 if (hwc->sample_period) {
3335 u64 period = max_t(u64, 10000, hwc->sample_period);
3336 __hrtimer_start_range_ns(&hwc->hrtimer,
3337 ns_to_ktime(period), 0,
3338 HRTIMER_MODE_REL, 0);
3344 static void task_clock_perf_counter_disable(struct perf_counter *counter)
3346 if (counter->hw.sample_period)
3347 hrtimer_cancel(&counter->hw.hrtimer);
3348 task_clock_perf_counter_update(counter, counter->ctx->time);
3352 static void task_clock_perf_counter_read(struct perf_counter *counter)
3357 update_context_time(counter->ctx);
3358 time = counter->ctx->time;
3360 u64 now = perf_clock();
3361 u64 delta = now - counter->ctx->timestamp;
3362 time = counter->ctx->time + delta;
3365 task_clock_perf_counter_update(counter, time);
3368 static const struct pmu perf_ops_task_clock = {
3369 .enable = task_clock_perf_counter_enable,
3370 .disable = task_clock_perf_counter_disable,
3371 .read = task_clock_perf_counter_read,
3375 * Software counter: cpu migrations
3377 void perf_counter_task_migration(struct task_struct *task, int cpu)
3379 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
3380 struct perf_counter_context *ctx;
3382 perf_swcounter_ctx_event(&cpuctx->ctx, PERF_TYPE_SOFTWARE,
3383 PERF_COUNT_CPU_MIGRATIONS,
3386 ctx = perf_pin_task_context(task);
3388 perf_swcounter_ctx_event(ctx, PERF_TYPE_SOFTWARE,
3389 PERF_COUNT_CPU_MIGRATIONS,
3391 perf_unpin_context(ctx);
3395 #ifdef CONFIG_EVENT_PROFILE
3396 void perf_tpcounter_event(int event_id)
3398 struct pt_regs *regs = get_irq_regs();
3401 regs = task_pt_regs(current);
3403 __perf_swcounter_event(PERF_TYPE_TRACEPOINT, event_id, 1, 1, regs, 0);
3405 EXPORT_SYMBOL_GPL(perf_tpcounter_event);
3407 extern int ftrace_profile_enable(int);
3408 extern void ftrace_profile_disable(int);
3410 static void tp_perf_counter_destroy(struct perf_counter *counter)
3412 ftrace_profile_disable(perf_event_id(&counter->attr));
3415 static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
3417 int event_id = perf_event_id(&counter->attr);
3420 ret = ftrace_profile_enable(event_id);
3424 counter->destroy = tp_perf_counter_destroy;
3426 return &perf_ops_generic;
3429 static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
3435 static const struct pmu *sw_perf_counter_init(struct perf_counter *counter)
3437 const struct pmu *pmu = NULL;
3440 * Software counters (currently) can't in general distinguish
3441 * between user, kernel and hypervisor events.
3442 * However, context switches and cpu migrations are considered
3443 * to be kernel events, and page faults are never hypervisor
3446 switch (counter->attr.config) {
3447 case PERF_COUNT_CPU_CLOCK:
3448 pmu = &perf_ops_cpu_clock;
3451 case PERF_COUNT_TASK_CLOCK:
3453 * If the user instantiates this as a per-cpu counter,
3454 * use the cpu_clock counter instead.
3456 if (counter->ctx->task)
3457 pmu = &perf_ops_task_clock;
3459 pmu = &perf_ops_cpu_clock;
3462 case PERF_COUNT_PAGE_FAULTS:
3463 case PERF_COUNT_PAGE_FAULTS_MIN:
3464 case PERF_COUNT_PAGE_FAULTS_MAJ:
3465 case PERF_COUNT_CONTEXT_SWITCHES:
3466 case PERF_COUNT_CPU_MIGRATIONS:
3467 pmu = &perf_ops_generic;
3475 * Allocate and initialize a counter structure
3477 static struct perf_counter *
3478 perf_counter_alloc(struct perf_counter_attr *attr,
3480 struct perf_counter_context *ctx,
3481 struct perf_counter *group_leader,
3484 const struct pmu *pmu;
3485 struct perf_counter *counter;
3486 struct hw_perf_counter *hwc;
3489 counter = kzalloc(sizeof(*counter), gfpflags);
3491 return ERR_PTR(-ENOMEM);
3494 * Single counters are their own group leaders, with an
3495 * empty sibling list:
3498 group_leader = counter;
3500 mutex_init(&counter->child_mutex);
3501 INIT_LIST_HEAD(&counter->child_list);
3503 INIT_LIST_HEAD(&counter->list_entry);
3504 INIT_LIST_HEAD(&counter->event_entry);
3505 INIT_LIST_HEAD(&counter->sibling_list);
3506 init_waitqueue_head(&counter->waitq);
3508 mutex_init(&counter->mmap_mutex);
3511 counter->attr = *attr;
3512 counter->group_leader = group_leader;
3513 counter->pmu = NULL;
3515 counter->oncpu = -1;
3517 counter->ns = get_pid_ns(current->nsproxy->pid_ns);
3518 counter->id = atomic64_inc_return(&perf_counter_id);
3520 counter->state = PERF_COUNTER_STATE_INACTIVE;
3523 counter->state = PERF_COUNTER_STATE_OFF;
3528 hwc->sample_period = attr->sample_period;
3529 if (attr->freq && attr->sample_freq)
3530 hwc->sample_period = 1;
3532 atomic64_set(&hwc->period_left, hwc->sample_period);
3535 * we currently do not support PERF_SAMPLE_GROUP on inherited counters
3537 if (attr->inherit && (attr->sample_type & PERF_SAMPLE_GROUP))
3540 if (attr->type == PERF_TYPE_RAW) {
3541 pmu = hw_perf_counter_init(counter);
3545 switch (attr->type) {
3546 case PERF_TYPE_HARDWARE:
3547 case PERF_TYPE_HW_CACHE:
3548 pmu = hw_perf_counter_init(counter);
3551 case PERF_TYPE_SOFTWARE:
3552 pmu = sw_perf_counter_init(counter);
3555 case PERF_TYPE_TRACEPOINT:
3556 pmu = tp_perf_counter_init(counter);
3563 else if (IS_ERR(pmu))
3568 put_pid_ns(counter->ns);
3570 return ERR_PTR(err);
3575 atomic_inc(&nr_counters);
3576 if (counter->attr.mmap)
3577 atomic_inc(&nr_mmap_counters);
3578 if (counter->attr.comm)
3579 atomic_inc(&nr_comm_counters);
3585 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
3587 * @attr_uptr: event type attributes for monitoring/sampling
3590 * @group_fd: group leader counter fd
3592 SYSCALL_DEFINE5(perf_counter_open,
3593 const struct perf_counter_attr __user *, attr_uptr,
3594 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
3596 struct perf_counter *counter, *group_leader;
3597 struct perf_counter_attr attr;
3598 struct perf_counter_context *ctx;
3599 struct file *counter_file = NULL;
3600 struct file *group_file = NULL;
3601 int fput_needed = 0;
3602 int fput_needed2 = 0;
3605 /* for future expandability... */
3609 if (copy_from_user(&attr, attr_uptr, sizeof(attr)) != 0)
3613 * Get the target context (task or percpu):
3615 ctx = find_get_context(pid, cpu);
3617 return PTR_ERR(ctx);
3620 * Look up the group leader (we will attach this counter to it):
3622 group_leader = NULL;
3623 if (group_fd != -1) {
3625 group_file = fget_light(group_fd, &fput_needed);
3627 goto err_put_context;
3628 if (group_file->f_op != &perf_fops)
3629 goto err_put_context;
3631 group_leader = group_file->private_data;
3633 * Do not allow a recursive hierarchy (this new sibling
3634 * becoming part of another group-sibling):
3636 if (group_leader->group_leader != group_leader)
3637 goto err_put_context;
3639 * Do not allow to attach to a group in a different
3640 * task or CPU context:
3642 if (group_leader->ctx != ctx)
3643 goto err_put_context;
3645 * Only a group leader can be exclusive or pinned
3647 if (attr.exclusive || attr.pinned)
3648 goto err_put_context;
3651 counter = perf_counter_alloc(&attr, cpu, ctx, group_leader,
3653 ret = PTR_ERR(counter);
3654 if (IS_ERR(counter))
3655 goto err_put_context;
3657 ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
3659 goto err_free_put_context;
3661 counter_file = fget_light(ret, &fput_needed2);
3663 goto err_free_put_context;
3665 counter->filp = counter_file;
3666 WARN_ON_ONCE(ctx->parent_ctx);
3667 mutex_lock(&ctx->mutex);
3668 perf_install_in_context(ctx, counter, cpu);
3670 mutex_unlock(&ctx->mutex);
3672 counter->owner = current;
3673 get_task_struct(current);
3674 mutex_lock(¤t->perf_counter_mutex);
3675 list_add_tail(&counter->owner_entry, ¤t->perf_counter_list);
3676 mutex_unlock(¤t->perf_counter_mutex);
3678 fput_light(counter_file, fput_needed2);
3681 fput_light(group_file, fput_needed);
3685 err_free_put_context:
3695 * inherit a counter from parent task to child task:
3697 static struct perf_counter *
3698 inherit_counter(struct perf_counter *parent_counter,
3699 struct task_struct *parent,
3700 struct perf_counter_context *parent_ctx,
3701 struct task_struct *child,
3702 struct perf_counter *group_leader,
3703 struct perf_counter_context *child_ctx)
3705 struct perf_counter *child_counter;
3708 * Instead of creating recursive hierarchies of counters,
3709 * we link inherited counters back to the original parent,
3710 * which has a filp for sure, which we use as the reference
3713 if (parent_counter->parent)
3714 parent_counter = parent_counter->parent;
3716 child_counter = perf_counter_alloc(&parent_counter->attr,
3717 parent_counter->cpu, child_ctx,
3718 group_leader, GFP_KERNEL);
3719 if (IS_ERR(child_counter))
3720 return child_counter;
3724 * Make the child state follow the state of the parent counter,
3725 * not its attr.disabled bit. We hold the parent's mutex,
3726 * so we won't race with perf_counter_{en, dis}able_family.
3728 if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE)
3729 child_counter->state = PERF_COUNTER_STATE_INACTIVE;
3731 child_counter->state = PERF_COUNTER_STATE_OFF;
3733 if (parent_counter->attr.freq)
3734 child_counter->hw.sample_period = parent_counter->hw.sample_period;
3737 * Link it up in the child's context:
3739 add_counter_to_ctx(child_counter, child_ctx);
3741 child_counter->parent = parent_counter;
3743 * inherit into child's child as well:
3745 child_counter->attr.inherit = 1;
3748 * Get a reference to the parent filp - we will fput it
3749 * when the child counter exits. This is safe to do because
3750 * we are in the parent and we know that the filp still
3751 * exists and has a nonzero count:
3753 atomic_long_inc(&parent_counter->filp->f_count);
3756 * Link this into the parent counter's child list
3758 WARN_ON_ONCE(parent_counter->ctx->parent_ctx);
3759 mutex_lock(&parent_counter->child_mutex);
3760 list_add_tail(&child_counter->child_list, &parent_counter->child_list);
3761 mutex_unlock(&parent_counter->child_mutex);
3763 return child_counter;
3766 static int inherit_group(struct perf_counter *parent_counter,
3767 struct task_struct *parent,
3768 struct perf_counter_context *parent_ctx,
3769 struct task_struct *child,
3770 struct perf_counter_context *child_ctx)
3772 struct perf_counter *leader;
3773 struct perf_counter *sub;
3774 struct perf_counter *child_ctr;
3776 leader = inherit_counter(parent_counter, parent, parent_ctx,
3777 child, NULL, child_ctx);
3779 return PTR_ERR(leader);
3780 list_for_each_entry(sub, &parent_counter->sibling_list, list_entry) {
3781 child_ctr = inherit_counter(sub, parent, parent_ctx,
3782 child, leader, child_ctx);
3783 if (IS_ERR(child_ctr))
3784 return PTR_ERR(child_ctr);
3789 static void sync_child_counter(struct perf_counter *child_counter,
3790 struct perf_counter *parent_counter)
3794 child_val = atomic64_read(&child_counter->count);
3797 * Add back the child's count to the parent's count:
3799 atomic64_add(child_val, &parent_counter->count);
3800 atomic64_add(child_counter->total_time_enabled,
3801 &parent_counter->child_total_time_enabled);
3802 atomic64_add(child_counter->total_time_running,
3803 &parent_counter->child_total_time_running);
3806 * Remove this counter from the parent's list
3808 WARN_ON_ONCE(parent_counter->ctx->parent_ctx);
3809 mutex_lock(&parent_counter->child_mutex);
3810 list_del_init(&child_counter->child_list);
3811 mutex_unlock(&parent_counter->child_mutex);
3814 * Release the parent counter, if this was the last
3817 fput(parent_counter->filp);
3821 __perf_counter_exit_task(struct perf_counter *child_counter,
3822 struct perf_counter_context *child_ctx)
3824 struct perf_counter *parent_counter;
3826 update_counter_times(child_counter);
3827 perf_counter_remove_from_context(child_counter);
3829 parent_counter = child_counter->parent;
3831 * It can happen that parent exits first, and has counters
3832 * that are still around due to the child reference. These
3833 * counters need to be zapped - but otherwise linger.
3835 if (parent_counter) {
3836 sync_child_counter(child_counter, parent_counter);
3837 free_counter(child_counter);
3842 * When a child task exits, feed back counter values to parent counters.
3844 void perf_counter_exit_task(struct task_struct *child)
3846 struct perf_counter *child_counter, *tmp;
3847 struct perf_counter_context *child_ctx;
3848 unsigned long flags;
3850 if (likely(!child->perf_counter_ctxp))
3853 local_irq_save(flags);
3855 * We can't reschedule here because interrupts are disabled,
3856 * and either child is current or it is a task that can't be
3857 * scheduled, so we are now safe from rescheduling changing
3860 child_ctx = child->perf_counter_ctxp;
3861 __perf_counter_task_sched_out(child_ctx);
3864 * Take the context lock here so that if find_get_context is
3865 * reading child->perf_counter_ctxp, we wait until it has
3866 * incremented the context's refcount before we do put_ctx below.
3868 spin_lock(&child_ctx->lock);
3869 child->perf_counter_ctxp = NULL;
3870 if (child_ctx->parent_ctx) {
3872 * This context is a clone; unclone it so it can't get
3873 * swapped to another process while we're removing all
3874 * the counters from it.
3876 put_ctx(child_ctx->parent_ctx);
3877 child_ctx->parent_ctx = NULL;
3879 spin_unlock(&child_ctx->lock);
3880 local_irq_restore(flags);
3883 * We can recurse on the same lock type through:
3885 * __perf_counter_exit_task()
3886 * sync_child_counter()
3887 * fput(parent_counter->filp)
3889 * mutex_lock(&ctx->mutex)
3891 * But since its the parent context it won't be the same instance.
3893 mutex_lock_nested(&child_ctx->mutex, SINGLE_DEPTH_NESTING);
3896 list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
3898 __perf_counter_exit_task(child_counter, child_ctx);
3901 * If the last counter was a group counter, it will have appended all
3902 * its siblings to the list, but we obtained 'tmp' before that which
3903 * will still point to the list head terminating the iteration.
3905 if (!list_empty(&child_ctx->counter_list))
3908 mutex_unlock(&child_ctx->mutex);
3914 * free an unexposed, unused context as created by inheritance by
3915 * init_task below, used by fork() in case of fail.
3917 void perf_counter_free_task(struct task_struct *task)
3919 struct perf_counter_context *ctx = task->perf_counter_ctxp;
3920 struct perf_counter *counter, *tmp;
3925 mutex_lock(&ctx->mutex);
3927 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry) {
3928 struct perf_counter *parent = counter->parent;
3930 if (WARN_ON_ONCE(!parent))
3933 mutex_lock(&parent->child_mutex);
3934 list_del_init(&counter->child_list);
3935 mutex_unlock(&parent->child_mutex);
3939 list_del_counter(counter, ctx);
3940 free_counter(counter);
3943 if (!list_empty(&ctx->counter_list))
3946 mutex_unlock(&ctx->mutex);
3952 * Initialize the perf_counter context in task_struct
3954 int perf_counter_init_task(struct task_struct *child)
3956 struct perf_counter_context *child_ctx, *parent_ctx;
3957 struct perf_counter_context *cloned_ctx;
3958 struct perf_counter *counter;
3959 struct task_struct *parent = current;
3960 int inherited_all = 1;
3963 child->perf_counter_ctxp = NULL;
3965 mutex_init(&child->perf_counter_mutex);
3966 INIT_LIST_HEAD(&child->perf_counter_list);
3968 if (likely(!parent->perf_counter_ctxp))
3972 * This is executed from the parent task context, so inherit
3973 * counters that have been marked for cloning.
3974 * First allocate and initialize a context for the child.
3977 child_ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
3981 __perf_counter_init_context(child_ctx, child);
3982 child->perf_counter_ctxp = child_ctx;
3983 get_task_struct(child);
3986 * If the parent's context is a clone, pin it so it won't get
3989 parent_ctx = perf_pin_task_context(parent);
3992 * No need to check if parent_ctx != NULL here; since we saw
3993 * it non-NULL earlier, the only reason for it to become NULL
3994 * is if we exit, and since we're currently in the middle of
3995 * a fork we can't be exiting at the same time.
3999 * Lock the parent list. No need to lock the child - not PID
4000 * hashed yet and not running, so nobody can access it.
4002 mutex_lock(&parent_ctx->mutex);
4005 * We dont have to disable NMIs - we are only looking at
4006 * the list, not manipulating it:
4008 list_for_each_entry_rcu(counter, &parent_ctx->event_list, event_entry) {
4009 if (counter != counter->group_leader)
4012 if (!counter->attr.inherit) {
4017 ret = inherit_group(counter, parent, parent_ctx,
4025 if (inherited_all) {
4027 * Mark the child context as a clone of the parent
4028 * context, or of whatever the parent is a clone of.
4029 * Note that if the parent is a clone, it could get
4030 * uncloned at any point, but that doesn't matter
4031 * because the list of counters and the generation
4032 * count can't have changed since we took the mutex.
4034 cloned_ctx = rcu_dereference(parent_ctx->parent_ctx);
4036 child_ctx->parent_ctx = cloned_ctx;
4037 child_ctx->parent_gen = parent_ctx->parent_gen;
4039 child_ctx->parent_ctx = parent_ctx;
4040 child_ctx->parent_gen = parent_ctx->generation;
4042 get_ctx(child_ctx->parent_ctx);
4045 mutex_unlock(&parent_ctx->mutex);
4047 perf_unpin_context(parent_ctx);
4052 static void __cpuinit perf_counter_init_cpu(int cpu)
4054 struct perf_cpu_context *cpuctx;
4056 cpuctx = &per_cpu(perf_cpu_context, cpu);
4057 __perf_counter_init_context(&cpuctx->ctx, NULL);
4059 spin_lock(&perf_resource_lock);
4060 cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
4061 spin_unlock(&perf_resource_lock);
4063 hw_perf_counter_setup(cpu);
4066 #ifdef CONFIG_HOTPLUG_CPU
4067 static void __perf_counter_exit_cpu(void *info)
4069 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
4070 struct perf_counter_context *ctx = &cpuctx->ctx;
4071 struct perf_counter *counter, *tmp;
4073 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
4074 __perf_counter_remove_from_context(counter);
4076 static void perf_counter_exit_cpu(int cpu)
4078 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
4079 struct perf_counter_context *ctx = &cpuctx->ctx;
4081 mutex_lock(&ctx->mutex);
4082 smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
4083 mutex_unlock(&ctx->mutex);
4086 static inline void perf_counter_exit_cpu(int cpu) { }
4089 static int __cpuinit
4090 perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
4092 unsigned int cpu = (long)hcpu;
4096 case CPU_UP_PREPARE:
4097 case CPU_UP_PREPARE_FROZEN:
4098 perf_counter_init_cpu(cpu);
4101 case CPU_DOWN_PREPARE:
4102 case CPU_DOWN_PREPARE_FROZEN:
4103 perf_counter_exit_cpu(cpu);
4114 * This has to have a higher priority than migration_notifier in sched.c.
4116 static struct notifier_block __cpuinitdata perf_cpu_nb = {
4117 .notifier_call = perf_cpu_notify,
4121 void __init perf_counter_init(void)
4123 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
4124 (void *)(long)smp_processor_id());
4125 register_cpu_notifier(&perf_cpu_nb);
4128 static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
4130 return sprintf(buf, "%d\n", perf_reserved_percpu);
4134 perf_set_reserve_percpu(struct sysdev_class *class,
4138 struct perf_cpu_context *cpuctx;
4142 err = strict_strtoul(buf, 10, &val);
4145 if (val > perf_max_counters)
4148 spin_lock(&perf_resource_lock);
4149 perf_reserved_percpu = val;
4150 for_each_online_cpu(cpu) {
4151 cpuctx = &per_cpu(perf_cpu_context, cpu);
4152 spin_lock_irq(&cpuctx->ctx.lock);
4153 mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
4154 perf_max_counters - perf_reserved_percpu);
4155 cpuctx->max_pertask = mpt;
4156 spin_unlock_irq(&cpuctx->ctx.lock);
4158 spin_unlock(&perf_resource_lock);
4163 static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
4165 return sprintf(buf, "%d\n", perf_overcommit);
4169 perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
4174 err = strict_strtoul(buf, 10, &val);
4180 spin_lock(&perf_resource_lock);
4181 perf_overcommit = val;
4182 spin_unlock(&perf_resource_lock);
4187 static SYSDEV_CLASS_ATTR(
4190 perf_show_reserve_percpu,
4191 perf_set_reserve_percpu
4194 static SYSDEV_CLASS_ATTR(
4197 perf_show_overcommit,
4201 static struct attribute *perfclass_attrs[] = {
4202 &attr_reserve_percpu.attr,
4203 &attr_overcommit.attr,
4207 static struct attribute_group perfclass_attr_group = {
4208 .attrs = perfclass_attrs,
4209 .name = "perf_counters",
4212 static int __init perf_counter_sysfs_init(void)
4214 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
4215 &perfclass_attr_group);
4217 device_initcall(perf_counter_sysfs_init);