perf_counter: Add scale information to the mmap control page
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / kernel / perf_counter.c
blob23614adab475b6fa7c4dfa90fb78dc1b7f9cd843
1 /*
2 * Performance counter core code
4 * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
5 * Copyright (C) 2008-2009 Red Hat, Inc., Ingo Molnar
6 * Copyright (C) 2008-2009 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
7 * Copyright © 2009 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
9 * For licensing details see kernel-base/COPYING
12 #include <linux/fs.h>
13 #include <linux/mm.h>
14 #include <linux/cpu.h>
15 #include <linux/smp.h>
16 #include <linux/file.h>
17 #include <linux/poll.h>
18 #include <linux/sysfs.h>
19 #include <linux/dcache.h>
20 #include <linux/percpu.h>
21 #include <linux/ptrace.h>
22 #include <linux/vmstat.h>
23 #include <linux/hardirq.h>
24 #include <linux/rculist.h>
25 #include <linux/uaccess.h>
26 #include <linux/syscalls.h>
27 #include <linux/anon_inodes.h>
28 #include <linux/kernel_stat.h>
29 #include <linux/perf_counter.h>
31 #include <asm/irq_regs.h>
34 * Each CPU has a list of per CPU counters:
36 DEFINE_PER_CPU(struct perf_cpu_context, perf_cpu_context);
38 int perf_max_counters __read_mostly = 1;
39 static int perf_reserved_percpu __read_mostly;
40 static int perf_overcommit __read_mostly = 1;
42 static atomic_t nr_counters __read_mostly;
43 static atomic_t nr_mmap_counters __read_mostly;
44 static atomic_t nr_comm_counters __read_mostly;
47 * perf counter paranoia level:
48 * 0 - not paranoid
49 * 1 - disallow cpu counters to unpriv
50 * 2 - disallow kernel profiling to unpriv
52 int sysctl_perf_counter_paranoid __read_mostly;
54 static inline bool perf_paranoid_cpu(void)
56 return sysctl_perf_counter_paranoid > 0;
59 static inline bool perf_paranoid_kernel(void)
61 return sysctl_perf_counter_paranoid > 1;
64 int sysctl_perf_counter_mlock __read_mostly = 512; /* 'free' kb per user */
67 * max perf counter sample rate
69 int sysctl_perf_counter_sample_rate __read_mostly = 100000;
71 static atomic64_t perf_counter_id;
74 * Lock for (sysadmin-configurable) counter reservations:
76 static DEFINE_SPINLOCK(perf_resource_lock);
79 * Architecture provided APIs - weak aliases:
81 extern __weak const struct pmu *hw_perf_counter_init(struct perf_counter *counter)
83 return NULL;
86 void __weak hw_perf_disable(void) { barrier(); }
87 void __weak hw_perf_enable(void) { barrier(); }
89 void __weak hw_perf_counter_setup(int cpu) { barrier(); }
91 int __weak
92 hw_perf_group_sched_in(struct perf_counter *group_leader,
93 struct perf_cpu_context *cpuctx,
94 struct perf_counter_context *ctx, int cpu)
96 return 0;
99 void __weak perf_counter_print_debug(void) { }
101 static DEFINE_PER_CPU(int, disable_count);
103 void __perf_disable(void)
105 __get_cpu_var(disable_count)++;
108 bool __perf_enable(void)
110 return !--__get_cpu_var(disable_count);
113 void perf_disable(void)
115 __perf_disable();
116 hw_perf_disable();
119 void perf_enable(void)
121 if (__perf_enable())
122 hw_perf_enable();
125 static void get_ctx(struct perf_counter_context *ctx)
127 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
130 static void free_ctx(struct rcu_head *head)
132 struct perf_counter_context *ctx;
134 ctx = container_of(head, struct perf_counter_context, rcu_head);
135 kfree(ctx);
138 static void put_ctx(struct perf_counter_context *ctx)
140 if (atomic_dec_and_test(&ctx->refcount)) {
141 if (ctx->parent_ctx)
142 put_ctx(ctx->parent_ctx);
143 if (ctx->task)
144 put_task_struct(ctx->task);
145 call_rcu(&ctx->rcu_head, free_ctx);
150 * Get the perf_counter_context for a task and lock it.
151 * This has to cope with with the fact that until it is locked,
152 * the context could get moved to another task.
154 static struct perf_counter_context *
155 perf_lock_task_context(struct task_struct *task, unsigned long *flags)
157 struct perf_counter_context *ctx;
159 rcu_read_lock();
160 retry:
161 ctx = rcu_dereference(task->perf_counter_ctxp);
162 if (ctx) {
164 * If this context is a clone of another, it might
165 * get swapped for another underneath us by
166 * perf_counter_task_sched_out, though the
167 * rcu_read_lock() protects us from any context
168 * getting freed. Lock the context and check if it
169 * got swapped before we could get the lock, and retry
170 * if so. If we locked the right context, then it
171 * can't get swapped on us any more.
173 spin_lock_irqsave(&ctx->lock, *flags);
174 if (ctx != rcu_dereference(task->perf_counter_ctxp)) {
175 spin_unlock_irqrestore(&ctx->lock, *flags);
176 goto retry;
179 if (!atomic_inc_not_zero(&ctx->refcount)) {
180 spin_unlock_irqrestore(&ctx->lock, *flags);
181 ctx = NULL;
184 rcu_read_unlock();
185 return ctx;
189 * Get the context for a task and increment its pin_count so it
190 * can't get swapped to another task. This also increments its
191 * reference count so that the context can't get freed.
193 static struct perf_counter_context *perf_pin_task_context(struct task_struct *task)
195 struct perf_counter_context *ctx;
196 unsigned long flags;
198 ctx = perf_lock_task_context(task, &flags);
199 if (ctx) {
200 ++ctx->pin_count;
201 spin_unlock_irqrestore(&ctx->lock, flags);
203 return ctx;
206 static void perf_unpin_context(struct perf_counter_context *ctx)
208 unsigned long flags;
210 spin_lock_irqsave(&ctx->lock, flags);
211 --ctx->pin_count;
212 spin_unlock_irqrestore(&ctx->lock, flags);
213 put_ctx(ctx);
217 * Add a counter from the lists for its context.
218 * Must be called with ctx->mutex and ctx->lock held.
220 static void
221 list_add_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
223 struct perf_counter *group_leader = counter->group_leader;
226 * Depending on whether it is a standalone or sibling counter,
227 * add it straight to the context's counter list, or to the group
228 * leader's sibling list:
230 if (group_leader == counter)
231 list_add_tail(&counter->list_entry, &ctx->counter_list);
232 else {
233 list_add_tail(&counter->list_entry, &group_leader->sibling_list);
234 group_leader->nr_siblings++;
237 list_add_rcu(&counter->event_entry, &ctx->event_list);
238 ctx->nr_counters++;
242 * Remove a counter from the lists for its context.
243 * Must be called with ctx->mutex and ctx->lock held.
245 static void
246 list_del_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
248 struct perf_counter *sibling, *tmp;
250 if (list_empty(&counter->list_entry))
251 return;
252 ctx->nr_counters--;
254 list_del_init(&counter->list_entry);
255 list_del_rcu(&counter->event_entry);
257 if (counter->group_leader != counter)
258 counter->group_leader->nr_siblings--;
261 * If this was a group counter with sibling counters then
262 * upgrade the siblings to singleton counters by adding them
263 * to the context list directly:
265 list_for_each_entry_safe(sibling, tmp,
266 &counter->sibling_list, list_entry) {
268 list_move_tail(&sibling->list_entry, &ctx->counter_list);
269 sibling->group_leader = sibling;
273 static void
274 counter_sched_out(struct perf_counter *counter,
275 struct perf_cpu_context *cpuctx,
276 struct perf_counter_context *ctx)
278 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
279 return;
281 counter->state = PERF_COUNTER_STATE_INACTIVE;
282 counter->tstamp_stopped = ctx->time;
283 counter->pmu->disable(counter);
284 counter->oncpu = -1;
286 if (!is_software_counter(counter))
287 cpuctx->active_oncpu--;
288 ctx->nr_active--;
289 if (counter->attr.exclusive || !cpuctx->active_oncpu)
290 cpuctx->exclusive = 0;
293 static void
294 group_sched_out(struct perf_counter *group_counter,
295 struct perf_cpu_context *cpuctx,
296 struct perf_counter_context *ctx)
298 struct perf_counter *counter;
300 if (group_counter->state != PERF_COUNTER_STATE_ACTIVE)
301 return;
303 counter_sched_out(group_counter, cpuctx, ctx);
306 * Schedule out siblings (if any):
308 list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
309 counter_sched_out(counter, cpuctx, ctx);
311 if (group_counter->attr.exclusive)
312 cpuctx->exclusive = 0;
316 * Cross CPU call to remove a performance counter
318 * We disable the counter on the hardware level first. After that we
319 * remove it from the context list.
321 static void __perf_counter_remove_from_context(void *info)
323 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
324 struct perf_counter *counter = info;
325 struct perf_counter_context *ctx = counter->ctx;
328 * If this is a task context, we need to check whether it is
329 * the current task context of this cpu. If not it has been
330 * scheduled out before the smp call arrived.
332 if (ctx->task && cpuctx->task_ctx != ctx)
333 return;
335 spin_lock(&ctx->lock);
337 * Protect the list operation against NMI by disabling the
338 * counters on a global level.
340 perf_disable();
342 counter_sched_out(counter, cpuctx, ctx);
344 list_del_counter(counter, ctx);
346 if (!ctx->task) {
348 * Allow more per task counters with respect to the
349 * reservation:
351 cpuctx->max_pertask =
352 min(perf_max_counters - ctx->nr_counters,
353 perf_max_counters - perf_reserved_percpu);
356 perf_enable();
357 spin_unlock(&ctx->lock);
362 * Remove the counter from a task's (or a CPU's) list of counters.
364 * Must be called with ctx->mutex held.
366 * CPU counters are removed with a smp call. For task counters we only
367 * call when the task is on a CPU.
369 * If counter->ctx is a cloned context, callers must make sure that
370 * every task struct that counter->ctx->task could possibly point to
371 * remains valid. This is OK when called from perf_release since
372 * that only calls us on the top-level context, which can't be a clone.
373 * When called from perf_counter_exit_task, it's OK because the
374 * context has been detached from its task.
376 static void perf_counter_remove_from_context(struct perf_counter *counter)
378 struct perf_counter_context *ctx = counter->ctx;
379 struct task_struct *task = ctx->task;
381 if (!task) {
383 * Per cpu counters are removed via an smp call and
384 * the removal is always sucessful.
386 smp_call_function_single(counter->cpu,
387 __perf_counter_remove_from_context,
388 counter, 1);
389 return;
392 retry:
393 task_oncpu_function_call(task, __perf_counter_remove_from_context,
394 counter);
396 spin_lock_irq(&ctx->lock);
398 * If the context is active we need to retry the smp call.
400 if (ctx->nr_active && !list_empty(&counter->list_entry)) {
401 spin_unlock_irq(&ctx->lock);
402 goto retry;
406 * The lock prevents that this context is scheduled in so we
407 * can remove the counter safely, if the call above did not
408 * succeed.
410 if (!list_empty(&counter->list_entry)) {
411 list_del_counter(counter, ctx);
413 spin_unlock_irq(&ctx->lock);
416 static inline u64 perf_clock(void)
418 return cpu_clock(smp_processor_id());
422 * Update the record of the current time in a context.
424 static void update_context_time(struct perf_counter_context *ctx)
426 u64 now = perf_clock();
428 ctx->time += now - ctx->timestamp;
429 ctx->timestamp = now;
433 * Update the total_time_enabled and total_time_running fields for a counter.
435 static void update_counter_times(struct perf_counter *counter)
437 struct perf_counter_context *ctx = counter->ctx;
438 u64 run_end;
440 if (counter->state < PERF_COUNTER_STATE_INACTIVE)
441 return;
443 counter->total_time_enabled = ctx->time - counter->tstamp_enabled;
445 if (counter->state == PERF_COUNTER_STATE_INACTIVE)
446 run_end = counter->tstamp_stopped;
447 else
448 run_end = ctx->time;
450 counter->total_time_running = run_end - counter->tstamp_running;
454 * Update total_time_enabled and total_time_running for all counters in a group.
456 static void update_group_times(struct perf_counter *leader)
458 struct perf_counter *counter;
460 update_counter_times(leader);
461 list_for_each_entry(counter, &leader->sibling_list, list_entry)
462 update_counter_times(counter);
466 * Cross CPU call to disable a performance counter
468 static void __perf_counter_disable(void *info)
470 struct perf_counter *counter = info;
471 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
472 struct perf_counter_context *ctx = counter->ctx;
475 * If this is a per-task counter, need to check whether this
476 * counter's task is the current task on this cpu.
478 if (ctx->task && cpuctx->task_ctx != ctx)
479 return;
481 spin_lock(&ctx->lock);
484 * If the counter is on, turn it off.
485 * If it is in error state, leave it in error state.
487 if (counter->state >= PERF_COUNTER_STATE_INACTIVE) {
488 update_context_time(ctx);
489 update_counter_times(counter);
490 if (counter == counter->group_leader)
491 group_sched_out(counter, cpuctx, ctx);
492 else
493 counter_sched_out(counter, cpuctx, ctx);
494 counter->state = PERF_COUNTER_STATE_OFF;
497 spin_unlock(&ctx->lock);
501 * Disable a counter.
503 * If counter->ctx is a cloned context, callers must make sure that
504 * every task struct that counter->ctx->task could possibly point to
505 * remains valid. This condition is satisifed when called through
506 * perf_counter_for_each_child or perf_counter_for_each because they
507 * hold the top-level counter's child_mutex, so any descendant that
508 * goes to exit will block in sync_child_counter.
509 * When called from perf_pending_counter it's OK because counter->ctx
510 * is the current context on this CPU and preemption is disabled,
511 * hence we can't get into perf_counter_task_sched_out for this context.
513 static void perf_counter_disable(struct perf_counter *counter)
515 struct perf_counter_context *ctx = counter->ctx;
516 struct task_struct *task = ctx->task;
518 if (!task) {
520 * Disable the counter on the cpu that it's on
522 smp_call_function_single(counter->cpu, __perf_counter_disable,
523 counter, 1);
524 return;
527 retry:
528 task_oncpu_function_call(task, __perf_counter_disable, counter);
530 spin_lock_irq(&ctx->lock);
532 * If the counter is still active, we need to retry the cross-call.
534 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
535 spin_unlock_irq(&ctx->lock);
536 goto retry;
540 * Since we have the lock this context can't be scheduled
541 * in, so we can change the state safely.
543 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
544 update_counter_times(counter);
545 counter->state = PERF_COUNTER_STATE_OFF;
548 spin_unlock_irq(&ctx->lock);
551 static int
552 counter_sched_in(struct perf_counter *counter,
553 struct perf_cpu_context *cpuctx,
554 struct perf_counter_context *ctx,
555 int cpu)
557 if (counter->state <= PERF_COUNTER_STATE_OFF)
558 return 0;
560 counter->state = PERF_COUNTER_STATE_ACTIVE;
561 counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
563 * The new state must be visible before we turn it on in the hardware:
565 smp_wmb();
567 if (counter->pmu->enable(counter)) {
568 counter->state = PERF_COUNTER_STATE_INACTIVE;
569 counter->oncpu = -1;
570 return -EAGAIN;
573 counter->tstamp_running += ctx->time - counter->tstamp_stopped;
575 if (!is_software_counter(counter))
576 cpuctx->active_oncpu++;
577 ctx->nr_active++;
579 if (counter->attr.exclusive)
580 cpuctx->exclusive = 1;
582 return 0;
585 static int
586 group_sched_in(struct perf_counter *group_counter,
587 struct perf_cpu_context *cpuctx,
588 struct perf_counter_context *ctx,
589 int cpu)
591 struct perf_counter *counter, *partial_group;
592 int ret;
594 if (group_counter->state == PERF_COUNTER_STATE_OFF)
595 return 0;
597 ret = hw_perf_group_sched_in(group_counter, cpuctx, ctx, cpu);
598 if (ret)
599 return ret < 0 ? ret : 0;
601 if (counter_sched_in(group_counter, cpuctx, ctx, cpu))
602 return -EAGAIN;
605 * Schedule in siblings as one group (if any):
607 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
608 if (counter_sched_in(counter, cpuctx, ctx, cpu)) {
609 partial_group = counter;
610 goto group_error;
614 return 0;
616 group_error:
618 * Groups can be scheduled in as one unit only, so undo any
619 * partial group before returning:
621 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
622 if (counter == partial_group)
623 break;
624 counter_sched_out(counter, cpuctx, ctx);
626 counter_sched_out(group_counter, cpuctx, ctx);
628 return -EAGAIN;
632 * Return 1 for a group consisting entirely of software counters,
633 * 0 if the group contains any hardware counters.
635 static int is_software_only_group(struct perf_counter *leader)
637 struct perf_counter *counter;
639 if (!is_software_counter(leader))
640 return 0;
642 list_for_each_entry(counter, &leader->sibling_list, list_entry)
643 if (!is_software_counter(counter))
644 return 0;
646 return 1;
650 * Work out whether we can put this counter group on the CPU now.
652 static int group_can_go_on(struct perf_counter *counter,
653 struct perf_cpu_context *cpuctx,
654 int can_add_hw)
657 * Groups consisting entirely of software counters can always go on.
659 if (is_software_only_group(counter))
660 return 1;
662 * If an exclusive group is already on, no other hardware
663 * counters can go on.
665 if (cpuctx->exclusive)
666 return 0;
668 * If this group is exclusive and there are already
669 * counters on the CPU, it can't go on.
671 if (counter->attr.exclusive && cpuctx->active_oncpu)
672 return 0;
674 * Otherwise, try to add it if all previous groups were able
675 * to go on.
677 return can_add_hw;
680 static void add_counter_to_ctx(struct perf_counter *counter,
681 struct perf_counter_context *ctx)
683 list_add_counter(counter, ctx);
684 counter->tstamp_enabled = ctx->time;
685 counter->tstamp_running = ctx->time;
686 counter->tstamp_stopped = ctx->time;
690 * Cross CPU call to install and enable a performance counter
692 * Must be called with ctx->mutex held
694 static void __perf_install_in_context(void *info)
696 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
697 struct perf_counter *counter = info;
698 struct perf_counter_context *ctx = counter->ctx;
699 struct perf_counter *leader = counter->group_leader;
700 int cpu = smp_processor_id();
701 int err;
704 * If this is a task context, we need to check whether it is
705 * the current task context of this cpu. If not it has been
706 * scheduled out before the smp call arrived.
707 * Or possibly this is the right context but it isn't
708 * on this cpu because it had no counters.
710 if (ctx->task && cpuctx->task_ctx != ctx) {
711 if (cpuctx->task_ctx || ctx->task != current)
712 return;
713 cpuctx->task_ctx = ctx;
716 spin_lock(&ctx->lock);
717 ctx->is_active = 1;
718 update_context_time(ctx);
721 * Protect the list operation against NMI by disabling the
722 * counters on a global level. NOP for non NMI based counters.
724 perf_disable();
726 add_counter_to_ctx(counter, ctx);
729 * Don't put the counter on if it is disabled or if
730 * it is in a group and the group isn't on.
732 if (counter->state != PERF_COUNTER_STATE_INACTIVE ||
733 (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE))
734 goto unlock;
737 * An exclusive counter can't go on if there are already active
738 * hardware counters, and no hardware counter can go on if there
739 * is already an exclusive counter on.
741 if (!group_can_go_on(counter, cpuctx, 1))
742 err = -EEXIST;
743 else
744 err = counter_sched_in(counter, cpuctx, ctx, cpu);
746 if (err) {
748 * This counter couldn't go on. If it is in a group
749 * then we have to pull the whole group off.
750 * If the counter group is pinned then put it in error state.
752 if (leader != counter)
753 group_sched_out(leader, cpuctx, ctx);
754 if (leader->attr.pinned) {
755 update_group_times(leader);
756 leader->state = PERF_COUNTER_STATE_ERROR;
760 if (!err && !ctx->task && cpuctx->max_pertask)
761 cpuctx->max_pertask--;
763 unlock:
764 perf_enable();
766 spin_unlock(&ctx->lock);
770 * Attach a performance counter to a context
772 * First we add the counter to the list with the hardware enable bit
773 * in counter->hw_config cleared.
775 * If the counter is attached to a task which is on a CPU we use a smp
776 * call to enable it in the task context. The task might have been
777 * scheduled away, but we check this in the smp call again.
779 * Must be called with ctx->mutex held.
781 static void
782 perf_install_in_context(struct perf_counter_context *ctx,
783 struct perf_counter *counter,
784 int cpu)
786 struct task_struct *task = ctx->task;
788 if (!task) {
790 * Per cpu counters are installed via an smp call and
791 * the install is always sucessful.
793 smp_call_function_single(cpu, __perf_install_in_context,
794 counter, 1);
795 return;
798 retry:
799 task_oncpu_function_call(task, __perf_install_in_context,
800 counter);
802 spin_lock_irq(&ctx->lock);
804 * we need to retry the smp call.
806 if (ctx->is_active && list_empty(&counter->list_entry)) {
807 spin_unlock_irq(&ctx->lock);
808 goto retry;
812 * The lock prevents that this context is scheduled in so we
813 * can add the counter safely, if it the call above did not
814 * succeed.
816 if (list_empty(&counter->list_entry))
817 add_counter_to_ctx(counter, ctx);
818 spin_unlock_irq(&ctx->lock);
822 * Cross CPU call to enable a performance counter
824 static void __perf_counter_enable(void *info)
826 struct perf_counter *counter = info;
827 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
828 struct perf_counter_context *ctx = counter->ctx;
829 struct perf_counter *leader = counter->group_leader;
830 int err;
833 * If this is a per-task counter, need to check whether this
834 * counter's task is the current task on this cpu.
836 if (ctx->task && cpuctx->task_ctx != ctx) {
837 if (cpuctx->task_ctx || ctx->task != current)
838 return;
839 cpuctx->task_ctx = ctx;
842 spin_lock(&ctx->lock);
843 ctx->is_active = 1;
844 update_context_time(ctx);
846 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
847 goto unlock;
848 counter->state = PERF_COUNTER_STATE_INACTIVE;
849 counter->tstamp_enabled = ctx->time - counter->total_time_enabled;
852 * If the counter is in a group and isn't the group leader,
853 * then don't put it on unless the group is on.
855 if (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE)
856 goto unlock;
858 if (!group_can_go_on(counter, cpuctx, 1)) {
859 err = -EEXIST;
860 } else {
861 perf_disable();
862 if (counter == leader)
863 err = group_sched_in(counter, cpuctx, ctx,
864 smp_processor_id());
865 else
866 err = counter_sched_in(counter, cpuctx, ctx,
867 smp_processor_id());
868 perf_enable();
871 if (err) {
873 * If this counter can't go on and it's part of a
874 * group, then the whole group has to come off.
876 if (leader != counter)
877 group_sched_out(leader, cpuctx, ctx);
878 if (leader->attr.pinned) {
879 update_group_times(leader);
880 leader->state = PERF_COUNTER_STATE_ERROR;
884 unlock:
885 spin_unlock(&ctx->lock);
889 * Enable a counter.
891 * If counter->ctx is a cloned context, callers must make sure that
892 * every task struct that counter->ctx->task could possibly point to
893 * remains valid. This condition is satisfied when called through
894 * perf_counter_for_each_child or perf_counter_for_each as described
895 * for perf_counter_disable.
897 static void perf_counter_enable(struct perf_counter *counter)
899 struct perf_counter_context *ctx = counter->ctx;
900 struct task_struct *task = ctx->task;
902 if (!task) {
904 * Enable the counter on the cpu that it's on
906 smp_call_function_single(counter->cpu, __perf_counter_enable,
907 counter, 1);
908 return;
911 spin_lock_irq(&ctx->lock);
912 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
913 goto out;
916 * If the counter is in error state, clear that first.
917 * That way, if we see the counter in error state below, we
918 * know that it has gone back into error state, as distinct
919 * from the task having been scheduled away before the
920 * cross-call arrived.
922 if (counter->state == PERF_COUNTER_STATE_ERROR)
923 counter->state = PERF_COUNTER_STATE_OFF;
925 retry:
926 spin_unlock_irq(&ctx->lock);
927 task_oncpu_function_call(task, __perf_counter_enable, counter);
929 spin_lock_irq(&ctx->lock);
932 * If the context is active and the counter is still off,
933 * we need to retry the cross-call.
935 if (ctx->is_active && counter->state == PERF_COUNTER_STATE_OFF)
936 goto retry;
939 * Since we have the lock this context can't be scheduled
940 * in, so we can change the state safely.
942 if (counter->state == PERF_COUNTER_STATE_OFF) {
943 counter->state = PERF_COUNTER_STATE_INACTIVE;
944 counter->tstamp_enabled =
945 ctx->time - counter->total_time_enabled;
947 out:
948 spin_unlock_irq(&ctx->lock);
951 static int perf_counter_refresh(struct perf_counter *counter, int refresh)
954 * not supported on inherited counters
956 if (counter->attr.inherit)
957 return -EINVAL;
959 atomic_add(refresh, &counter->event_limit);
960 perf_counter_enable(counter);
962 return 0;
965 void __perf_counter_sched_out(struct perf_counter_context *ctx,
966 struct perf_cpu_context *cpuctx)
968 struct perf_counter *counter;
970 spin_lock(&ctx->lock);
971 ctx->is_active = 0;
972 if (likely(!ctx->nr_counters))
973 goto out;
974 update_context_time(ctx);
976 perf_disable();
977 if (ctx->nr_active) {
978 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
979 if (counter != counter->group_leader)
980 counter_sched_out(counter, cpuctx, ctx);
981 else
982 group_sched_out(counter, cpuctx, ctx);
985 perf_enable();
986 out:
987 spin_unlock(&ctx->lock);
991 * Test whether two contexts are equivalent, i.e. whether they
992 * have both been cloned from the same version of the same context
993 * and they both have the same number of enabled counters.
994 * If the number of enabled counters is the same, then the set
995 * of enabled counters should be the same, because these are both
996 * inherited contexts, therefore we can't access individual counters
997 * in them directly with an fd; we can only enable/disable all
998 * counters via prctl, or enable/disable all counters in a family
999 * via ioctl, which will have the same effect on both contexts.
1001 static int context_equiv(struct perf_counter_context *ctx1,
1002 struct perf_counter_context *ctx2)
1004 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
1005 && ctx1->parent_gen == ctx2->parent_gen
1006 && !ctx1->pin_count && !ctx2->pin_count;
1010 * Called from scheduler to remove the counters of the current task,
1011 * with interrupts disabled.
1013 * We stop each counter and update the counter value in counter->count.
1015 * This does not protect us against NMI, but disable()
1016 * sets the disabled bit in the control field of counter _before_
1017 * accessing the counter control register. If a NMI hits, then it will
1018 * not restart the counter.
1020 void perf_counter_task_sched_out(struct task_struct *task,
1021 struct task_struct *next, int cpu)
1023 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
1024 struct perf_counter_context *ctx = task->perf_counter_ctxp;
1025 struct perf_counter_context *next_ctx;
1026 struct perf_counter_context *parent;
1027 struct pt_regs *regs;
1028 int do_switch = 1;
1030 regs = task_pt_regs(task);
1031 perf_swcounter_event(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 1, regs, 0);
1033 if (likely(!ctx || !cpuctx->task_ctx))
1034 return;
1036 update_context_time(ctx);
1038 rcu_read_lock();
1039 parent = rcu_dereference(ctx->parent_ctx);
1040 next_ctx = next->perf_counter_ctxp;
1041 if (parent && next_ctx &&
1042 rcu_dereference(next_ctx->parent_ctx) == parent) {
1044 * Looks like the two contexts are clones, so we might be
1045 * able to optimize the context switch. We lock both
1046 * contexts and check that they are clones under the
1047 * lock (including re-checking that neither has been
1048 * uncloned in the meantime). It doesn't matter which
1049 * order we take the locks because no other cpu could
1050 * be trying to lock both of these tasks.
1052 spin_lock(&ctx->lock);
1053 spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
1054 if (context_equiv(ctx, next_ctx)) {
1056 * XXX do we need a memory barrier of sorts
1057 * wrt to rcu_dereference() of perf_counter_ctxp
1059 task->perf_counter_ctxp = next_ctx;
1060 next->perf_counter_ctxp = ctx;
1061 ctx->task = next;
1062 next_ctx->task = task;
1063 do_switch = 0;
1065 spin_unlock(&next_ctx->lock);
1066 spin_unlock(&ctx->lock);
1068 rcu_read_unlock();
1070 if (do_switch) {
1071 __perf_counter_sched_out(ctx, cpuctx);
1072 cpuctx->task_ctx = NULL;
1077 * Called with IRQs disabled
1079 static void __perf_counter_task_sched_out(struct perf_counter_context *ctx)
1081 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1083 if (!cpuctx->task_ctx)
1084 return;
1086 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
1087 return;
1089 __perf_counter_sched_out(ctx, cpuctx);
1090 cpuctx->task_ctx = NULL;
1094 * Called with IRQs disabled
1096 static void perf_counter_cpu_sched_out(struct perf_cpu_context *cpuctx)
1098 __perf_counter_sched_out(&cpuctx->ctx, cpuctx);
1101 static void
1102 __perf_counter_sched_in(struct perf_counter_context *ctx,
1103 struct perf_cpu_context *cpuctx, int cpu)
1105 struct perf_counter *counter;
1106 int can_add_hw = 1;
1108 spin_lock(&ctx->lock);
1109 ctx->is_active = 1;
1110 if (likely(!ctx->nr_counters))
1111 goto out;
1113 ctx->timestamp = perf_clock();
1115 perf_disable();
1118 * First go through the list and put on any pinned groups
1119 * in order to give them the best chance of going on.
1121 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1122 if (counter->state <= PERF_COUNTER_STATE_OFF ||
1123 !counter->attr.pinned)
1124 continue;
1125 if (counter->cpu != -1 && counter->cpu != cpu)
1126 continue;
1128 if (counter != counter->group_leader)
1129 counter_sched_in(counter, cpuctx, ctx, cpu);
1130 else {
1131 if (group_can_go_on(counter, cpuctx, 1))
1132 group_sched_in(counter, cpuctx, ctx, cpu);
1136 * If this pinned group hasn't been scheduled,
1137 * put it in error state.
1139 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1140 update_group_times(counter);
1141 counter->state = PERF_COUNTER_STATE_ERROR;
1145 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1147 * Ignore counters in OFF or ERROR state, and
1148 * ignore pinned counters since we did them already.
1150 if (counter->state <= PERF_COUNTER_STATE_OFF ||
1151 counter->attr.pinned)
1152 continue;
1155 * Listen to the 'cpu' scheduling filter constraint
1156 * of counters:
1158 if (counter->cpu != -1 && counter->cpu != cpu)
1159 continue;
1161 if (counter != counter->group_leader) {
1162 if (counter_sched_in(counter, cpuctx, ctx, cpu))
1163 can_add_hw = 0;
1164 } else {
1165 if (group_can_go_on(counter, cpuctx, can_add_hw)) {
1166 if (group_sched_in(counter, cpuctx, ctx, cpu))
1167 can_add_hw = 0;
1171 perf_enable();
1172 out:
1173 spin_unlock(&ctx->lock);
1177 * Called from scheduler to add the counters of the current task
1178 * with interrupts disabled.
1180 * We restore the counter value and then enable it.
1182 * This does not protect us against NMI, but enable()
1183 * sets the enabled bit in the control field of counter _before_
1184 * accessing the counter control register. If a NMI hits, then it will
1185 * keep the counter running.
1187 void perf_counter_task_sched_in(struct task_struct *task, int cpu)
1189 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
1190 struct perf_counter_context *ctx = task->perf_counter_ctxp;
1192 if (likely(!ctx))
1193 return;
1194 if (cpuctx->task_ctx == ctx)
1195 return;
1196 __perf_counter_sched_in(ctx, cpuctx, cpu);
1197 cpuctx->task_ctx = ctx;
1200 static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu)
1202 struct perf_counter_context *ctx = &cpuctx->ctx;
1204 __perf_counter_sched_in(ctx, cpuctx, cpu);
1207 #define MAX_INTERRUPTS (~0ULL)
1209 static void perf_log_throttle(struct perf_counter *counter, int enable);
1210 static void perf_log_period(struct perf_counter *counter, u64 period);
1212 static void perf_adjust_period(struct perf_counter *counter, u64 events)
1214 struct hw_perf_counter *hwc = &counter->hw;
1215 u64 period, sample_period;
1216 s64 delta;
1218 events *= hwc->sample_period;
1219 period = div64_u64(events, counter->attr.sample_freq);
1221 delta = (s64)(period - hwc->sample_period);
1222 delta = (delta + 7) / 8; /* low pass filter */
1224 sample_period = hwc->sample_period + delta;
1226 if (!sample_period)
1227 sample_period = 1;
1229 perf_log_period(counter, sample_period);
1231 hwc->sample_period = sample_period;
1234 static void perf_ctx_adjust_freq(struct perf_counter_context *ctx)
1236 struct perf_counter *counter;
1237 struct hw_perf_counter *hwc;
1238 u64 interrupts, freq;
1240 spin_lock(&ctx->lock);
1241 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1242 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
1243 continue;
1245 hwc = &counter->hw;
1247 interrupts = hwc->interrupts;
1248 hwc->interrupts = 0;
1251 * unthrottle counters on the tick
1253 if (interrupts == MAX_INTERRUPTS) {
1254 perf_log_throttle(counter, 1);
1255 counter->pmu->unthrottle(counter);
1256 interrupts = 2*sysctl_perf_counter_sample_rate/HZ;
1259 if (!counter->attr.freq || !counter->attr.sample_freq)
1260 continue;
1263 * if the specified freq < HZ then we need to skip ticks
1265 if (counter->attr.sample_freq < HZ) {
1266 freq = counter->attr.sample_freq;
1268 hwc->freq_count += freq;
1269 hwc->freq_interrupts += interrupts;
1271 if (hwc->freq_count < HZ)
1272 continue;
1274 interrupts = hwc->freq_interrupts;
1275 hwc->freq_interrupts = 0;
1276 hwc->freq_count -= HZ;
1277 } else
1278 freq = HZ;
1280 perf_adjust_period(counter, freq * interrupts);
1283 * In order to avoid being stalled by an (accidental) huge
1284 * sample period, force reset the sample period if we didn't
1285 * get any events in this freq period.
1287 if (!interrupts) {
1288 perf_disable();
1289 counter->pmu->disable(counter);
1290 atomic64_set(&hwc->period_left, 0);
1291 counter->pmu->enable(counter);
1292 perf_enable();
1295 spin_unlock(&ctx->lock);
1299 * Round-robin a context's counters:
1301 static void rotate_ctx(struct perf_counter_context *ctx)
1303 struct perf_counter *counter;
1305 if (!ctx->nr_counters)
1306 return;
1308 spin_lock(&ctx->lock);
1310 * Rotate the first entry last (works just fine for group counters too):
1312 perf_disable();
1313 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1314 list_move_tail(&counter->list_entry, &ctx->counter_list);
1315 break;
1317 perf_enable();
1319 spin_unlock(&ctx->lock);
1322 void perf_counter_task_tick(struct task_struct *curr, int cpu)
1324 struct perf_cpu_context *cpuctx;
1325 struct perf_counter_context *ctx;
1327 if (!atomic_read(&nr_counters))
1328 return;
1330 cpuctx = &per_cpu(perf_cpu_context, cpu);
1331 ctx = curr->perf_counter_ctxp;
1333 perf_ctx_adjust_freq(&cpuctx->ctx);
1334 if (ctx)
1335 perf_ctx_adjust_freq(ctx);
1337 perf_counter_cpu_sched_out(cpuctx);
1338 if (ctx)
1339 __perf_counter_task_sched_out(ctx);
1341 rotate_ctx(&cpuctx->ctx);
1342 if (ctx)
1343 rotate_ctx(ctx);
1345 perf_counter_cpu_sched_in(cpuctx, cpu);
1346 if (ctx)
1347 perf_counter_task_sched_in(curr, cpu);
1351 * Cross CPU call to read the hardware counter
1353 static void __read(void *info)
1355 struct perf_counter *counter = info;
1356 struct perf_counter_context *ctx = counter->ctx;
1357 unsigned long flags;
1359 local_irq_save(flags);
1360 if (ctx->is_active)
1361 update_context_time(ctx);
1362 counter->pmu->read(counter);
1363 update_counter_times(counter);
1364 local_irq_restore(flags);
1367 static u64 perf_counter_read(struct perf_counter *counter)
1370 * If counter is enabled and currently active on a CPU, update the
1371 * value in the counter structure:
1373 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
1374 smp_call_function_single(counter->oncpu,
1375 __read, counter, 1);
1376 } else if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1377 update_counter_times(counter);
1380 return atomic64_read(&counter->count);
1384 * Initialize the perf_counter context in a task_struct:
1386 static void
1387 __perf_counter_init_context(struct perf_counter_context *ctx,
1388 struct task_struct *task)
1390 memset(ctx, 0, sizeof(*ctx));
1391 spin_lock_init(&ctx->lock);
1392 mutex_init(&ctx->mutex);
1393 INIT_LIST_HEAD(&ctx->counter_list);
1394 INIT_LIST_HEAD(&ctx->event_list);
1395 atomic_set(&ctx->refcount, 1);
1396 ctx->task = task;
1399 static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
1401 struct perf_counter_context *parent_ctx;
1402 struct perf_counter_context *ctx;
1403 struct perf_cpu_context *cpuctx;
1404 struct task_struct *task;
1405 unsigned long flags;
1406 int err;
1409 * If cpu is not a wildcard then this is a percpu counter:
1411 if (cpu != -1) {
1412 /* Must be root to operate on a CPU counter: */
1413 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
1414 return ERR_PTR(-EACCES);
1416 if (cpu < 0 || cpu > num_possible_cpus())
1417 return ERR_PTR(-EINVAL);
1420 * We could be clever and allow to attach a counter to an
1421 * offline CPU and activate it when the CPU comes up, but
1422 * that's for later.
1424 if (!cpu_isset(cpu, cpu_online_map))
1425 return ERR_PTR(-ENODEV);
1427 cpuctx = &per_cpu(perf_cpu_context, cpu);
1428 ctx = &cpuctx->ctx;
1429 get_ctx(ctx);
1431 return ctx;
1434 rcu_read_lock();
1435 if (!pid)
1436 task = current;
1437 else
1438 task = find_task_by_vpid(pid);
1439 if (task)
1440 get_task_struct(task);
1441 rcu_read_unlock();
1443 if (!task)
1444 return ERR_PTR(-ESRCH);
1447 * Can't attach counters to a dying task.
1449 err = -ESRCH;
1450 if (task->flags & PF_EXITING)
1451 goto errout;
1453 /* Reuse ptrace permission checks for now. */
1454 err = -EACCES;
1455 if (!ptrace_may_access(task, PTRACE_MODE_READ))
1456 goto errout;
1458 retry:
1459 ctx = perf_lock_task_context(task, &flags);
1460 if (ctx) {
1461 parent_ctx = ctx->parent_ctx;
1462 if (parent_ctx) {
1463 put_ctx(parent_ctx);
1464 ctx->parent_ctx = NULL; /* no longer a clone */
1466 spin_unlock_irqrestore(&ctx->lock, flags);
1469 if (!ctx) {
1470 ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
1471 err = -ENOMEM;
1472 if (!ctx)
1473 goto errout;
1474 __perf_counter_init_context(ctx, task);
1475 get_ctx(ctx);
1476 if (cmpxchg(&task->perf_counter_ctxp, NULL, ctx)) {
1478 * We raced with some other task; use
1479 * the context they set.
1481 kfree(ctx);
1482 goto retry;
1484 get_task_struct(task);
1487 put_task_struct(task);
1488 return ctx;
1490 errout:
1491 put_task_struct(task);
1492 return ERR_PTR(err);
1495 static void free_counter_rcu(struct rcu_head *head)
1497 struct perf_counter *counter;
1499 counter = container_of(head, struct perf_counter, rcu_head);
1500 if (counter->ns)
1501 put_pid_ns(counter->ns);
1502 kfree(counter);
1505 static void perf_pending_sync(struct perf_counter *counter);
1507 static void free_counter(struct perf_counter *counter)
1509 perf_pending_sync(counter);
1511 if (!counter->parent) {
1512 atomic_dec(&nr_counters);
1513 if (counter->attr.mmap)
1514 atomic_dec(&nr_mmap_counters);
1515 if (counter->attr.comm)
1516 atomic_dec(&nr_comm_counters);
1519 if (counter->destroy)
1520 counter->destroy(counter);
1522 put_ctx(counter->ctx);
1523 call_rcu(&counter->rcu_head, free_counter_rcu);
1527 * Called when the last reference to the file is gone.
1529 static int perf_release(struct inode *inode, struct file *file)
1531 struct perf_counter *counter = file->private_data;
1532 struct perf_counter_context *ctx = counter->ctx;
1534 file->private_data = NULL;
1536 WARN_ON_ONCE(ctx->parent_ctx);
1537 mutex_lock(&ctx->mutex);
1538 perf_counter_remove_from_context(counter);
1539 mutex_unlock(&ctx->mutex);
1541 mutex_lock(&counter->owner->perf_counter_mutex);
1542 list_del_init(&counter->owner_entry);
1543 mutex_unlock(&counter->owner->perf_counter_mutex);
1544 put_task_struct(counter->owner);
1546 free_counter(counter);
1548 return 0;
1552 * Read the performance counter - simple non blocking version for now
1554 static ssize_t
1555 perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
1557 u64 values[4];
1558 int n;
1561 * Return end-of-file for a read on a counter that is in
1562 * error state (i.e. because it was pinned but it couldn't be
1563 * scheduled on to the CPU at some point).
1565 if (counter->state == PERF_COUNTER_STATE_ERROR)
1566 return 0;
1568 WARN_ON_ONCE(counter->ctx->parent_ctx);
1569 mutex_lock(&counter->child_mutex);
1570 values[0] = perf_counter_read(counter);
1571 n = 1;
1572 if (counter->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1573 values[n++] = counter->total_time_enabled +
1574 atomic64_read(&counter->child_total_time_enabled);
1575 if (counter->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1576 values[n++] = counter->total_time_running +
1577 atomic64_read(&counter->child_total_time_running);
1578 if (counter->attr.read_format & PERF_FORMAT_ID)
1579 values[n++] = counter->id;
1580 mutex_unlock(&counter->child_mutex);
1582 if (count < n * sizeof(u64))
1583 return -EINVAL;
1584 count = n * sizeof(u64);
1586 if (copy_to_user(buf, values, count))
1587 return -EFAULT;
1589 return count;
1592 static ssize_t
1593 perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
1595 struct perf_counter *counter = file->private_data;
1597 return perf_read_hw(counter, buf, count);
1600 static unsigned int perf_poll(struct file *file, poll_table *wait)
1602 struct perf_counter *counter = file->private_data;
1603 struct perf_mmap_data *data;
1604 unsigned int events = POLL_HUP;
1606 rcu_read_lock();
1607 data = rcu_dereference(counter->data);
1608 if (data)
1609 events = atomic_xchg(&data->poll, 0);
1610 rcu_read_unlock();
1612 poll_wait(file, &counter->waitq, wait);
1614 return events;
1617 static void perf_counter_reset(struct perf_counter *counter)
1619 (void)perf_counter_read(counter);
1620 atomic64_set(&counter->count, 0);
1621 perf_counter_update_userpage(counter);
1625 * Holding the top-level counter's child_mutex means that any
1626 * descendant process that has inherited this counter will block
1627 * in sync_child_counter if it goes to exit, thus satisfying the
1628 * task existence requirements of perf_counter_enable/disable.
1630 static void perf_counter_for_each_child(struct perf_counter *counter,
1631 void (*func)(struct perf_counter *))
1633 struct perf_counter *child;
1635 WARN_ON_ONCE(counter->ctx->parent_ctx);
1636 mutex_lock(&counter->child_mutex);
1637 func(counter);
1638 list_for_each_entry(child, &counter->child_list, child_list)
1639 func(child);
1640 mutex_unlock(&counter->child_mutex);
1643 static void perf_counter_for_each(struct perf_counter *counter,
1644 void (*func)(struct perf_counter *))
1646 struct perf_counter_context *ctx = counter->ctx;
1647 struct perf_counter *sibling;
1649 WARN_ON_ONCE(ctx->parent_ctx);
1650 mutex_lock(&ctx->mutex);
1651 counter = counter->group_leader;
1653 perf_counter_for_each_child(counter, func);
1654 func(counter);
1655 list_for_each_entry(sibling, &counter->sibling_list, list_entry)
1656 perf_counter_for_each_child(counter, func);
1657 mutex_unlock(&ctx->mutex);
1660 static int perf_counter_period(struct perf_counter *counter, u64 __user *arg)
1662 struct perf_counter_context *ctx = counter->ctx;
1663 unsigned long size;
1664 int ret = 0;
1665 u64 value;
1667 if (!counter->attr.sample_period)
1668 return -EINVAL;
1670 size = copy_from_user(&value, arg, sizeof(value));
1671 if (size != sizeof(value))
1672 return -EFAULT;
1674 if (!value)
1675 return -EINVAL;
1677 spin_lock_irq(&ctx->lock);
1678 if (counter->attr.freq) {
1679 if (value > sysctl_perf_counter_sample_rate) {
1680 ret = -EINVAL;
1681 goto unlock;
1684 counter->attr.sample_freq = value;
1685 } else {
1686 perf_log_period(counter, value);
1688 counter->attr.sample_period = value;
1689 counter->hw.sample_period = value;
1691 unlock:
1692 spin_unlock_irq(&ctx->lock);
1694 return ret;
1697 static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1699 struct perf_counter *counter = file->private_data;
1700 void (*func)(struct perf_counter *);
1701 u32 flags = arg;
1703 switch (cmd) {
1704 case PERF_COUNTER_IOC_ENABLE:
1705 func = perf_counter_enable;
1706 break;
1707 case PERF_COUNTER_IOC_DISABLE:
1708 func = perf_counter_disable;
1709 break;
1710 case PERF_COUNTER_IOC_RESET:
1711 func = perf_counter_reset;
1712 break;
1714 case PERF_COUNTER_IOC_REFRESH:
1715 return perf_counter_refresh(counter, arg);
1717 case PERF_COUNTER_IOC_PERIOD:
1718 return perf_counter_period(counter, (u64 __user *)arg);
1720 default:
1721 return -ENOTTY;
1724 if (flags & PERF_IOC_FLAG_GROUP)
1725 perf_counter_for_each(counter, func);
1726 else
1727 perf_counter_for_each_child(counter, func);
1729 return 0;
1732 int perf_counter_task_enable(void)
1734 struct perf_counter *counter;
1736 mutex_lock(&current->perf_counter_mutex);
1737 list_for_each_entry(counter, &current->perf_counter_list, owner_entry)
1738 perf_counter_for_each_child(counter, perf_counter_enable);
1739 mutex_unlock(&current->perf_counter_mutex);
1741 return 0;
1744 int perf_counter_task_disable(void)
1746 struct perf_counter *counter;
1748 mutex_lock(&current->perf_counter_mutex);
1749 list_for_each_entry(counter, &current->perf_counter_list, owner_entry)
1750 perf_counter_for_each_child(counter, perf_counter_disable);
1751 mutex_unlock(&current->perf_counter_mutex);
1753 return 0;
1757 * Callers need to ensure there can be no nesting of this function, otherwise
1758 * the seqlock logic goes bad. We can not serialize this because the arch
1759 * code calls this from NMI context.
1761 void perf_counter_update_userpage(struct perf_counter *counter)
1763 struct perf_counter_mmap_page *userpg;
1764 struct perf_mmap_data *data;
1766 rcu_read_lock();
1767 data = rcu_dereference(counter->data);
1768 if (!data)
1769 goto unlock;
1771 userpg = data->user_page;
1774 * Disable preemption so as to not let the corresponding user-space
1775 * spin too long if we get preempted.
1777 preempt_disable();
1778 ++userpg->lock;
1779 barrier();
1780 userpg->index = counter->hw.idx;
1781 userpg->offset = atomic64_read(&counter->count);
1782 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
1783 userpg->offset -= atomic64_read(&counter->hw.prev_count);
1785 userpg->time_enabled = counter->total_time_enabled +
1786 atomic64_read(&counter->child_total_time_enabled);
1788 userpg->time_running = counter->total_time_running +
1789 atomic64_read(&counter->child_total_time_running);
1791 barrier();
1792 ++userpg->lock;
1793 preempt_enable();
1794 unlock:
1795 rcu_read_unlock();
1798 static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1800 struct perf_counter *counter = vma->vm_file->private_data;
1801 struct perf_mmap_data *data;
1802 int ret = VM_FAULT_SIGBUS;
1804 if (vmf->flags & FAULT_FLAG_MKWRITE) {
1805 if (vmf->pgoff == 0)
1806 ret = 0;
1807 return ret;
1810 rcu_read_lock();
1811 data = rcu_dereference(counter->data);
1812 if (!data)
1813 goto unlock;
1815 if (vmf->pgoff == 0) {
1816 vmf->page = virt_to_page(data->user_page);
1817 } else {
1818 int nr = vmf->pgoff - 1;
1820 if ((unsigned)nr > data->nr_pages)
1821 goto unlock;
1823 if (vmf->flags & FAULT_FLAG_WRITE)
1824 goto unlock;
1826 vmf->page = virt_to_page(data->data_pages[nr]);
1829 get_page(vmf->page);
1830 vmf->page->mapping = vma->vm_file->f_mapping;
1831 vmf->page->index = vmf->pgoff;
1833 ret = 0;
1834 unlock:
1835 rcu_read_unlock();
1837 return ret;
1840 static int perf_mmap_data_alloc(struct perf_counter *counter, int nr_pages)
1842 struct perf_mmap_data *data;
1843 unsigned long size;
1844 int i;
1846 WARN_ON(atomic_read(&counter->mmap_count));
1848 size = sizeof(struct perf_mmap_data);
1849 size += nr_pages * sizeof(void *);
1851 data = kzalloc(size, GFP_KERNEL);
1852 if (!data)
1853 goto fail;
1855 data->user_page = (void *)get_zeroed_page(GFP_KERNEL);
1856 if (!data->user_page)
1857 goto fail_user_page;
1859 for (i = 0; i < nr_pages; i++) {
1860 data->data_pages[i] = (void *)get_zeroed_page(GFP_KERNEL);
1861 if (!data->data_pages[i])
1862 goto fail_data_pages;
1865 data->nr_pages = nr_pages;
1866 atomic_set(&data->lock, -1);
1868 rcu_assign_pointer(counter->data, data);
1870 return 0;
1872 fail_data_pages:
1873 for (i--; i >= 0; i--)
1874 free_page((unsigned long)data->data_pages[i]);
1876 free_page((unsigned long)data->user_page);
1878 fail_user_page:
1879 kfree(data);
1881 fail:
1882 return -ENOMEM;
1885 static void perf_mmap_free_page(unsigned long addr)
1887 struct page *page = virt_to_page(addr);
1889 page->mapping = NULL;
1890 __free_page(page);
1893 static void __perf_mmap_data_free(struct rcu_head *rcu_head)
1895 struct perf_mmap_data *data;
1896 int i;
1898 data = container_of(rcu_head, struct perf_mmap_data, rcu_head);
1900 perf_mmap_free_page((unsigned long)data->user_page);
1901 for (i = 0; i < data->nr_pages; i++)
1902 perf_mmap_free_page((unsigned long)data->data_pages[i]);
1904 kfree(data);
1907 static void perf_mmap_data_free(struct perf_counter *counter)
1909 struct perf_mmap_data *data = counter->data;
1911 WARN_ON(atomic_read(&counter->mmap_count));
1913 rcu_assign_pointer(counter->data, NULL);
1914 call_rcu(&data->rcu_head, __perf_mmap_data_free);
1917 static void perf_mmap_open(struct vm_area_struct *vma)
1919 struct perf_counter *counter = vma->vm_file->private_data;
1921 atomic_inc(&counter->mmap_count);
1924 static void perf_mmap_close(struct vm_area_struct *vma)
1926 struct perf_counter *counter = vma->vm_file->private_data;
1928 WARN_ON_ONCE(counter->ctx->parent_ctx);
1929 if (atomic_dec_and_mutex_lock(&counter->mmap_count, &counter->mmap_mutex)) {
1930 struct user_struct *user = current_user();
1932 atomic_long_sub(counter->data->nr_pages + 1, &user->locked_vm);
1933 vma->vm_mm->locked_vm -= counter->data->nr_locked;
1934 perf_mmap_data_free(counter);
1935 mutex_unlock(&counter->mmap_mutex);
1939 static struct vm_operations_struct perf_mmap_vmops = {
1940 .open = perf_mmap_open,
1941 .close = perf_mmap_close,
1942 .fault = perf_mmap_fault,
1943 .page_mkwrite = perf_mmap_fault,
1946 static int perf_mmap(struct file *file, struct vm_area_struct *vma)
1948 struct perf_counter *counter = file->private_data;
1949 unsigned long user_locked, user_lock_limit;
1950 struct user_struct *user = current_user();
1951 unsigned long locked, lock_limit;
1952 unsigned long vma_size;
1953 unsigned long nr_pages;
1954 long user_extra, extra;
1955 int ret = 0;
1957 if (!(vma->vm_flags & VM_SHARED))
1958 return -EINVAL;
1960 vma_size = vma->vm_end - vma->vm_start;
1961 nr_pages = (vma_size / PAGE_SIZE) - 1;
1964 * If we have data pages ensure they're a power-of-two number, so we
1965 * can do bitmasks instead of modulo.
1967 if (nr_pages != 0 && !is_power_of_2(nr_pages))
1968 return -EINVAL;
1970 if (vma_size != PAGE_SIZE * (1 + nr_pages))
1971 return -EINVAL;
1973 if (vma->vm_pgoff != 0)
1974 return -EINVAL;
1976 WARN_ON_ONCE(counter->ctx->parent_ctx);
1977 mutex_lock(&counter->mmap_mutex);
1978 if (atomic_inc_not_zero(&counter->mmap_count)) {
1979 if (nr_pages != counter->data->nr_pages)
1980 ret = -EINVAL;
1981 goto unlock;
1984 user_extra = nr_pages + 1;
1985 user_lock_limit = sysctl_perf_counter_mlock >> (PAGE_SHIFT - 10);
1988 * Increase the limit linearly with more CPUs:
1990 user_lock_limit *= num_online_cpus();
1992 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
1994 extra = 0;
1995 if (user_locked > user_lock_limit)
1996 extra = user_locked - user_lock_limit;
1998 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
1999 lock_limit >>= PAGE_SHIFT;
2000 locked = vma->vm_mm->locked_vm + extra;
2002 if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) {
2003 ret = -EPERM;
2004 goto unlock;
2007 WARN_ON(counter->data);
2008 ret = perf_mmap_data_alloc(counter, nr_pages);
2009 if (ret)
2010 goto unlock;
2012 atomic_set(&counter->mmap_count, 1);
2013 atomic_long_add(user_extra, &user->locked_vm);
2014 vma->vm_mm->locked_vm += extra;
2015 counter->data->nr_locked = extra;
2016 if (vma->vm_flags & VM_WRITE)
2017 counter->data->writable = 1;
2019 unlock:
2020 mutex_unlock(&counter->mmap_mutex);
2022 vma->vm_flags |= VM_RESERVED;
2023 vma->vm_ops = &perf_mmap_vmops;
2025 return ret;
2028 static int perf_fasync(int fd, struct file *filp, int on)
2030 struct inode *inode = filp->f_path.dentry->d_inode;
2031 struct perf_counter *counter = filp->private_data;
2032 int retval;
2034 mutex_lock(&inode->i_mutex);
2035 retval = fasync_helper(fd, filp, on, &counter->fasync);
2036 mutex_unlock(&inode->i_mutex);
2038 if (retval < 0)
2039 return retval;
2041 return 0;
2044 static const struct file_operations perf_fops = {
2045 .release = perf_release,
2046 .read = perf_read,
2047 .poll = perf_poll,
2048 .unlocked_ioctl = perf_ioctl,
2049 .compat_ioctl = perf_ioctl,
2050 .mmap = perf_mmap,
2051 .fasync = perf_fasync,
2055 * Perf counter wakeup
2057 * If there's data, ensure we set the poll() state and publish everything
2058 * to user-space before waking everybody up.
2061 void perf_counter_wakeup(struct perf_counter *counter)
2063 wake_up_all(&counter->waitq);
2065 if (counter->pending_kill) {
2066 kill_fasync(&counter->fasync, SIGIO, counter->pending_kill);
2067 counter->pending_kill = 0;
2072 * Pending wakeups
2074 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
2076 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
2077 * single linked list and use cmpxchg() to add entries lockless.
2080 static void perf_pending_counter(struct perf_pending_entry *entry)
2082 struct perf_counter *counter = container_of(entry,
2083 struct perf_counter, pending);
2085 if (counter->pending_disable) {
2086 counter->pending_disable = 0;
2087 perf_counter_disable(counter);
2090 if (counter->pending_wakeup) {
2091 counter->pending_wakeup = 0;
2092 perf_counter_wakeup(counter);
2096 #define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
2098 static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
2099 PENDING_TAIL,
2102 static void perf_pending_queue(struct perf_pending_entry *entry,
2103 void (*func)(struct perf_pending_entry *))
2105 struct perf_pending_entry **head;
2107 if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
2108 return;
2110 entry->func = func;
2112 head = &get_cpu_var(perf_pending_head);
2114 do {
2115 entry->next = *head;
2116 } while (cmpxchg(head, entry->next, entry) != entry->next);
2118 set_perf_counter_pending();
2120 put_cpu_var(perf_pending_head);
2123 static int __perf_pending_run(void)
2125 struct perf_pending_entry *list;
2126 int nr = 0;
2128 list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
2129 while (list != PENDING_TAIL) {
2130 void (*func)(struct perf_pending_entry *);
2131 struct perf_pending_entry *entry = list;
2133 list = list->next;
2135 func = entry->func;
2136 entry->next = NULL;
2138 * Ensure we observe the unqueue before we issue the wakeup,
2139 * so that we won't be waiting forever.
2140 * -- see perf_not_pending().
2142 smp_wmb();
2144 func(entry);
2145 nr++;
2148 return nr;
2151 static inline int perf_not_pending(struct perf_counter *counter)
2154 * If we flush on whatever cpu we run, there is a chance we don't
2155 * need to wait.
2157 get_cpu();
2158 __perf_pending_run();
2159 put_cpu();
2162 * Ensure we see the proper queue state before going to sleep
2163 * so that we do not miss the wakeup. -- see perf_pending_handle()
2165 smp_rmb();
2166 return counter->pending.next == NULL;
2169 static void perf_pending_sync(struct perf_counter *counter)
2171 wait_event(counter->waitq, perf_not_pending(counter));
2174 void perf_counter_do_pending(void)
2176 __perf_pending_run();
2180 * Callchain support -- arch specific
2183 __weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
2185 return NULL;
2189 * Output
2192 struct perf_output_handle {
2193 struct perf_counter *counter;
2194 struct perf_mmap_data *data;
2195 unsigned long head;
2196 unsigned long offset;
2197 int nmi;
2198 int sample;
2199 int locked;
2200 unsigned long flags;
2203 static bool perf_output_space(struct perf_mmap_data *data,
2204 unsigned int offset, unsigned int head)
2206 unsigned long tail;
2207 unsigned long mask;
2209 if (!data->writable)
2210 return true;
2212 mask = (data->nr_pages << PAGE_SHIFT) - 1;
2214 * Userspace could choose to issue a mb() before updating the tail
2215 * pointer. So that all reads will be completed before the write is
2216 * issued.
2218 tail = ACCESS_ONCE(data->user_page->data_tail);
2219 smp_rmb();
2221 offset = (offset - tail) & mask;
2222 head = (head - tail) & mask;
2224 if ((int)(head - offset) < 0)
2225 return false;
2227 return true;
2230 static void perf_output_wakeup(struct perf_output_handle *handle)
2232 atomic_set(&handle->data->poll, POLL_IN);
2234 if (handle->nmi) {
2235 handle->counter->pending_wakeup = 1;
2236 perf_pending_queue(&handle->counter->pending,
2237 perf_pending_counter);
2238 } else
2239 perf_counter_wakeup(handle->counter);
2243 * Curious locking construct.
2245 * We need to ensure a later event doesn't publish a head when a former
2246 * event isn't done writing. However since we need to deal with NMIs we
2247 * cannot fully serialize things.
2249 * What we do is serialize between CPUs so we only have to deal with NMI
2250 * nesting on a single CPU.
2252 * We only publish the head (and generate a wakeup) when the outer-most
2253 * event completes.
2255 static void perf_output_lock(struct perf_output_handle *handle)
2257 struct perf_mmap_data *data = handle->data;
2258 int cpu;
2260 handle->locked = 0;
2262 local_irq_save(handle->flags);
2263 cpu = smp_processor_id();
2265 if (in_nmi() && atomic_read(&data->lock) == cpu)
2266 return;
2268 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
2269 cpu_relax();
2271 handle->locked = 1;
2274 static void perf_output_unlock(struct perf_output_handle *handle)
2276 struct perf_mmap_data *data = handle->data;
2277 unsigned long head;
2278 int cpu;
2280 data->done_head = data->head;
2282 if (!handle->locked)
2283 goto out;
2285 again:
2287 * The xchg implies a full barrier that ensures all writes are done
2288 * before we publish the new head, matched by a rmb() in userspace when
2289 * reading this position.
2291 while ((head = atomic_long_xchg(&data->done_head, 0)))
2292 data->user_page->data_head = head;
2295 * NMI can happen here, which means we can miss a done_head update.
2298 cpu = atomic_xchg(&data->lock, -1);
2299 WARN_ON_ONCE(cpu != smp_processor_id());
2302 * Therefore we have to validate we did not indeed do so.
2304 if (unlikely(atomic_long_read(&data->done_head))) {
2306 * Since we had it locked, we can lock it again.
2308 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
2309 cpu_relax();
2311 goto again;
2314 if (atomic_xchg(&data->wakeup, 0))
2315 perf_output_wakeup(handle);
2316 out:
2317 local_irq_restore(handle->flags);
2320 static void perf_output_copy(struct perf_output_handle *handle,
2321 const void *buf, unsigned int len)
2323 unsigned int pages_mask;
2324 unsigned int offset;
2325 unsigned int size;
2326 void **pages;
2328 offset = handle->offset;
2329 pages_mask = handle->data->nr_pages - 1;
2330 pages = handle->data->data_pages;
2332 do {
2333 unsigned int page_offset;
2334 int nr;
2336 nr = (offset >> PAGE_SHIFT) & pages_mask;
2337 page_offset = offset & (PAGE_SIZE - 1);
2338 size = min_t(unsigned int, PAGE_SIZE - page_offset, len);
2340 memcpy(pages[nr] + page_offset, buf, size);
2342 len -= size;
2343 buf += size;
2344 offset += size;
2345 } while (len);
2347 handle->offset = offset;
2350 * Check we didn't copy past our reservation window, taking the
2351 * possible unsigned int wrap into account.
2353 WARN_ON_ONCE(((long)(handle->head - handle->offset)) < 0);
2356 #define perf_output_put(handle, x) \
2357 perf_output_copy((handle), &(x), sizeof(x))
2359 static int perf_output_begin(struct perf_output_handle *handle,
2360 struct perf_counter *counter, unsigned int size,
2361 int nmi, int sample)
2363 struct perf_mmap_data *data;
2364 unsigned int offset, head;
2365 int have_lost;
2366 struct {
2367 struct perf_event_header header;
2368 u64 id;
2369 u64 lost;
2370 } lost_event;
2373 * For inherited counters we send all the output towards the parent.
2375 if (counter->parent)
2376 counter = counter->parent;
2378 rcu_read_lock();
2379 data = rcu_dereference(counter->data);
2380 if (!data)
2381 goto out;
2383 handle->data = data;
2384 handle->counter = counter;
2385 handle->nmi = nmi;
2386 handle->sample = sample;
2388 if (!data->nr_pages)
2389 goto fail;
2391 have_lost = atomic_read(&data->lost);
2392 if (have_lost)
2393 size += sizeof(lost_event);
2395 perf_output_lock(handle);
2397 do {
2398 offset = head = atomic_long_read(&data->head);
2399 head += size;
2400 if (unlikely(!perf_output_space(data, offset, head)))
2401 goto fail;
2402 } while (atomic_long_cmpxchg(&data->head, offset, head) != offset);
2404 handle->offset = offset;
2405 handle->head = head;
2407 if ((offset >> PAGE_SHIFT) != (head >> PAGE_SHIFT))
2408 atomic_set(&data->wakeup, 1);
2410 if (have_lost) {
2411 lost_event.header.type = PERF_EVENT_LOST;
2412 lost_event.header.misc = 0;
2413 lost_event.header.size = sizeof(lost_event);
2414 lost_event.id = counter->id;
2415 lost_event.lost = atomic_xchg(&data->lost, 0);
2417 perf_output_put(handle, lost_event);
2420 return 0;
2422 fail:
2423 atomic_inc(&data->lost);
2424 perf_output_unlock(handle);
2425 out:
2426 rcu_read_unlock();
2428 return -ENOSPC;
2431 static void perf_output_end(struct perf_output_handle *handle)
2433 struct perf_counter *counter = handle->counter;
2434 struct perf_mmap_data *data = handle->data;
2436 int wakeup_events = counter->attr.wakeup_events;
2438 if (handle->sample && wakeup_events) {
2439 int events = atomic_inc_return(&data->events);
2440 if (events >= wakeup_events) {
2441 atomic_sub(wakeup_events, &data->events);
2442 atomic_set(&data->wakeup, 1);
2446 perf_output_unlock(handle);
2447 rcu_read_unlock();
2450 static u32 perf_counter_pid(struct perf_counter *counter, struct task_struct *p)
2453 * only top level counters have the pid namespace they were created in
2455 if (counter->parent)
2456 counter = counter->parent;
2458 return task_tgid_nr_ns(p, counter->ns);
2461 static u32 perf_counter_tid(struct perf_counter *counter, struct task_struct *p)
2464 * only top level counters have the pid namespace they were created in
2466 if (counter->parent)
2467 counter = counter->parent;
2469 return task_pid_nr_ns(p, counter->ns);
2472 static void perf_counter_output(struct perf_counter *counter, int nmi,
2473 struct perf_sample_data *data)
2475 int ret;
2476 u64 sample_type = counter->attr.sample_type;
2477 struct perf_output_handle handle;
2478 struct perf_event_header header;
2479 u64 ip;
2480 struct {
2481 u32 pid, tid;
2482 } tid_entry;
2483 struct {
2484 u64 id;
2485 u64 counter;
2486 } group_entry;
2487 struct perf_callchain_entry *callchain = NULL;
2488 int callchain_size = 0;
2489 u64 time;
2490 struct {
2491 u32 cpu, reserved;
2492 } cpu_entry;
2494 header.type = 0;
2495 header.size = sizeof(header);
2497 header.misc = PERF_EVENT_MISC_OVERFLOW;
2498 header.misc |= perf_misc_flags(data->regs);
2500 if (sample_type & PERF_SAMPLE_IP) {
2501 ip = perf_instruction_pointer(data->regs);
2502 header.type |= PERF_SAMPLE_IP;
2503 header.size += sizeof(ip);
2506 if (sample_type & PERF_SAMPLE_TID) {
2507 /* namespace issues */
2508 tid_entry.pid = perf_counter_pid(counter, current);
2509 tid_entry.tid = perf_counter_tid(counter, current);
2511 header.type |= PERF_SAMPLE_TID;
2512 header.size += sizeof(tid_entry);
2515 if (sample_type & PERF_SAMPLE_TIME) {
2517 * Maybe do better on x86 and provide cpu_clock_nmi()
2519 time = sched_clock();
2521 header.type |= PERF_SAMPLE_TIME;
2522 header.size += sizeof(u64);
2525 if (sample_type & PERF_SAMPLE_ADDR) {
2526 header.type |= PERF_SAMPLE_ADDR;
2527 header.size += sizeof(u64);
2530 if (sample_type & PERF_SAMPLE_ID) {
2531 header.type |= PERF_SAMPLE_ID;
2532 header.size += sizeof(u64);
2535 if (sample_type & PERF_SAMPLE_CPU) {
2536 header.type |= PERF_SAMPLE_CPU;
2537 header.size += sizeof(cpu_entry);
2539 cpu_entry.cpu = raw_smp_processor_id();
2542 if (sample_type & PERF_SAMPLE_PERIOD) {
2543 header.type |= PERF_SAMPLE_PERIOD;
2544 header.size += sizeof(u64);
2547 if (sample_type & PERF_SAMPLE_GROUP) {
2548 header.type |= PERF_SAMPLE_GROUP;
2549 header.size += sizeof(u64) +
2550 counter->nr_siblings * sizeof(group_entry);
2553 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
2554 callchain = perf_callchain(data->regs);
2556 if (callchain) {
2557 callchain_size = (1 + callchain->nr) * sizeof(u64);
2559 header.type |= PERF_SAMPLE_CALLCHAIN;
2560 header.size += callchain_size;
2564 ret = perf_output_begin(&handle, counter, header.size, nmi, 1);
2565 if (ret)
2566 return;
2568 perf_output_put(&handle, header);
2570 if (sample_type & PERF_SAMPLE_IP)
2571 perf_output_put(&handle, ip);
2573 if (sample_type & PERF_SAMPLE_TID)
2574 perf_output_put(&handle, tid_entry);
2576 if (sample_type & PERF_SAMPLE_TIME)
2577 perf_output_put(&handle, time);
2579 if (sample_type & PERF_SAMPLE_ADDR)
2580 perf_output_put(&handle, data->addr);
2582 if (sample_type & PERF_SAMPLE_ID)
2583 perf_output_put(&handle, counter->id);
2585 if (sample_type & PERF_SAMPLE_CPU)
2586 perf_output_put(&handle, cpu_entry);
2588 if (sample_type & PERF_SAMPLE_PERIOD)
2589 perf_output_put(&handle, data->period);
2592 * XXX PERF_SAMPLE_GROUP vs inherited counters seems difficult.
2594 if (sample_type & PERF_SAMPLE_GROUP) {
2595 struct perf_counter *leader, *sub;
2596 u64 nr = counter->nr_siblings;
2598 perf_output_put(&handle, nr);
2600 leader = counter->group_leader;
2601 list_for_each_entry(sub, &leader->sibling_list, list_entry) {
2602 if (sub != counter)
2603 sub->pmu->read(sub);
2605 group_entry.id = sub->id;
2606 group_entry.counter = atomic64_read(&sub->count);
2608 perf_output_put(&handle, group_entry);
2612 if (callchain)
2613 perf_output_copy(&handle, callchain, callchain_size);
2615 perf_output_end(&handle);
2619 * fork tracking
2622 struct perf_fork_event {
2623 struct task_struct *task;
2625 struct {
2626 struct perf_event_header header;
2628 u32 pid;
2629 u32 ppid;
2630 } event;
2633 static void perf_counter_fork_output(struct perf_counter *counter,
2634 struct perf_fork_event *fork_event)
2636 struct perf_output_handle handle;
2637 int size = fork_event->event.header.size;
2638 struct task_struct *task = fork_event->task;
2639 int ret = perf_output_begin(&handle, counter, size, 0, 0);
2641 if (ret)
2642 return;
2644 fork_event->event.pid = perf_counter_pid(counter, task);
2645 fork_event->event.ppid = perf_counter_pid(counter, task->real_parent);
2647 perf_output_put(&handle, fork_event->event);
2648 perf_output_end(&handle);
2651 static int perf_counter_fork_match(struct perf_counter *counter)
2653 if (counter->attr.comm || counter->attr.mmap)
2654 return 1;
2656 return 0;
2659 static void perf_counter_fork_ctx(struct perf_counter_context *ctx,
2660 struct perf_fork_event *fork_event)
2662 struct perf_counter *counter;
2664 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2665 return;
2667 rcu_read_lock();
2668 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2669 if (perf_counter_fork_match(counter))
2670 perf_counter_fork_output(counter, fork_event);
2672 rcu_read_unlock();
2675 static void perf_counter_fork_event(struct perf_fork_event *fork_event)
2677 struct perf_cpu_context *cpuctx;
2678 struct perf_counter_context *ctx;
2680 cpuctx = &get_cpu_var(perf_cpu_context);
2681 perf_counter_fork_ctx(&cpuctx->ctx, fork_event);
2682 put_cpu_var(perf_cpu_context);
2684 rcu_read_lock();
2686 * doesn't really matter which of the child contexts the
2687 * events ends up in.
2689 ctx = rcu_dereference(current->perf_counter_ctxp);
2690 if (ctx)
2691 perf_counter_fork_ctx(ctx, fork_event);
2692 rcu_read_unlock();
2695 void perf_counter_fork(struct task_struct *task)
2697 struct perf_fork_event fork_event;
2699 if (!atomic_read(&nr_comm_counters) &&
2700 !atomic_read(&nr_mmap_counters))
2701 return;
2703 fork_event = (struct perf_fork_event){
2704 .task = task,
2705 .event = {
2706 .header = {
2707 .type = PERF_EVENT_FORK,
2708 .size = sizeof(fork_event.event),
2713 perf_counter_fork_event(&fork_event);
2717 * comm tracking
2720 struct perf_comm_event {
2721 struct task_struct *task;
2722 char *comm;
2723 int comm_size;
2725 struct {
2726 struct perf_event_header header;
2728 u32 pid;
2729 u32 tid;
2730 } event;
2733 static void perf_counter_comm_output(struct perf_counter *counter,
2734 struct perf_comm_event *comm_event)
2736 struct perf_output_handle handle;
2737 int size = comm_event->event.header.size;
2738 int ret = perf_output_begin(&handle, counter, size, 0, 0);
2740 if (ret)
2741 return;
2743 comm_event->event.pid = perf_counter_pid(counter, comm_event->task);
2744 comm_event->event.tid = perf_counter_tid(counter, comm_event->task);
2746 perf_output_put(&handle, comm_event->event);
2747 perf_output_copy(&handle, comm_event->comm,
2748 comm_event->comm_size);
2749 perf_output_end(&handle);
2752 static int perf_counter_comm_match(struct perf_counter *counter)
2754 if (counter->attr.comm)
2755 return 1;
2757 return 0;
2760 static void perf_counter_comm_ctx(struct perf_counter_context *ctx,
2761 struct perf_comm_event *comm_event)
2763 struct perf_counter *counter;
2765 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2766 return;
2768 rcu_read_lock();
2769 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2770 if (perf_counter_comm_match(counter))
2771 perf_counter_comm_output(counter, comm_event);
2773 rcu_read_unlock();
2776 static void perf_counter_comm_event(struct perf_comm_event *comm_event)
2778 struct perf_cpu_context *cpuctx;
2779 struct perf_counter_context *ctx;
2780 unsigned int size;
2781 char *comm = comm_event->task->comm;
2783 size = ALIGN(strlen(comm)+1, sizeof(u64));
2785 comm_event->comm = comm;
2786 comm_event->comm_size = size;
2788 comm_event->event.header.size = sizeof(comm_event->event) + size;
2790 cpuctx = &get_cpu_var(perf_cpu_context);
2791 perf_counter_comm_ctx(&cpuctx->ctx, comm_event);
2792 put_cpu_var(perf_cpu_context);
2794 rcu_read_lock();
2796 * doesn't really matter which of the child contexts the
2797 * events ends up in.
2799 ctx = rcu_dereference(current->perf_counter_ctxp);
2800 if (ctx)
2801 perf_counter_comm_ctx(ctx, comm_event);
2802 rcu_read_unlock();
2805 void perf_counter_comm(struct task_struct *task)
2807 struct perf_comm_event comm_event;
2809 if (!atomic_read(&nr_comm_counters))
2810 return;
2812 comm_event = (struct perf_comm_event){
2813 .task = task,
2814 .event = {
2815 .header = { .type = PERF_EVENT_COMM, },
2819 perf_counter_comm_event(&comm_event);
2823 * mmap tracking
2826 struct perf_mmap_event {
2827 struct vm_area_struct *vma;
2829 const char *file_name;
2830 int file_size;
2832 struct {
2833 struct perf_event_header header;
2835 u32 pid;
2836 u32 tid;
2837 u64 start;
2838 u64 len;
2839 u64 pgoff;
2840 } event;
2843 static void perf_counter_mmap_output(struct perf_counter *counter,
2844 struct perf_mmap_event *mmap_event)
2846 struct perf_output_handle handle;
2847 int size = mmap_event->event.header.size;
2848 int ret = perf_output_begin(&handle, counter, size, 0, 0);
2850 if (ret)
2851 return;
2853 mmap_event->event.pid = perf_counter_pid(counter, current);
2854 mmap_event->event.tid = perf_counter_tid(counter, current);
2856 perf_output_put(&handle, mmap_event->event);
2857 perf_output_copy(&handle, mmap_event->file_name,
2858 mmap_event->file_size);
2859 perf_output_end(&handle);
2862 static int perf_counter_mmap_match(struct perf_counter *counter,
2863 struct perf_mmap_event *mmap_event)
2865 if (counter->attr.mmap)
2866 return 1;
2868 return 0;
2871 static void perf_counter_mmap_ctx(struct perf_counter_context *ctx,
2872 struct perf_mmap_event *mmap_event)
2874 struct perf_counter *counter;
2876 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2877 return;
2879 rcu_read_lock();
2880 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2881 if (perf_counter_mmap_match(counter, mmap_event))
2882 perf_counter_mmap_output(counter, mmap_event);
2884 rcu_read_unlock();
2887 static void perf_counter_mmap_event(struct perf_mmap_event *mmap_event)
2889 struct perf_cpu_context *cpuctx;
2890 struct perf_counter_context *ctx;
2891 struct vm_area_struct *vma = mmap_event->vma;
2892 struct file *file = vma->vm_file;
2893 unsigned int size;
2894 char tmp[16];
2895 char *buf = NULL;
2896 const char *name;
2898 if (file) {
2899 buf = kzalloc(PATH_MAX, GFP_KERNEL);
2900 if (!buf) {
2901 name = strncpy(tmp, "//enomem", sizeof(tmp));
2902 goto got_name;
2904 name = d_path(&file->f_path, buf, PATH_MAX);
2905 if (IS_ERR(name)) {
2906 name = strncpy(tmp, "//toolong", sizeof(tmp));
2907 goto got_name;
2909 } else {
2910 name = arch_vma_name(mmap_event->vma);
2911 if (name)
2912 goto got_name;
2914 if (!vma->vm_mm) {
2915 name = strncpy(tmp, "[vdso]", sizeof(tmp));
2916 goto got_name;
2919 name = strncpy(tmp, "//anon", sizeof(tmp));
2920 goto got_name;
2923 got_name:
2924 size = ALIGN(strlen(name)+1, sizeof(u64));
2926 mmap_event->file_name = name;
2927 mmap_event->file_size = size;
2929 mmap_event->event.header.size = sizeof(mmap_event->event) + size;
2931 cpuctx = &get_cpu_var(perf_cpu_context);
2932 perf_counter_mmap_ctx(&cpuctx->ctx, mmap_event);
2933 put_cpu_var(perf_cpu_context);
2935 rcu_read_lock();
2937 * doesn't really matter which of the child contexts the
2938 * events ends up in.
2940 ctx = rcu_dereference(current->perf_counter_ctxp);
2941 if (ctx)
2942 perf_counter_mmap_ctx(ctx, mmap_event);
2943 rcu_read_unlock();
2945 kfree(buf);
2948 void __perf_counter_mmap(struct vm_area_struct *vma)
2950 struct perf_mmap_event mmap_event;
2952 if (!atomic_read(&nr_mmap_counters))
2953 return;
2955 mmap_event = (struct perf_mmap_event){
2956 .vma = vma,
2957 .event = {
2958 .header = { .type = PERF_EVENT_MMAP, },
2959 .start = vma->vm_start,
2960 .len = vma->vm_end - vma->vm_start,
2961 .pgoff = vma->vm_pgoff,
2965 perf_counter_mmap_event(&mmap_event);
2969 * Log sample_period changes so that analyzing tools can re-normalize the
2970 * event flow.
2973 struct freq_event {
2974 struct perf_event_header header;
2975 u64 time;
2976 u64 id;
2977 u64 period;
2980 static void perf_log_period(struct perf_counter *counter, u64 period)
2982 struct perf_output_handle handle;
2983 struct freq_event event;
2984 int ret;
2986 if (counter->hw.sample_period == period)
2987 return;
2989 if (counter->attr.sample_type & PERF_SAMPLE_PERIOD)
2990 return;
2992 event = (struct freq_event) {
2993 .header = {
2994 .type = PERF_EVENT_PERIOD,
2995 .misc = 0,
2996 .size = sizeof(event),
2998 .time = sched_clock(),
2999 .id = counter->id,
3000 .period = period,
3003 ret = perf_output_begin(&handle, counter, sizeof(event), 1, 0);
3004 if (ret)
3005 return;
3007 perf_output_put(&handle, event);
3008 perf_output_end(&handle);
3012 * IRQ throttle logging
3015 static void perf_log_throttle(struct perf_counter *counter, int enable)
3017 struct perf_output_handle handle;
3018 int ret;
3020 struct {
3021 struct perf_event_header header;
3022 u64 time;
3023 u64 id;
3024 } throttle_event = {
3025 .header = {
3026 .type = PERF_EVENT_THROTTLE + 1,
3027 .misc = 0,
3028 .size = sizeof(throttle_event),
3030 .time = sched_clock(),
3031 .id = counter->id,
3034 ret = perf_output_begin(&handle, counter, sizeof(throttle_event), 1, 0);
3035 if (ret)
3036 return;
3038 perf_output_put(&handle, throttle_event);
3039 perf_output_end(&handle);
3043 * Generic counter overflow handling, sampling.
3046 int perf_counter_overflow(struct perf_counter *counter, int nmi,
3047 struct perf_sample_data *data)
3049 int events = atomic_read(&counter->event_limit);
3050 int throttle = counter->pmu->unthrottle != NULL;
3051 struct hw_perf_counter *hwc = &counter->hw;
3052 int ret = 0;
3054 if (!throttle) {
3055 hwc->interrupts++;
3056 } else {
3057 if (hwc->interrupts != MAX_INTERRUPTS) {
3058 hwc->interrupts++;
3059 if (HZ * hwc->interrupts >
3060 (u64)sysctl_perf_counter_sample_rate) {
3061 hwc->interrupts = MAX_INTERRUPTS;
3062 perf_log_throttle(counter, 0);
3063 ret = 1;
3065 } else {
3067 * Keep re-disabling counters even though on the previous
3068 * pass we disabled it - just in case we raced with a
3069 * sched-in and the counter got enabled again:
3071 ret = 1;
3075 if (counter->attr.freq) {
3076 u64 now = sched_clock();
3077 s64 delta = now - hwc->freq_stamp;
3079 hwc->freq_stamp = now;
3081 if (delta > 0 && delta < TICK_NSEC)
3082 perf_adjust_period(counter, NSEC_PER_SEC / (int)delta);
3086 * XXX event_limit might not quite work as expected on inherited
3087 * counters
3090 counter->pending_kill = POLL_IN;
3091 if (events && atomic_dec_and_test(&counter->event_limit)) {
3092 ret = 1;
3093 counter->pending_kill = POLL_HUP;
3094 if (nmi) {
3095 counter->pending_disable = 1;
3096 perf_pending_queue(&counter->pending,
3097 perf_pending_counter);
3098 } else
3099 perf_counter_disable(counter);
3102 perf_counter_output(counter, nmi, data);
3103 return ret;
3107 * Generic software counter infrastructure
3110 static void perf_swcounter_update(struct perf_counter *counter)
3112 struct hw_perf_counter *hwc = &counter->hw;
3113 u64 prev, now;
3114 s64 delta;
3116 again:
3117 prev = atomic64_read(&hwc->prev_count);
3118 now = atomic64_read(&hwc->count);
3119 if (atomic64_cmpxchg(&hwc->prev_count, prev, now) != prev)
3120 goto again;
3122 delta = now - prev;
3124 atomic64_add(delta, &counter->count);
3125 atomic64_sub(delta, &hwc->period_left);
3128 static void perf_swcounter_set_period(struct perf_counter *counter)
3130 struct hw_perf_counter *hwc = &counter->hw;
3131 s64 left = atomic64_read(&hwc->period_left);
3132 s64 period = hwc->sample_period;
3134 if (unlikely(left <= -period)) {
3135 left = period;
3136 atomic64_set(&hwc->period_left, left);
3137 hwc->last_period = period;
3140 if (unlikely(left <= 0)) {
3141 left += period;
3142 atomic64_add(period, &hwc->period_left);
3143 hwc->last_period = period;
3146 atomic64_set(&hwc->prev_count, -left);
3147 atomic64_set(&hwc->count, -left);
3150 static enum hrtimer_restart perf_swcounter_hrtimer(struct hrtimer *hrtimer)
3152 enum hrtimer_restart ret = HRTIMER_RESTART;
3153 struct perf_sample_data data;
3154 struct perf_counter *counter;
3155 u64 period;
3157 counter = container_of(hrtimer, struct perf_counter, hw.hrtimer);
3158 counter->pmu->read(counter);
3160 data.addr = 0;
3161 data.regs = get_irq_regs();
3163 * In case we exclude kernel IPs or are somehow not in interrupt
3164 * context, provide the next best thing, the user IP.
3166 if ((counter->attr.exclude_kernel || !data.regs) &&
3167 !counter->attr.exclude_user)
3168 data.regs = task_pt_regs(current);
3170 if (data.regs) {
3171 if (perf_counter_overflow(counter, 0, &data))
3172 ret = HRTIMER_NORESTART;
3175 period = max_t(u64, 10000, counter->hw.sample_period);
3176 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
3178 return ret;
3181 static void perf_swcounter_overflow(struct perf_counter *counter,
3182 int nmi, struct perf_sample_data *data)
3184 data->period = counter->hw.last_period;
3186 perf_swcounter_update(counter);
3187 perf_swcounter_set_period(counter);
3188 if (perf_counter_overflow(counter, nmi, data))
3189 /* soft-disable the counter */
3193 static int perf_swcounter_is_counting(struct perf_counter *counter)
3195 struct perf_counter_context *ctx;
3196 unsigned long flags;
3197 int count;
3199 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
3200 return 1;
3202 if (counter->state != PERF_COUNTER_STATE_INACTIVE)
3203 return 0;
3206 * If the counter is inactive, it could be just because
3207 * its task is scheduled out, or because it's in a group
3208 * which could not go on the PMU. We want to count in
3209 * the first case but not the second. If the context is
3210 * currently active then an inactive software counter must
3211 * be the second case. If it's not currently active then
3212 * we need to know whether the counter was active when the
3213 * context was last active, which we can determine by
3214 * comparing counter->tstamp_stopped with ctx->time.
3216 * We are within an RCU read-side critical section,
3217 * which protects the existence of *ctx.
3219 ctx = counter->ctx;
3220 spin_lock_irqsave(&ctx->lock, flags);
3221 count = 1;
3222 /* Re-check state now we have the lock */
3223 if (counter->state < PERF_COUNTER_STATE_INACTIVE ||
3224 counter->ctx->is_active ||
3225 counter->tstamp_stopped < ctx->time)
3226 count = 0;
3227 spin_unlock_irqrestore(&ctx->lock, flags);
3228 return count;
3231 static int perf_swcounter_match(struct perf_counter *counter,
3232 enum perf_type_id type,
3233 u32 event, struct pt_regs *regs)
3235 if (!perf_swcounter_is_counting(counter))
3236 return 0;
3238 if (counter->attr.type != type)
3239 return 0;
3240 if (counter->attr.config != event)
3241 return 0;
3243 if (regs) {
3244 if (counter->attr.exclude_user && user_mode(regs))
3245 return 0;
3247 if (counter->attr.exclude_kernel && !user_mode(regs))
3248 return 0;
3251 return 1;
3254 static void perf_swcounter_add(struct perf_counter *counter, u64 nr,
3255 int nmi, struct perf_sample_data *data)
3257 int neg = atomic64_add_negative(nr, &counter->hw.count);
3259 if (counter->hw.sample_period && !neg && data->regs)
3260 perf_swcounter_overflow(counter, nmi, data);
3263 static void perf_swcounter_ctx_event(struct perf_counter_context *ctx,
3264 enum perf_type_id type,
3265 u32 event, u64 nr, int nmi,
3266 struct perf_sample_data *data)
3268 struct perf_counter *counter;
3270 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
3271 return;
3273 rcu_read_lock();
3274 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
3275 if (perf_swcounter_match(counter, type, event, data->regs))
3276 perf_swcounter_add(counter, nr, nmi, data);
3278 rcu_read_unlock();
3281 static int *perf_swcounter_recursion_context(struct perf_cpu_context *cpuctx)
3283 if (in_nmi())
3284 return &cpuctx->recursion[3];
3286 if (in_irq())
3287 return &cpuctx->recursion[2];
3289 if (in_softirq())
3290 return &cpuctx->recursion[1];
3292 return &cpuctx->recursion[0];
3295 static void do_perf_swcounter_event(enum perf_type_id type, u32 event,
3296 u64 nr, int nmi,
3297 struct perf_sample_data *data)
3299 struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
3300 int *recursion = perf_swcounter_recursion_context(cpuctx);
3301 struct perf_counter_context *ctx;
3303 if (*recursion)
3304 goto out;
3306 (*recursion)++;
3307 barrier();
3309 perf_swcounter_ctx_event(&cpuctx->ctx, type, event,
3310 nr, nmi, data);
3311 rcu_read_lock();
3313 * doesn't really matter which of the child contexts the
3314 * events ends up in.
3316 ctx = rcu_dereference(current->perf_counter_ctxp);
3317 if (ctx)
3318 perf_swcounter_ctx_event(ctx, type, event, nr, nmi, data);
3319 rcu_read_unlock();
3321 barrier();
3322 (*recursion)--;
3324 out:
3325 put_cpu_var(perf_cpu_context);
3328 void __perf_swcounter_event(u32 event, u64 nr, int nmi,
3329 struct pt_regs *regs, u64 addr)
3331 struct perf_sample_data data = {
3332 .regs = regs,
3333 .addr = addr,
3336 do_perf_swcounter_event(PERF_TYPE_SOFTWARE, event, nr, nmi, &data);
3339 static void perf_swcounter_read(struct perf_counter *counter)
3341 perf_swcounter_update(counter);
3344 static int perf_swcounter_enable(struct perf_counter *counter)
3346 perf_swcounter_set_period(counter);
3347 return 0;
3350 static void perf_swcounter_disable(struct perf_counter *counter)
3352 perf_swcounter_update(counter);
3355 static const struct pmu perf_ops_generic = {
3356 .enable = perf_swcounter_enable,
3357 .disable = perf_swcounter_disable,
3358 .read = perf_swcounter_read,
3362 * Software counter: cpu wall time clock
3365 static void cpu_clock_perf_counter_update(struct perf_counter *counter)
3367 int cpu = raw_smp_processor_id();
3368 s64 prev;
3369 u64 now;
3371 now = cpu_clock(cpu);
3372 prev = atomic64_read(&counter->hw.prev_count);
3373 atomic64_set(&counter->hw.prev_count, now);
3374 atomic64_add(now - prev, &counter->count);
3377 static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
3379 struct hw_perf_counter *hwc = &counter->hw;
3380 int cpu = raw_smp_processor_id();
3382 atomic64_set(&hwc->prev_count, cpu_clock(cpu));
3383 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3384 hwc->hrtimer.function = perf_swcounter_hrtimer;
3385 if (hwc->sample_period) {
3386 u64 period = max_t(u64, 10000, hwc->sample_period);
3387 __hrtimer_start_range_ns(&hwc->hrtimer,
3388 ns_to_ktime(period), 0,
3389 HRTIMER_MODE_REL, 0);
3392 return 0;
3395 static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
3397 if (counter->hw.sample_period)
3398 hrtimer_cancel(&counter->hw.hrtimer);
3399 cpu_clock_perf_counter_update(counter);
3402 static void cpu_clock_perf_counter_read(struct perf_counter *counter)
3404 cpu_clock_perf_counter_update(counter);
3407 static const struct pmu perf_ops_cpu_clock = {
3408 .enable = cpu_clock_perf_counter_enable,
3409 .disable = cpu_clock_perf_counter_disable,
3410 .read = cpu_clock_perf_counter_read,
3414 * Software counter: task time clock
3417 static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
3419 u64 prev;
3420 s64 delta;
3422 prev = atomic64_xchg(&counter->hw.prev_count, now);
3423 delta = now - prev;
3424 atomic64_add(delta, &counter->count);
3427 static int task_clock_perf_counter_enable(struct perf_counter *counter)
3429 struct hw_perf_counter *hwc = &counter->hw;
3430 u64 now;
3432 now = counter->ctx->time;
3434 atomic64_set(&hwc->prev_count, now);
3435 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3436 hwc->hrtimer.function = perf_swcounter_hrtimer;
3437 if (hwc->sample_period) {
3438 u64 period = max_t(u64, 10000, hwc->sample_period);
3439 __hrtimer_start_range_ns(&hwc->hrtimer,
3440 ns_to_ktime(period), 0,
3441 HRTIMER_MODE_REL, 0);
3444 return 0;
3447 static void task_clock_perf_counter_disable(struct perf_counter *counter)
3449 if (counter->hw.sample_period)
3450 hrtimer_cancel(&counter->hw.hrtimer);
3451 task_clock_perf_counter_update(counter, counter->ctx->time);
3455 static void task_clock_perf_counter_read(struct perf_counter *counter)
3457 u64 time;
3459 if (!in_nmi()) {
3460 update_context_time(counter->ctx);
3461 time = counter->ctx->time;
3462 } else {
3463 u64 now = perf_clock();
3464 u64 delta = now - counter->ctx->timestamp;
3465 time = counter->ctx->time + delta;
3468 task_clock_perf_counter_update(counter, time);
3471 static const struct pmu perf_ops_task_clock = {
3472 .enable = task_clock_perf_counter_enable,
3473 .disable = task_clock_perf_counter_disable,
3474 .read = task_clock_perf_counter_read,
3477 #ifdef CONFIG_EVENT_PROFILE
3478 void perf_tpcounter_event(int event_id)
3480 struct perf_sample_data data = {
3481 .regs = get_irq_regs();
3482 .addr = 0,
3485 if (!data.regs)
3486 data.regs = task_pt_regs(current);
3488 do_perf_swcounter_event(PERF_TYPE_TRACEPOINT, event_id, 1, 1, &data);
3490 EXPORT_SYMBOL_GPL(perf_tpcounter_event);
3492 extern int ftrace_profile_enable(int);
3493 extern void ftrace_profile_disable(int);
3495 static void tp_perf_counter_destroy(struct perf_counter *counter)
3497 ftrace_profile_disable(perf_event_id(&counter->attr));
3500 static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
3502 int event_id = perf_event_id(&counter->attr);
3503 int ret;
3505 ret = ftrace_profile_enable(event_id);
3506 if (ret)
3507 return NULL;
3509 counter->destroy = tp_perf_counter_destroy;
3511 return &perf_ops_generic;
3513 #else
3514 static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
3516 return NULL;
3518 #endif
3520 atomic_t perf_swcounter_enabled[PERF_COUNT_SW_MAX];
3522 static void sw_perf_counter_destroy(struct perf_counter *counter)
3524 u64 event = counter->attr.config;
3526 WARN_ON(counter->parent);
3528 atomic_dec(&perf_swcounter_enabled[event]);
3531 static const struct pmu *sw_perf_counter_init(struct perf_counter *counter)
3533 const struct pmu *pmu = NULL;
3534 u64 event = counter->attr.config;
3537 * Software counters (currently) can't in general distinguish
3538 * between user, kernel and hypervisor events.
3539 * However, context switches and cpu migrations are considered
3540 * to be kernel events, and page faults are never hypervisor
3541 * events.
3543 switch (event) {
3544 case PERF_COUNT_SW_CPU_CLOCK:
3545 pmu = &perf_ops_cpu_clock;
3547 break;
3548 case PERF_COUNT_SW_TASK_CLOCK:
3550 * If the user instantiates this as a per-cpu counter,
3551 * use the cpu_clock counter instead.
3553 if (counter->ctx->task)
3554 pmu = &perf_ops_task_clock;
3555 else
3556 pmu = &perf_ops_cpu_clock;
3558 break;
3559 case PERF_COUNT_SW_PAGE_FAULTS:
3560 case PERF_COUNT_SW_PAGE_FAULTS_MIN:
3561 case PERF_COUNT_SW_PAGE_FAULTS_MAJ:
3562 case PERF_COUNT_SW_CONTEXT_SWITCHES:
3563 case PERF_COUNT_SW_CPU_MIGRATIONS:
3564 if (!counter->parent) {
3565 atomic_inc(&perf_swcounter_enabled[event]);
3566 counter->destroy = sw_perf_counter_destroy;
3568 pmu = &perf_ops_generic;
3569 break;
3572 return pmu;
3576 * Allocate and initialize a counter structure
3578 static struct perf_counter *
3579 perf_counter_alloc(struct perf_counter_attr *attr,
3580 int cpu,
3581 struct perf_counter_context *ctx,
3582 struct perf_counter *group_leader,
3583 struct perf_counter *parent_counter,
3584 gfp_t gfpflags)
3586 const struct pmu *pmu;
3587 struct perf_counter *counter;
3588 struct hw_perf_counter *hwc;
3589 long err;
3591 counter = kzalloc(sizeof(*counter), gfpflags);
3592 if (!counter)
3593 return ERR_PTR(-ENOMEM);
3596 * Single counters are their own group leaders, with an
3597 * empty sibling list:
3599 if (!group_leader)
3600 group_leader = counter;
3602 mutex_init(&counter->child_mutex);
3603 INIT_LIST_HEAD(&counter->child_list);
3605 INIT_LIST_HEAD(&counter->list_entry);
3606 INIT_LIST_HEAD(&counter->event_entry);
3607 INIT_LIST_HEAD(&counter->sibling_list);
3608 init_waitqueue_head(&counter->waitq);
3610 mutex_init(&counter->mmap_mutex);
3612 counter->cpu = cpu;
3613 counter->attr = *attr;
3614 counter->group_leader = group_leader;
3615 counter->pmu = NULL;
3616 counter->ctx = ctx;
3617 counter->oncpu = -1;
3619 counter->parent = parent_counter;
3621 counter->ns = get_pid_ns(current->nsproxy->pid_ns);
3622 counter->id = atomic64_inc_return(&perf_counter_id);
3624 counter->state = PERF_COUNTER_STATE_INACTIVE;
3626 if (attr->disabled)
3627 counter->state = PERF_COUNTER_STATE_OFF;
3629 pmu = NULL;
3631 hwc = &counter->hw;
3632 hwc->sample_period = attr->sample_period;
3633 if (attr->freq && attr->sample_freq)
3634 hwc->sample_period = 1;
3636 atomic64_set(&hwc->period_left, hwc->sample_period);
3639 * we currently do not support PERF_SAMPLE_GROUP on inherited counters
3641 if (attr->inherit && (attr->sample_type & PERF_SAMPLE_GROUP))
3642 goto done;
3644 switch (attr->type) {
3645 case PERF_TYPE_RAW:
3646 case PERF_TYPE_HARDWARE:
3647 case PERF_TYPE_HW_CACHE:
3648 pmu = hw_perf_counter_init(counter);
3649 break;
3651 case PERF_TYPE_SOFTWARE:
3652 pmu = sw_perf_counter_init(counter);
3653 break;
3655 case PERF_TYPE_TRACEPOINT:
3656 pmu = tp_perf_counter_init(counter);
3657 break;
3659 default:
3660 break;
3662 done:
3663 err = 0;
3664 if (!pmu)
3665 err = -EINVAL;
3666 else if (IS_ERR(pmu))
3667 err = PTR_ERR(pmu);
3669 if (err) {
3670 if (counter->ns)
3671 put_pid_ns(counter->ns);
3672 kfree(counter);
3673 return ERR_PTR(err);
3676 counter->pmu = pmu;
3678 if (!counter->parent) {
3679 atomic_inc(&nr_counters);
3680 if (counter->attr.mmap)
3681 atomic_inc(&nr_mmap_counters);
3682 if (counter->attr.comm)
3683 atomic_inc(&nr_comm_counters);
3686 return counter;
3689 static int perf_copy_attr(struct perf_counter_attr __user *uattr,
3690 struct perf_counter_attr *attr)
3692 int ret;
3693 u32 size;
3695 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
3696 return -EFAULT;
3699 * zero the full structure, so that a short copy will be nice.
3701 memset(attr, 0, sizeof(*attr));
3703 ret = get_user(size, &uattr->size);
3704 if (ret)
3705 return ret;
3707 if (size > PAGE_SIZE) /* silly large */
3708 goto err_size;
3710 if (!size) /* abi compat */
3711 size = PERF_ATTR_SIZE_VER0;
3713 if (size < PERF_ATTR_SIZE_VER0)
3714 goto err_size;
3717 * If we're handed a bigger struct than we know of,
3718 * ensure all the unknown bits are 0.
3720 if (size > sizeof(*attr)) {
3721 unsigned long val;
3722 unsigned long __user *addr;
3723 unsigned long __user *end;
3725 addr = PTR_ALIGN((void __user *)uattr + sizeof(*attr),
3726 sizeof(unsigned long));
3727 end = PTR_ALIGN((void __user *)uattr + size,
3728 sizeof(unsigned long));
3730 for (; addr < end; addr += sizeof(unsigned long)) {
3731 ret = get_user(val, addr);
3732 if (ret)
3733 return ret;
3734 if (val)
3735 goto err_size;
3739 ret = copy_from_user(attr, uattr, size);
3740 if (ret)
3741 return -EFAULT;
3744 * If the type exists, the corresponding creation will verify
3745 * the attr->config.
3747 if (attr->type >= PERF_TYPE_MAX)
3748 return -EINVAL;
3750 if (attr->__reserved_1 || attr->__reserved_2 || attr->__reserved_3)
3751 return -EINVAL;
3753 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
3754 return -EINVAL;
3756 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
3757 return -EINVAL;
3759 out:
3760 return ret;
3762 err_size:
3763 put_user(sizeof(*attr), &uattr->size);
3764 ret = -E2BIG;
3765 goto out;
3769 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
3771 * @attr_uptr: event type attributes for monitoring/sampling
3772 * @pid: target pid
3773 * @cpu: target cpu
3774 * @group_fd: group leader counter fd
3776 SYSCALL_DEFINE5(perf_counter_open,
3777 struct perf_counter_attr __user *, attr_uptr,
3778 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
3780 struct perf_counter *counter, *group_leader;
3781 struct perf_counter_attr attr;
3782 struct perf_counter_context *ctx;
3783 struct file *counter_file = NULL;
3784 struct file *group_file = NULL;
3785 int fput_needed = 0;
3786 int fput_needed2 = 0;
3787 int ret;
3789 /* for future expandability... */
3790 if (flags)
3791 return -EINVAL;
3793 ret = perf_copy_attr(attr_uptr, &attr);
3794 if (ret)
3795 return ret;
3797 if (!attr.exclude_kernel) {
3798 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
3799 return -EACCES;
3802 if (attr.freq) {
3803 if (attr.sample_freq > sysctl_perf_counter_sample_rate)
3804 return -EINVAL;
3808 * Get the target context (task or percpu):
3810 ctx = find_get_context(pid, cpu);
3811 if (IS_ERR(ctx))
3812 return PTR_ERR(ctx);
3815 * Look up the group leader (we will attach this counter to it):
3817 group_leader = NULL;
3818 if (group_fd != -1) {
3819 ret = -EINVAL;
3820 group_file = fget_light(group_fd, &fput_needed);
3821 if (!group_file)
3822 goto err_put_context;
3823 if (group_file->f_op != &perf_fops)
3824 goto err_put_context;
3826 group_leader = group_file->private_data;
3828 * Do not allow a recursive hierarchy (this new sibling
3829 * becoming part of another group-sibling):
3831 if (group_leader->group_leader != group_leader)
3832 goto err_put_context;
3834 * Do not allow to attach to a group in a different
3835 * task or CPU context:
3837 if (group_leader->ctx != ctx)
3838 goto err_put_context;
3840 * Only a group leader can be exclusive or pinned
3842 if (attr.exclusive || attr.pinned)
3843 goto err_put_context;
3846 counter = perf_counter_alloc(&attr, cpu, ctx, group_leader,
3847 NULL, GFP_KERNEL);
3848 ret = PTR_ERR(counter);
3849 if (IS_ERR(counter))
3850 goto err_put_context;
3852 ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
3853 if (ret < 0)
3854 goto err_free_put_context;
3856 counter_file = fget_light(ret, &fput_needed2);
3857 if (!counter_file)
3858 goto err_free_put_context;
3860 counter->filp = counter_file;
3861 WARN_ON_ONCE(ctx->parent_ctx);
3862 mutex_lock(&ctx->mutex);
3863 perf_install_in_context(ctx, counter, cpu);
3864 ++ctx->generation;
3865 mutex_unlock(&ctx->mutex);
3867 counter->owner = current;
3868 get_task_struct(current);
3869 mutex_lock(&current->perf_counter_mutex);
3870 list_add_tail(&counter->owner_entry, &current->perf_counter_list);
3871 mutex_unlock(&current->perf_counter_mutex);
3873 fput_light(counter_file, fput_needed2);
3875 out_fput:
3876 fput_light(group_file, fput_needed);
3878 return ret;
3880 err_free_put_context:
3881 kfree(counter);
3883 err_put_context:
3884 put_ctx(ctx);
3886 goto out_fput;
3890 * inherit a counter from parent task to child task:
3892 static struct perf_counter *
3893 inherit_counter(struct perf_counter *parent_counter,
3894 struct task_struct *parent,
3895 struct perf_counter_context *parent_ctx,
3896 struct task_struct *child,
3897 struct perf_counter *group_leader,
3898 struct perf_counter_context *child_ctx)
3900 struct perf_counter *child_counter;
3903 * Instead of creating recursive hierarchies of counters,
3904 * we link inherited counters back to the original parent,
3905 * which has a filp for sure, which we use as the reference
3906 * count:
3908 if (parent_counter->parent)
3909 parent_counter = parent_counter->parent;
3911 child_counter = perf_counter_alloc(&parent_counter->attr,
3912 parent_counter->cpu, child_ctx,
3913 group_leader, parent_counter,
3914 GFP_KERNEL);
3915 if (IS_ERR(child_counter))
3916 return child_counter;
3917 get_ctx(child_ctx);
3920 * Make the child state follow the state of the parent counter,
3921 * not its attr.disabled bit. We hold the parent's mutex,
3922 * so we won't race with perf_counter_{en, dis}able_family.
3924 if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE)
3925 child_counter->state = PERF_COUNTER_STATE_INACTIVE;
3926 else
3927 child_counter->state = PERF_COUNTER_STATE_OFF;
3929 if (parent_counter->attr.freq)
3930 child_counter->hw.sample_period = parent_counter->hw.sample_period;
3933 * Link it up in the child's context:
3935 add_counter_to_ctx(child_counter, child_ctx);
3938 * Get a reference to the parent filp - we will fput it
3939 * when the child counter exits. This is safe to do because
3940 * we are in the parent and we know that the filp still
3941 * exists and has a nonzero count:
3943 atomic_long_inc(&parent_counter->filp->f_count);
3946 * Link this into the parent counter's child list
3948 WARN_ON_ONCE(parent_counter->ctx->parent_ctx);
3949 mutex_lock(&parent_counter->child_mutex);
3950 list_add_tail(&child_counter->child_list, &parent_counter->child_list);
3951 mutex_unlock(&parent_counter->child_mutex);
3953 return child_counter;
3956 static int inherit_group(struct perf_counter *parent_counter,
3957 struct task_struct *parent,
3958 struct perf_counter_context *parent_ctx,
3959 struct task_struct *child,
3960 struct perf_counter_context *child_ctx)
3962 struct perf_counter *leader;
3963 struct perf_counter *sub;
3964 struct perf_counter *child_ctr;
3966 leader = inherit_counter(parent_counter, parent, parent_ctx,
3967 child, NULL, child_ctx);
3968 if (IS_ERR(leader))
3969 return PTR_ERR(leader);
3970 list_for_each_entry(sub, &parent_counter->sibling_list, list_entry) {
3971 child_ctr = inherit_counter(sub, parent, parent_ctx,
3972 child, leader, child_ctx);
3973 if (IS_ERR(child_ctr))
3974 return PTR_ERR(child_ctr);
3976 return 0;
3979 static void sync_child_counter(struct perf_counter *child_counter,
3980 struct perf_counter *parent_counter)
3982 u64 child_val;
3984 child_val = atomic64_read(&child_counter->count);
3987 * Add back the child's count to the parent's count:
3989 atomic64_add(child_val, &parent_counter->count);
3990 atomic64_add(child_counter->total_time_enabled,
3991 &parent_counter->child_total_time_enabled);
3992 atomic64_add(child_counter->total_time_running,
3993 &parent_counter->child_total_time_running);
3996 * Remove this counter from the parent's list
3998 WARN_ON_ONCE(parent_counter->ctx->parent_ctx);
3999 mutex_lock(&parent_counter->child_mutex);
4000 list_del_init(&child_counter->child_list);
4001 mutex_unlock(&parent_counter->child_mutex);
4004 * Release the parent counter, if this was the last
4005 * reference to it.
4007 fput(parent_counter->filp);
4010 static void
4011 __perf_counter_exit_task(struct perf_counter *child_counter,
4012 struct perf_counter_context *child_ctx)
4014 struct perf_counter *parent_counter;
4016 update_counter_times(child_counter);
4017 perf_counter_remove_from_context(child_counter);
4019 parent_counter = child_counter->parent;
4021 * It can happen that parent exits first, and has counters
4022 * that are still around due to the child reference. These
4023 * counters need to be zapped - but otherwise linger.
4025 if (parent_counter) {
4026 sync_child_counter(child_counter, parent_counter);
4027 free_counter(child_counter);
4032 * When a child task exits, feed back counter values to parent counters.
4034 void perf_counter_exit_task(struct task_struct *child)
4036 struct perf_counter *child_counter, *tmp;
4037 struct perf_counter_context *child_ctx;
4038 unsigned long flags;
4040 if (likely(!child->perf_counter_ctxp))
4041 return;
4043 local_irq_save(flags);
4045 * We can't reschedule here because interrupts are disabled,
4046 * and either child is current or it is a task that can't be
4047 * scheduled, so we are now safe from rescheduling changing
4048 * our context.
4050 child_ctx = child->perf_counter_ctxp;
4051 __perf_counter_task_sched_out(child_ctx);
4054 * Take the context lock here so that if find_get_context is
4055 * reading child->perf_counter_ctxp, we wait until it has
4056 * incremented the context's refcount before we do put_ctx below.
4058 spin_lock(&child_ctx->lock);
4059 child->perf_counter_ctxp = NULL;
4060 if (child_ctx->parent_ctx) {
4062 * This context is a clone; unclone it so it can't get
4063 * swapped to another process while we're removing all
4064 * the counters from it.
4066 put_ctx(child_ctx->parent_ctx);
4067 child_ctx->parent_ctx = NULL;
4069 spin_unlock(&child_ctx->lock);
4070 local_irq_restore(flags);
4073 * We can recurse on the same lock type through:
4075 * __perf_counter_exit_task()
4076 * sync_child_counter()
4077 * fput(parent_counter->filp)
4078 * perf_release()
4079 * mutex_lock(&ctx->mutex)
4081 * But since its the parent context it won't be the same instance.
4083 mutex_lock_nested(&child_ctx->mutex, SINGLE_DEPTH_NESTING);
4085 again:
4086 list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
4087 list_entry)
4088 __perf_counter_exit_task(child_counter, child_ctx);
4091 * If the last counter was a group counter, it will have appended all
4092 * its siblings to the list, but we obtained 'tmp' before that which
4093 * will still point to the list head terminating the iteration.
4095 if (!list_empty(&child_ctx->counter_list))
4096 goto again;
4098 mutex_unlock(&child_ctx->mutex);
4100 put_ctx(child_ctx);
4104 * free an unexposed, unused context as created by inheritance by
4105 * init_task below, used by fork() in case of fail.
4107 void perf_counter_free_task(struct task_struct *task)
4109 struct perf_counter_context *ctx = task->perf_counter_ctxp;
4110 struct perf_counter *counter, *tmp;
4112 if (!ctx)
4113 return;
4115 mutex_lock(&ctx->mutex);
4116 again:
4117 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry) {
4118 struct perf_counter *parent = counter->parent;
4120 if (WARN_ON_ONCE(!parent))
4121 continue;
4123 mutex_lock(&parent->child_mutex);
4124 list_del_init(&counter->child_list);
4125 mutex_unlock(&parent->child_mutex);
4127 fput(parent->filp);
4129 list_del_counter(counter, ctx);
4130 free_counter(counter);
4133 if (!list_empty(&ctx->counter_list))
4134 goto again;
4136 mutex_unlock(&ctx->mutex);
4138 put_ctx(ctx);
4142 * Initialize the perf_counter context in task_struct
4144 int perf_counter_init_task(struct task_struct *child)
4146 struct perf_counter_context *child_ctx, *parent_ctx;
4147 struct perf_counter_context *cloned_ctx;
4148 struct perf_counter *counter;
4149 struct task_struct *parent = current;
4150 int inherited_all = 1;
4151 int ret = 0;
4153 child->perf_counter_ctxp = NULL;
4155 mutex_init(&child->perf_counter_mutex);
4156 INIT_LIST_HEAD(&child->perf_counter_list);
4158 if (likely(!parent->perf_counter_ctxp))
4159 return 0;
4162 * This is executed from the parent task context, so inherit
4163 * counters that have been marked for cloning.
4164 * First allocate and initialize a context for the child.
4167 child_ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
4168 if (!child_ctx)
4169 return -ENOMEM;
4171 __perf_counter_init_context(child_ctx, child);
4172 child->perf_counter_ctxp = child_ctx;
4173 get_task_struct(child);
4176 * If the parent's context is a clone, pin it so it won't get
4177 * swapped under us.
4179 parent_ctx = perf_pin_task_context(parent);
4182 * No need to check if parent_ctx != NULL here; since we saw
4183 * it non-NULL earlier, the only reason for it to become NULL
4184 * is if we exit, and since we're currently in the middle of
4185 * a fork we can't be exiting at the same time.
4189 * Lock the parent list. No need to lock the child - not PID
4190 * hashed yet and not running, so nobody can access it.
4192 mutex_lock(&parent_ctx->mutex);
4195 * We dont have to disable NMIs - we are only looking at
4196 * the list, not manipulating it:
4198 list_for_each_entry_rcu(counter, &parent_ctx->event_list, event_entry) {
4199 if (counter != counter->group_leader)
4200 continue;
4202 if (!counter->attr.inherit) {
4203 inherited_all = 0;
4204 continue;
4207 ret = inherit_group(counter, parent, parent_ctx,
4208 child, child_ctx);
4209 if (ret) {
4210 inherited_all = 0;
4211 break;
4215 if (inherited_all) {
4217 * Mark the child context as a clone of the parent
4218 * context, or of whatever the parent is a clone of.
4219 * Note that if the parent is a clone, it could get
4220 * uncloned at any point, but that doesn't matter
4221 * because the list of counters and the generation
4222 * count can't have changed since we took the mutex.
4224 cloned_ctx = rcu_dereference(parent_ctx->parent_ctx);
4225 if (cloned_ctx) {
4226 child_ctx->parent_ctx = cloned_ctx;
4227 child_ctx->parent_gen = parent_ctx->parent_gen;
4228 } else {
4229 child_ctx->parent_ctx = parent_ctx;
4230 child_ctx->parent_gen = parent_ctx->generation;
4232 get_ctx(child_ctx->parent_ctx);
4235 mutex_unlock(&parent_ctx->mutex);
4237 perf_unpin_context(parent_ctx);
4239 return ret;
4242 static void __cpuinit perf_counter_init_cpu(int cpu)
4244 struct perf_cpu_context *cpuctx;
4246 cpuctx = &per_cpu(perf_cpu_context, cpu);
4247 __perf_counter_init_context(&cpuctx->ctx, NULL);
4249 spin_lock(&perf_resource_lock);
4250 cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
4251 spin_unlock(&perf_resource_lock);
4253 hw_perf_counter_setup(cpu);
4256 #ifdef CONFIG_HOTPLUG_CPU
4257 static void __perf_counter_exit_cpu(void *info)
4259 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
4260 struct perf_counter_context *ctx = &cpuctx->ctx;
4261 struct perf_counter *counter, *tmp;
4263 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
4264 __perf_counter_remove_from_context(counter);
4266 static void perf_counter_exit_cpu(int cpu)
4268 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
4269 struct perf_counter_context *ctx = &cpuctx->ctx;
4271 mutex_lock(&ctx->mutex);
4272 smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
4273 mutex_unlock(&ctx->mutex);
4275 #else
4276 static inline void perf_counter_exit_cpu(int cpu) { }
4277 #endif
4279 static int __cpuinit
4280 perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
4282 unsigned int cpu = (long)hcpu;
4284 switch (action) {
4286 case CPU_UP_PREPARE:
4287 case CPU_UP_PREPARE_FROZEN:
4288 perf_counter_init_cpu(cpu);
4289 break;
4291 case CPU_DOWN_PREPARE:
4292 case CPU_DOWN_PREPARE_FROZEN:
4293 perf_counter_exit_cpu(cpu);
4294 break;
4296 default:
4297 break;
4300 return NOTIFY_OK;
4304 * This has to have a higher priority than migration_notifier in sched.c.
4306 static struct notifier_block __cpuinitdata perf_cpu_nb = {
4307 .notifier_call = perf_cpu_notify,
4308 .priority = 20,
4311 void __init perf_counter_init(void)
4313 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
4314 (void *)(long)smp_processor_id());
4315 register_cpu_notifier(&perf_cpu_nb);
4318 static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
4320 return sprintf(buf, "%d\n", perf_reserved_percpu);
4323 static ssize_t
4324 perf_set_reserve_percpu(struct sysdev_class *class,
4325 const char *buf,
4326 size_t count)
4328 struct perf_cpu_context *cpuctx;
4329 unsigned long val;
4330 int err, cpu, mpt;
4332 err = strict_strtoul(buf, 10, &val);
4333 if (err)
4334 return err;
4335 if (val > perf_max_counters)
4336 return -EINVAL;
4338 spin_lock(&perf_resource_lock);
4339 perf_reserved_percpu = val;
4340 for_each_online_cpu(cpu) {
4341 cpuctx = &per_cpu(perf_cpu_context, cpu);
4342 spin_lock_irq(&cpuctx->ctx.lock);
4343 mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
4344 perf_max_counters - perf_reserved_percpu);
4345 cpuctx->max_pertask = mpt;
4346 spin_unlock_irq(&cpuctx->ctx.lock);
4348 spin_unlock(&perf_resource_lock);
4350 return count;
4353 static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
4355 return sprintf(buf, "%d\n", perf_overcommit);
4358 static ssize_t
4359 perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
4361 unsigned long val;
4362 int err;
4364 err = strict_strtoul(buf, 10, &val);
4365 if (err)
4366 return err;
4367 if (val > 1)
4368 return -EINVAL;
4370 spin_lock(&perf_resource_lock);
4371 perf_overcommit = val;
4372 spin_unlock(&perf_resource_lock);
4374 return count;
4377 static SYSDEV_CLASS_ATTR(
4378 reserve_percpu,
4379 0644,
4380 perf_show_reserve_percpu,
4381 perf_set_reserve_percpu
4384 static SYSDEV_CLASS_ATTR(
4385 overcommit,
4386 0644,
4387 perf_show_overcommit,
4388 perf_set_overcommit
4391 static struct attribute *perfclass_attrs[] = {
4392 &attr_reserve_percpu.attr,
4393 &attr_overcommit.attr,
4394 NULL
4397 static struct attribute_group perfclass_attr_group = {
4398 .attrs = perfclass_attrs,
4399 .name = "perf_counters",
4402 static int __init perf_counter_sysfs_init(void)
4404 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
4405 &perfclass_attr_group);
4407 device_initcall(perf_counter_sysfs_init);