perf_counter: revamp syscall input ABI
[safe/jmp/linux-2.6] / kernel / perf_counter.c
1 /*
2  * Performance counter core code
3  *
4  *  Copyright(C) 2008 Thomas Gleixner <tglx@linutronix.de>
5  *  Copyright(C) 2008 Red Hat, Inc., Ingo Molnar
6  *
7  *  For licencing details see kernel-base/COPYING
8  */
9
10 #include <linux/fs.h>
11 #include <linux/cpu.h>
12 #include <linux/smp.h>
13 #include <linux/file.h>
14 #include <linux/poll.h>
15 #include <linux/sysfs.h>
16 #include <linux/ptrace.h>
17 #include <linux/percpu.h>
18 #include <linux/uaccess.h>
19 #include <linux/syscalls.h>
20 #include <linux/anon_inodes.h>
21 #include <linux/kernel_stat.h>
22 #include <linux/perf_counter.h>
23 #include <linux/mm.h>
24 #include <linux/vmstat.h>
25 #include <linux/rculist.h>
26
27 #include <asm/irq_regs.h>
28
29 /*
30  * Each CPU has a list of per CPU counters:
31  */
32 DEFINE_PER_CPU(struct perf_cpu_context, perf_cpu_context);
33
34 int perf_max_counters __read_mostly = 1;
35 static int perf_reserved_percpu __read_mostly;
36 static int perf_overcommit __read_mostly = 1;
37
38 /*
39  * Mutex for (sysadmin-configurable) counter reservations:
40  */
41 static DEFINE_MUTEX(perf_resource_mutex);
42
43 /*
44  * Architecture provided APIs - weak aliases:
45  */
46 extern __weak const struct hw_perf_counter_ops *
47 hw_perf_counter_init(struct perf_counter *counter)
48 {
49         return NULL;
50 }
51
52 u64 __weak hw_perf_save_disable(void)           { return 0; }
53 void __weak hw_perf_restore(u64 ctrl)           { barrier(); }
54 void __weak hw_perf_counter_setup(int cpu)      { barrier(); }
55 int __weak hw_perf_group_sched_in(struct perf_counter *group_leader,
56                struct perf_cpu_context *cpuctx,
57                struct perf_counter_context *ctx, int cpu)
58 {
59         return 0;
60 }
61
62 void __weak perf_counter_print_debug(void)      { }
63
64 static void
65 list_add_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
66 {
67         struct perf_counter *group_leader = counter->group_leader;
68
69         /*
70          * Depending on whether it is a standalone or sibling counter,
71          * add it straight to the context's counter list, or to the group
72          * leader's sibling list:
73          */
74         if (counter->group_leader == counter)
75                 list_add_tail(&counter->list_entry, &ctx->counter_list);
76         else
77                 list_add_tail(&counter->list_entry, &group_leader->sibling_list);
78
79         list_add_rcu(&counter->event_entry, &ctx->event_list);
80 }
81
82 static void
83 list_del_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
84 {
85         struct perf_counter *sibling, *tmp;
86
87         list_del_init(&counter->list_entry);
88         list_del_rcu(&counter->event_entry);
89
90         /*
91          * If this was a group counter with sibling counters then
92          * upgrade the siblings to singleton counters by adding them
93          * to the context list directly:
94          */
95         list_for_each_entry_safe(sibling, tmp,
96                                  &counter->sibling_list, list_entry) {
97
98                 list_move_tail(&sibling->list_entry, &ctx->counter_list);
99                 sibling->group_leader = sibling;
100         }
101 }
102
103 static void
104 counter_sched_out(struct perf_counter *counter,
105                   struct perf_cpu_context *cpuctx,
106                   struct perf_counter_context *ctx)
107 {
108         if (counter->state != PERF_COUNTER_STATE_ACTIVE)
109                 return;
110
111         counter->state = PERF_COUNTER_STATE_INACTIVE;
112         counter->hw_ops->disable(counter);
113         counter->oncpu = -1;
114
115         if (!is_software_counter(counter))
116                 cpuctx->active_oncpu--;
117         ctx->nr_active--;
118         if (counter->hw_event.exclusive || !cpuctx->active_oncpu)
119                 cpuctx->exclusive = 0;
120 }
121
122 static void
123 group_sched_out(struct perf_counter *group_counter,
124                 struct perf_cpu_context *cpuctx,
125                 struct perf_counter_context *ctx)
126 {
127         struct perf_counter *counter;
128
129         if (group_counter->state != PERF_COUNTER_STATE_ACTIVE)
130                 return;
131
132         counter_sched_out(group_counter, cpuctx, ctx);
133
134         /*
135          * Schedule out siblings (if any):
136          */
137         list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
138                 counter_sched_out(counter, cpuctx, ctx);
139
140         if (group_counter->hw_event.exclusive)
141                 cpuctx->exclusive = 0;
142 }
143
144 /*
145  * Cross CPU call to remove a performance counter
146  *
147  * We disable the counter on the hardware level first. After that we
148  * remove it from the context list.
149  */
150 static void __perf_counter_remove_from_context(void *info)
151 {
152         struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
153         struct perf_counter *counter = info;
154         struct perf_counter_context *ctx = counter->ctx;
155         unsigned long flags;
156         u64 perf_flags;
157
158         /*
159          * If this is a task context, we need to check whether it is
160          * the current task context of this cpu. If not it has been
161          * scheduled out before the smp call arrived.
162          */
163         if (ctx->task && cpuctx->task_ctx != ctx)
164                 return;
165
166         curr_rq_lock_irq_save(&flags);
167         spin_lock(&ctx->lock);
168
169         counter_sched_out(counter, cpuctx, ctx);
170
171         counter->task = NULL;
172         ctx->nr_counters--;
173
174         /*
175          * Protect the list operation against NMI by disabling the
176          * counters on a global level. NOP for non NMI based counters.
177          */
178         perf_flags = hw_perf_save_disable();
179         list_del_counter(counter, ctx);
180         hw_perf_restore(perf_flags);
181
182         if (!ctx->task) {
183                 /*
184                  * Allow more per task counters with respect to the
185                  * reservation:
186                  */
187                 cpuctx->max_pertask =
188                         min(perf_max_counters - ctx->nr_counters,
189                             perf_max_counters - perf_reserved_percpu);
190         }
191
192         spin_unlock(&ctx->lock);
193         curr_rq_unlock_irq_restore(&flags);
194 }
195
196
197 /*
198  * Remove the counter from a task's (or a CPU's) list of counters.
199  *
200  * Must be called with counter->mutex and ctx->mutex held.
201  *
202  * CPU counters are removed with a smp call. For task counters we only
203  * call when the task is on a CPU.
204  */
205 static void perf_counter_remove_from_context(struct perf_counter *counter)
206 {
207         struct perf_counter_context *ctx = counter->ctx;
208         struct task_struct *task = ctx->task;
209
210         if (!task) {
211                 /*
212                  * Per cpu counters are removed via an smp call and
213                  * the removal is always sucessful.
214                  */
215                 smp_call_function_single(counter->cpu,
216                                          __perf_counter_remove_from_context,
217                                          counter, 1);
218                 return;
219         }
220
221 retry:
222         task_oncpu_function_call(task, __perf_counter_remove_from_context,
223                                  counter);
224
225         spin_lock_irq(&ctx->lock);
226         /*
227          * If the context is active we need to retry the smp call.
228          */
229         if (ctx->nr_active && !list_empty(&counter->list_entry)) {
230                 spin_unlock_irq(&ctx->lock);
231                 goto retry;
232         }
233
234         /*
235          * The lock prevents that this context is scheduled in so we
236          * can remove the counter safely, if the call above did not
237          * succeed.
238          */
239         if (!list_empty(&counter->list_entry)) {
240                 ctx->nr_counters--;
241                 list_del_counter(counter, ctx);
242                 counter->task = NULL;
243         }
244         spin_unlock_irq(&ctx->lock);
245 }
246
247 /*
248  * Cross CPU call to disable a performance counter
249  */
250 static void __perf_counter_disable(void *info)
251 {
252         struct perf_counter *counter = info;
253         struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
254         struct perf_counter_context *ctx = counter->ctx;
255         unsigned long flags;
256
257         /*
258          * If this is a per-task counter, need to check whether this
259          * counter's task is the current task on this cpu.
260          */
261         if (ctx->task && cpuctx->task_ctx != ctx)
262                 return;
263
264         curr_rq_lock_irq_save(&flags);
265         spin_lock(&ctx->lock);
266
267         /*
268          * If the counter is on, turn it off.
269          * If it is in error state, leave it in error state.
270          */
271         if (counter->state >= PERF_COUNTER_STATE_INACTIVE) {
272                 if (counter == counter->group_leader)
273                         group_sched_out(counter, cpuctx, ctx);
274                 else
275                         counter_sched_out(counter, cpuctx, ctx);
276                 counter->state = PERF_COUNTER_STATE_OFF;
277         }
278
279         spin_unlock(&ctx->lock);
280         curr_rq_unlock_irq_restore(&flags);
281 }
282
283 /*
284  * Disable a counter.
285  */
286 static void perf_counter_disable(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                  * Disable the counter on the cpu that it's on
294                  */
295                 smp_call_function_single(counter->cpu, __perf_counter_disable,
296                                          counter, 1);
297                 return;
298         }
299
300  retry:
301         task_oncpu_function_call(task, __perf_counter_disable, counter);
302
303         spin_lock_irq(&ctx->lock);
304         /*
305          * If the counter is still active, we need to retry the cross-call.
306          */
307         if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
308                 spin_unlock_irq(&ctx->lock);
309                 goto retry;
310         }
311
312         /*
313          * Since we have the lock this context can't be scheduled
314          * in, so we can change the state safely.
315          */
316         if (counter->state == PERF_COUNTER_STATE_INACTIVE)
317                 counter->state = PERF_COUNTER_STATE_OFF;
318
319         spin_unlock_irq(&ctx->lock);
320 }
321
322 /*
323  * Disable a counter and all its children.
324  */
325 static void perf_counter_disable_family(struct perf_counter *counter)
326 {
327         struct perf_counter *child;
328
329         perf_counter_disable(counter);
330
331         /*
332          * Lock the mutex to protect the list of children
333          */
334         mutex_lock(&counter->mutex);
335         list_for_each_entry(child, &counter->child_list, child_list)
336                 perf_counter_disable(child);
337         mutex_unlock(&counter->mutex);
338 }
339
340 static int
341 counter_sched_in(struct perf_counter *counter,
342                  struct perf_cpu_context *cpuctx,
343                  struct perf_counter_context *ctx,
344                  int cpu)
345 {
346         if (counter->state <= PERF_COUNTER_STATE_OFF)
347                 return 0;
348
349         counter->state = PERF_COUNTER_STATE_ACTIVE;
350         counter->oncpu = cpu;   /* TODO: put 'cpu' into cpuctx->cpu */
351         /*
352          * The new state must be visible before we turn it on in the hardware:
353          */
354         smp_wmb();
355
356         if (counter->hw_ops->enable(counter)) {
357                 counter->state = PERF_COUNTER_STATE_INACTIVE;
358                 counter->oncpu = -1;
359                 return -EAGAIN;
360         }
361
362         if (!is_software_counter(counter))
363                 cpuctx->active_oncpu++;
364         ctx->nr_active++;
365
366         if (counter->hw_event.exclusive)
367                 cpuctx->exclusive = 1;
368
369         return 0;
370 }
371
372 /*
373  * Return 1 for a group consisting entirely of software counters,
374  * 0 if the group contains any hardware counters.
375  */
376 static int is_software_only_group(struct perf_counter *leader)
377 {
378         struct perf_counter *counter;
379
380         if (!is_software_counter(leader))
381                 return 0;
382         list_for_each_entry(counter, &leader->sibling_list, list_entry)
383                 if (!is_software_counter(counter))
384                         return 0;
385         return 1;
386 }
387
388 /*
389  * Work out whether we can put this counter group on the CPU now.
390  */
391 static int group_can_go_on(struct perf_counter *counter,
392                            struct perf_cpu_context *cpuctx,
393                            int can_add_hw)
394 {
395         /*
396          * Groups consisting entirely of software counters can always go on.
397          */
398         if (is_software_only_group(counter))
399                 return 1;
400         /*
401          * If an exclusive group is already on, no other hardware
402          * counters can go on.
403          */
404         if (cpuctx->exclusive)
405                 return 0;
406         /*
407          * If this group is exclusive and there are already
408          * counters on the CPU, it can't go on.
409          */
410         if (counter->hw_event.exclusive && cpuctx->active_oncpu)
411                 return 0;
412         /*
413          * Otherwise, try to add it if all previous groups were able
414          * to go on.
415          */
416         return can_add_hw;
417 }
418
419 /*
420  * Cross CPU call to install and enable a performance counter
421  */
422 static void __perf_install_in_context(void *info)
423 {
424         struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
425         struct perf_counter *counter = info;
426         struct perf_counter_context *ctx = counter->ctx;
427         struct perf_counter *leader = counter->group_leader;
428         int cpu = smp_processor_id();
429         unsigned long flags;
430         u64 perf_flags;
431         int err;
432
433         /*
434          * If this is a task context, we need to check whether it is
435          * the current task context of this cpu. If not it has been
436          * scheduled out before the smp call arrived.
437          */
438         if (ctx->task && cpuctx->task_ctx != ctx)
439                 return;
440
441         curr_rq_lock_irq_save(&flags);
442         spin_lock(&ctx->lock);
443
444         /*
445          * Protect the list operation against NMI by disabling the
446          * counters on a global level. NOP for non NMI based counters.
447          */
448         perf_flags = hw_perf_save_disable();
449
450         list_add_counter(counter, ctx);
451         ctx->nr_counters++;
452         counter->prev_state = PERF_COUNTER_STATE_OFF;
453
454         /*
455          * Don't put the counter on if it is disabled or if
456          * it is in a group and the group isn't on.
457          */
458         if (counter->state != PERF_COUNTER_STATE_INACTIVE ||
459             (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE))
460                 goto unlock;
461
462         /*
463          * An exclusive counter can't go on if there are already active
464          * hardware counters, and no hardware counter can go on if there
465          * is already an exclusive counter on.
466          */
467         if (!group_can_go_on(counter, cpuctx, 1))
468                 err = -EEXIST;
469         else
470                 err = counter_sched_in(counter, cpuctx, ctx, cpu);
471
472         if (err) {
473                 /*
474                  * This counter couldn't go on.  If it is in a group
475                  * then we have to pull the whole group off.
476                  * If the counter group is pinned then put it in error state.
477                  */
478                 if (leader != counter)
479                         group_sched_out(leader, cpuctx, ctx);
480                 if (leader->hw_event.pinned)
481                         leader->state = PERF_COUNTER_STATE_ERROR;
482         }
483
484         if (!err && !ctx->task && cpuctx->max_pertask)
485                 cpuctx->max_pertask--;
486
487  unlock:
488         hw_perf_restore(perf_flags);
489
490         spin_unlock(&ctx->lock);
491         curr_rq_unlock_irq_restore(&flags);
492 }
493
494 /*
495  * Attach a performance counter to a context
496  *
497  * First we add the counter to the list with the hardware enable bit
498  * in counter->hw_config cleared.
499  *
500  * If the counter is attached to a task which is on a CPU we use a smp
501  * call to enable it in the task context. The task might have been
502  * scheduled away, but we check this in the smp call again.
503  *
504  * Must be called with ctx->mutex held.
505  */
506 static void
507 perf_install_in_context(struct perf_counter_context *ctx,
508                         struct perf_counter *counter,
509                         int cpu)
510 {
511         struct task_struct *task = ctx->task;
512
513         if (!task) {
514                 /*
515                  * Per cpu counters are installed via an smp call and
516                  * the install is always sucessful.
517                  */
518                 smp_call_function_single(cpu, __perf_install_in_context,
519                                          counter, 1);
520                 return;
521         }
522
523         counter->task = task;
524 retry:
525         task_oncpu_function_call(task, __perf_install_in_context,
526                                  counter);
527
528         spin_lock_irq(&ctx->lock);
529         /*
530          * we need to retry the smp call.
531          */
532         if (ctx->is_active && list_empty(&counter->list_entry)) {
533                 spin_unlock_irq(&ctx->lock);
534                 goto retry;
535         }
536
537         /*
538          * The lock prevents that this context is scheduled in so we
539          * can add the counter safely, if it the call above did not
540          * succeed.
541          */
542         if (list_empty(&counter->list_entry)) {
543                 list_add_counter(counter, ctx);
544                 ctx->nr_counters++;
545         }
546         spin_unlock_irq(&ctx->lock);
547 }
548
549 /*
550  * Cross CPU call to enable a performance counter
551  */
552 static void __perf_counter_enable(void *info)
553 {
554         struct perf_counter *counter = info;
555         struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
556         struct perf_counter_context *ctx = counter->ctx;
557         struct perf_counter *leader = counter->group_leader;
558         unsigned long flags;
559         int err;
560
561         /*
562          * If this is a per-task counter, need to check whether this
563          * counter's task is the current task on this cpu.
564          */
565         if (ctx->task && cpuctx->task_ctx != ctx)
566                 return;
567
568         curr_rq_lock_irq_save(&flags);
569         spin_lock(&ctx->lock);
570
571         counter->prev_state = counter->state;
572         if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
573                 goto unlock;
574         counter->state = PERF_COUNTER_STATE_INACTIVE;
575
576         /*
577          * If the counter is in a group and isn't the group leader,
578          * then don't put it on unless the group is on.
579          */
580         if (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE)
581                 goto unlock;
582
583         if (!group_can_go_on(counter, cpuctx, 1))
584                 err = -EEXIST;
585         else
586                 err = counter_sched_in(counter, cpuctx, ctx,
587                                        smp_processor_id());
588
589         if (err) {
590                 /*
591                  * If this counter can't go on and it's part of a
592                  * group, then the whole group has to come off.
593                  */
594                 if (leader != counter)
595                         group_sched_out(leader, cpuctx, ctx);
596                 if (leader->hw_event.pinned)
597                         leader->state = PERF_COUNTER_STATE_ERROR;
598         }
599
600  unlock:
601         spin_unlock(&ctx->lock);
602         curr_rq_unlock_irq_restore(&flags);
603 }
604
605 /*
606  * Enable a counter.
607  */
608 static void perf_counter_enable(struct perf_counter *counter)
609 {
610         struct perf_counter_context *ctx = counter->ctx;
611         struct task_struct *task = ctx->task;
612
613         if (!task) {
614                 /*
615                  * Enable the counter on the cpu that it's on
616                  */
617                 smp_call_function_single(counter->cpu, __perf_counter_enable,
618                                          counter, 1);
619                 return;
620         }
621
622         spin_lock_irq(&ctx->lock);
623         if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
624                 goto out;
625
626         /*
627          * If the counter is in error state, clear that first.
628          * That way, if we see the counter in error state below, we
629          * know that it has gone back into error state, as distinct
630          * from the task having been scheduled away before the
631          * cross-call arrived.
632          */
633         if (counter->state == PERF_COUNTER_STATE_ERROR)
634                 counter->state = PERF_COUNTER_STATE_OFF;
635
636  retry:
637         spin_unlock_irq(&ctx->lock);
638         task_oncpu_function_call(task, __perf_counter_enable, counter);
639
640         spin_lock_irq(&ctx->lock);
641
642         /*
643          * If the context is active and the counter is still off,
644          * we need to retry the cross-call.
645          */
646         if (ctx->is_active && counter->state == PERF_COUNTER_STATE_OFF)
647                 goto retry;
648
649         /*
650          * Since we have the lock this context can't be scheduled
651          * in, so we can change the state safely.
652          */
653         if (counter->state == PERF_COUNTER_STATE_OFF)
654                 counter->state = PERF_COUNTER_STATE_INACTIVE;
655  out:
656         spin_unlock_irq(&ctx->lock);
657 }
658
659 /*
660  * Enable a counter and all its children.
661  */
662 static void perf_counter_enable_family(struct perf_counter *counter)
663 {
664         struct perf_counter *child;
665
666         perf_counter_enable(counter);
667
668         /*
669          * Lock the mutex to protect the list of children
670          */
671         mutex_lock(&counter->mutex);
672         list_for_each_entry(child, &counter->child_list, child_list)
673                 perf_counter_enable(child);
674         mutex_unlock(&counter->mutex);
675 }
676
677 void __perf_counter_sched_out(struct perf_counter_context *ctx,
678                               struct perf_cpu_context *cpuctx)
679 {
680         struct perf_counter *counter;
681         u64 flags;
682
683         spin_lock(&ctx->lock);
684         ctx->is_active = 0;
685         if (likely(!ctx->nr_counters))
686                 goto out;
687
688         flags = hw_perf_save_disable();
689         if (ctx->nr_active) {
690                 list_for_each_entry(counter, &ctx->counter_list, list_entry)
691                         group_sched_out(counter, cpuctx, ctx);
692         }
693         hw_perf_restore(flags);
694  out:
695         spin_unlock(&ctx->lock);
696 }
697
698 /*
699  * Called from scheduler to remove the counters of the current task,
700  * with interrupts disabled.
701  *
702  * We stop each counter and update the counter value in counter->count.
703  *
704  * This does not protect us against NMI, but disable()
705  * sets the disabled bit in the control field of counter _before_
706  * accessing the counter control register. If a NMI hits, then it will
707  * not restart the counter.
708  */
709 void perf_counter_task_sched_out(struct task_struct *task, int cpu)
710 {
711         struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
712         struct perf_counter_context *ctx = &task->perf_counter_ctx;
713         struct pt_regs *regs;
714
715         if (likely(!cpuctx->task_ctx))
716                 return;
717
718         regs = task_pt_regs(task);
719         perf_swcounter_event(PERF_COUNT_CONTEXT_SWITCHES, 1, 1, regs);
720         __perf_counter_sched_out(ctx, cpuctx);
721
722         cpuctx->task_ctx = NULL;
723 }
724
725 static void perf_counter_cpu_sched_out(struct perf_cpu_context *cpuctx)
726 {
727         __perf_counter_sched_out(&cpuctx->ctx, cpuctx);
728 }
729
730 static int
731 group_sched_in(struct perf_counter *group_counter,
732                struct perf_cpu_context *cpuctx,
733                struct perf_counter_context *ctx,
734                int cpu)
735 {
736         struct perf_counter *counter, *partial_group;
737         int ret;
738
739         if (group_counter->state == PERF_COUNTER_STATE_OFF)
740                 return 0;
741
742         ret = hw_perf_group_sched_in(group_counter, cpuctx, ctx, cpu);
743         if (ret)
744                 return ret < 0 ? ret : 0;
745
746         group_counter->prev_state = group_counter->state;
747         if (counter_sched_in(group_counter, cpuctx, ctx, cpu))
748                 return -EAGAIN;
749
750         /*
751          * Schedule in siblings as one group (if any):
752          */
753         list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
754                 counter->prev_state = counter->state;
755                 if (counter_sched_in(counter, cpuctx, ctx, cpu)) {
756                         partial_group = counter;
757                         goto group_error;
758                 }
759         }
760
761         return 0;
762
763 group_error:
764         /*
765          * Groups can be scheduled in as one unit only, so undo any
766          * partial group before returning:
767          */
768         list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
769                 if (counter == partial_group)
770                         break;
771                 counter_sched_out(counter, cpuctx, ctx);
772         }
773         counter_sched_out(group_counter, cpuctx, ctx);
774
775         return -EAGAIN;
776 }
777
778 static void
779 __perf_counter_sched_in(struct perf_counter_context *ctx,
780                         struct perf_cpu_context *cpuctx, int cpu)
781 {
782         struct perf_counter *counter;
783         u64 flags;
784         int can_add_hw = 1;
785
786         spin_lock(&ctx->lock);
787         ctx->is_active = 1;
788         if (likely(!ctx->nr_counters))
789                 goto out;
790
791         flags = hw_perf_save_disable();
792
793         /*
794          * First go through the list and put on any pinned groups
795          * in order to give them the best chance of going on.
796          */
797         list_for_each_entry(counter, &ctx->counter_list, list_entry) {
798                 if (counter->state <= PERF_COUNTER_STATE_OFF ||
799                     !counter->hw_event.pinned)
800                         continue;
801                 if (counter->cpu != -1 && counter->cpu != cpu)
802                         continue;
803
804                 if (group_can_go_on(counter, cpuctx, 1))
805                         group_sched_in(counter, cpuctx, ctx, cpu);
806
807                 /*
808                  * If this pinned group hasn't been scheduled,
809                  * put it in error state.
810                  */
811                 if (counter->state == PERF_COUNTER_STATE_INACTIVE)
812                         counter->state = PERF_COUNTER_STATE_ERROR;
813         }
814
815         list_for_each_entry(counter, &ctx->counter_list, list_entry) {
816                 /*
817                  * Ignore counters in OFF or ERROR state, and
818                  * ignore pinned counters since we did them already.
819                  */
820                 if (counter->state <= PERF_COUNTER_STATE_OFF ||
821                     counter->hw_event.pinned)
822                         continue;
823
824                 /*
825                  * Listen to the 'cpu' scheduling filter constraint
826                  * of counters:
827                  */
828                 if (counter->cpu != -1 && counter->cpu != cpu)
829                         continue;
830
831                 if (group_can_go_on(counter, cpuctx, can_add_hw)) {
832                         if (group_sched_in(counter, cpuctx, ctx, cpu))
833                                 can_add_hw = 0;
834                 }
835         }
836         hw_perf_restore(flags);
837  out:
838         spin_unlock(&ctx->lock);
839 }
840
841 /*
842  * Called from scheduler to add the counters of the current task
843  * with interrupts disabled.
844  *
845  * We restore the counter value and then enable it.
846  *
847  * This does not protect us against NMI, but enable()
848  * sets the enabled bit in the control field of counter _before_
849  * accessing the counter control register. If a NMI hits, then it will
850  * keep the counter running.
851  */
852 void perf_counter_task_sched_in(struct task_struct *task, int cpu)
853 {
854         struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
855         struct perf_counter_context *ctx = &task->perf_counter_ctx;
856
857         __perf_counter_sched_in(ctx, cpuctx, cpu);
858         cpuctx->task_ctx = ctx;
859 }
860
861 static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu)
862 {
863         struct perf_counter_context *ctx = &cpuctx->ctx;
864
865         __perf_counter_sched_in(ctx, cpuctx, cpu);
866 }
867
868 int perf_counter_task_disable(void)
869 {
870         struct task_struct *curr = current;
871         struct perf_counter_context *ctx = &curr->perf_counter_ctx;
872         struct perf_counter *counter;
873         unsigned long flags;
874         u64 perf_flags;
875         int cpu;
876
877         if (likely(!ctx->nr_counters))
878                 return 0;
879
880         curr_rq_lock_irq_save(&flags);
881         cpu = smp_processor_id();
882
883         /* force the update of the task clock: */
884         __task_delta_exec(curr, 1);
885
886         perf_counter_task_sched_out(curr, cpu);
887
888         spin_lock(&ctx->lock);
889
890         /*
891          * Disable all the counters:
892          */
893         perf_flags = hw_perf_save_disable();
894
895         list_for_each_entry(counter, &ctx->counter_list, list_entry) {
896                 if (counter->state != PERF_COUNTER_STATE_ERROR)
897                         counter->state = PERF_COUNTER_STATE_OFF;
898         }
899
900         hw_perf_restore(perf_flags);
901
902         spin_unlock(&ctx->lock);
903
904         curr_rq_unlock_irq_restore(&flags);
905
906         return 0;
907 }
908
909 int perf_counter_task_enable(void)
910 {
911         struct task_struct *curr = current;
912         struct perf_counter_context *ctx = &curr->perf_counter_ctx;
913         struct perf_counter *counter;
914         unsigned long flags;
915         u64 perf_flags;
916         int cpu;
917
918         if (likely(!ctx->nr_counters))
919                 return 0;
920
921         curr_rq_lock_irq_save(&flags);
922         cpu = smp_processor_id();
923
924         /* force the update of the task clock: */
925         __task_delta_exec(curr, 1);
926
927         perf_counter_task_sched_out(curr, cpu);
928
929         spin_lock(&ctx->lock);
930
931         /*
932          * Disable all the counters:
933          */
934         perf_flags = hw_perf_save_disable();
935
936         list_for_each_entry(counter, &ctx->counter_list, list_entry) {
937                 if (counter->state > PERF_COUNTER_STATE_OFF)
938                         continue;
939                 counter->state = PERF_COUNTER_STATE_INACTIVE;
940                 counter->hw_event.disabled = 0;
941         }
942         hw_perf_restore(perf_flags);
943
944         spin_unlock(&ctx->lock);
945
946         perf_counter_task_sched_in(curr, cpu);
947
948         curr_rq_unlock_irq_restore(&flags);
949
950         return 0;
951 }
952
953 /*
954  * Round-robin a context's counters:
955  */
956 static void rotate_ctx(struct perf_counter_context *ctx)
957 {
958         struct perf_counter *counter;
959         u64 perf_flags;
960
961         if (!ctx->nr_counters)
962                 return;
963
964         spin_lock(&ctx->lock);
965         /*
966          * Rotate the first entry last (works just fine for group counters too):
967          */
968         perf_flags = hw_perf_save_disable();
969         list_for_each_entry(counter, &ctx->counter_list, list_entry) {
970                 list_move_tail(&counter->list_entry, &ctx->counter_list);
971                 break;
972         }
973         hw_perf_restore(perf_flags);
974
975         spin_unlock(&ctx->lock);
976 }
977
978 void perf_counter_task_tick(struct task_struct *curr, int cpu)
979 {
980         struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
981         struct perf_counter_context *ctx = &curr->perf_counter_ctx;
982         const int rotate_percpu = 0;
983
984         if (rotate_percpu)
985                 perf_counter_cpu_sched_out(cpuctx);
986         perf_counter_task_sched_out(curr, cpu);
987
988         if (rotate_percpu)
989                 rotate_ctx(&cpuctx->ctx);
990         rotate_ctx(ctx);
991
992         if (rotate_percpu)
993                 perf_counter_cpu_sched_in(cpuctx, cpu);
994         perf_counter_task_sched_in(curr, cpu);
995 }
996
997 /*
998  * Cross CPU call to read the hardware counter
999  */
1000 static void __read(void *info)
1001 {
1002         struct perf_counter *counter = info;
1003         unsigned long flags;
1004
1005         curr_rq_lock_irq_save(&flags);
1006         counter->hw_ops->read(counter);
1007         curr_rq_unlock_irq_restore(&flags);
1008 }
1009
1010 static u64 perf_counter_read(struct perf_counter *counter)
1011 {
1012         /*
1013          * If counter is enabled and currently active on a CPU, update the
1014          * value in the counter structure:
1015          */
1016         if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
1017                 smp_call_function_single(counter->oncpu,
1018                                          __read, counter, 1);
1019         }
1020
1021         return atomic64_read(&counter->count);
1022 }
1023
1024 /*
1025  * Cross CPU call to switch performance data pointers
1026  */
1027 static void __perf_switch_irq_data(void *info)
1028 {
1029         struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1030         struct perf_counter *counter = info;
1031         struct perf_counter_context *ctx = counter->ctx;
1032         struct perf_data *oldirqdata = counter->irqdata;
1033
1034         /*
1035          * If this is a task context, we need to check whether it is
1036          * the current task context of this cpu. If not it has been
1037          * scheduled out before the smp call arrived.
1038          */
1039         if (ctx->task) {
1040                 if (cpuctx->task_ctx != ctx)
1041                         return;
1042                 spin_lock(&ctx->lock);
1043         }
1044
1045         /* Change the pointer NMI safe */
1046         atomic_long_set((atomic_long_t *)&counter->irqdata,
1047                         (unsigned long) counter->usrdata);
1048         counter->usrdata = oldirqdata;
1049
1050         if (ctx->task)
1051                 spin_unlock(&ctx->lock);
1052 }
1053
1054 static struct perf_data *perf_switch_irq_data(struct perf_counter *counter)
1055 {
1056         struct perf_counter_context *ctx = counter->ctx;
1057         struct perf_data *oldirqdata = counter->irqdata;
1058         struct task_struct *task = ctx->task;
1059
1060         if (!task) {
1061                 smp_call_function_single(counter->cpu,
1062                                          __perf_switch_irq_data,
1063                                          counter, 1);
1064                 return counter->usrdata;
1065         }
1066
1067 retry:
1068         spin_lock_irq(&ctx->lock);
1069         if (counter->state != PERF_COUNTER_STATE_ACTIVE) {
1070                 counter->irqdata = counter->usrdata;
1071                 counter->usrdata = oldirqdata;
1072                 spin_unlock_irq(&ctx->lock);
1073                 return oldirqdata;
1074         }
1075         spin_unlock_irq(&ctx->lock);
1076         task_oncpu_function_call(task, __perf_switch_irq_data, counter);
1077         /* Might have failed, because task was scheduled out */
1078         if (counter->irqdata == oldirqdata)
1079                 goto retry;
1080
1081         return counter->usrdata;
1082 }
1083
1084 static void put_context(struct perf_counter_context *ctx)
1085 {
1086         if (ctx->task)
1087                 put_task_struct(ctx->task);
1088 }
1089
1090 static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
1091 {
1092         struct perf_cpu_context *cpuctx;
1093         struct perf_counter_context *ctx;
1094         struct task_struct *task;
1095
1096         /*
1097          * If cpu is not a wildcard then this is a percpu counter:
1098          */
1099         if (cpu != -1) {
1100                 /* Must be root to operate on a CPU counter: */
1101                 if (!capable(CAP_SYS_ADMIN))
1102                         return ERR_PTR(-EACCES);
1103
1104                 if (cpu < 0 || cpu > num_possible_cpus())
1105                         return ERR_PTR(-EINVAL);
1106
1107                 /*
1108                  * We could be clever and allow to attach a counter to an
1109                  * offline CPU and activate it when the CPU comes up, but
1110                  * that's for later.
1111                  */
1112                 if (!cpu_isset(cpu, cpu_online_map))
1113                         return ERR_PTR(-ENODEV);
1114
1115                 cpuctx = &per_cpu(perf_cpu_context, cpu);
1116                 ctx = &cpuctx->ctx;
1117
1118                 return ctx;
1119         }
1120
1121         rcu_read_lock();
1122         if (!pid)
1123                 task = current;
1124         else
1125                 task = find_task_by_vpid(pid);
1126         if (task)
1127                 get_task_struct(task);
1128         rcu_read_unlock();
1129
1130         if (!task)
1131                 return ERR_PTR(-ESRCH);
1132
1133         ctx = &task->perf_counter_ctx;
1134         ctx->task = task;
1135
1136         /* Reuse ptrace permission checks for now. */
1137         if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
1138                 put_context(ctx);
1139                 return ERR_PTR(-EACCES);
1140         }
1141
1142         return ctx;
1143 }
1144
1145 static void free_counter_rcu(struct rcu_head *head)
1146 {
1147         struct perf_counter *counter;
1148
1149         counter = container_of(head, struct perf_counter, rcu_head);
1150         kfree(counter);
1151 }
1152
1153 static void free_counter(struct perf_counter *counter)
1154 {
1155         if (counter->destroy)
1156                 counter->destroy(counter);
1157
1158         call_rcu(&counter->rcu_head, free_counter_rcu);
1159 }
1160
1161 /*
1162  * Called when the last reference to the file is gone.
1163  */
1164 static int perf_release(struct inode *inode, struct file *file)
1165 {
1166         struct perf_counter *counter = file->private_data;
1167         struct perf_counter_context *ctx = counter->ctx;
1168
1169         file->private_data = NULL;
1170
1171         mutex_lock(&ctx->mutex);
1172         mutex_lock(&counter->mutex);
1173
1174         perf_counter_remove_from_context(counter);
1175
1176         mutex_unlock(&counter->mutex);
1177         mutex_unlock(&ctx->mutex);
1178
1179         free_counter(counter);
1180         put_context(ctx);
1181
1182         return 0;
1183 }
1184
1185 /*
1186  * Read the performance counter - simple non blocking version for now
1187  */
1188 static ssize_t
1189 perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
1190 {
1191         u64 cntval;
1192
1193         if (count != sizeof(cntval))
1194                 return -EINVAL;
1195
1196         /*
1197          * Return end-of-file for a read on a counter that is in
1198          * error state (i.e. because it was pinned but it couldn't be
1199          * scheduled on to the CPU at some point).
1200          */
1201         if (counter->state == PERF_COUNTER_STATE_ERROR)
1202                 return 0;
1203
1204         mutex_lock(&counter->mutex);
1205         cntval = perf_counter_read(counter);
1206         mutex_unlock(&counter->mutex);
1207
1208         return put_user(cntval, (u64 __user *) buf) ? -EFAULT : sizeof(cntval);
1209 }
1210
1211 static ssize_t
1212 perf_copy_usrdata(struct perf_data *usrdata, char __user *buf, size_t count)
1213 {
1214         if (!usrdata->len)
1215                 return 0;
1216
1217         count = min(count, (size_t)usrdata->len);
1218         if (copy_to_user(buf, usrdata->data + usrdata->rd_idx, count))
1219                 return -EFAULT;
1220
1221         /* Adjust the counters */
1222         usrdata->len -= count;
1223         if (!usrdata->len)
1224                 usrdata->rd_idx = 0;
1225         else
1226                 usrdata->rd_idx += count;
1227
1228         return count;
1229 }
1230
1231 static ssize_t
1232 perf_read_irq_data(struct perf_counter  *counter,
1233                    char __user          *buf,
1234                    size_t               count,
1235                    int                  nonblocking)
1236 {
1237         struct perf_data *irqdata, *usrdata;
1238         DECLARE_WAITQUEUE(wait, current);
1239         ssize_t res, res2;
1240
1241         irqdata = counter->irqdata;
1242         usrdata = counter->usrdata;
1243
1244         if (usrdata->len + irqdata->len >= count)
1245                 goto read_pending;
1246
1247         if (nonblocking)
1248                 return -EAGAIN;
1249
1250         spin_lock_irq(&counter->waitq.lock);
1251         __add_wait_queue(&counter->waitq, &wait);
1252         for (;;) {
1253                 set_current_state(TASK_INTERRUPTIBLE);
1254                 if (usrdata->len + irqdata->len >= count)
1255                         break;
1256
1257                 if (signal_pending(current))
1258                         break;
1259
1260                 if (counter->state == PERF_COUNTER_STATE_ERROR)
1261                         break;
1262
1263                 spin_unlock_irq(&counter->waitq.lock);
1264                 schedule();
1265                 spin_lock_irq(&counter->waitq.lock);
1266         }
1267         __remove_wait_queue(&counter->waitq, &wait);
1268         __set_current_state(TASK_RUNNING);
1269         spin_unlock_irq(&counter->waitq.lock);
1270
1271         if (usrdata->len + irqdata->len < count &&
1272             counter->state != PERF_COUNTER_STATE_ERROR)
1273                 return -ERESTARTSYS;
1274 read_pending:
1275         mutex_lock(&counter->mutex);
1276
1277         /* Drain pending data first: */
1278         res = perf_copy_usrdata(usrdata, buf, count);
1279         if (res < 0 || res == count)
1280                 goto out;
1281
1282         /* Switch irq buffer: */
1283         usrdata = perf_switch_irq_data(counter);
1284         res2 = perf_copy_usrdata(usrdata, buf + res, count - res);
1285         if (res2 < 0) {
1286                 if (!res)
1287                         res = -EFAULT;
1288         } else {
1289                 res += res2;
1290         }
1291 out:
1292         mutex_unlock(&counter->mutex);
1293
1294         return res;
1295 }
1296
1297 static ssize_t
1298 perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
1299 {
1300         struct perf_counter *counter = file->private_data;
1301
1302         switch (counter->hw_event.record_type) {
1303         case PERF_RECORD_SIMPLE:
1304                 return perf_read_hw(counter, buf, count);
1305
1306         case PERF_RECORD_IRQ:
1307         case PERF_RECORD_GROUP:
1308                 return perf_read_irq_data(counter, buf, count,
1309                                           file->f_flags & O_NONBLOCK);
1310         }
1311         return -EINVAL;
1312 }
1313
1314 static unsigned int perf_poll(struct file *file, poll_table *wait)
1315 {
1316         struct perf_counter *counter = file->private_data;
1317         unsigned int events = 0;
1318         unsigned long flags;
1319
1320         poll_wait(file, &counter->waitq, wait);
1321
1322         spin_lock_irqsave(&counter->waitq.lock, flags);
1323         if (counter->usrdata->len || counter->irqdata->len)
1324                 events |= POLLIN;
1325         spin_unlock_irqrestore(&counter->waitq.lock, flags);
1326
1327         return events;
1328 }
1329
1330 static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1331 {
1332         struct perf_counter *counter = file->private_data;
1333         int err = 0;
1334
1335         switch (cmd) {
1336         case PERF_COUNTER_IOC_ENABLE:
1337                 perf_counter_enable_family(counter);
1338                 break;
1339         case PERF_COUNTER_IOC_DISABLE:
1340                 perf_counter_disable_family(counter);
1341                 break;
1342         default:
1343                 err = -ENOTTY;
1344         }
1345         return err;
1346 }
1347
1348 static const struct file_operations perf_fops = {
1349         .release                = perf_release,
1350         .read                   = perf_read,
1351         .poll                   = perf_poll,
1352         .unlocked_ioctl         = perf_ioctl,
1353         .compat_ioctl           = perf_ioctl,
1354 };
1355
1356 /*
1357  * Generic software counter infrastructure
1358  */
1359
1360 static void perf_swcounter_update(struct perf_counter *counter)
1361 {
1362         struct hw_perf_counter *hwc = &counter->hw;
1363         u64 prev, now;
1364         s64 delta;
1365
1366 again:
1367         prev = atomic64_read(&hwc->prev_count);
1368         now = atomic64_read(&hwc->count);
1369         if (atomic64_cmpxchg(&hwc->prev_count, prev, now) != prev)
1370                 goto again;
1371
1372         delta = now - prev;
1373
1374         atomic64_add(delta, &counter->count);
1375         atomic64_sub(delta, &hwc->period_left);
1376 }
1377
1378 static void perf_swcounter_set_period(struct perf_counter *counter)
1379 {
1380         struct hw_perf_counter *hwc = &counter->hw;
1381         s64 left = atomic64_read(&hwc->period_left);
1382         s64 period = hwc->irq_period;
1383
1384         if (unlikely(left <= -period)) {
1385                 left = period;
1386                 atomic64_set(&hwc->period_left, left);
1387         }
1388
1389         if (unlikely(left <= 0)) {
1390                 left += period;
1391                 atomic64_add(period, &hwc->period_left);
1392         }
1393
1394         atomic64_set(&hwc->prev_count, -left);
1395         atomic64_set(&hwc->count, -left);
1396 }
1397
1398 static void perf_swcounter_store_irq(struct perf_counter *counter, u64 data)
1399 {
1400         struct perf_data *irqdata = counter->irqdata;
1401
1402         if (irqdata->len > PERF_DATA_BUFLEN - sizeof(u64)) {
1403                 irqdata->overrun++;
1404         } else {
1405                 u64 *p = (u64 *) &irqdata->data[irqdata->len];
1406
1407                 *p = data;
1408                 irqdata->len += sizeof(u64);
1409         }
1410 }
1411
1412 static void perf_swcounter_handle_group(struct perf_counter *sibling)
1413 {
1414         struct perf_counter *counter, *group_leader = sibling->group_leader;
1415
1416         list_for_each_entry(counter, &group_leader->sibling_list, list_entry) {
1417                 counter->hw_ops->read(counter);
1418                 perf_swcounter_store_irq(sibling, counter->hw_event.event_config);
1419                 perf_swcounter_store_irq(sibling, atomic64_read(&counter->count));
1420         }
1421 }
1422
1423 static void perf_swcounter_interrupt(struct perf_counter *counter,
1424                                      int nmi, struct pt_regs *regs)
1425 {
1426         switch (counter->hw_event.record_type) {
1427         case PERF_RECORD_SIMPLE:
1428                 break;
1429
1430         case PERF_RECORD_IRQ:
1431                 perf_swcounter_store_irq(counter, instruction_pointer(regs));
1432                 break;
1433
1434         case PERF_RECORD_GROUP:
1435                 perf_swcounter_handle_group(counter);
1436                 break;
1437         }
1438
1439         if (nmi) {
1440                 counter->wakeup_pending = 1;
1441                 set_perf_counter_pending();
1442         } else
1443                 wake_up(&counter->waitq);
1444 }
1445
1446 static enum hrtimer_restart perf_swcounter_hrtimer(struct hrtimer *hrtimer)
1447 {
1448         struct perf_counter *counter;
1449         struct pt_regs *regs;
1450
1451         counter = container_of(hrtimer, struct perf_counter, hw.hrtimer);
1452         counter->hw_ops->read(counter);
1453
1454         regs = get_irq_regs();
1455         /*
1456          * In case we exclude kernel IPs or are somehow not in interrupt
1457          * context, provide the next best thing, the user IP.
1458          */
1459         if ((counter->hw_event.exclude_kernel || !regs) &&
1460                         !counter->hw_event.exclude_user)
1461                 regs = task_pt_regs(current);
1462
1463         if (regs)
1464                 perf_swcounter_interrupt(counter, 0, regs);
1465
1466         hrtimer_forward_now(hrtimer, ns_to_ktime(counter->hw.irq_period));
1467
1468         return HRTIMER_RESTART;
1469 }
1470
1471 static void perf_swcounter_overflow(struct perf_counter *counter,
1472                                     int nmi, struct pt_regs *regs)
1473 {
1474         perf_swcounter_update(counter);
1475         perf_swcounter_set_period(counter);
1476         perf_swcounter_interrupt(counter, nmi, regs);
1477 }
1478
1479 static int perf_swcounter_match(struct perf_counter *counter,
1480                                 enum perf_event_types type,
1481                                 u32 event, struct pt_regs *regs)
1482 {
1483         if (counter->state != PERF_COUNTER_STATE_ACTIVE)
1484                 return 0;
1485
1486         if (counter->hw_event.raw_type)
1487                 return 0;
1488
1489         if (counter->hw_event.type != type)
1490                 return 0;
1491
1492         if (counter->hw_event.event_id != event)
1493                 return 0;
1494
1495         if (counter->hw_event.exclude_user && user_mode(regs))
1496                 return 0;
1497
1498         if (counter->hw_event.exclude_kernel && !user_mode(regs))
1499                 return 0;
1500
1501         return 1;
1502 }
1503
1504 static void perf_swcounter_add(struct perf_counter *counter, u64 nr,
1505                                int nmi, struct pt_regs *regs)
1506 {
1507         int neg = atomic64_add_negative(nr, &counter->hw.count);
1508         if (counter->hw.irq_period && !neg)
1509                 perf_swcounter_overflow(counter, nmi, regs);
1510 }
1511
1512 static void perf_swcounter_ctx_event(struct perf_counter_context *ctx,
1513                                      enum perf_event_types type, u32 event,
1514                                      u64 nr, int nmi, struct pt_regs *regs)
1515 {
1516         struct perf_counter *counter;
1517
1518         if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
1519                 return;
1520
1521         rcu_read_lock();
1522         list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
1523                 if (perf_swcounter_match(counter, type, event, regs))
1524                         perf_swcounter_add(counter, nr, nmi, regs);
1525         }
1526         rcu_read_unlock();
1527 }
1528
1529 static void __perf_swcounter_event(enum perf_event_types type, u32 event,
1530                                    u64 nr, int nmi, struct pt_regs *regs)
1531 {
1532         struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
1533
1534         perf_swcounter_ctx_event(&cpuctx->ctx, type, event, nr, nmi, regs);
1535         if (cpuctx->task_ctx) {
1536                 perf_swcounter_ctx_event(cpuctx->task_ctx, type, event,
1537                                 nr, nmi, regs);
1538         }
1539
1540         put_cpu_var(perf_cpu_context);
1541 }
1542
1543 void perf_swcounter_event(u32 event, u64 nr, int nmi, struct pt_regs *regs)
1544 {
1545         __perf_swcounter_event(PERF_TYPE_SOFTWARE, event, nr, nmi, regs);
1546 }
1547
1548 static void perf_swcounter_read(struct perf_counter *counter)
1549 {
1550         perf_swcounter_update(counter);
1551 }
1552
1553 static int perf_swcounter_enable(struct perf_counter *counter)
1554 {
1555         perf_swcounter_set_period(counter);
1556         return 0;
1557 }
1558
1559 static void perf_swcounter_disable(struct perf_counter *counter)
1560 {
1561         perf_swcounter_update(counter);
1562 }
1563
1564 static const struct hw_perf_counter_ops perf_ops_generic = {
1565         .enable         = perf_swcounter_enable,
1566         .disable        = perf_swcounter_disable,
1567         .read           = perf_swcounter_read,
1568 };
1569
1570 /*
1571  * Software counter: cpu wall time clock
1572  */
1573
1574 static void cpu_clock_perf_counter_update(struct perf_counter *counter)
1575 {
1576         int cpu = raw_smp_processor_id();
1577         s64 prev;
1578         u64 now;
1579
1580         now = cpu_clock(cpu);
1581         prev = atomic64_read(&counter->hw.prev_count);
1582         atomic64_set(&counter->hw.prev_count, now);
1583         atomic64_add(now - prev, &counter->count);
1584 }
1585
1586 static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
1587 {
1588         struct hw_perf_counter *hwc = &counter->hw;
1589         int cpu = raw_smp_processor_id();
1590
1591         atomic64_set(&hwc->prev_count, cpu_clock(cpu));
1592         hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1593         hwc->hrtimer.function = perf_swcounter_hrtimer;
1594         if (hwc->irq_period) {
1595                 __hrtimer_start_range_ns(&hwc->hrtimer,
1596                                 ns_to_ktime(hwc->irq_period), 0,
1597                                 HRTIMER_MODE_REL, 0);
1598         }
1599
1600         return 0;
1601 }
1602
1603 static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
1604 {
1605         hrtimer_cancel(&counter->hw.hrtimer);
1606         cpu_clock_perf_counter_update(counter);
1607 }
1608
1609 static void cpu_clock_perf_counter_read(struct perf_counter *counter)
1610 {
1611         cpu_clock_perf_counter_update(counter);
1612 }
1613
1614 static const struct hw_perf_counter_ops perf_ops_cpu_clock = {
1615         .enable         = cpu_clock_perf_counter_enable,
1616         .disable        = cpu_clock_perf_counter_disable,
1617         .read           = cpu_clock_perf_counter_read,
1618 };
1619
1620 /*
1621  * Software counter: task time clock
1622  */
1623
1624 /*
1625  * Called from within the scheduler:
1626  */
1627 static u64 task_clock_perf_counter_val(struct perf_counter *counter, int update)
1628 {
1629         struct task_struct *curr = counter->task;
1630         u64 delta;
1631
1632         delta = __task_delta_exec(curr, update);
1633
1634         return curr->se.sum_exec_runtime + delta;
1635 }
1636
1637 static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
1638 {
1639         u64 prev;
1640         s64 delta;
1641
1642         prev = atomic64_read(&counter->hw.prev_count);
1643
1644         atomic64_set(&counter->hw.prev_count, now);
1645
1646         delta = now - prev;
1647
1648         atomic64_add(delta, &counter->count);
1649 }
1650
1651 static int task_clock_perf_counter_enable(struct perf_counter *counter)
1652 {
1653         struct hw_perf_counter *hwc = &counter->hw;
1654
1655         atomic64_set(&hwc->prev_count, task_clock_perf_counter_val(counter, 0));
1656         hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1657         hwc->hrtimer.function = perf_swcounter_hrtimer;
1658         if (hwc->irq_period) {
1659                 __hrtimer_start_range_ns(&hwc->hrtimer,
1660                                 ns_to_ktime(hwc->irq_period), 0,
1661                                 HRTIMER_MODE_REL, 0);
1662         }
1663
1664         return 0;
1665 }
1666
1667 static void task_clock_perf_counter_disable(struct perf_counter *counter)
1668 {
1669         hrtimer_cancel(&counter->hw.hrtimer);
1670         task_clock_perf_counter_update(counter,
1671                         task_clock_perf_counter_val(counter, 0));
1672 }
1673
1674 static void task_clock_perf_counter_read(struct perf_counter *counter)
1675 {
1676         task_clock_perf_counter_update(counter,
1677                         task_clock_perf_counter_val(counter, 1));
1678 }
1679
1680 static const struct hw_perf_counter_ops perf_ops_task_clock = {
1681         .enable         = task_clock_perf_counter_enable,
1682         .disable        = task_clock_perf_counter_disable,
1683         .read           = task_clock_perf_counter_read,
1684 };
1685
1686 /*
1687  * Software counter: cpu migrations
1688  */
1689
1690 static inline u64 get_cpu_migrations(struct perf_counter *counter)
1691 {
1692         struct task_struct *curr = counter->ctx->task;
1693
1694         if (curr)
1695                 return curr->se.nr_migrations;
1696         return cpu_nr_migrations(smp_processor_id());
1697 }
1698
1699 static void cpu_migrations_perf_counter_update(struct perf_counter *counter)
1700 {
1701         u64 prev, now;
1702         s64 delta;
1703
1704         prev = atomic64_read(&counter->hw.prev_count);
1705         now = get_cpu_migrations(counter);
1706
1707         atomic64_set(&counter->hw.prev_count, now);
1708
1709         delta = now - prev;
1710
1711         atomic64_add(delta, &counter->count);
1712 }
1713
1714 static void cpu_migrations_perf_counter_read(struct perf_counter *counter)
1715 {
1716         cpu_migrations_perf_counter_update(counter);
1717 }
1718
1719 static int cpu_migrations_perf_counter_enable(struct perf_counter *counter)
1720 {
1721         if (counter->prev_state <= PERF_COUNTER_STATE_OFF)
1722                 atomic64_set(&counter->hw.prev_count,
1723                              get_cpu_migrations(counter));
1724         return 0;
1725 }
1726
1727 static void cpu_migrations_perf_counter_disable(struct perf_counter *counter)
1728 {
1729         cpu_migrations_perf_counter_update(counter);
1730 }
1731
1732 static const struct hw_perf_counter_ops perf_ops_cpu_migrations = {
1733         .enable         = cpu_migrations_perf_counter_enable,
1734         .disable        = cpu_migrations_perf_counter_disable,
1735         .read           = cpu_migrations_perf_counter_read,
1736 };
1737
1738 #ifdef CONFIG_EVENT_PROFILE
1739 void perf_tpcounter_event(int event_id)
1740 {
1741         struct pt_regs *regs = get_irq_regs();
1742
1743         if (!regs)
1744                 regs = task_pt_regs(current);
1745
1746         __perf_swcounter_event(PERF_TYPE_TRACEPOINT, event_id, 1, 1, regs);
1747 }
1748
1749 extern int ftrace_profile_enable(int);
1750 extern void ftrace_profile_disable(int);
1751
1752 static void tp_perf_counter_destroy(struct perf_counter *counter)
1753 {
1754         ftrace_profile_disable(counter->hw_event.event_id);
1755 }
1756
1757 static const struct hw_perf_counter_ops *
1758 tp_perf_counter_init(struct perf_counter *counter)
1759 {
1760         int event_id = counter->hw_event.event_id;
1761         int ret;
1762
1763         ret = ftrace_profile_enable(event_id);
1764         if (ret)
1765                 return NULL;
1766
1767         counter->destroy = tp_perf_counter_destroy;
1768         counter->hw.irq_period = counter->hw_event.irq_period;
1769
1770         return &perf_ops_generic;
1771 }
1772 #else
1773 static const struct hw_perf_counter_ops *
1774 tp_perf_counter_init(struct perf_counter *counter)
1775 {
1776         return NULL;
1777 }
1778 #endif
1779
1780 static const struct hw_perf_counter_ops *
1781 sw_perf_counter_init(struct perf_counter *counter)
1782 {
1783         struct perf_counter_hw_event *hw_event = &counter->hw_event;
1784         const struct hw_perf_counter_ops *hw_ops = NULL;
1785         struct hw_perf_counter *hwc = &counter->hw;
1786
1787         /*
1788          * Software counters (currently) can't in general distinguish
1789          * between user, kernel and hypervisor events.
1790          * However, context switches and cpu migrations are considered
1791          * to be kernel events, and page faults are never hypervisor
1792          * events.
1793          */
1794         switch (counter->hw_event.event_id) {
1795         case PERF_COUNT_CPU_CLOCK:
1796                 hw_ops = &perf_ops_cpu_clock;
1797
1798                 if (hw_event->irq_period && hw_event->irq_period < 10000)
1799                         hw_event->irq_period = 10000;
1800                 break;
1801         case PERF_COUNT_TASK_CLOCK:
1802                 /*
1803                  * If the user instantiates this as a per-cpu counter,
1804                  * use the cpu_clock counter instead.
1805                  */
1806                 if (counter->ctx->task)
1807                         hw_ops = &perf_ops_task_clock;
1808                 else
1809                         hw_ops = &perf_ops_cpu_clock;
1810
1811                 if (hw_event->irq_period && hw_event->irq_period < 10000)
1812                         hw_event->irq_period = 10000;
1813                 break;
1814         case PERF_COUNT_PAGE_FAULTS:
1815         case PERF_COUNT_PAGE_FAULTS_MIN:
1816         case PERF_COUNT_PAGE_FAULTS_MAJ:
1817         case PERF_COUNT_CONTEXT_SWITCHES:
1818                 hw_ops = &perf_ops_generic;
1819                 break;
1820         case PERF_COUNT_CPU_MIGRATIONS:
1821                 if (!counter->hw_event.exclude_kernel)
1822                         hw_ops = &perf_ops_cpu_migrations;
1823                 break;
1824         }
1825
1826         if (hw_ops)
1827                 hwc->irq_period = hw_event->irq_period;
1828
1829         return hw_ops;
1830 }
1831
1832 /*
1833  * Allocate and initialize a counter structure
1834  */
1835 static struct perf_counter *
1836 perf_counter_alloc(struct perf_counter_hw_event *hw_event,
1837                    int cpu,
1838                    struct perf_counter_context *ctx,
1839                    struct perf_counter *group_leader,
1840                    gfp_t gfpflags)
1841 {
1842         const struct hw_perf_counter_ops *hw_ops;
1843         struct perf_counter *counter;
1844
1845         counter = kzalloc(sizeof(*counter), gfpflags);
1846         if (!counter)
1847                 return NULL;
1848
1849         /*
1850          * Single counters are their own group leaders, with an
1851          * empty sibling list:
1852          */
1853         if (!group_leader)
1854                 group_leader = counter;
1855
1856         mutex_init(&counter->mutex);
1857         INIT_LIST_HEAD(&counter->list_entry);
1858         INIT_LIST_HEAD(&counter->event_entry);
1859         INIT_LIST_HEAD(&counter->sibling_list);
1860         init_waitqueue_head(&counter->waitq);
1861
1862         INIT_LIST_HEAD(&counter->child_list);
1863
1864         counter->irqdata                = &counter->data[0];
1865         counter->usrdata                = &counter->data[1];
1866         counter->cpu                    = cpu;
1867         counter->hw_event               = *hw_event;
1868         counter->wakeup_pending         = 0;
1869         counter->group_leader           = group_leader;
1870         counter->hw_ops                 = NULL;
1871         counter->ctx                    = ctx;
1872
1873         counter->state = PERF_COUNTER_STATE_INACTIVE;
1874         if (hw_event->disabled)
1875                 counter->state = PERF_COUNTER_STATE_OFF;
1876
1877         hw_ops = NULL;
1878
1879         if (hw_event->raw_type)
1880                 hw_ops = hw_perf_counter_init(counter);
1881         else switch (hw_event->type) {
1882         case PERF_TYPE_HARDWARE:
1883                 hw_ops = hw_perf_counter_init(counter);
1884                 break;
1885
1886         case PERF_TYPE_SOFTWARE:
1887                 hw_ops = sw_perf_counter_init(counter);
1888                 break;
1889
1890         case PERF_TYPE_TRACEPOINT:
1891                 hw_ops = tp_perf_counter_init(counter);
1892                 break;
1893         }
1894
1895         if (!hw_ops) {
1896                 kfree(counter);
1897                 return NULL;
1898         }
1899         counter->hw_ops = hw_ops;
1900
1901         return counter;
1902 }
1903
1904 /**
1905  * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
1906  *
1907  * @hw_event_uptr:      event type attributes for monitoring/sampling
1908  * @pid:                target pid
1909  * @cpu:                target cpu
1910  * @group_fd:           group leader counter fd
1911  */
1912 SYSCALL_DEFINE5(perf_counter_open,
1913                 const struct perf_counter_hw_event __user *, hw_event_uptr,
1914                 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
1915 {
1916         struct perf_counter *counter, *group_leader;
1917         struct perf_counter_hw_event hw_event;
1918         struct perf_counter_context *ctx;
1919         struct file *counter_file = NULL;
1920         struct file *group_file = NULL;
1921         int fput_needed = 0;
1922         int fput_needed2 = 0;
1923         int ret;
1924
1925         /* for future expandability... */
1926         if (flags)
1927                 return -EINVAL;
1928
1929         if (copy_from_user(&hw_event, hw_event_uptr, sizeof(hw_event)) != 0)
1930                 return -EFAULT;
1931
1932         /*
1933          * Get the target context (task or percpu):
1934          */
1935         ctx = find_get_context(pid, cpu);
1936         if (IS_ERR(ctx))
1937                 return PTR_ERR(ctx);
1938
1939         /*
1940          * Look up the group leader (we will attach this counter to it):
1941          */
1942         group_leader = NULL;
1943         if (group_fd != -1) {
1944                 ret = -EINVAL;
1945                 group_file = fget_light(group_fd, &fput_needed);
1946                 if (!group_file)
1947                         goto err_put_context;
1948                 if (group_file->f_op != &perf_fops)
1949                         goto err_put_context;
1950
1951                 group_leader = group_file->private_data;
1952                 /*
1953                  * Do not allow a recursive hierarchy (this new sibling
1954                  * becoming part of another group-sibling):
1955                  */
1956                 if (group_leader->group_leader != group_leader)
1957                         goto err_put_context;
1958                 /*
1959                  * Do not allow to attach to a group in a different
1960                  * task or CPU context:
1961                  */
1962                 if (group_leader->ctx != ctx)
1963                         goto err_put_context;
1964                 /*
1965                  * Only a group leader can be exclusive or pinned
1966                  */
1967                 if (hw_event.exclusive || hw_event.pinned)
1968                         goto err_put_context;
1969         }
1970
1971         ret = -EINVAL;
1972         counter = perf_counter_alloc(&hw_event, cpu, ctx, group_leader,
1973                                      GFP_KERNEL);
1974         if (!counter)
1975                 goto err_put_context;
1976
1977         ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
1978         if (ret < 0)
1979                 goto err_free_put_context;
1980
1981         counter_file = fget_light(ret, &fput_needed2);
1982         if (!counter_file)
1983                 goto err_free_put_context;
1984
1985         counter->filp = counter_file;
1986         mutex_lock(&ctx->mutex);
1987         perf_install_in_context(ctx, counter, cpu);
1988         mutex_unlock(&ctx->mutex);
1989
1990         fput_light(counter_file, fput_needed2);
1991
1992 out_fput:
1993         fput_light(group_file, fput_needed);
1994
1995         return ret;
1996
1997 err_free_put_context:
1998         kfree(counter);
1999
2000 err_put_context:
2001         put_context(ctx);
2002
2003         goto out_fput;
2004 }
2005
2006 /*
2007  * Initialize the perf_counter context in a task_struct:
2008  */
2009 static void
2010 __perf_counter_init_context(struct perf_counter_context *ctx,
2011                             struct task_struct *task)
2012 {
2013         memset(ctx, 0, sizeof(*ctx));
2014         spin_lock_init(&ctx->lock);
2015         mutex_init(&ctx->mutex);
2016         INIT_LIST_HEAD(&ctx->counter_list);
2017         INIT_LIST_HEAD(&ctx->event_list);
2018         ctx->task = task;
2019 }
2020
2021 /*
2022  * inherit a counter from parent task to child task:
2023  */
2024 static struct perf_counter *
2025 inherit_counter(struct perf_counter *parent_counter,
2026               struct task_struct *parent,
2027               struct perf_counter_context *parent_ctx,
2028               struct task_struct *child,
2029               struct perf_counter *group_leader,
2030               struct perf_counter_context *child_ctx)
2031 {
2032         struct perf_counter *child_counter;
2033
2034         /*
2035          * Instead of creating recursive hierarchies of counters,
2036          * we link inherited counters back to the original parent,
2037          * which has a filp for sure, which we use as the reference
2038          * count:
2039          */
2040         if (parent_counter->parent)
2041                 parent_counter = parent_counter->parent;
2042
2043         child_counter = perf_counter_alloc(&parent_counter->hw_event,
2044                                            parent_counter->cpu, child_ctx,
2045                                            group_leader, GFP_KERNEL);
2046         if (!child_counter)
2047                 return NULL;
2048
2049         /*
2050          * Link it up in the child's context:
2051          */
2052         child_counter->task = child;
2053         list_add_counter(child_counter, child_ctx);
2054         child_ctx->nr_counters++;
2055
2056         child_counter->parent = parent_counter;
2057         /*
2058          * inherit into child's child as well:
2059          */
2060         child_counter->hw_event.inherit = 1;
2061
2062         /*
2063          * Get a reference to the parent filp - we will fput it
2064          * when the child counter exits. This is safe to do because
2065          * we are in the parent and we know that the filp still
2066          * exists and has a nonzero count:
2067          */
2068         atomic_long_inc(&parent_counter->filp->f_count);
2069
2070         /*
2071          * Link this into the parent counter's child list
2072          */
2073         mutex_lock(&parent_counter->mutex);
2074         list_add_tail(&child_counter->child_list, &parent_counter->child_list);
2075
2076         /*
2077          * Make the child state follow the state of the parent counter,
2078          * not its hw_event.disabled bit.  We hold the parent's mutex,
2079          * so we won't race with perf_counter_{en,dis}able_family.
2080          */
2081         if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE)
2082                 child_counter->state = PERF_COUNTER_STATE_INACTIVE;
2083         else
2084                 child_counter->state = PERF_COUNTER_STATE_OFF;
2085
2086         mutex_unlock(&parent_counter->mutex);
2087
2088         return child_counter;
2089 }
2090
2091 static int inherit_group(struct perf_counter *parent_counter,
2092               struct task_struct *parent,
2093               struct perf_counter_context *parent_ctx,
2094               struct task_struct *child,
2095               struct perf_counter_context *child_ctx)
2096 {
2097         struct perf_counter *leader;
2098         struct perf_counter *sub;
2099
2100         leader = inherit_counter(parent_counter, parent, parent_ctx,
2101                                  child, NULL, child_ctx);
2102         if (!leader)
2103                 return -ENOMEM;
2104         list_for_each_entry(sub, &parent_counter->sibling_list, list_entry) {
2105                 if (!inherit_counter(sub, parent, parent_ctx,
2106                                      child, leader, child_ctx))
2107                         return -ENOMEM;
2108         }
2109         return 0;
2110 }
2111
2112 static void sync_child_counter(struct perf_counter *child_counter,
2113                                struct perf_counter *parent_counter)
2114 {
2115         u64 parent_val, child_val;
2116
2117         parent_val = atomic64_read(&parent_counter->count);
2118         child_val = atomic64_read(&child_counter->count);
2119
2120         /*
2121          * Add back the child's count to the parent's count:
2122          */
2123         atomic64_add(child_val, &parent_counter->count);
2124
2125         /*
2126          * Remove this counter from the parent's list
2127          */
2128         mutex_lock(&parent_counter->mutex);
2129         list_del_init(&child_counter->child_list);
2130         mutex_unlock(&parent_counter->mutex);
2131
2132         /*
2133          * Release the parent counter, if this was the last
2134          * reference to it.
2135          */
2136         fput(parent_counter->filp);
2137 }
2138
2139 static void
2140 __perf_counter_exit_task(struct task_struct *child,
2141                          struct perf_counter *child_counter,
2142                          struct perf_counter_context *child_ctx)
2143 {
2144         struct perf_counter *parent_counter;
2145         struct perf_counter *sub, *tmp;
2146
2147         /*
2148          * If we do not self-reap then we have to wait for the
2149          * child task to unschedule (it will happen for sure),
2150          * so that its counter is at its final count. (This
2151          * condition triggers rarely - child tasks usually get
2152          * off their CPU before the parent has a chance to
2153          * get this far into the reaping action)
2154          */
2155         if (child != current) {
2156                 wait_task_inactive(child, 0);
2157                 list_del_init(&child_counter->list_entry);
2158         } else {
2159                 struct perf_cpu_context *cpuctx;
2160                 unsigned long flags;
2161                 u64 perf_flags;
2162
2163                 /*
2164                  * Disable and unlink this counter.
2165                  *
2166                  * Be careful about zapping the list - IRQ/NMI context
2167                  * could still be processing it:
2168                  */
2169                 curr_rq_lock_irq_save(&flags);
2170                 perf_flags = hw_perf_save_disable();
2171
2172                 cpuctx = &__get_cpu_var(perf_cpu_context);
2173
2174                 group_sched_out(child_counter, cpuctx, child_ctx);
2175
2176                 list_del_init(&child_counter->list_entry);
2177
2178                 child_ctx->nr_counters--;
2179
2180                 hw_perf_restore(perf_flags);
2181                 curr_rq_unlock_irq_restore(&flags);
2182         }
2183
2184         parent_counter = child_counter->parent;
2185         /*
2186          * It can happen that parent exits first, and has counters
2187          * that are still around due to the child reference. These
2188          * counters need to be zapped - but otherwise linger.
2189          */
2190         if (parent_counter) {
2191                 sync_child_counter(child_counter, parent_counter);
2192                 list_for_each_entry_safe(sub, tmp, &child_counter->sibling_list,
2193                                          list_entry) {
2194                         if (sub->parent) {
2195                                 sync_child_counter(sub, sub->parent);
2196                                 free_counter(sub);
2197                         }
2198                 }
2199                 free_counter(child_counter);
2200         }
2201 }
2202
2203 /*
2204  * When a child task exits, feed back counter values to parent counters.
2205  *
2206  * Note: we may be running in child context, but the PID is not hashed
2207  * anymore so new counters will not be added.
2208  */
2209 void perf_counter_exit_task(struct task_struct *child)
2210 {
2211         struct perf_counter *child_counter, *tmp;
2212         struct perf_counter_context *child_ctx;
2213
2214         child_ctx = &child->perf_counter_ctx;
2215
2216         if (likely(!child_ctx->nr_counters))
2217                 return;
2218
2219         list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
2220                                  list_entry)
2221                 __perf_counter_exit_task(child, child_counter, child_ctx);
2222 }
2223
2224 /*
2225  * Initialize the perf_counter context in task_struct
2226  */
2227 void perf_counter_init_task(struct task_struct *child)
2228 {
2229         struct perf_counter_context *child_ctx, *parent_ctx;
2230         struct perf_counter *counter;
2231         struct task_struct *parent = current;
2232
2233         child_ctx  =  &child->perf_counter_ctx;
2234         parent_ctx = &parent->perf_counter_ctx;
2235
2236         __perf_counter_init_context(child_ctx, child);
2237
2238         /*
2239          * This is executed from the parent task context, so inherit
2240          * counters that have been marked for cloning:
2241          */
2242
2243         if (likely(!parent_ctx->nr_counters))
2244                 return;
2245
2246         /*
2247          * Lock the parent list. No need to lock the child - not PID
2248          * hashed yet and not running, so nobody can access it.
2249          */
2250         mutex_lock(&parent_ctx->mutex);
2251
2252         /*
2253          * We dont have to disable NMIs - we are only looking at
2254          * the list, not manipulating it:
2255          */
2256         list_for_each_entry(counter, &parent_ctx->counter_list, list_entry) {
2257                 if (!counter->hw_event.inherit)
2258                         continue;
2259
2260                 if (inherit_group(counter, parent,
2261                                   parent_ctx, child, child_ctx))
2262                         break;
2263         }
2264
2265         mutex_unlock(&parent_ctx->mutex);
2266 }
2267
2268 static void __cpuinit perf_counter_init_cpu(int cpu)
2269 {
2270         struct perf_cpu_context *cpuctx;
2271
2272         cpuctx = &per_cpu(perf_cpu_context, cpu);
2273         __perf_counter_init_context(&cpuctx->ctx, NULL);
2274
2275         mutex_lock(&perf_resource_mutex);
2276         cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
2277         mutex_unlock(&perf_resource_mutex);
2278
2279         hw_perf_counter_setup(cpu);
2280 }
2281
2282 #ifdef CONFIG_HOTPLUG_CPU
2283 static void __perf_counter_exit_cpu(void *info)
2284 {
2285         struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
2286         struct perf_counter_context *ctx = &cpuctx->ctx;
2287         struct perf_counter *counter, *tmp;
2288
2289         list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
2290                 __perf_counter_remove_from_context(counter);
2291 }
2292 static void perf_counter_exit_cpu(int cpu)
2293 {
2294         struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
2295         struct perf_counter_context *ctx = &cpuctx->ctx;
2296
2297         mutex_lock(&ctx->mutex);
2298         smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
2299         mutex_unlock(&ctx->mutex);
2300 }
2301 #else
2302 static inline void perf_counter_exit_cpu(int cpu) { }
2303 #endif
2304
2305 static int __cpuinit
2306 perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
2307 {
2308         unsigned int cpu = (long)hcpu;
2309
2310         switch (action) {
2311
2312         case CPU_UP_PREPARE:
2313         case CPU_UP_PREPARE_FROZEN:
2314                 perf_counter_init_cpu(cpu);
2315                 break;
2316
2317         case CPU_DOWN_PREPARE:
2318         case CPU_DOWN_PREPARE_FROZEN:
2319                 perf_counter_exit_cpu(cpu);
2320                 break;
2321
2322         default:
2323                 break;
2324         }
2325
2326         return NOTIFY_OK;
2327 }
2328
2329 static struct notifier_block __cpuinitdata perf_cpu_nb = {
2330         .notifier_call          = perf_cpu_notify,
2331 };
2332
2333 static int __init perf_counter_init(void)
2334 {
2335         perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
2336                         (void *)(long)smp_processor_id());
2337         register_cpu_notifier(&perf_cpu_nb);
2338
2339         return 0;
2340 }
2341 early_initcall(perf_counter_init);
2342
2343 static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
2344 {
2345         return sprintf(buf, "%d\n", perf_reserved_percpu);
2346 }
2347
2348 static ssize_t
2349 perf_set_reserve_percpu(struct sysdev_class *class,
2350                         const char *buf,
2351                         size_t count)
2352 {
2353         struct perf_cpu_context *cpuctx;
2354         unsigned long val;
2355         int err, cpu, mpt;
2356
2357         err = strict_strtoul(buf, 10, &val);
2358         if (err)
2359                 return err;
2360         if (val > perf_max_counters)
2361                 return -EINVAL;
2362
2363         mutex_lock(&perf_resource_mutex);
2364         perf_reserved_percpu = val;
2365         for_each_online_cpu(cpu) {
2366                 cpuctx = &per_cpu(perf_cpu_context, cpu);
2367                 spin_lock_irq(&cpuctx->ctx.lock);
2368                 mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
2369                           perf_max_counters - perf_reserved_percpu);
2370                 cpuctx->max_pertask = mpt;
2371                 spin_unlock_irq(&cpuctx->ctx.lock);
2372         }
2373         mutex_unlock(&perf_resource_mutex);
2374
2375         return count;
2376 }
2377
2378 static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
2379 {
2380         return sprintf(buf, "%d\n", perf_overcommit);
2381 }
2382
2383 static ssize_t
2384 perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
2385 {
2386         unsigned long val;
2387         int err;
2388
2389         err = strict_strtoul(buf, 10, &val);
2390         if (err)
2391                 return err;
2392         if (val > 1)
2393                 return -EINVAL;
2394
2395         mutex_lock(&perf_resource_mutex);
2396         perf_overcommit = val;
2397         mutex_unlock(&perf_resource_mutex);
2398
2399         return count;
2400 }
2401
2402 static SYSDEV_CLASS_ATTR(
2403                                 reserve_percpu,
2404                                 0644,
2405                                 perf_show_reserve_percpu,
2406                                 perf_set_reserve_percpu
2407                         );
2408
2409 static SYSDEV_CLASS_ATTR(
2410                                 overcommit,
2411                                 0644,
2412                                 perf_show_overcommit,
2413                                 perf_set_overcommit
2414                         );
2415
2416 static struct attribute *perfclass_attrs[] = {
2417         &attr_reserve_percpu.attr,
2418         &attr_overcommit.attr,
2419         NULL
2420 };
2421
2422 static struct attribute_group perfclass_attr_group = {
2423         .attrs                  = perfclass_attrs,
2424         .name                   = "perf_counters",
2425 };
2426
2427 static int __init perf_counter_sysfs_init(void)
2428 {
2429         return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
2430                                   &perfclass_attr_group);
2431 }
2432 device_initcall(perf_counter_sysfs_init);