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