perf_counter: uncouple data_head updates from wakeups
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / kernel / perf_counter.c
1 /*
2 * Performance counter core code
3 *
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>
8 *
9 * For licensing details see kernel-base/COPYING
10 */
11
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/ptrace.h>
20 #include <linux/percpu.h>
21 #include <linux/vmstat.h>
22 #include <linux/hardirq.h>
23 #include <linux/rculist.h>
24 #include <linux/uaccess.h>
25 #include <linux/syscalls.h>
26 #include <linux/anon_inodes.h>
27 #include <linux/kernel_stat.h>
28 #include <linux/perf_counter.h>
29 #include <linux/dcache.h>
30
31 #include <asm/irq_regs.h>
32
33 /*
34 * Each CPU has a list of per CPU counters:
35 */
36 DEFINE_PER_CPU(struct perf_cpu_context, perf_cpu_context);
37
38 int perf_max_counters __read_mostly = 1;
39 static int perf_reserved_percpu __read_mostly;
40 static int perf_overcommit __read_mostly = 1;
41
42 static atomic_t nr_mmap_tracking __read_mostly;
43 static atomic_t nr_munmap_tracking __read_mostly;
44 static atomic_t nr_comm_tracking __read_mostly;
45
46 int sysctl_perf_counter_priv __read_mostly; /* do we need to be privileged */
47
48 /*
49 * Lock for (sysadmin-configurable) counter reservations:
50 */
51 static DEFINE_SPINLOCK(perf_resource_lock);
52
53 /*
54 * Architecture provided APIs - weak aliases:
55 */
56 extern __weak const struct pmu *hw_perf_counter_init(struct perf_counter *counter)
57 {
58 return NULL;
59 }
60
61 u64 __weak hw_perf_save_disable(void) { return 0; }
62 void __weak hw_perf_restore(u64 ctrl) { barrier(); }
63 void __weak hw_perf_counter_setup(int cpu) { barrier(); }
64 int __weak hw_perf_group_sched_in(struct perf_counter *group_leader,
65 struct perf_cpu_context *cpuctx,
66 struct perf_counter_context *ctx, int cpu)
67 {
68 return 0;
69 }
70
71 void __weak perf_counter_print_debug(void) { }
72
73 static void
74 list_add_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
75 {
76 struct perf_counter *group_leader = counter->group_leader;
77
78 /*
79 * Depending on whether it is a standalone or sibling counter,
80 * add it straight to the context's counter list, or to the group
81 * leader's sibling list:
82 */
83 if (counter->group_leader == counter)
84 list_add_tail(&counter->list_entry, &ctx->counter_list);
85 else {
86 list_add_tail(&counter->list_entry, &group_leader->sibling_list);
87 group_leader->nr_siblings++;
88 }
89
90 list_add_rcu(&counter->event_entry, &ctx->event_list);
91 }
92
93 static void
94 list_del_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
95 {
96 struct perf_counter *sibling, *tmp;
97
98 list_del_init(&counter->list_entry);
99 list_del_rcu(&counter->event_entry);
100
101 if (counter->group_leader != counter)
102 counter->group_leader->nr_siblings--;
103
104 /*
105 * If this was a group counter with sibling counters then
106 * upgrade the siblings to singleton counters by adding them
107 * to the context list directly:
108 */
109 list_for_each_entry_safe(sibling, tmp,
110 &counter->sibling_list, list_entry) {
111
112 list_move_tail(&sibling->list_entry, &ctx->counter_list);
113 sibling->group_leader = sibling;
114 }
115 }
116
117 static void
118 counter_sched_out(struct perf_counter *counter,
119 struct perf_cpu_context *cpuctx,
120 struct perf_counter_context *ctx)
121 {
122 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
123 return;
124
125 counter->state = PERF_COUNTER_STATE_INACTIVE;
126 counter->tstamp_stopped = ctx->time;
127 counter->pmu->disable(counter);
128 counter->oncpu = -1;
129
130 if (!is_software_counter(counter))
131 cpuctx->active_oncpu--;
132 ctx->nr_active--;
133 if (counter->hw_event.exclusive || !cpuctx->active_oncpu)
134 cpuctx->exclusive = 0;
135 }
136
137 static void
138 group_sched_out(struct perf_counter *group_counter,
139 struct perf_cpu_context *cpuctx,
140 struct perf_counter_context *ctx)
141 {
142 struct perf_counter *counter;
143
144 if (group_counter->state != PERF_COUNTER_STATE_ACTIVE)
145 return;
146
147 counter_sched_out(group_counter, cpuctx, ctx);
148
149 /*
150 * Schedule out siblings (if any):
151 */
152 list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
153 counter_sched_out(counter, cpuctx, ctx);
154
155 if (group_counter->hw_event.exclusive)
156 cpuctx->exclusive = 0;
157 }
158
159 /*
160 * Cross CPU call to remove a performance counter
161 *
162 * We disable the counter on the hardware level first. After that we
163 * remove it from the context list.
164 */
165 static void __perf_counter_remove_from_context(void *info)
166 {
167 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
168 struct perf_counter *counter = info;
169 struct perf_counter_context *ctx = counter->ctx;
170 unsigned long flags;
171 u64 perf_flags;
172
173 /*
174 * If this is a task context, we need to check whether it is
175 * the current task context of this cpu. If not it has been
176 * scheduled out before the smp call arrived.
177 */
178 if (ctx->task && cpuctx->task_ctx != ctx)
179 return;
180
181 spin_lock_irqsave(&ctx->lock, flags);
182
183 counter_sched_out(counter, cpuctx, ctx);
184
185 counter->task = NULL;
186 ctx->nr_counters--;
187
188 /*
189 * Protect the list operation against NMI by disabling the
190 * counters on a global level. NOP for non NMI based counters.
191 */
192 perf_flags = hw_perf_save_disable();
193 list_del_counter(counter, ctx);
194 hw_perf_restore(perf_flags);
195
196 if (!ctx->task) {
197 /*
198 * Allow more per task counters with respect to the
199 * reservation:
200 */
201 cpuctx->max_pertask =
202 min(perf_max_counters - ctx->nr_counters,
203 perf_max_counters - perf_reserved_percpu);
204 }
205
206 spin_unlock_irqrestore(&ctx->lock, flags);
207 }
208
209
210 /*
211 * Remove the counter from a task's (or a CPU's) list of counters.
212 *
213 * Must be called with counter->mutex and ctx->mutex held.
214 *
215 * CPU counters are removed with a smp call. For task counters we only
216 * call when the task is on a CPU.
217 */
218 static void perf_counter_remove_from_context(struct perf_counter *counter)
219 {
220 struct perf_counter_context *ctx = counter->ctx;
221 struct task_struct *task = ctx->task;
222
223 if (!task) {
224 /*
225 * Per cpu counters are removed via an smp call and
226 * the removal is always sucessful.
227 */
228 smp_call_function_single(counter->cpu,
229 __perf_counter_remove_from_context,
230 counter, 1);
231 return;
232 }
233
234 retry:
235 task_oncpu_function_call(task, __perf_counter_remove_from_context,
236 counter);
237
238 spin_lock_irq(&ctx->lock);
239 /*
240 * If the context is active we need to retry the smp call.
241 */
242 if (ctx->nr_active && !list_empty(&counter->list_entry)) {
243 spin_unlock_irq(&ctx->lock);
244 goto retry;
245 }
246
247 /*
248 * The lock prevents that this context is scheduled in so we
249 * can remove the counter safely, if the call above did not
250 * succeed.
251 */
252 if (!list_empty(&counter->list_entry)) {
253 ctx->nr_counters--;
254 list_del_counter(counter, ctx);
255 counter->task = NULL;
256 }
257 spin_unlock_irq(&ctx->lock);
258 }
259
260 static inline u64 perf_clock(void)
261 {
262 return cpu_clock(smp_processor_id());
263 }
264
265 /*
266 * Update the record of the current time in a context.
267 */
268 static void update_context_time(struct perf_counter_context *ctx)
269 {
270 u64 now = perf_clock();
271
272 ctx->time += now - ctx->timestamp;
273 ctx->timestamp = now;
274 }
275
276 /*
277 * Update the total_time_enabled and total_time_running fields for a counter.
278 */
279 static void update_counter_times(struct perf_counter *counter)
280 {
281 struct perf_counter_context *ctx = counter->ctx;
282 u64 run_end;
283
284 if (counter->state < PERF_COUNTER_STATE_INACTIVE)
285 return;
286
287 counter->total_time_enabled = ctx->time - counter->tstamp_enabled;
288
289 if (counter->state == PERF_COUNTER_STATE_INACTIVE)
290 run_end = counter->tstamp_stopped;
291 else
292 run_end = ctx->time;
293
294 counter->total_time_running = run_end - counter->tstamp_running;
295 }
296
297 /*
298 * Update total_time_enabled and total_time_running for all counters in a group.
299 */
300 static void update_group_times(struct perf_counter *leader)
301 {
302 struct perf_counter *counter;
303
304 update_counter_times(leader);
305 list_for_each_entry(counter, &leader->sibling_list, list_entry)
306 update_counter_times(counter);
307 }
308
309 /*
310 * Cross CPU call to disable a performance counter
311 */
312 static void __perf_counter_disable(void *info)
313 {
314 struct perf_counter *counter = info;
315 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
316 struct perf_counter_context *ctx = counter->ctx;
317 unsigned long flags;
318
319 /*
320 * If this is a per-task counter, need to check whether this
321 * counter's task is the current task on this cpu.
322 */
323 if (ctx->task && cpuctx->task_ctx != ctx)
324 return;
325
326 spin_lock_irqsave(&ctx->lock, flags);
327
328 /*
329 * If the counter is on, turn it off.
330 * If it is in error state, leave it in error state.
331 */
332 if (counter->state >= PERF_COUNTER_STATE_INACTIVE) {
333 update_context_time(ctx);
334 update_counter_times(counter);
335 if (counter == counter->group_leader)
336 group_sched_out(counter, cpuctx, ctx);
337 else
338 counter_sched_out(counter, cpuctx, ctx);
339 counter->state = PERF_COUNTER_STATE_OFF;
340 }
341
342 spin_unlock_irqrestore(&ctx->lock, flags);
343 }
344
345 /*
346 * Disable a counter.
347 */
348 static void perf_counter_disable(struct perf_counter *counter)
349 {
350 struct perf_counter_context *ctx = counter->ctx;
351 struct task_struct *task = ctx->task;
352
353 if (!task) {
354 /*
355 * Disable the counter on the cpu that it's on
356 */
357 smp_call_function_single(counter->cpu, __perf_counter_disable,
358 counter, 1);
359 return;
360 }
361
362 retry:
363 task_oncpu_function_call(task, __perf_counter_disable, counter);
364
365 spin_lock_irq(&ctx->lock);
366 /*
367 * If the counter is still active, we need to retry the cross-call.
368 */
369 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
370 spin_unlock_irq(&ctx->lock);
371 goto retry;
372 }
373
374 /*
375 * Since we have the lock this context can't be scheduled
376 * in, so we can change the state safely.
377 */
378 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
379 update_counter_times(counter);
380 counter->state = PERF_COUNTER_STATE_OFF;
381 }
382
383 spin_unlock_irq(&ctx->lock);
384 }
385
386 /*
387 * Disable a counter and all its children.
388 */
389 static void perf_counter_disable_family(struct perf_counter *counter)
390 {
391 struct perf_counter *child;
392
393 perf_counter_disable(counter);
394
395 /*
396 * Lock the mutex to protect the list of children
397 */
398 mutex_lock(&counter->mutex);
399 list_for_each_entry(child, &counter->child_list, child_list)
400 perf_counter_disable(child);
401 mutex_unlock(&counter->mutex);
402 }
403
404 static int
405 counter_sched_in(struct perf_counter *counter,
406 struct perf_cpu_context *cpuctx,
407 struct perf_counter_context *ctx,
408 int cpu)
409 {
410 if (counter->state <= PERF_COUNTER_STATE_OFF)
411 return 0;
412
413 counter->state = PERF_COUNTER_STATE_ACTIVE;
414 counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
415 /*
416 * The new state must be visible before we turn it on in the hardware:
417 */
418 smp_wmb();
419
420 if (counter->pmu->enable(counter)) {
421 counter->state = PERF_COUNTER_STATE_INACTIVE;
422 counter->oncpu = -1;
423 return -EAGAIN;
424 }
425
426 counter->tstamp_running += ctx->time - counter->tstamp_stopped;
427
428 if (!is_software_counter(counter))
429 cpuctx->active_oncpu++;
430 ctx->nr_active++;
431
432 if (counter->hw_event.exclusive)
433 cpuctx->exclusive = 1;
434
435 return 0;
436 }
437
438 /*
439 * Return 1 for a group consisting entirely of software counters,
440 * 0 if the group contains any hardware counters.
441 */
442 static int is_software_only_group(struct perf_counter *leader)
443 {
444 struct perf_counter *counter;
445
446 if (!is_software_counter(leader))
447 return 0;
448
449 list_for_each_entry(counter, &leader->sibling_list, list_entry)
450 if (!is_software_counter(counter))
451 return 0;
452
453 return 1;
454 }
455
456 /*
457 * Work out whether we can put this counter group on the CPU now.
458 */
459 static int group_can_go_on(struct perf_counter *counter,
460 struct perf_cpu_context *cpuctx,
461 int can_add_hw)
462 {
463 /*
464 * Groups consisting entirely of software counters can always go on.
465 */
466 if (is_software_only_group(counter))
467 return 1;
468 /*
469 * If an exclusive group is already on, no other hardware
470 * counters can go on.
471 */
472 if (cpuctx->exclusive)
473 return 0;
474 /*
475 * If this group is exclusive and there are already
476 * counters on the CPU, it can't go on.
477 */
478 if (counter->hw_event.exclusive && cpuctx->active_oncpu)
479 return 0;
480 /*
481 * Otherwise, try to add it if all previous groups were able
482 * to go on.
483 */
484 return can_add_hw;
485 }
486
487 static void add_counter_to_ctx(struct perf_counter *counter,
488 struct perf_counter_context *ctx)
489 {
490 list_add_counter(counter, ctx);
491 ctx->nr_counters++;
492 counter->prev_state = PERF_COUNTER_STATE_OFF;
493 counter->tstamp_enabled = ctx->time;
494 counter->tstamp_running = ctx->time;
495 counter->tstamp_stopped = ctx->time;
496 }
497
498 /*
499 * Cross CPU call to install and enable a performance counter
500 */
501 static void __perf_install_in_context(void *info)
502 {
503 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
504 struct perf_counter *counter = info;
505 struct perf_counter_context *ctx = counter->ctx;
506 struct perf_counter *leader = counter->group_leader;
507 int cpu = smp_processor_id();
508 unsigned long flags;
509 u64 perf_flags;
510 int err;
511
512 /*
513 * If this is a task context, we need to check whether it is
514 * the current task context of this cpu. If not it has been
515 * scheduled out before the smp call arrived.
516 */
517 if (ctx->task && cpuctx->task_ctx != ctx)
518 return;
519
520 spin_lock_irqsave(&ctx->lock, flags);
521 update_context_time(ctx);
522
523 /*
524 * Protect the list operation against NMI by disabling the
525 * counters on a global level. NOP for non NMI based counters.
526 */
527 perf_flags = hw_perf_save_disable();
528
529 add_counter_to_ctx(counter, ctx);
530
531 /*
532 * Don't put the counter on if it is disabled or if
533 * it is in a group and the group isn't on.
534 */
535 if (counter->state != PERF_COUNTER_STATE_INACTIVE ||
536 (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE))
537 goto unlock;
538
539 /*
540 * An exclusive counter can't go on if there are already active
541 * hardware counters, and no hardware counter can go on if there
542 * is already an exclusive counter on.
543 */
544 if (!group_can_go_on(counter, cpuctx, 1))
545 err = -EEXIST;
546 else
547 err = counter_sched_in(counter, cpuctx, ctx, cpu);
548
549 if (err) {
550 /*
551 * This counter couldn't go on. If it is in a group
552 * then we have to pull the whole group off.
553 * If the counter group is pinned then put it in error state.
554 */
555 if (leader != counter)
556 group_sched_out(leader, cpuctx, ctx);
557 if (leader->hw_event.pinned) {
558 update_group_times(leader);
559 leader->state = PERF_COUNTER_STATE_ERROR;
560 }
561 }
562
563 if (!err && !ctx->task && cpuctx->max_pertask)
564 cpuctx->max_pertask--;
565
566 unlock:
567 hw_perf_restore(perf_flags);
568
569 spin_unlock_irqrestore(&ctx->lock, flags);
570 }
571
572 /*
573 * Attach a performance counter to a context
574 *
575 * First we add the counter to the list with the hardware enable bit
576 * in counter->hw_config cleared.
577 *
578 * If the counter is attached to a task which is on a CPU we use a smp
579 * call to enable it in the task context. The task might have been
580 * scheduled away, but we check this in the smp call again.
581 *
582 * Must be called with ctx->mutex held.
583 */
584 static void
585 perf_install_in_context(struct perf_counter_context *ctx,
586 struct perf_counter *counter,
587 int cpu)
588 {
589 struct task_struct *task = ctx->task;
590
591 if (!task) {
592 /*
593 * Per cpu counters are installed via an smp call and
594 * the install is always sucessful.
595 */
596 smp_call_function_single(cpu, __perf_install_in_context,
597 counter, 1);
598 return;
599 }
600
601 counter->task = task;
602 retry:
603 task_oncpu_function_call(task, __perf_install_in_context,
604 counter);
605
606 spin_lock_irq(&ctx->lock);
607 /*
608 * we need to retry the smp call.
609 */
610 if (ctx->is_active && list_empty(&counter->list_entry)) {
611 spin_unlock_irq(&ctx->lock);
612 goto retry;
613 }
614
615 /*
616 * The lock prevents that this context is scheduled in so we
617 * can add the counter safely, if it the call above did not
618 * succeed.
619 */
620 if (list_empty(&counter->list_entry))
621 add_counter_to_ctx(counter, ctx);
622 spin_unlock_irq(&ctx->lock);
623 }
624
625 /*
626 * Cross CPU call to enable a performance counter
627 */
628 static void __perf_counter_enable(void *info)
629 {
630 struct perf_counter *counter = info;
631 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
632 struct perf_counter_context *ctx = counter->ctx;
633 struct perf_counter *leader = counter->group_leader;
634 unsigned long flags;
635 int err;
636
637 /*
638 * If this is a per-task counter, need to check whether this
639 * counter's task is the current task on this cpu.
640 */
641 if (ctx->task && cpuctx->task_ctx != ctx)
642 return;
643
644 spin_lock_irqsave(&ctx->lock, flags);
645 update_context_time(ctx);
646
647 counter->prev_state = counter->state;
648 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
649 goto unlock;
650 counter->state = PERF_COUNTER_STATE_INACTIVE;
651 counter->tstamp_enabled = ctx->time - counter->total_time_enabled;
652
653 /*
654 * If the counter is in a group and isn't the group leader,
655 * then don't put it on unless the group is on.
656 */
657 if (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE)
658 goto unlock;
659
660 if (!group_can_go_on(counter, cpuctx, 1))
661 err = -EEXIST;
662 else
663 err = counter_sched_in(counter, cpuctx, ctx,
664 smp_processor_id());
665
666 if (err) {
667 /*
668 * If this counter can't go on and it's part of a
669 * group, then the whole group has to come off.
670 */
671 if (leader != counter)
672 group_sched_out(leader, cpuctx, ctx);
673 if (leader->hw_event.pinned) {
674 update_group_times(leader);
675 leader->state = PERF_COUNTER_STATE_ERROR;
676 }
677 }
678
679 unlock:
680 spin_unlock_irqrestore(&ctx->lock, flags);
681 }
682
683 /*
684 * Enable a counter.
685 */
686 static void perf_counter_enable(struct perf_counter *counter)
687 {
688 struct perf_counter_context *ctx = counter->ctx;
689 struct task_struct *task = ctx->task;
690
691 if (!task) {
692 /*
693 * Enable the counter on the cpu that it's on
694 */
695 smp_call_function_single(counter->cpu, __perf_counter_enable,
696 counter, 1);
697 return;
698 }
699
700 spin_lock_irq(&ctx->lock);
701 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
702 goto out;
703
704 /*
705 * If the counter is in error state, clear that first.
706 * That way, if we see the counter in error state below, we
707 * know that it has gone back into error state, as distinct
708 * from the task having been scheduled away before the
709 * cross-call arrived.
710 */
711 if (counter->state == PERF_COUNTER_STATE_ERROR)
712 counter->state = PERF_COUNTER_STATE_OFF;
713
714 retry:
715 spin_unlock_irq(&ctx->lock);
716 task_oncpu_function_call(task, __perf_counter_enable, counter);
717
718 spin_lock_irq(&ctx->lock);
719
720 /*
721 * If the context is active and the counter is still off,
722 * we need to retry the cross-call.
723 */
724 if (ctx->is_active && counter->state == PERF_COUNTER_STATE_OFF)
725 goto retry;
726
727 /*
728 * Since we have the lock this context can't be scheduled
729 * in, so we can change the state safely.
730 */
731 if (counter->state == PERF_COUNTER_STATE_OFF) {
732 counter->state = PERF_COUNTER_STATE_INACTIVE;
733 counter->tstamp_enabled =
734 ctx->time - counter->total_time_enabled;
735 }
736 out:
737 spin_unlock_irq(&ctx->lock);
738 }
739
740 static void perf_counter_refresh(struct perf_counter *counter, int refresh)
741 {
742 atomic_add(refresh, &counter->event_limit);
743 perf_counter_enable(counter);
744 }
745
746 /*
747 * Enable a counter and all its children.
748 */
749 static void perf_counter_enable_family(struct perf_counter *counter)
750 {
751 struct perf_counter *child;
752
753 perf_counter_enable(counter);
754
755 /*
756 * Lock the mutex to protect the list of children
757 */
758 mutex_lock(&counter->mutex);
759 list_for_each_entry(child, &counter->child_list, child_list)
760 perf_counter_enable(child);
761 mutex_unlock(&counter->mutex);
762 }
763
764 void __perf_counter_sched_out(struct perf_counter_context *ctx,
765 struct perf_cpu_context *cpuctx)
766 {
767 struct perf_counter *counter;
768 u64 flags;
769
770 spin_lock(&ctx->lock);
771 ctx->is_active = 0;
772 if (likely(!ctx->nr_counters))
773 goto out;
774 update_context_time(ctx);
775
776 flags = hw_perf_save_disable();
777 if (ctx->nr_active) {
778 list_for_each_entry(counter, &ctx->counter_list, list_entry)
779 group_sched_out(counter, cpuctx, ctx);
780 }
781 hw_perf_restore(flags);
782 out:
783 spin_unlock(&ctx->lock);
784 }
785
786 /*
787 * Called from scheduler to remove the counters of the current task,
788 * with interrupts disabled.
789 *
790 * We stop each counter and update the counter value in counter->count.
791 *
792 * This does not protect us against NMI, but disable()
793 * sets the disabled bit in the control field of counter _before_
794 * accessing the counter control register. If a NMI hits, then it will
795 * not restart the counter.
796 */
797 void perf_counter_task_sched_out(struct task_struct *task, int cpu)
798 {
799 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
800 struct perf_counter_context *ctx = &task->perf_counter_ctx;
801 struct pt_regs *regs;
802
803 if (likely(!cpuctx->task_ctx))
804 return;
805
806 update_context_time(ctx);
807
808 regs = task_pt_regs(task);
809 perf_swcounter_event(PERF_COUNT_CONTEXT_SWITCHES, 1, 1, regs, 0);
810 __perf_counter_sched_out(ctx, cpuctx);
811
812 cpuctx->task_ctx = NULL;
813 }
814
815 static void perf_counter_cpu_sched_out(struct perf_cpu_context *cpuctx)
816 {
817 __perf_counter_sched_out(&cpuctx->ctx, cpuctx);
818 }
819
820 static int
821 group_sched_in(struct perf_counter *group_counter,
822 struct perf_cpu_context *cpuctx,
823 struct perf_counter_context *ctx,
824 int cpu)
825 {
826 struct perf_counter *counter, *partial_group;
827 int ret;
828
829 if (group_counter->state == PERF_COUNTER_STATE_OFF)
830 return 0;
831
832 ret = hw_perf_group_sched_in(group_counter, cpuctx, ctx, cpu);
833 if (ret)
834 return ret < 0 ? ret : 0;
835
836 group_counter->prev_state = group_counter->state;
837 if (counter_sched_in(group_counter, cpuctx, ctx, cpu))
838 return -EAGAIN;
839
840 /*
841 * Schedule in siblings as one group (if any):
842 */
843 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
844 counter->prev_state = counter->state;
845 if (counter_sched_in(counter, cpuctx, ctx, cpu)) {
846 partial_group = counter;
847 goto group_error;
848 }
849 }
850
851 return 0;
852
853 group_error:
854 /*
855 * Groups can be scheduled in as one unit only, so undo any
856 * partial group before returning:
857 */
858 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
859 if (counter == partial_group)
860 break;
861 counter_sched_out(counter, cpuctx, ctx);
862 }
863 counter_sched_out(group_counter, cpuctx, ctx);
864
865 return -EAGAIN;
866 }
867
868 static void
869 __perf_counter_sched_in(struct perf_counter_context *ctx,
870 struct perf_cpu_context *cpuctx, int cpu)
871 {
872 struct perf_counter *counter;
873 u64 flags;
874 int can_add_hw = 1;
875
876 spin_lock(&ctx->lock);
877 ctx->is_active = 1;
878 if (likely(!ctx->nr_counters))
879 goto out;
880
881 ctx->timestamp = perf_clock();
882
883 flags = hw_perf_save_disable();
884
885 /*
886 * First go through the list and put on any pinned groups
887 * in order to give them the best chance of going on.
888 */
889 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
890 if (counter->state <= PERF_COUNTER_STATE_OFF ||
891 !counter->hw_event.pinned)
892 continue;
893 if (counter->cpu != -1 && counter->cpu != cpu)
894 continue;
895
896 if (group_can_go_on(counter, cpuctx, 1))
897 group_sched_in(counter, cpuctx, ctx, cpu);
898
899 /*
900 * If this pinned group hasn't been scheduled,
901 * put it in error state.
902 */
903 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
904 update_group_times(counter);
905 counter->state = PERF_COUNTER_STATE_ERROR;
906 }
907 }
908
909 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
910 /*
911 * Ignore counters in OFF or ERROR state, and
912 * ignore pinned counters since we did them already.
913 */
914 if (counter->state <= PERF_COUNTER_STATE_OFF ||
915 counter->hw_event.pinned)
916 continue;
917
918 /*
919 * Listen to the 'cpu' scheduling filter constraint
920 * of counters:
921 */
922 if (counter->cpu != -1 && counter->cpu != cpu)
923 continue;
924
925 if (group_can_go_on(counter, cpuctx, can_add_hw)) {
926 if (group_sched_in(counter, cpuctx, ctx, cpu))
927 can_add_hw = 0;
928 }
929 }
930 hw_perf_restore(flags);
931 out:
932 spin_unlock(&ctx->lock);
933 }
934
935 /*
936 * Called from scheduler to add the counters of the current task
937 * with interrupts disabled.
938 *
939 * We restore the counter value and then enable it.
940 *
941 * This does not protect us against NMI, but enable()
942 * sets the enabled bit in the control field of counter _before_
943 * accessing the counter control register. If a NMI hits, then it will
944 * keep the counter running.
945 */
946 void perf_counter_task_sched_in(struct task_struct *task, int cpu)
947 {
948 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
949 struct perf_counter_context *ctx = &task->perf_counter_ctx;
950
951 __perf_counter_sched_in(ctx, cpuctx, cpu);
952 cpuctx->task_ctx = ctx;
953 }
954
955 static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu)
956 {
957 struct perf_counter_context *ctx = &cpuctx->ctx;
958
959 __perf_counter_sched_in(ctx, cpuctx, cpu);
960 }
961
962 int perf_counter_task_disable(void)
963 {
964 struct task_struct *curr = current;
965 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
966 struct perf_counter *counter;
967 unsigned long flags;
968 u64 perf_flags;
969 int cpu;
970
971 if (likely(!ctx->nr_counters))
972 return 0;
973
974 local_irq_save(flags);
975 cpu = smp_processor_id();
976
977 perf_counter_task_sched_out(curr, cpu);
978
979 spin_lock(&ctx->lock);
980
981 /*
982 * Disable all the counters:
983 */
984 perf_flags = hw_perf_save_disable();
985
986 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
987 if (counter->state != PERF_COUNTER_STATE_ERROR) {
988 update_group_times(counter);
989 counter->state = PERF_COUNTER_STATE_OFF;
990 }
991 }
992
993 hw_perf_restore(perf_flags);
994
995 spin_unlock_irqrestore(&ctx->lock, flags);
996
997 return 0;
998 }
999
1000 int perf_counter_task_enable(void)
1001 {
1002 struct task_struct *curr = current;
1003 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
1004 struct perf_counter *counter;
1005 unsigned long flags;
1006 u64 perf_flags;
1007 int cpu;
1008
1009 if (likely(!ctx->nr_counters))
1010 return 0;
1011
1012 local_irq_save(flags);
1013 cpu = smp_processor_id();
1014
1015 perf_counter_task_sched_out(curr, cpu);
1016
1017 spin_lock(&ctx->lock);
1018
1019 /*
1020 * Disable all the counters:
1021 */
1022 perf_flags = hw_perf_save_disable();
1023
1024 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1025 if (counter->state > PERF_COUNTER_STATE_OFF)
1026 continue;
1027 counter->state = PERF_COUNTER_STATE_INACTIVE;
1028 counter->tstamp_enabled =
1029 ctx->time - counter->total_time_enabled;
1030 counter->hw_event.disabled = 0;
1031 }
1032 hw_perf_restore(perf_flags);
1033
1034 spin_unlock(&ctx->lock);
1035
1036 perf_counter_task_sched_in(curr, cpu);
1037
1038 local_irq_restore(flags);
1039
1040 return 0;
1041 }
1042
1043 /*
1044 * Round-robin a context's counters:
1045 */
1046 static void rotate_ctx(struct perf_counter_context *ctx)
1047 {
1048 struct perf_counter *counter;
1049 u64 perf_flags;
1050
1051 if (!ctx->nr_counters)
1052 return;
1053
1054 spin_lock(&ctx->lock);
1055 /*
1056 * Rotate the first entry last (works just fine for group counters too):
1057 */
1058 perf_flags = hw_perf_save_disable();
1059 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1060 list_move_tail(&counter->list_entry, &ctx->counter_list);
1061 break;
1062 }
1063 hw_perf_restore(perf_flags);
1064
1065 spin_unlock(&ctx->lock);
1066 }
1067
1068 void perf_counter_task_tick(struct task_struct *curr, int cpu)
1069 {
1070 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
1071 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
1072
1073 perf_counter_cpu_sched_out(cpuctx);
1074 perf_counter_task_sched_out(curr, cpu);
1075
1076 rotate_ctx(&cpuctx->ctx);
1077 rotate_ctx(ctx);
1078
1079 perf_counter_cpu_sched_in(cpuctx, cpu);
1080 perf_counter_task_sched_in(curr, cpu);
1081 }
1082
1083 /*
1084 * Cross CPU call to read the hardware counter
1085 */
1086 static void __read(void *info)
1087 {
1088 struct perf_counter *counter = info;
1089 struct perf_counter_context *ctx = counter->ctx;
1090 unsigned long flags;
1091
1092 local_irq_save(flags);
1093 if (ctx->is_active)
1094 update_context_time(ctx);
1095 counter->pmu->read(counter);
1096 update_counter_times(counter);
1097 local_irq_restore(flags);
1098 }
1099
1100 static u64 perf_counter_read(struct perf_counter *counter)
1101 {
1102 /*
1103 * If counter is enabled and currently active on a CPU, update the
1104 * value in the counter structure:
1105 */
1106 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
1107 smp_call_function_single(counter->oncpu,
1108 __read, counter, 1);
1109 } else if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1110 update_counter_times(counter);
1111 }
1112
1113 return atomic64_read(&counter->count);
1114 }
1115
1116 static void put_context(struct perf_counter_context *ctx)
1117 {
1118 if (ctx->task)
1119 put_task_struct(ctx->task);
1120 }
1121
1122 static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
1123 {
1124 struct perf_cpu_context *cpuctx;
1125 struct perf_counter_context *ctx;
1126 struct task_struct *task;
1127
1128 /*
1129 * If cpu is not a wildcard then this is a percpu counter:
1130 */
1131 if (cpu != -1) {
1132 /* Must be root to operate on a CPU counter: */
1133 if (sysctl_perf_counter_priv && !capable(CAP_SYS_ADMIN))
1134 return ERR_PTR(-EACCES);
1135
1136 if (cpu < 0 || cpu > num_possible_cpus())
1137 return ERR_PTR(-EINVAL);
1138
1139 /*
1140 * We could be clever and allow to attach a counter to an
1141 * offline CPU and activate it when the CPU comes up, but
1142 * that's for later.
1143 */
1144 if (!cpu_isset(cpu, cpu_online_map))
1145 return ERR_PTR(-ENODEV);
1146
1147 cpuctx = &per_cpu(perf_cpu_context, cpu);
1148 ctx = &cpuctx->ctx;
1149
1150 return ctx;
1151 }
1152
1153 rcu_read_lock();
1154 if (!pid)
1155 task = current;
1156 else
1157 task = find_task_by_vpid(pid);
1158 if (task)
1159 get_task_struct(task);
1160 rcu_read_unlock();
1161
1162 if (!task)
1163 return ERR_PTR(-ESRCH);
1164
1165 ctx = &task->perf_counter_ctx;
1166 ctx->task = task;
1167
1168 /* Reuse ptrace permission checks for now. */
1169 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
1170 put_context(ctx);
1171 return ERR_PTR(-EACCES);
1172 }
1173
1174 return ctx;
1175 }
1176
1177 static void free_counter_rcu(struct rcu_head *head)
1178 {
1179 struct perf_counter *counter;
1180
1181 counter = container_of(head, struct perf_counter, rcu_head);
1182 kfree(counter);
1183 }
1184
1185 static void perf_pending_sync(struct perf_counter *counter);
1186
1187 static void free_counter(struct perf_counter *counter)
1188 {
1189 perf_pending_sync(counter);
1190
1191 if (counter->hw_event.mmap)
1192 atomic_dec(&nr_mmap_tracking);
1193 if (counter->hw_event.munmap)
1194 atomic_dec(&nr_munmap_tracking);
1195 if (counter->hw_event.comm)
1196 atomic_dec(&nr_comm_tracking);
1197
1198 if (counter->destroy)
1199 counter->destroy(counter);
1200
1201 call_rcu(&counter->rcu_head, free_counter_rcu);
1202 }
1203
1204 /*
1205 * Called when the last reference to the file is gone.
1206 */
1207 static int perf_release(struct inode *inode, struct file *file)
1208 {
1209 struct perf_counter *counter = file->private_data;
1210 struct perf_counter_context *ctx = counter->ctx;
1211
1212 file->private_data = NULL;
1213
1214 mutex_lock(&ctx->mutex);
1215 mutex_lock(&counter->mutex);
1216
1217 perf_counter_remove_from_context(counter);
1218
1219 mutex_unlock(&counter->mutex);
1220 mutex_unlock(&ctx->mutex);
1221
1222 free_counter(counter);
1223 put_context(ctx);
1224
1225 return 0;
1226 }
1227
1228 /*
1229 * Read the performance counter - simple non blocking version for now
1230 */
1231 static ssize_t
1232 perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
1233 {
1234 u64 values[3];
1235 int n;
1236
1237 /*
1238 * Return end-of-file for a read on a counter that is in
1239 * error state (i.e. because it was pinned but it couldn't be
1240 * scheduled on to the CPU at some point).
1241 */
1242 if (counter->state == PERF_COUNTER_STATE_ERROR)
1243 return 0;
1244
1245 mutex_lock(&counter->mutex);
1246 values[0] = perf_counter_read(counter);
1247 n = 1;
1248 if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1249 values[n++] = counter->total_time_enabled +
1250 atomic64_read(&counter->child_total_time_enabled);
1251 if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1252 values[n++] = counter->total_time_running +
1253 atomic64_read(&counter->child_total_time_running);
1254 mutex_unlock(&counter->mutex);
1255
1256 if (count < n * sizeof(u64))
1257 return -EINVAL;
1258 count = n * sizeof(u64);
1259
1260 if (copy_to_user(buf, values, count))
1261 return -EFAULT;
1262
1263 return count;
1264 }
1265
1266 static ssize_t
1267 perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
1268 {
1269 struct perf_counter *counter = file->private_data;
1270
1271 return perf_read_hw(counter, buf, count);
1272 }
1273
1274 static unsigned int perf_poll(struct file *file, poll_table *wait)
1275 {
1276 struct perf_counter *counter = file->private_data;
1277 struct perf_mmap_data *data;
1278 unsigned int events = POLL_HUP;
1279
1280 rcu_read_lock();
1281 data = rcu_dereference(counter->data);
1282 if (data)
1283 events = atomic_xchg(&data->poll, 0);
1284 rcu_read_unlock();
1285
1286 poll_wait(file, &counter->waitq, wait);
1287
1288 return events;
1289 }
1290
1291 static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1292 {
1293 struct perf_counter *counter = file->private_data;
1294 int err = 0;
1295
1296 switch (cmd) {
1297 case PERF_COUNTER_IOC_ENABLE:
1298 perf_counter_enable_family(counter);
1299 break;
1300 case PERF_COUNTER_IOC_DISABLE:
1301 perf_counter_disable_family(counter);
1302 break;
1303 case PERF_COUNTER_IOC_REFRESH:
1304 perf_counter_refresh(counter, arg);
1305 break;
1306 default:
1307 err = -ENOTTY;
1308 }
1309 return err;
1310 }
1311
1312 /*
1313 * Callers need to ensure there can be no nesting of this function, otherwise
1314 * the seqlock logic goes bad. We can not serialize this because the arch
1315 * code calls this from NMI context.
1316 */
1317 void perf_counter_update_userpage(struct perf_counter *counter)
1318 {
1319 struct perf_mmap_data *data;
1320 struct perf_counter_mmap_page *userpg;
1321
1322 rcu_read_lock();
1323 data = rcu_dereference(counter->data);
1324 if (!data)
1325 goto unlock;
1326
1327 userpg = data->user_page;
1328
1329 /*
1330 * Disable preemption so as to not let the corresponding user-space
1331 * spin too long if we get preempted.
1332 */
1333 preempt_disable();
1334 ++userpg->lock;
1335 barrier();
1336 userpg->index = counter->hw.idx;
1337 userpg->offset = atomic64_read(&counter->count);
1338 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
1339 userpg->offset -= atomic64_read(&counter->hw.prev_count);
1340
1341 barrier();
1342 ++userpg->lock;
1343 preempt_enable();
1344 unlock:
1345 rcu_read_unlock();
1346 }
1347
1348 static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1349 {
1350 struct perf_counter *counter = vma->vm_file->private_data;
1351 struct perf_mmap_data *data;
1352 int ret = VM_FAULT_SIGBUS;
1353
1354 rcu_read_lock();
1355 data = rcu_dereference(counter->data);
1356 if (!data)
1357 goto unlock;
1358
1359 if (vmf->pgoff == 0) {
1360 vmf->page = virt_to_page(data->user_page);
1361 } else {
1362 int nr = vmf->pgoff - 1;
1363
1364 if ((unsigned)nr > data->nr_pages)
1365 goto unlock;
1366
1367 vmf->page = virt_to_page(data->data_pages[nr]);
1368 }
1369 get_page(vmf->page);
1370 ret = 0;
1371 unlock:
1372 rcu_read_unlock();
1373
1374 return ret;
1375 }
1376
1377 static int perf_mmap_data_alloc(struct perf_counter *counter, int nr_pages)
1378 {
1379 struct perf_mmap_data *data;
1380 unsigned long size;
1381 int i;
1382
1383 WARN_ON(atomic_read(&counter->mmap_count));
1384
1385 size = sizeof(struct perf_mmap_data);
1386 size += nr_pages * sizeof(void *);
1387
1388 data = kzalloc(size, GFP_KERNEL);
1389 if (!data)
1390 goto fail;
1391
1392 data->user_page = (void *)get_zeroed_page(GFP_KERNEL);
1393 if (!data->user_page)
1394 goto fail_user_page;
1395
1396 for (i = 0; i < nr_pages; i++) {
1397 data->data_pages[i] = (void *)get_zeroed_page(GFP_KERNEL);
1398 if (!data->data_pages[i])
1399 goto fail_data_pages;
1400 }
1401
1402 data->nr_pages = nr_pages;
1403
1404 rcu_assign_pointer(counter->data, data);
1405
1406 return 0;
1407
1408 fail_data_pages:
1409 for (i--; i >= 0; i--)
1410 free_page((unsigned long)data->data_pages[i]);
1411
1412 free_page((unsigned long)data->user_page);
1413
1414 fail_user_page:
1415 kfree(data);
1416
1417 fail:
1418 return -ENOMEM;
1419 }
1420
1421 static void __perf_mmap_data_free(struct rcu_head *rcu_head)
1422 {
1423 struct perf_mmap_data *data = container_of(rcu_head,
1424 struct perf_mmap_data, rcu_head);
1425 int i;
1426
1427 free_page((unsigned long)data->user_page);
1428 for (i = 0; i < data->nr_pages; i++)
1429 free_page((unsigned long)data->data_pages[i]);
1430 kfree(data);
1431 }
1432
1433 static void perf_mmap_data_free(struct perf_counter *counter)
1434 {
1435 struct perf_mmap_data *data = counter->data;
1436
1437 WARN_ON(atomic_read(&counter->mmap_count));
1438
1439 rcu_assign_pointer(counter->data, NULL);
1440 call_rcu(&data->rcu_head, __perf_mmap_data_free);
1441 }
1442
1443 static void perf_mmap_open(struct vm_area_struct *vma)
1444 {
1445 struct perf_counter *counter = vma->vm_file->private_data;
1446
1447 atomic_inc(&counter->mmap_count);
1448 }
1449
1450 static void perf_mmap_close(struct vm_area_struct *vma)
1451 {
1452 struct perf_counter *counter = vma->vm_file->private_data;
1453
1454 if (atomic_dec_and_mutex_lock(&counter->mmap_count,
1455 &counter->mmap_mutex)) {
1456 vma->vm_mm->locked_vm -= counter->data->nr_pages + 1;
1457 perf_mmap_data_free(counter);
1458 mutex_unlock(&counter->mmap_mutex);
1459 }
1460 }
1461
1462 static struct vm_operations_struct perf_mmap_vmops = {
1463 .open = perf_mmap_open,
1464 .close = perf_mmap_close,
1465 .fault = perf_mmap_fault,
1466 };
1467
1468 static int perf_mmap(struct file *file, struct vm_area_struct *vma)
1469 {
1470 struct perf_counter *counter = file->private_data;
1471 unsigned long vma_size;
1472 unsigned long nr_pages;
1473 unsigned long locked, lock_limit;
1474 int ret = 0;
1475
1476 if (!(vma->vm_flags & VM_SHARED) || (vma->vm_flags & VM_WRITE))
1477 return -EINVAL;
1478
1479 vma_size = vma->vm_end - vma->vm_start;
1480 nr_pages = (vma_size / PAGE_SIZE) - 1;
1481
1482 /*
1483 * If we have data pages ensure they're a power-of-two number, so we
1484 * can do bitmasks instead of modulo.
1485 */
1486 if (nr_pages != 0 && !is_power_of_2(nr_pages))
1487 return -EINVAL;
1488
1489 if (vma_size != PAGE_SIZE * (1 + nr_pages))
1490 return -EINVAL;
1491
1492 if (vma->vm_pgoff != 0)
1493 return -EINVAL;
1494
1495 mutex_lock(&counter->mmap_mutex);
1496 if (atomic_inc_not_zero(&counter->mmap_count)) {
1497 if (nr_pages != counter->data->nr_pages)
1498 ret = -EINVAL;
1499 goto unlock;
1500 }
1501
1502 locked = vma->vm_mm->locked_vm;
1503 locked += nr_pages + 1;
1504
1505 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
1506 lock_limit >>= PAGE_SHIFT;
1507
1508 if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) {
1509 ret = -EPERM;
1510 goto unlock;
1511 }
1512
1513 WARN_ON(counter->data);
1514 ret = perf_mmap_data_alloc(counter, nr_pages);
1515 if (ret)
1516 goto unlock;
1517
1518 atomic_set(&counter->mmap_count, 1);
1519 vma->vm_mm->locked_vm += nr_pages + 1;
1520 unlock:
1521 mutex_unlock(&counter->mmap_mutex);
1522
1523 vma->vm_flags &= ~VM_MAYWRITE;
1524 vma->vm_flags |= VM_RESERVED;
1525 vma->vm_ops = &perf_mmap_vmops;
1526
1527 return ret;
1528 }
1529
1530 static int perf_fasync(int fd, struct file *filp, int on)
1531 {
1532 struct perf_counter *counter = filp->private_data;
1533 struct inode *inode = filp->f_path.dentry->d_inode;
1534 int retval;
1535
1536 mutex_lock(&inode->i_mutex);
1537 retval = fasync_helper(fd, filp, on, &counter->fasync);
1538 mutex_unlock(&inode->i_mutex);
1539
1540 if (retval < 0)
1541 return retval;
1542
1543 return 0;
1544 }
1545
1546 static const struct file_operations perf_fops = {
1547 .release = perf_release,
1548 .read = perf_read,
1549 .poll = perf_poll,
1550 .unlocked_ioctl = perf_ioctl,
1551 .compat_ioctl = perf_ioctl,
1552 .mmap = perf_mmap,
1553 .fasync = perf_fasync,
1554 };
1555
1556 /*
1557 * Perf counter wakeup
1558 *
1559 * If there's data, ensure we set the poll() state and publish everything
1560 * to user-space before waking everybody up.
1561 */
1562
1563 void perf_counter_wakeup(struct perf_counter *counter)
1564 {
1565 wake_up_all(&counter->waitq);
1566
1567 if (counter->pending_kill) {
1568 kill_fasync(&counter->fasync, SIGIO, counter->pending_kill);
1569 counter->pending_kill = 0;
1570 }
1571 }
1572
1573 /*
1574 * Pending wakeups
1575 *
1576 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
1577 *
1578 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
1579 * single linked list and use cmpxchg() to add entries lockless.
1580 */
1581
1582 static void perf_pending_counter(struct perf_pending_entry *entry)
1583 {
1584 struct perf_counter *counter = container_of(entry,
1585 struct perf_counter, pending);
1586
1587 if (counter->pending_disable) {
1588 counter->pending_disable = 0;
1589 perf_counter_disable(counter);
1590 }
1591
1592 if (counter->pending_wakeup) {
1593 counter->pending_wakeup = 0;
1594 perf_counter_wakeup(counter);
1595 }
1596 }
1597
1598 #define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
1599
1600 static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
1601 PENDING_TAIL,
1602 };
1603
1604 static void perf_pending_queue(struct perf_pending_entry *entry,
1605 void (*func)(struct perf_pending_entry *))
1606 {
1607 struct perf_pending_entry **head;
1608
1609 if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
1610 return;
1611
1612 entry->func = func;
1613
1614 head = &get_cpu_var(perf_pending_head);
1615
1616 do {
1617 entry->next = *head;
1618 } while (cmpxchg(head, entry->next, entry) != entry->next);
1619
1620 set_perf_counter_pending();
1621
1622 put_cpu_var(perf_pending_head);
1623 }
1624
1625 static int __perf_pending_run(void)
1626 {
1627 struct perf_pending_entry *list;
1628 int nr = 0;
1629
1630 list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
1631 while (list != PENDING_TAIL) {
1632 void (*func)(struct perf_pending_entry *);
1633 struct perf_pending_entry *entry = list;
1634
1635 list = list->next;
1636
1637 func = entry->func;
1638 entry->next = NULL;
1639 /*
1640 * Ensure we observe the unqueue before we issue the wakeup,
1641 * so that we won't be waiting forever.
1642 * -- see perf_not_pending().
1643 */
1644 smp_wmb();
1645
1646 func(entry);
1647 nr++;
1648 }
1649
1650 return nr;
1651 }
1652
1653 static inline int perf_not_pending(struct perf_counter *counter)
1654 {
1655 /*
1656 * If we flush on whatever cpu we run, there is a chance we don't
1657 * need to wait.
1658 */
1659 get_cpu();
1660 __perf_pending_run();
1661 put_cpu();
1662
1663 /*
1664 * Ensure we see the proper queue state before going to sleep
1665 * so that we do not miss the wakeup. -- see perf_pending_handle()
1666 */
1667 smp_rmb();
1668 return counter->pending.next == NULL;
1669 }
1670
1671 static void perf_pending_sync(struct perf_counter *counter)
1672 {
1673 wait_event(counter->waitq, perf_not_pending(counter));
1674 }
1675
1676 void perf_counter_do_pending(void)
1677 {
1678 __perf_pending_run();
1679 }
1680
1681 /*
1682 * Callchain support -- arch specific
1683 */
1684
1685 __weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
1686 {
1687 return NULL;
1688 }
1689
1690 /*
1691 * Output
1692 */
1693
1694 struct perf_output_handle {
1695 struct perf_counter *counter;
1696 struct perf_mmap_data *data;
1697 unsigned int offset;
1698 unsigned int head;
1699 int nmi;
1700 int overflow;
1701 int locked;
1702 unsigned long flags;
1703 };
1704
1705 static void perf_output_wakeup(struct perf_output_handle *handle)
1706 {
1707 atomic_set(&handle->data->poll, POLL_IN);
1708
1709 if (handle->nmi) {
1710 handle->counter->pending_wakeup = 1;
1711 perf_pending_queue(&handle->counter->pending,
1712 perf_pending_counter);
1713 } else
1714 perf_counter_wakeup(handle->counter);
1715 }
1716
1717 /*
1718 * Curious locking construct.
1719 *
1720 * We need to ensure a later event doesn't publish a head when a former
1721 * event isn't done writing. However since we need to deal with NMIs we
1722 * cannot fully serialize things.
1723 *
1724 * What we do is serialize between CPUs so we only have to deal with NMI
1725 * nesting on a single CPU.
1726 *
1727 * We only publish the head (and generate a wakeup) when the outer-most
1728 * event completes.
1729 */
1730 static void perf_output_lock(struct perf_output_handle *handle)
1731 {
1732 struct perf_mmap_data *data = handle->data;
1733 int cpu;
1734
1735 handle->locked = 0;
1736
1737 local_irq_save(handle->flags);
1738 cpu = smp_processor_id();
1739
1740 if (in_nmi() && atomic_read(&data->lock) == cpu)
1741 return;
1742
1743 while (atomic_cmpxchg(&data->lock, 0, cpu) != 0)
1744 cpu_relax();
1745
1746 handle->locked = 1;
1747 }
1748
1749 static void perf_output_unlock(struct perf_output_handle *handle)
1750 {
1751 struct perf_mmap_data *data = handle->data;
1752 int head, cpu;
1753
1754 data->done_head = data->head;
1755
1756 if (!handle->locked)
1757 goto out;
1758
1759 again:
1760 /*
1761 * The xchg implies a full barrier that ensures all writes are done
1762 * before we publish the new head, matched by a rmb() in userspace when
1763 * reading this position.
1764 */
1765 while ((head = atomic_xchg(&data->done_head, 0)))
1766 data->user_page->data_head = head;
1767
1768 /*
1769 * NMI can happen here, which means we can miss a done_head update.
1770 */
1771
1772 cpu = atomic_xchg(&data->lock, 0);
1773 WARN_ON_ONCE(cpu != smp_processor_id());
1774
1775 /*
1776 * Therefore we have to validate we did not indeed do so.
1777 */
1778 if (unlikely(atomic_read(&data->done_head))) {
1779 /*
1780 * Since we had it locked, we can lock it again.
1781 */
1782 while (atomic_cmpxchg(&data->lock, 0, cpu) != 0)
1783 cpu_relax();
1784
1785 goto again;
1786 }
1787
1788 if (atomic_xchg(&data->wakeup, 0))
1789 perf_output_wakeup(handle);
1790 out:
1791 local_irq_restore(handle->flags);
1792 }
1793
1794 static int perf_output_begin(struct perf_output_handle *handle,
1795 struct perf_counter *counter, unsigned int size,
1796 int nmi, int overflow)
1797 {
1798 struct perf_mmap_data *data;
1799 unsigned int offset, head;
1800
1801 rcu_read_lock();
1802 data = rcu_dereference(counter->data);
1803 if (!data)
1804 goto out;
1805
1806 handle->data = data;
1807 handle->counter = counter;
1808 handle->nmi = nmi;
1809 handle->overflow = overflow;
1810
1811 if (!data->nr_pages)
1812 goto fail;
1813
1814 perf_output_lock(handle);
1815
1816 do {
1817 offset = head = atomic_read(&data->head);
1818 head += size;
1819 } while (atomic_cmpxchg(&data->head, offset, head) != offset);
1820
1821 handle->offset = offset;
1822 handle->head = head;
1823
1824 if ((offset >> PAGE_SHIFT) != (head >> PAGE_SHIFT))
1825 atomic_set(&data->wakeup, 1);
1826
1827 return 0;
1828
1829 fail:
1830 perf_output_wakeup(handle);
1831 out:
1832 rcu_read_unlock();
1833
1834 return -ENOSPC;
1835 }
1836
1837 static void perf_output_copy(struct perf_output_handle *handle,
1838 void *buf, unsigned int len)
1839 {
1840 unsigned int pages_mask;
1841 unsigned int offset;
1842 unsigned int size;
1843 void **pages;
1844
1845 offset = handle->offset;
1846 pages_mask = handle->data->nr_pages - 1;
1847 pages = handle->data->data_pages;
1848
1849 do {
1850 unsigned int page_offset;
1851 int nr;
1852
1853 nr = (offset >> PAGE_SHIFT) & pages_mask;
1854 page_offset = offset & (PAGE_SIZE - 1);
1855 size = min_t(unsigned int, PAGE_SIZE - page_offset, len);
1856
1857 memcpy(pages[nr] + page_offset, buf, size);
1858
1859 len -= size;
1860 buf += size;
1861 offset += size;
1862 } while (len);
1863
1864 handle->offset = offset;
1865
1866 WARN_ON_ONCE(handle->offset > handle->head);
1867 }
1868
1869 #define perf_output_put(handle, x) \
1870 perf_output_copy((handle), &(x), sizeof(x))
1871
1872 static void perf_output_end(struct perf_output_handle *handle)
1873 {
1874 struct perf_counter *counter = handle->counter;
1875 struct perf_mmap_data *data = handle->data;
1876
1877 int wakeup_events = counter->hw_event.wakeup_events;
1878
1879 if (handle->overflow && wakeup_events) {
1880 int events = atomic_inc_return(&data->events);
1881 if (events >= wakeup_events) {
1882 atomic_sub(wakeup_events, &data->events);
1883 atomic_set(&data->wakeup, 1);
1884 }
1885 }
1886
1887 perf_output_unlock(handle);
1888 rcu_read_unlock();
1889 }
1890
1891 static void perf_counter_output(struct perf_counter *counter,
1892 int nmi, struct pt_regs *regs, u64 addr)
1893 {
1894 int ret;
1895 u64 record_type = counter->hw_event.record_type;
1896 struct perf_output_handle handle;
1897 struct perf_event_header header;
1898 u64 ip;
1899 struct {
1900 u32 pid, tid;
1901 } tid_entry;
1902 struct {
1903 u64 event;
1904 u64 counter;
1905 } group_entry;
1906 struct perf_callchain_entry *callchain = NULL;
1907 int callchain_size = 0;
1908 u64 time;
1909
1910 header.type = 0;
1911 header.size = sizeof(header);
1912
1913 header.misc = PERF_EVENT_MISC_OVERFLOW;
1914 header.misc |= user_mode(regs) ?
1915 PERF_EVENT_MISC_USER : PERF_EVENT_MISC_KERNEL;
1916
1917 if (record_type & PERF_RECORD_IP) {
1918 ip = instruction_pointer(regs);
1919 header.type |= PERF_RECORD_IP;
1920 header.size += sizeof(ip);
1921 }
1922
1923 if (record_type & PERF_RECORD_TID) {
1924 /* namespace issues */
1925 tid_entry.pid = current->group_leader->pid;
1926 tid_entry.tid = current->pid;
1927
1928 header.type |= PERF_RECORD_TID;
1929 header.size += sizeof(tid_entry);
1930 }
1931
1932 if (record_type & PERF_RECORD_TIME) {
1933 /*
1934 * Maybe do better on x86 and provide cpu_clock_nmi()
1935 */
1936 time = sched_clock();
1937
1938 header.type |= PERF_RECORD_TIME;
1939 header.size += sizeof(u64);
1940 }
1941
1942 if (record_type & PERF_RECORD_ADDR) {
1943 header.type |= PERF_RECORD_ADDR;
1944 header.size += sizeof(u64);
1945 }
1946
1947 if (record_type & PERF_RECORD_GROUP) {
1948 header.type |= PERF_RECORD_GROUP;
1949 header.size += sizeof(u64) +
1950 counter->nr_siblings * sizeof(group_entry);
1951 }
1952
1953 if (record_type & PERF_RECORD_CALLCHAIN) {
1954 callchain = perf_callchain(regs);
1955
1956 if (callchain) {
1957 callchain_size = (1 + callchain->nr) * sizeof(u64);
1958
1959 header.type |= PERF_RECORD_CALLCHAIN;
1960 header.size += callchain_size;
1961 }
1962 }
1963
1964 ret = perf_output_begin(&handle, counter, header.size, nmi, 1);
1965 if (ret)
1966 return;
1967
1968 perf_output_put(&handle, header);
1969
1970 if (record_type & PERF_RECORD_IP)
1971 perf_output_put(&handle, ip);
1972
1973 if (record_type & PERF_RECORD_TID)
1974 perf_output_put(&handle, tid_entry);
1975
1976 if (record_type & PERF_RECORD_TIME)
1977 perf_output_put(&handle, time);
1978
1979 if (record_type & PERF_RECORD_ADDR)
1980 perf_output_put(&handle, addr);
1981
1982 if (record_type & PERF_RECORD_GROUP) {
1983 struct perf_counter *leader, *sub;
1984 u64 nr = counter->nr_siblings;
1985
1986 perf_output_put(&handle, nr);
1987
1988 leader = counter->group_leader;
1989 list_for_each_entry(sub, &leader->sibling_list, list_entry) {
1990 if (sub != counter)
1991 sub->pmu->read(sub);
1992
1993 group_entry.event = sub->hw_event.config;
1994 group_entry.counter = atomic64_read(&sub->count);
1995
1996 perf_output_put(&handle, group_entry);
1997 }
1998 }
1999
2000 if (callchain)
2001 perf_output_copy(&handle, callchain, callchain_size);
2002
2003 perf_output_end(&handle);
2004 }
2005
2006 /*
2007 * comm tracking
2008 */
2009
2010 struct perf_comm_event {
2011 struct task_struct *task;
2012 char *comm;
2013 int comm_size;
2014
2015 struct {
2016 struct perf_event_header header;
2017
2018 u32 pid;
2019 u32 tid;
2020 } event;
2021 };
2022
2023 static void perf_counter_comm_output(struct perf_counter *counter,
2024 struct perf_comm_event *comm_event)
2025 {
2026 struct perf_output_handle handle;
2027 int size = comm_event->event.header.size;
2028 int ret = perf_output_begin(&handle, counter, size, 0, 0);
2029
2030 if (ret)
2031 return;
2032
2033 perf_output_put(&handle, comm_event->event);
2034 perf_output_copy(&handle, comm_event->comm,
2035 comm_event->comm_size);
2036 perf_output_end(&handle);
2037 }
2038
2039 static int perf_counter_comm_match(struct perf_counter *counter,
2040 struct perf_comm_event *comm_event)
2041 {
2042 if (counter->hw_event.comm &&
2043 comm_event->event.header.type == PERF_EVENT_COMM)
2044 return 1;
2045
2046 return 0;
2047 }
2048
2049 static void perf_counter_comm_ctx(struct perf_counter_context *ctx,
2050 struct perf_comm_event *comm_event)
2051 {
2052 struct perf_counter *counter;
2053
2054 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2055 return;
2056
2057 rcu_read_lock();
2058 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2059 if (perf_counter_comm_match(counter, comm_event))
2060 perf_counter_comm_output(counter, comm_event);
2061 }
2062 rcu_read_unlock();
2063 }
2064
2065 static void perf_counter_comm_event(struct perf_comm_event *comm_event)
2066 {
2067 struct perf_cpu_context *cpuctx;
2068 unsigned int size;
2069 char *comm = comm_event->task->comm;
2070
2071 size = ALIGN(strlen(comm)+1, sizeof(u64));
2072
2073 comm_event->comm = comm;
2074 comm_event->comm_size = size;
2075
2076 comm_event->event.header.size = sizeof(comm_event->event) + size;
2077
2078 cpuctx = &get_cpu_var(perf_cpu_context);
2079 perf_counter_comm_ctx(&cpuctx->ctx, comm_event);
2080 put_cpu_var(perf_cpu_context);
2081
2082 perf_counter_comm_ctx(&current->perf_counter_ctx, comm_event);
2083 }
2084
2085 void perf_counter_comm(struct task_struct *task)
2086 {
2087 struct perf_comm_event comm_event;
2088
2089 if (!atomic_read(&nr_comm_tracking))
2090 return;
2091
2092 comm_event = (struct perf_comm_event){
2093 .task = task,
2094 .event = {
2095 .header = { .type = PERF_EVENT_COMM, },
2096 .pid = task->group_leader->pid,
2097 .tid = task->pid,
2098 },
2099 };
2100
2101 perf_counter_comm_event(&comm_event);
2102 }
2103
2104 /*
2105 * mmap tracking
2106 */
2107
2108 struct perf_mmap_event {
2109 struct file *file;
2110 char *file_name;
2111 int file_size;
2112
2113 struct {
2114 struct perf_event_header header;
2115
2116 u32 pid;
2117 u32 tid;
2118 u64 start;
2119 u64 len;
2120 u64 pgoff;
2121 } event;
2122 };
2123
2124 static void perf_counter_mmap_output(struct perf_counter *counter,
2125 struct perf_mmap_event *mmap_event)
2126 {
2127 struct perf_output_handle handle;
2128 int size = mmap_event->event.header.size;
2129 int ret = perf_output_begin(&handle, counter, size, 0, 0);
2130
2131 if (ret)
2132 return;
2133
2134 perf_output_put(&handle, mmap_event->event);
2135 perf_output_copy(&handle, mmap_event->file_name,
2136 mmap_event->file_size);
2137 perf_output_end(&handle);
2138 }
2139
2140 static int perf_counter_mmap_match(struct perf_counter *counter,
2141 struct perf_mmap_event *mmap_event)
2142 {
2143 if (counter->hw_event.mmap &&
2144 mmap_event->event.header.type == PERF_EVENT_MMAP)
2145 return 1;
2146
2147 if (counter->hw_event.munmap &&
2148 mmap_event->event.header.type == PERF_EVENT_MUNMAP)
2149 return 1;
2150
2151 return 0;
2152 }
2153
2154 static void perf_counter_mmap_ctx(struct perf_counter_context *ctx,
2155 struct perf_mmap_event *mmap_event)
2156 {
2157 struct perf_counter *counter;
2158
2159 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2160 return;
2161
2162 rcu_read_lock();
2163 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2164 if (perf_counter_mmap_match(counter, mmap_event))
2165 perf_counter_mmap_output(counter, mmap_event);
2166 }
2167 rcu_read_unlock();
2168 }
2169
2170 static void perf_counter_mmap_event(struct perf_mmap_event *mmap_event)
2171 {
2172 struct perf_cpu_context *cpuctx;
2173 struct file *file = mmap_event->file;
2174 unsigned int size;
2175 char tmp[16];
2176 char *buf = NULL;
2177 char *name;
2178
2179 if (file) {
2180 buf = kzalloc(PATH_MAX, GFP_KERNEL);
2181 if (!buf) {
2182 name = strncpy(tmp, "//enomem", sizeof(tmp));
2183 goto got_name;
2184 }
2185 name = d_path(&file->f_path, buf, PATH_MAX);
2186 if (IS_ERR(name)) {
2187 name = strncpy(tmp, "//toolong", sizeof(tmp));
2188 goto got_name;
2189 }
2190 } else {
2191 name = strncpy(tmp, "//anon", sizeof(tmp));
2192 goto got_name;
2193 }
2194
2195 got_name:
2196 size = ALIGN(strlen(name)+1, sizeof(u64));
2197
2198 mmap_event->file_name = name;
2199 mmap_event->file_size = size;
2200
2201 mmap_event->event.header.size = sizeof(mmap_event->event) + size;
2202
2203 cpuctx = &get_cpu_var(perf_cpu_context);
2204 perf_counter_mmap_ctx(&cpuctx->ctx, mmap_event);
2205 put_cpu_var(perf_cpu_context);
2206
2207 perf_counter_mmap_ctx(&current->perf_counter_ctx, mmap_event);
2208
2209 kfree(buf);
2210 }
2211
2212 void perf_counter_mmap(unsigned long addr, unsigned long len,
2213 unsigned long pgoff, struct file *file)
2214 {
2215 struct perf_mmap_event mmap_event;
2216
2217 if (!atomic_read(&nr_mmap_tracking))
2218 return;
2219
2220 mmap_event = (struct perf_mmap_event){
2221 .file = file,
2222 .event = {
2223 .header = { .type = PERF_EVENT_MMAP, },
2224 .pid = current->group_leader->pid,
2225 .tid = current->pid,
2226 .start = addr,
2227 .len = len,
2228 .pgoff = pgoff,
2229 },
2230 };
2231
2232 perf_counter_mmap_event(&mmap_event);
2233 }
2234
2235 void perf_counter_munmap(unsigned long addr, unsigned long len,
2236 unsigned long pgoff, struct file *file)
2237 {
2238 struct perf_mmap_event mmap_event;
2239
2240 if (!atomic_read(&nr_munmap_tracking))
2241 return;
2242
2243 mmap_event = (struct perf_mmap_event){
2244 .file = file,
2245 .event = {
2246 .header = { .type = PERF_EVENT_MUNMAP, },
2247 .pid = current->group_leader->pid,
2248 .tid = current->pid,
2249 .start = addr,
2250 .len = len,
2251 .pgoff = pgoff,
2252 },
2253 };
2254
2255 perf_counter_mmap_event(&mmap_event);
2256 }
2257
2258 /*
2259 * Generic counter overflow handling.
2260 */
2261
2262 int perf_counter_overflow(struct perf_counter *counter,
2263 int nmi, struct pt_regs *regs, u64 addr)
2264 {
2265 int events = atomic_read(&counter->event_limit);
2266 int ret = 0;
2267
2268 counter->pending_kill = POLL_IN;
2269 if (events && atomic_dec_and_test(&counter->event_limit)) {
2270 ret = 1;
2271 counter->pending_kill = POLL_HUP;
2272 if (nmi) {
2273 counter->pending_disable = 1;
2274 perf_pending_queue(&counter->pending,
2275 perf_pending_counter);
2276 } else
2277 perf_counter_disable(counter);
2278 }
2279
2280 perf_counter_output(counter, nmi, regs, addr);
2281 return ret;
2282 }
2283
2284 /*
2285 * Generic software counter infrastructure
2286 */
2287
2288 static void perf_swcounter_update(struct perf_counter *counter)
2289 {
2290 struct hw_perf_counter *hwc = &counter->hw;
2291 u64 prev, now;
2292 s64 delta;
2293
2294 again:
2295 prev = atomic64_read(&hwc->prev_count);
2296 now = atomic64_read(&hwc->count);
2297 if (atomic64_cmpxchg(&hwc->prev_count, prev, now) != prev)
2298 goto again;
2299
2300 delta = now - prev;
2301
2302 atomic64_add(delta, &counter->count);
2303 atomic64_sub(delta, &hwc->period_left);
2304 }
2305
2306 static void perf_swcounter_set_period(struct perf_counter *counter)
2307 {
2308 struct hw_perf_counter *hwc = &counter->hw;
2309 s64 left = atomic64_read(&hwc->period_left);
2310 s64 period = hwc->irq_period;
2311
2312 if (unlikely(left <= -period)) {
2313 left = period;
2314 atomic64_set(&hwc->period_left, left);
2315 }
2316
2317 if (unlikely(left <= 0)) {
2318 left += period;
2319 atomic64_add(period, &hwc->period_left);
2320 }
2321
2322 atomic64_set(&hwc->prev_count, -left);
2323 atomic64_set(&hwc->count, -left);
2324 }
2325
2326 static enum hrtimer_restart perf_swcounter_hrtimer(struct hrtimer *hrtimer)
2327 {
2328 enum hrtimer_restart ret = HRTIMER_RESTART;
2329 struct perf_counter *counter;
2330 struct pt_regs *regs;
2331
2332 counter = container_of(hrtimer, struct perf_counter, hw.hrtimer);
2333 counter->pmu->read(counter);
2334
2335 regs = get_irq_regs();
2336 /*
2337 * In case we exclude kernel IPs or are somehow not in interrupt
2338 * context, provide the next best thing, the user IP.
2339 */
2340 if ((counter->hw_event.exclude_kernel || !regs) &&
2341 !counter->hw_event.exclude_user)
2342 regs = task_pt_regs(current);
2343
2344 if (regs) {
2345 if (perf_counter_overflow(counter, 0, regs, 0))
2346 ret = HRTIMER_NORESTART;
2347 }
2348
2349 hrtimer_forward_now(hrtimer, ns_to_ktime(counter->hw.irq_period));
2350
2351 return ret;
2352 }
2353
2354 static void perf_swcounter_overflow(struct perf_counter *counter,
2355 int nmi, struct pt_regs *regs, u64 addr)
2356 {
2357 perf_swcounter_update(counter);
2358 perf_swcounter_set_period(counter);
2359 if (perf_counter_overflow(counter, nmi, regs, addr))
2360 /* soft-disable the counter */
2361 ;
2362
2363 }
2364
2365 static int perf_swcounter_match(struct perf_counter *counter,
2366 enum perf_event_types type,
2367 u32 event, struct pt_regs *regs)
2368 {
2369 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
2370 return 0;
2371
2372 if (perf_event_raw(&counter->hw_event))
2373 return 0;
2374
2375 if (perf_event_type(&counter->hw_event) != type)
2376 return 0;
2377
2378 if (perf_event_id(&counter->hw_event) != event)
2379 return 0;
2380
2381 if (counter->hw_event.exclude_user && user_mode(regs))
2382 return 0;
2383
2384 if (counter->hw_event.exclude_kernel && !user_mode(regs))
2385 return 0;
2386
2387 return 1;
2388 }
2389
2390 static void perf_swcounter_add(struct perf_counter *counter, u64 nr,
2391 int nmi, struct pt_regs *regs, u64 addr)
2392 {
2393 int neg = atomic64_add_negative(nr, &counter->hw.count);
2394 if (counter->hw.irq_period && !neg)
2395 perf_swcounter_overflow(counter, nmi, regs, addr);
2396 }
2397
2398 static void perf_swcounter_ctx_event(struct perf_counter_context *ctx,
2399 enum perf_event_types type, u32 event,
2400 u64 nr, int nmi, struct pt_regs *regs,
2401 u64 addr)
2402 {
2403 struct perf_counter *counter;
2404
2405 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2406 return;
2407
2408 rcu_read_lock();
2409 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2410 if (perf_swcounter_match(counter, type, event, regs))
2411 perf_swcounter_add(counter, nr, nmi, regs, addr);
2412 }
2413 rcu_read_unlock();
2414 }
2415
2416 static int *perf_swcounter_recursion_context(struct perf_cpu_context *cpuctx)
2417 {
2418 if (in_nmi())
2419 return &cpuctx->recursion[3];
2420
2421 if (in_irq())
2422 return &cpuctx->recursion[2];
2423
2424 if (in_softirq())
2425 return &cpuctx->recursion[1];
2426
2427 return &cpuctx->recursion[0];
2428 }
2429
2430 static void __perf_swcounter_event(enum perf_event_types type, u32 event,
2431 u64 nr, int nmi, struct pt_regs *regs,
2432 u64 addr)
2433 {
2434 struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
2435 int *recursion = perf_swcounter_recursion_context(cpuctx);
2436
2437 if (*recursion)
2438 goto out;
2439
2440 (*recursion)++;
2441 barrier();
2442
2443 perf_swcounter_ctx_event(&cpuctx->ctx, type, event,
2444 nr, nmi, regs, addr);
2445 if (cpuctx->task_ctx) {
2446 perf_swcounter_ctx_event(cpuctx->task_ctx, type, event,
2447 nr, nmi, regs, addr);
2448 }
2449
2450 barrier();
2451 (*recursion)--;
2452
2453 out:
2454 put_cpu_var(perf_cpu_context);
2455 }
2456
2457 void
2458 perf_swcounter_event(u32 event, u64 nr, int nmi, struct pt_regs *regs, u64 addr)
2459 {
2460 __perf_swcounter_event(PERF_TYPE_SOFTWARE, event, nr, nmi, regs, addr);
2461 }
2462
2463 static void perf_swcounter_read(struct perf_counter *counter)
2464 {
2465 perf_swcounter_update(counter);
2466 }
2467
2468 static int perf_swcounter_enable(struct perf_counter *counter)
2469 {
2470 perf_swcounter_set_period(counter);
2471 return 0;
2472 }
2473
2474 static void perf_swcounter_disable(struct perf_counter *counter)
2475 {
2476 perf_swcounter_update(counter);
2477 }
2478
2479 static const struct pmu perf_ops_generic = {
2480 .enable = perf_swcounter_enable,
2481 .disable = perf_swcounter_disable,
2482 .read = perf_swcounter_read,
2483 };
2484
2485 /*
2486 * Software counter: cpu wall time clock
2487 */
2488
2489 static void cpu_clock_perf_counter_update(struct perf_counter *counter)
2490 {
2491 int cpu = raw_smp_processor_id();
2492 s64 prev;
2493 u64 now;
2494
2495 now = cpu_clock(cpu);
2496 prev = atomic64_read(&counter->hw.prev_count);
2497 atomic64_set(&counter->hw.prev_count, now);
2498 atomic64_add(now - prev, &counter->count);
2499 }
2500
2501 static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
2502 {
2503 struct hw_perf_counter *hwc = &counter->hw;
2504 int cpu = raw_smp_processor_id();
2505
2506 atomic64_set(&hwc->prev_count, cpu_clock(cpu));
2507 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2508 hwc->hrtimer.function = perf_swcounter_hrtimer;
2509 if (hwc->irq_period) {
2510 __hrtimer_start_range_ns(&hwc->hrtimer,
2511 ns_to_ktime(hwc->irq_period), 0,
2512 HRTIMER_MODE_REL, 0);
2513 }
2514
2515 return 0;
2516 }
2517
2518 static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
2519 {
2520 hrtimer_cancel(&counter->hw.hrtimer);
2521 cpu_clock_perf_counter_update(counter);
2522 }
2523
2524 static void cpu_clock_perf_counter_read(struct perf_counter *counter)
2525 {
2526 cpu_clock_perf_counter_update(counter);
2527 }
2528
2529 static const struct pmu perf_ops_cpu_clock = {
2530 .enable = cpu_clock_perf_counter_enable,
2531 .disable = cpu_clock_perf_counter_disable,
2532 .read = cpu_clock_perf_counter_read,
2533 };
2534
2535 /*
2536 * Software counter: task time clock
2537 */
2538
2539 static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
2540 {
2541 u64 prev;
2542 s64 delta;
2543
2544 prev = atomic64_xchg(&counter->hw.prev_count, now);
2545 delta = now - prev;
2546 atomic64_add(delta, &counter->count);
2547 }
2548
2549 static int task_clock_perf_counter_enable(struct perf_counter *counter)
2550 {
2551 struct hw_perf_counter *hwc = &counter->hw;
2552 u64 now;
2553
2554 now = counter->ctx->time;
2555
2556 atomic64_set(&hwc->prev_count, now);
2557 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2558 hwc->hrtimer.function = perf_swcounter_hrtimer;
2559 if (hwc->irq_period) {
2560 __hrtimer_start_range_ns(&hwc->hrtimer,
2561 ns_to_ktime(hwc->irq_period), 0,
2562 HRTIMER_MODE_REL, 0);
2563 }
2564
2565 return 0;
2566 }
2567
2568 static void task_clock_perf_counter_disable(struct perf_counter *counter)
2569 {
2570 hrtimer_cancel(&counter->hw.hrtimer);
2571 task_clock_perf_counter_update(counter, counter->ctx->time);
2572
2573 }
2574
2575 static void task_clock_perf_counter_read(struct perf_counter *counter)
2576 {
2577 u64 time;
2578
2579 if (!in_nmi()) {
2580 update_context_time(counter->ctx);
2581 time = counter->ctx->time;
2582 } else {
2583 u64 now = perf_clock();
2584 u64 delta = now - counter->ctx->timestamp;
2585 time = counter->ctx->time + delta;
2586 }
2587
2588 task_clock_perf_counter_update(counter, time);
2589 }
2590
2591 static const struct pmu perf_ops_task_clock = {
2592 .enable = task_clock_perf_counter_enable,
2593 .disable = task_clock_perf_counter_disable,
2594 .read = task_clock_perf_counter_read,
2595 };
2596
2597 /*
2598 * Software counter: cpu migrations
2599 */
2600
2601 static inline u64 get_cpu_migrations(struct perf_counter *counter)
2602 {
2603 struct task_struct *curr = counter->ctx->task;
2604
2605 if (curr)
2606 return curr->se.nr_migrations;
2607 return cpu_nr_migrations(smp_processor_id());
2608 }
2609
2610 static void cpu_migrations_perf_counter_update(struct perf_counter *counter)
2611 {
2612 u64 prev, now;
2613 s64 delta;
2614
2615 prev = atomic64_read(&counter->hw.prev_count);
2616 now = get_cpu_migrations(counter);
2617
2618 atomic64_set(&counter->hw.prev_count, now);
2619
2620 delta = now - prev;
2621
2622 atomic64_add(delta, &counter->count);
2623 }
2624
2625 static void cpu_migrations_perf_counter_read(struct perf_counter *counter)
2626 {
2627 cpu_migrations_perf_counter_update(counter);
2628 }
2629
2630 static int cpu_migrations_perf_counter_enable(struct perf_counter *counter)
2631 {
2632 if (counter->prev_state <= PERF_COUNTER_STATE_OFF)
2633 atomic64_set(&counter->hw.prev_count,
2634 get_cpu_migrations(counter));
2635 return 0;
2636 }
2637
2638 static void cpu_migrations_perf_counter_disable(struct perf_counter *counter)
2639 {
2640 cpu_migrations_perf_counter_update(counter);
2641 }
2642
2643 static const struct pmu perf_ops_cpu_migrations = {
2644 .enable = cpu_migrations_perf_counter_enable,
2645 .disable = cpu_migrations_perf_counter_disable,
2646 .read = cpu_migrations_perf_counter_read,
2647 };
2648
2649 #ifdef CONFIG_EVENT_PROFILE
2650 void perf_tpcounter_event(int event_id)
2651 {
2652 struct pt_regs *regs = get_irq_regs();
2653
2654 if (!regs)
2655 regs = task_pt_regs(current);
2656
2657 __perf_swcounter_event(PERF_TYPE_TRACEPOINT, event_id, 1, 1, regs, 0);
2658 }
2659 EXPORT_SYMBOL_GPL(perf_tpcounter_event);
2660
2661 extern int ftrace_profile_enable(int);
2662 extern void ftrace_profile_disable(int);
2663
2664 static void tp_perf_counter_destroy(struct perf_counter *counter)
2665 {
2666 ftrace_profile_disable(perf_event_id(&counter->hw_event));
2667 }
2668
2669 static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
2670 {
2671 int event_id = perf_event_id(&counter->hw_event);
2672 int ret;
2673
2674 ret = ftrace_profile_enable(event_id);
2675 if (ret)
2676 return NULL;
2677
2678 counter->destroy = tp_perf_counter_destroy;
2679 counter->hw.irq_period = counter->hw_event.irq_period;
2680
2681 return &perf_ops_generic;
2682 }
2683 #else
2684 static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
2685 {
2686 return NULL;
2687 }
2688 #endif
2689
2690 static const struct pmu *sw_perf_counter_init(struct perf_counter *counter)
2691 {
2692 struct perf_counter_hw_event *hw_event = &counter->hw_event;
2693 const struct pmu *pmu = NULL;
2694 struct hw_perf_counter *hwc = &counter->hw;
2695
2696 /*
2697 * Software counters (currently) can't in general distinguish
2698 * between user, kernel and hypervisor events.
2699 * However, context switches and cpu migrations are considered
2700 * to be kernel events, and page faults are never hypervisor
2701 * events.
2702 */
2703 switch (perf_event_id(&counter->hw_event)) {
2704 case PERF_COUNT_CPU_CLOCK:
2705 pmu = &perf_ops_cpu_clock;
2706
2707 if (hw_event->irq_period && hw_event->irq_period < 10000)
2708 hw_event->irq_period = 10000;
2709 break;
2710 case PERF_COUNT_TASK_CLOCK:
2711 /*
2712 * If the user instantiates this as a per-cpu counter,
2713 * use the cpu_clock counter instead.
2714 */
2715 if (counter->ctx->task)
2716 pmu = &perf_ops_task_clock;
2717 else
2718 pmu = &perf_ops_cpu_clock;
2719
2720 if (hw_event->irq_period && hw_event->irq_period < 10000)
2721 hw_event->irq_period = 10000;
2722 break;
2723 case PERF_COUNT_PAGE_FAULTS:
2724 case PERF_COUNT_PAGE_FAULTS_MIN:
2725 case PERF_COUNT_PAGE_FAULTS_MAJ:
2726 case PERF_COUNT_CONTEXT_SWITCHES:
2727 pmu = &perf_ops_generic;
2728 break;
2729 case PERF_COUNT_CPU_MIGRATIONS:
2730 if (!counter->hw_event.exclude_kernel)
2731 pmu = &perf_ops_cpu_migrations;
2732 break;
2733 }
2734
2735 if (pmu)
2736 hwc->irq_period = hw_event->irq_period;
2737
2738 return pmu;
2739 }
2740
2741 /*
2742 * Allocate and initialize a counter structure
2743 */
2744 static struct perf_counter *
2745 perf_counter_alloc(struct perf_counter_hw_event *hw_event,
2746 int cpu,
2747 struct perf_counter_context *ctx,
2748 struct perf_counter *group_leader,
2749 gfp_t gfpflags)
2750 {
2751 const struct pmu *pmu;
2752 struct perf_counter *counter;
2753 long err;
2754
2755 counter = kzalloc(sizeof(*counter), gfpflags);
2756 if (!counter)
2757 return ERR_PTR(-ENOMEM);
2758
2759 /*
2760 * Single counters are their own group leaders, with an
2761 * empty sibling list:
2762 */
2763 if (!group_leader)
2764 group_leader = counter;
2765
2766 mutex_init(&counter->mutex);
2767 INIT_LIST_HEAD(&counter->list_entry);
2768 INIT_LIST_HEAD(&counter->event_entry);
2769 INIT_LIST_HEAD(&counter->sibling_list);
2770 init_waitqueue_head(&counter->waitq);
2771
2772 mutex_init(&counter->mmap_mutex);
2773
2774 INIT_LIST_HEAD(&counter->child_list);
2775
2776 counter->cpu = cpu;
2777 counter->hw_event = *hw_event;
2778 counter->group_leader = group_leader;
2779 counter->pmu = NULL;
2780 counter->ctx = ctx;
2781
2782 counter->state = PERF_COUNTER_STATE_INACTIVE;
2783 if (hw_event->disabled)
2784 counter->state = PERF_COUNTER_STATE_OFF;
2785
2786 pmu = NULL;
2787
2788 if (perf_event_raw(hw_event)) {
2789 pmu = hw_perf_counter_init(counter);
2790 goto done;
2791 }
2792
2793 switch (perf_event_type(hw_event)) {
2794 case PERF_TYPE_HARDWARE:
2795 pmu = hw_perf_counter_init(counter);
2796 break;
2797
2798 case PERF_TYPE_SOFTWARE:
2799 pmu = sw_perf_counter_init(counter);
2800 break;
2801
2802 case PERF_TYPE_TRACEPOINT:
2803 pmu = tp_perf_counter_init(counter);
2804 break;
2805 }
2806 done:
2807 err = 0;
2808 if (!pmu)
2809 err = -EINVAL;
2810 else if (IS_ERR(pmu))
2811 err = PTR_ERR(pmu);
2812
2813 if (err) {
2814 kfree(counter);
2815 return ERR_PTR(err);
2816 }
2817
2818 counter->pmu = pmu;
2819
2820 if (counter->hw_event.mmap)
2821 atomic_inc(&nr_mmap_tracking);
2822 if (counter->hw_event.munmap)
2823 atomic_inc(&nr_munmap_tracking);
2824 if (counter->hw_event.comm)
2825 atomic_inc(&nr_comm_tracking);
2826
2827 return counter;
2828 }
2829
2830 /**
2831 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
2832 *
2833 * @hw_event_uptr: event type attributes for monitoring/sampling
2834 * @pid: target pid
2835 * @cpu: target cpu
2836 * @group_fd: group leader counter fd
2837 */
2838 SYSCALL_DEFINE5(perf_counter_open,
2839 const struct perf_counter_hw_event __user *, hw_event_uptr,
2840 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
2841 {
2842 struct perf_counter *counter, *group_leader;
2843 struct perf_counter_hw_event hw_event;
2844 struct perf_counter_context *ctx;
2845 struct file *counter_file = NULL;
2846 struct file *group_file = NULL;
2847 int fput_needed = 0;
2848 int fput_needed2 = 0;
2849 int ret;
2850
2851 /* for future expandability... */
2852 if (flags)
2853 return -EINVAL;
2854
2855 if (copy_from_user(&hw_event, hw_event_uptr, sizeof(hw_event)) != 0)
2856 return -EFAULT;
2857
2858 /*
2859 * Get the target context (task or percpu):
2860 */
2861 ctx = find_get_context(pid, cpu);
2862 if (IS_ERR(ctx))
2863 return PTR_ERR(ctx);
2864
2865 /*
2866 * Look up the group leader (we will attach this counter to it):
2867 */
2868 group_leader = NULL;
2869 if (group_fd != -1) {
2870 ret = -EINVAL;
2871 group_file = fget_light(group_fd, &fput_needed);
2872 if (!group_file)
2873 goto err_put_context;
2874 if (group_file->f_op != &perf_fops)
2875 goto err_put_context;
2876
2877 group_leader = group_file->private_data;
2878 /*
2879 * Do not allow a recursive hierarchy (this new sibling
2880 * becoming part of another group-sibling):
2881 */
2882 if (group_leader->group_leader != group_leader)
2883 goto err_put_context;
2884 /*
2885 * Do not allow to attach to a group in a different
2886 * task or CPU context:
2887 */
2888 if (group_leader->ctx != ctx)
2889 goto err_put_context;
2890 /*
2891 * Only a group leader can be exclusive or pinned
2892 */
2893 if (hw_event.exclusive || hw_event.pinned)
2894 goto err_put_context;
2895 }
2896
2897 counter = perf_counter_alloc(&hw_event, cpu, ctx, group_leader,
2898 GFP_KERNEL);
2899 ret = PTR_ERR(counter);
2900 if (IS_ERR(counter))
2901 goto err_put_context;
2902
2903 ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
2904 if (ret < 0)
2905 goto err_free_put_context;
2906
2907 counter_file = fget_light(ret, &fput_needed2);
2908 if (!counter_file)
2909 goto err_free_put_context;
2910
2911 counter->filp = counter_file;
2912 mutex_lock(&ctx->mutex);
2913 perf_install_in_context(ctx, counter, cpu);
2914 mutex_unlock(&ctx->mutex);
2915
2916 fput_light(counter_file, fput_needed2);
2917
2918 out_fput:
2919 fput_light(group_file, fput_needed);
2920
2921 return ret;
2922
2923 err_free_put_context:
2924 kfree(counter);
2925
2926 err_put_context:
2927 put_context(ctx);
2928
2929 goto out_fput;
2930 }
2931
2932 /*
2933 * Initialize the perf_counter context in a task_struct:
2934 */
2935 static void
2936 __perf_counter_init_context(struct perf_counter_context *ctx,
2937 struct task_struct *task)
2938 {
2939 memset(ctx, 0, sizeof(*ctx));
2940 spin_lock_init(&ctx->lock);
2941 mutex_init(&ctx->mutex);
2942 INIT_LIST_HEAD(&ctx->counter_list);
2943 INIT_LIST_HEAD(&ctx->event_list);
2944 ctx->task = task;
2945 }
2946
2947 /*
2948 * inherit a counter from parent task to child task:
2949 */
2950 static struct perf_counter *
2951 inherit_counter(struct perf_counter *parent_counter,
2952 struct task_struct *parent,
2953 struct perf_counter_context *parent_ctx,
2954 struct task_struct *child,
2955 struct perf_counter *group_leader,
2956 struct perf_counter_context *child_ctx)
2957 {
2958 struct perf_counter *child_counter;
2959
2960 /*
2961 * Instead of creating recursive hierarchies of counters,
2962 * we link inherited counters back to the original parent,
2963 * which has a filp for sure, which we use as the reference
2964 * count:
2965 */
2966 if (parent_counter->parent)
2967 parent_counter = parent_counter->parent;
2968
2969 child_counter = perf_counter_alloc(&parent_counter->hw_event,
2970 parent_counter->cpu, child_ctx,
2971 group_leader, GFP_KERNEL);
2972 if (IS_ERR(child_counter))
2973 return child_counter;
2974
2975 /*
2976 * Link it up in the child's context:
2977 */
2978 child_counter->task = child;
2979 add_counter_to_ctx(child_counter, child_ctx);
2980
2981 child_counter->parent = parent_counter;
2982 /*
2983 * inherit into child's child as well:
2984 */
2985 child_counter->hw_event.inherit = 1;
2986
2987 /*
2988 * Get a reference to the parent filp - we will fput it
2989 * when the child counter exits. This is safe to do because
2990 * we are in the parent and we know that the filp still
2991 * exists and has a nonzero count:
2992 */
2993 atomic_long_inc(&parent_counter->filp->f_count);
2994
2995 /*
2996 * Link this into the parent counter's child list
2997 */
2998 mutex_lock(&parent_counter->mutex);
2999 list_add_tail(&child_counter->child_list, &parent_counter->child_list);
3000
3001 /*
3002 * Make the child state follow the state of the parent counter,
3003 * not its hw_event.disabled bit. We hold the parent's mutex,
3004 * so we won't race with perf_counter_{en,dis}able_family.
3005 */
3006 if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE)
3007 child_counter->state = PERF_COUNTER_STATE_INACTIVE;
3008 else
3009 child_counter->state = PERF_COUNTER_STATE_OFF;
3010
3011 mutex_unlock(&parent_counter->mutex);
3012
3013 return child_counter;
3014 }
3015
3016 static int inherit_group(struct perf_counter *parent_counter,
3017 struct task_struct *parent,
3018 struct perf_counter_context *parent_ctx,
3019 struct task_struct *child,
3020 struct perf_counter_context *child_ctx)
3021 {
3022 struct perf_counter *leader;
3023 struct perf_counter *sub;
3024 struct perf_counter *child_ctr;
3025
3026 leader = inherit_counter(parent_counter, parent, parent_ctx,
3027 child, NULL, child_ctx);
3028 if (IS_ERR(leader))
3029 return PTR_ERR(leader);
3030 list_for_each_entry(sub, &parent_counter->sibling_list, list_entry) {
3031 child_ctr = inherit_counter(sub, parent, parent_ctx,
3032 child, leader, child_ctx);
3033 if (IS_ERR(child_ctr))
3034 return PTR_ERR(child_ctr);
3035 }
3036 return 0;
3037 }
3038
3039 static void sync_child_counter(struct perf_counter *child_counter,
3040 struct perf_counter *parent_counter)
3041 {
3042 u64 parent_val, child_val;
3043
3044 parent_val = atomic64_read(&parent_counter->count);
3045 child_val = atomic64_read(&child_counter->count);
3046
3047 /*
3048 * Add back the child's count to the parent's count:
3049 */
3050 atomic64_add(child_val, &parent_counter->count);
3051 atomic64_add(child_counter->total_time_enabled,
3052 &parent_counter->child_total_time_enabled);
3053 atomic64_add(child_counter->total_time_running,
3054 &parent_counter->child_total_time_running);
3055
3056 /*
3057 * Remove this counter from the parent's list
3058 */
3059 mutex_lock(&parent_counter->mutex);
3060 list_del_init(&child_counter->child_list);
3061 mutex_unlock(&parent_counter->mutex);
3062
3063 /*
3064 * Release the parent counter, if this was the last
3065 * reference to it.
3066 */
3067 fput(parent_counter->filp);
3068 }
3069
3070 static void
3071 __perf_counter_exit_task(struct task_struct *child,
3072 struct perf_counter *child_counter,
3073 struct perf_counter_context *child_ctx)
3074 {
3075 struct perf_counter *parent_counter;
3076 struct perf_counter *sub, *tmp;
3077
3078 /*
3079 * If we do not self-reap then we have to wait for the
3080 * child task to unschedule (it will happen for sure),
3081 * so that its counter is at its final count. (This
3082 * condition triggers rarely - child tasks usually get
3083 * off their CPU before the parent has a chance to
3084 * get this far into the reaping action)
3085 */
3086 if (child != current) {
3087 wait_task_inactive(child, 0);
3088 list_del_init(&child_counter->list_entry);
3089 update_counter_times(child_counter);
3090 } else {
3091 struct perf_cpu_context *cpuctx;
3092 unsigned long flags;
3093 u64 perf_flags;
3094
3095 /*
3096 * Disable and unlink this counter.
3097 *
3098 * Be careful about zapping the list - IRQ/NMI context
3099 * could still be processing it:
3100 */
3101 local_irq_save(flags);
3102 perf_flags = hw_perf_save_disable();
3103
3104 cpuctx = &__get_cpu_var(perf_cpu_context);
3105
3106 group_sched_out(child_counter, cpuctx, child_ctx);
3107 update_counter_times(child_counter);
3108
3109 list_del_init(&child_counter->list_entry);
3110
3111 child_ctx->nr_counters--;
3112
3113 hw_perf_restore(perf_flags);
3114 local_irq_restore(flags);
3115 }
3116
3117 parent_counter = child_counter->parent;
3118 /*
3119 * It can happen that parent exits first, and has counters
3120 * that are still around due to the child reference. These
3121 * counters need to be zapped - but otherwise linger.
3122 */
3123 if (parent_counter) {
3124 sync_child_counter(child_counter, parent_counter);
3125 list_for_each_entry_safe(sub, tmp, &child_counter->sibling_list,
3126 list_entry) {
3127 if (sub->parent) {
3128 sync_child_counter(sub, sub->parent);
3129 free_counter(sub);
3130 }
3131 }
3132 free_counter(child_counter);
3133 }
3134 }
3135
3136 /*
3137 * When a child task exits, feed back counter values to parent counters.
3138 *
3139 * Note: we may be running in child context, but the PID is not hashed
3140 * anymore so new counters will not be added.
3141 */
3142 void perf_counter_exit_task(struct task_struct *child)
3143 {
3144 struct perf_counter *child_counter, *tmp;
3145 struct perf_counter_context *child_ctx;
3146
3147 child_ctx = &child->perf_counter_ctx;
3148
3149 if (likely(!child_ctx->nr_counters))
3150 return;
3151
3152 list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
3153 list_entry)
3154 __perf_counter_exit_task(child, child_counter, child_ctx);
3155 }
3156
3157 /*
3158 * Initialize the perf_counter context in task_struct
3159 */
3160 void perf_counter_init_task(struct task_struct *child)
3161 {
3162 struct perf_counter_context *child_ctx, *parent_ctx;
3163 struct perf_counter *counter;
3164 struct task_struct *parent = current;
3165
3166 child_ctx = &child->perf_counter_ctx;
3167 parent_ctx = &parent->perf_counter_ctx;
3168
3169 __perf_counter_init_context(child_ctx, child);
3170
3171 /*
3172 * This is executed from the parent task context, so inherit
3173 * counters that have been marked for cloning:
3174 */
3175
3176 if (likely(!parent_ctx->nr_counters))
3177 return;
3178
3179 /*
3180 * Lock the parent list. No need to lock the child - not PID
3181 * hashed yet and not running, so nobody can access it.
3182 */
3183 mutex_lock(&parent_ctx->mutex);
3184
3185 /*
3186 * We dont have to disable NMIs - we are only looking at
3187 * the list, not manipulating it:
3188 */
3189 list_for_each_entry(counter, &parent_ctx->counter_list, list_entry) {
3190 if (!counter->hw_event.inherit)
3191 continue;
3192
3193 if (inherit_group(counter, parent,
3194 parent_ctx, child, child_ctx))
3195 break;
3196 }
3197
3198 mutex_unlock(&parent_ctx->mutex);
3199 }
3200
3201 static void __cpuinit perf_counter_init_cpu(int cpu)
3202 {
3203 struct perf_cpu_context *cpuctx;
3204
3205 cpuctx = &per_cpu(perf_cpu_context, cpu);
3206 __perf_counter_init_context(&cpuctx->ctx, NULL);
3207
3208 spin_lock(&perf_resource_lock);
3209 cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
3210 spin_unlock(&perf_resource_lock);
3211
3212 hw_perf_counter_setup(cpu);
3213 }
3214
3215 #ifdef CONFIG_HOTPLUG_CPU
3216 static void __perf_counter_exit_cpu(void *info)
3217 {
3218 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
3219 struct perf_counter_context *ctx = &cpuctx->ctx;
3220 struct perf_counter *counter, *tmp;
3221
3222 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
3223 __perf_counter_remove_from_context(counter);
3224 }
3225 static void perf_counter_exit_cpu(int cpu)
3226 {
3227 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
3228 struct perf_counter_context *ctx = &cpuctx->ctx;
3229
3230 mutex_lock(&ctx->mutex);
3231 smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
3232 mutex_unlock(&ctx->mutex);
3233 }
3234 #else
3235 static inline void perf_counter_exit_cpu(int cpu) { }
3236 #endif
3237
3238 static int __cpuinit
3239 perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
3240 {
3241 unsigned int cpu = (long)hcpu;
3242
3243 switch (action) {
3244
3245 case CPU_UP_PREPARE:
3246 case CPU_UP_PREPARE_FROZEN:
3247 perf_counter_init_cpu(cpu);
3248 break;
3249
3250 case CPU_DOWN_PREPARE:
3251 case CPU_DOWN_PREPARE_FROZEN:
3252 perf_counter_exit_cpu(cpu);
3253 break;
3254
3255 default:
3256 break;
3257 }
3258
3259 return NOTIFY_OK;
3260 }
3261
3262 static struct notifier_block __cpuinitdata perf_cpu_nb = {
3263 .notifier_call = perf_cpu_notify,
3264 };
3265
3266 void __init perf_counter_init(void)
3267 {
3268 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
3269 (void *)(long)smp_processor_id());
3270 register_cpu_notifier(&perf_cpu_nb);
3271 }
3272
3273 static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
3274 {
3275 return sprintf(buf, "%d\n", perf_reserved_percpu);
3276 }
3277
3278 static ssize_t
3279 perf_set_reserve_percpu(struct sysdev_class *class,
3280 const char *buf,
3281 size_t count)
3282 {
3283 struct perf_cpu_context *cpuctx;
3284 unsigned long val;
3285 int err, cpu, mpt;
3286
3287 err = strict_strtoul(buf, 10, &val);
3288 if (err)
3289 return err;
3290 if (val > perf_max_counters)
3291 return -EINVAL;
3292
3293 spin_lock(&perf_resource_lock);
3294 perf_reserved_percpu = val;
3295 for_each_online_cpu(cpu) {
3296 cpuctx = &per_cpu(perf_cpu_context, cpu);
3297 spin_lock_irq(&cpuctx->ctx.lock);
3298 mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
3299 perf_max_counters - perf_reserved_percpu);
3300 cpuctx->max_pertask = mpt;
3301 spin_unlock_irq(&cpuctx->ctx.lock);
3302 }
3303 spin_unlock(&perf_resource_lock);
3304
3305 return count;
3306 }
3307
3308 static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
3309 {
3310 return sprintf(buf, "%d\n", perf_overcommit);
3311 }
3312
3313 static ssize_t
3314 perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
3315 {
3316 unsigned long val;
3317 int err;
3318
3319 err = strict_strtoul(buf, 10, &val);
3320 if (err)
3321 return err;
3322 if (val > 1)
3323 return -EINVAL;
3324
3325 spin_lock(&perf_resource_lock);
3326 perf_overcommit = val;
3327 spin_unlock(&perf_resource_lock);
3328
3329 return count;
3330 }
3331
3332 static SYSDEV_CLASS_ATTR(
3333 reserve_percpu,
3334 0644,
3335 perf_show_reserve_percpu,
3336 perf_set_reserve_percpu
3337 );
3338
3339 static SYSDEV_CLASS_ATTR(
3340 overcommit,
3341 0644,
3342 perf_show_overcommit,
3343 perf_set_overcommit
3344 );
3345
3346 static struct attribute *perfclass_attrs[] = {
3347 &attr_reserve_percpu.attr,
3348 &attr_overcommit.attr,
3349 NULL
3350 };
3351
3352 static struct attribute_group perfclass_attr_group = {
3353 .attrs = perfclass_attrs,
3354 .name = "perf_counters",
3355 };
3356
3357 static int __init perf_counter_sysfs_init(void)
3358 {
3359 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
3360 &perfclass_attr_group);
3361 }
3362 device_initcall(perf_counter_sysfs_init);