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