]> git.karo-electronics.de Git - mv-sheeva.git/blob - kernel/perf_counter.c
perf_counter: Do not throttle single swcounter events
[mv-sheeva.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/dcache.h>
20 #include <linux/percpu.h>
21 #include <linux/ptrace.h>
22 #include <linux/vmstat.h>
23 #include <linux/hardirq.h>
24 #include <linux/rculist.h>
25 #include <linux/uaccess.h>
26 #include <linux/syscalls.h>
27 #include <linux/anon_inodes.h>
28 #include <linux/kernel_stat.h>
29 #include <linux/perf_counter.h>
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_counters __read_mostly;
44 static atomic_t nr_comm_counters __read_mostly;
45 static atomic_t nr_task_counters __read_mostly;
46
47 /*
48  * perf counter paranoia level:
49  *  -1 - not paranoid at all
50  *   0 - disallow raw tracepoint access for unpriv
51  *   1 - disallow cpu counters for unpriv
52  *   2 - disallow kernel profiling for unpriv
53  */
54 int sysctl_perf_counter_paranoid __read_mostly = 1;
55
56 static inline bool perf_paranoid_tracepoint_raw(void)
57 {
58         return sysctl_perf_counter_paranoid > -1;
59 }
60
61 static inline bool perf_paranoid_cpu(void)
62 {
63         return sysctl_perf_counter_paranoid > 0;
64 }
65
66 static inline bool perf_paranoid_kernel(void)
67 {
68         return sysctl_perf_counter_paranoid > 1;
69 }
70
71 int sysctl_perf_counter_mlock __read_mostly = 512; /* 'free' kb per user */
72
73 /*
74  * max perf counter sample rate
75  */
76 int sysctl_perf_counter_sample_rate __read_mostly = 100000;
77
78 static atomic64_t perf_counter_id;
79
80 /*
81  * Lock for (sysadmin-configurable) counter reservations:
82  */
83 static DEFINE_SPINLOCK(perf_resource_lock);
84
85 /*
86  * Architecture provided APIs - weak aliases:
87  */
88 extern __weak const struct pmu *hw_perf_counter_init(struct perf_counter *counter)
89 {
90         return NULL;
91 }
92
93 void __weak hw_perf_disable(void)               { barrier(); }
94 void __weak hw_perf_enable(void)                { barrier(); }
95
96 void __weak hw_perf_counter_setup(int cpu)      { barrier(); }
97 void __weak hw_perf_counter_setup_online(int cpu)       { barrier(); }
98
99 int __weak
100 hw_perf_group_sched_in(struct perf_counter *group_leader,
101                struct perf_cpu_context *cpuctx,
102                struct perf_counter_context *ctx, int cpu)
103 {
104         return 0;
105 }
106
107 void __weak perf_counter_print_debug(void)      { }
108
109 static DEFINE_PER_CPU(int, disable_count);
110
111 void __perf_disable(void)
112 {
113         __get_cpu_var(disable_count)++;
114 }
115
116 bool __perf_enable(void)
117 {
118         return !--__get_cpu_var(disable_count);
119 }
120
121 void perf_disable(void)
122 {
123         __perf_disable();
124         hw_perf_disable();
125 }
126
127 void perf_enable(void)
128 {
129         if (__perf_enable())
130                 hw_perf_enable();
131 }
132
133 static void get_ctx(struct perf_counter_context *ctx)
134 {
135         WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
136 }
137
138 static void free_ctx(struct rcu_head *head)
139 {
140         struct perf_counter_context *ctx;
141
142         ctx = container_of(head, struct perf_counter_context, rcu_head);
143         kfree(ctx);
144 }
145
146 static void put_ctx(struct perf_counter_context *ctx)
147 {
148         if (atomic_dec_and_test(&ctx->refcount)) {
149                 if (ctx->parent_ctx)
150                         put_ctx(ctx->parent_ctx);
151                 if (ctx->task)
152                         put_task_struct(ctx->task);
153                 call_rcu(&ctx->rcu_head, free_ctx);
154         }
155 }
156
157 static void unclone_ctx(struct perf_counter_context *ctx)
158 {
159         if (ctx->parent_ctx) {
160                 put_ctx(ctx->parent_ctx);
161                 ctx->parent_ctx = NULL;
162         }
163 }
164
165 /*
166  * If we inherit counters we want to return the parent counter id
167  * to userspace.
168  */
169 static u64 primary_counter_id(struct perf_counter *counter)
170 {
171         u64 id = counter->id;
172
173         if (counter->parent)
174                 id = counter->parent->id;
175
176         return id;
177 }
178
179 /*
180  * Get the perf_counter_context for a task and lock it.
181  * This has to cope with with the fact that until it is locked,
182  * the context could get moved to another task.
183  */
184 static struct perf_counter_context *
185 perf_lock_task_context(struct task_struct *task, unsigned long *flags)
186 {
187         struct perf_counter_context *ctx;
188
189         rcu_read_lock();
190  retry:
191         ctx = rcu_dereference(task->perf_counter_ctxp);
192         if (ctx) {
193                 /*
194                  * If this context is a clone of another, it might
195                  * get swapped for another underneath us by
196                  * perf_counter_task_sched_out, though the
197                  * rcu_read_lock() protects us from any context
198                  * getting freed.  Lock the context and check if it
199                  * got swapped before we could get the lock, and retry
200                  * if so.  If we locked the right context, then it
201                  * can't get swapped on us any more.
202                  */
203                 spin_lock_irqsave(&ctx->lock, *flags);
204                 if (ctx != rcu_dereference(task->perf_counter_ctxp)) {
205                         spin_unlock_irqrestore(&ctx->lock, *flags);
206                         goto retry;
207                 }
208
209                 if (!atomic_inc_not_zero(&ctx->refcount)) {
210                         spin_unlock_irqrestore(&ctx->lock, *flags);
211                         ctx = NULL;
212                 }
213         }
214         rcu_read_unlock();
215         return ctx;
216 }
217
218 /*
219  * Get the context for a task and increment its pin_count so it
220  * can't get swapped to another task.  This also increments its
221  * reference count so that the context can't get freed.
222  */
223 static struct perf_counter_context *perf_pin_task_context(struct task_struct *task)
224 {
225         struct perf_counter_context *ctx;
226         unsigned long flags;
227
228         ctx = perf_lock_task_context(task, &flags);
229         if (ctx) {
230                 ++ctx->pin_count;
231                 spin_unlock_irqrestore(&ctx->lock, flags);
232         }
233         return ctx;
234 }
235
236 static void perf_unpin_context(struct perf_counter_context *ctx)
237 {
238         unsigned long flags;
239
240         spin_lock_irqsave(&ctx->lock, flags);
241         --ctx->pin_count;
242         spin_unlock_irqrestore(&ctx->lock, flags);
243         put_ctx(ctx);
244 }
245
246 /*
247  * Add a counter from the lists for its context.
248  * Must be called with ctx->mutex and ctx->lock held.
249  */
250 static void
251 list_add_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
252 {
253         struct perf_counter *group_leader = counter->group_leader;
254
255         /*
256          * Depending on whether it is a standalone or sibling counter,
257          * add it straight to the context's counter list, or to the group
258          * leader's sibling list:
259          */
260         if (group_leader == counter)
261                 list_add_tail(&counter->list_entry, &ctx->counter_list);
262         else {
263                 list_add_tail(&counter->list_entry, &group_leader->sibling_list);
264                 group_leader->nr_siblings++;
265         }
266
267         list_add_rcu(&counter->event_entry, &ctx->event_list);
268         ctx->nr_counters++;
269         if (counter->attr.inherit_stat)
270                 ctx->nr_stat++;
271 }
272
273 /*
274  * Remove a counter from the lists for its context.
275  * Must be called with ctx->mutex and ctx->lock held.
276  */
277 static void
278 list_del_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
279 {
280         struct perf_counter *sibling, *tmp;
281
282         if (list_empty(&counter->list_entry))
283                 return;
284         ctx->nr_counters--;
285         if (counter->attr.inherit_stat)
286                 ctx->nr_stat--;
287
288         list_del_init(&counter->list_entry);
289         list_del_rcu(&counter->event_entry);
290
291         if (counter->group_leader != counter)
292                 counter->group_leader->nr_siblings--;
293
294         /*
295          * If this was a group counter with sibling counters then
296          * upgrade the siblings to singleton counters by adding them
297          * to the context list directly:
298          */
299         list_for_each_entry_safe(sibling, tmp,
300                                  &counter->sibling_list, list_entry) {
301
302                 list_move_tail(&sibling->list_entry, &ctx->counter_list);
303                 sibling->group_leader = sibling;
304         }
305 }
306
307 static void
308 counter_sched_out(struct perf_counter *counter,
309                   struct perf_cpu_context *cpuctx,
310                   struct perf_counter_context *ctx)
311 {
312         if (counter->state != PERF_COUNTER_STATE_ACTIVE)
313                 return;
314
315         counter->state = PERF_COUNTER_STATE_INACTIVE;
316         if (counter->pending_disable) {
317                 counter->pending_disable = 0;
318                 counter->state = PERF_COUNTER_STATE_OFF;
319         }
320         counter->tstamp_stopped = ctx->time;
321         counter->pmu->disable(counter);
322         counter->oncpu = -1;
323
324         if (!is_software_counter(counter))
325                 cpuctx->active_oncpu--;
326         ctx->nr_active--;
327         if (counter->attr.exclusive || !cpuctx->active_oncpu)
328                 cpuctx->exclusive = 0;
329 }
330
331 static void
332 group_sched_out(struct perf_counter *group_counter,
333                 struct perf_cpu_context *cpuctx,
334                 struct perf_counter_context *ctx)
335 {
336         struct perf_counter *counter;
337
338         if (group_counter->state != PERF_COUNTER_STATE_ACTIVE)
339                 return;
340
341         counter_sched_out(group_counter, cpuctx, ctx);
342
343         /*
344          * Schedule out siblings (if any):
345          */
346         list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
347                 counter_sched_out(counter, cpuctx, ctx);
348
349         if (group_counter->attr.exclusive)
350                 cpuctx->exclusive = 0;
351 }
352
353 /*
354  * Cross CPU call to remove a performance counter
355  *
356  * We disable the counter on the hardware level first. After that we
357  * remove it from the context list.
358  */
359 static void __perf_counter_remove_from_context(void *info)
360 {
361         struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
362         struct perf_counter *counter = info;
363         struct perf_counter_context *ctx = counter->ctx;
364
365         /*
366          * If this is a task context, we need to check whether it is
367          * the current task context of this cpu. If not it has been
368          * scheduled out before the smp call arrived.
369          */
370         if (ctx->task && cpuctx->task_ctx != ctx)
371                 return;
372
373         spin_lock(&ctx->lock);
374         /*
375          * Protect the list operation against NMI by disabling the
376          * counters on a global level.
377          */
378         perf_disable();
379
380         counter_sched_out(counter, cpuctx, ctx);
381
382         list_del_counter(counter, ctx);
383
384         if (!ctx->task) {
385                 /*
386                  * Allow more per task counters with respect to the
387                  * reservation:
388                  */
389                 cpuctx->max_pertask =
390                         min(perf_max_counters - ctx->nr_counters,
391                             perf_max_counters - perf_reserved_percpu);
392         }
393
394         perf_enable();
395         spin_unlock(&ctx->lock);
396 }
397
398
399 /*
400  * Remove the counter from a task's (or a CPU's) list of counters.
401  *
402  * Must be called with ctx->mutex held.
403  *
404  * CPU counters are removed with a smp call. For task counters we only
405  * call when the task is on a CPU.
406  *
407  * If counter->ctx is a cloned context, callers must make sure that
408  * every task struct that counter->ctx->task could possibly point to
409  * remains valid.  This is OK when called from perf_release since
410  * that only calls us on the top-level context, which can't be a clone.
411  * When called from perf_counter_exit_task, it's OK because the
412  * context has been detached from its task.
413  */
414 static void perf_counter_remove_from_context(struct perf_counter *counter)
415 {
416         struct perf_counter_context *ctx = counter->ctx;
417         struct task_struct *task = ctx->task;
418
419         if (!task) {
420                 /*
421                  * Per cpu counters are removed via an smp call and
422                  * the removal is always sucessful.
423                  */
424                 smp_call_function_single(counter->cpu,
425                                          __perf_counter_remove_from_context,
426                                          counter, 1);
427                 return;
428         }
429
430 retry:
431         task_oncpu_function_call(task, __perf_counter_remove_from_context,
432                                  counter);
433
434         spin_lock_irq(&ctx->lock);
435         /*
436          * If the context is active we need to retry the smp call.
437          */
438         if (ctx->nr_active && !list_empty(&counter->list_entry)) {
439                 spin_unlock_irq(&ctx->lock);
440                 goto retry;
441         }
442
443         /*
444          * The lock prevents that this context is scheduled in so we
445          * can remove the counter safely, if the call above did not
446          * succeed.
447          */
448         if (!list_empty(&counter->list_entry)) {
449                 list_del_counter(counter, ctx);
450         }
451         spin_unlock_irq(&ctx->lock);
452 }
453
454 static inline u64 perf_clock(void)
455 {
456         return cpu_clock(smp_processor_id());
457 }
458
459 /*
460  * Update the record of the current time in a context.
461  */
462 static void update_context_time(struct perf_counter_context *ctx)
463 {
464         u64 now = perf_clock();
465
466         ctx->time += now - ctx->timestamp;
467         ctx->timestamp = now;
468 }
469
470 /*
471  * Update the total_time_enabled and total_time_running fields for a counter.
472  */
473 static void update_counter_times(struct perf_counter *counter)
474 {
475         struct perf_counter_context *ctx = counter->ctx;
476         u64 run_end;
477
478         if (counter->state < PERF_COUNTER_STATE_INACTIVE ||
479             counter->group_leader->state < PERF_COUNTER_STATE_INACTIVE)
480                 return;
481
482         counter->total_time_enabled = ctx->time - counter->tstamp_enabled;
483
484         if (counter->state == PERF_COUNTER_STATE_INACTIVE)
485                 run_end = counter->tstamp_stopped;
486         else
487                 run_end = ctx->time;
488
489         counter->total_time_running = run_end - counter->tstamp_running;
490 }
491
492 /*
493  * Update total_time_enabled and total_time_running for all counters in a group.
494  */
495 static void update_group_times(struct perf_counter *leader)
496 {
497         struct perf_counter *counter;
498
499         update_counter_times(leader);
500         list_for_each_entry(counter, &leader->sibling_list, list_entry)
501                 update_counter_times(counter);
502 }
503
504 /*
505  * Cross CPU call to disable a performance counter
506  */
507 static void __perf_counter_disable(void *info)
508 {
509         struct perf_counter *counter = info;
510         struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
511         struct perf_counter_context *ctx = counter->ctx;
512
513         /*
514          * If this is a per-task counter, need to check whether this
515          * counter's task is the current task on this cpu.
516          */
517         if (ctx->task && cpuctx->task_ctx != ctx)
518                 return;
519
520         spin_lock(&ctx->lock);
521
522         /*
523          * If the counter is on, turn it off.
524          * If it is in error state, leave it in error state.
525          */
526         if (counter->state >= PERF_COUNTER_STATE_INACTIVE) {
527                 update_context_time(ctx);
528                 update_group_times(counter);
529                 if (counter == counter->group_leader)
530                         group_sched_out(counter, cpuctx, ctx);
531                 else
532                         counter_sched_out(counter, cpuctx, ctx);
533                 counter->state = PERF_COUNTER_STATE_OFF;
534         }
535
536         spin_unlock(&ctx->lock);
537 }
538
539 /*
540  * Disable a counter.
541  *
542  * If counter->ctx is a cloned context, callers must make sure that
543  * every task struct that counter->ctx->task could possibly point to
544  * remains valid.  This condition is satisifed when called through
545  * perf_counter_for_each_child or perf_counter_for_each because they
546  * hold the top-level counter's child_mutex, so any descendant that
547  * goes to exit will block in sync_child_counter.
548  * When called from perf_pending_counter it's OK because counter->ctx
549  * is the current context on this CPU and preemption is disabled,
550  * hence we can't get into perf_counter_task_sched_out for this context.
551  */
552 static void perf_counter_disable(struct perf_counter *counter)
553 {
554         struct perf_counter_context *ctx = counter->ctx;
555         struct task_struct *task = ctx->task;
556
557         if (!task) {
558                 /*
559                  * Disable the counter on the cpu that it's on
560                  */
561                 smp_call_function_single(counter->cpu, __perf_counter_disable,
562                                          counter, 1);
563                 return;
564         }
565
566  retry:
567         task_oncpu_function_call(task, __perf_counter_disable, counter);
568
569         spin_lock_irq(&ctx->lock);
570         /*
571          * If the counter is still active, we need to retry the cross-call.
572          */
573         if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
574                 spin_unlock_irq(&ctx->lock);
575                 goto retry;
576         }
577
578         /*
579          * Since we have the lock this context can't be scheduled
580          * in, so we can change the state safely.
581          */
582         if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
583                 update_group_times(counter);
584                 counter->state = PERF_COUNTER_STATE_OFF;
585         }
586
587         spin_unlock_irq(&ctx->lock);
588 }
589
590 static int
591 counter_sched_in(struct perf_counter *counter,
592                  struct perf_cpu_context *cpuctx,
593                  struct perf_counter_context *ctx,
594                  int cpu)
595 {
596         if (counter->state <= PERF_COUNTER_STATE_OFF)
597                 return 0;
598
599         counter->state = PERF_COUNTER_STATE_ACTIVE;
600         counter->oncpu = cpu;   /* TODO: put 'cpu' into cpuctx->cpu */
601         /*
602          * The new state must be visible before we turn it on in the hardware:
603          */
604         smp_wmb();
605
606         if (counter->pmu->enable(counter)) {
607                 counter->state = PERF_COUNTER_STATE_INACTIVE;
608                 counter->oncpu = -1;
609                 return -EAGAIN;
610         }
611
612         counter->tstamp_running += ctx->time - counter->tstamp_stopped;
613
614         if (!is_software_counter(counter))
615                 cpuctx->active_oncpu++;
616         ctx->nr_active++;
617
618         if (counter->attr.exclusive)
619                 cpuctx->exclusive = 1;
620
621         return 0;
622 }
623
624 static int
625 group_sched_in(struct perf_counter *group_counter,
626                struct perf_cpu_context *cpuctx,
627                struct perf_counter_context *ctx,
628                int cpu)
629 {
630         struct perf_counter *counter, *partial_group;
631         int ret;
632
633         if (group_counter->state == PERF_COUNTER_STATE_OFF)
634                 return 0;
635
636         ret = hw_perf_group_sched_in(group_counter, cpuctx, ctx, cpu);
637         if (ret)
638                 return ret < 0 ? ret : 0;
639
640         if (counter_sched_in(group_counter, cpuctx, ctx, cpu))
641                 return -EAGAIN;
642
643         /*
644          * Schedule in siblings as one group (if any):
645          */
646         list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
647                 if (counter_sched_in(counter, cpuctx, ctx, cpu)) {
648                         partial_group = counter;
649                         goto group_error;
650                 }
651         }
652
653         return 0;
654
655 group_error:
656         /*
657          * Groups can be scheduled in as one unit only, so undo any
658          * partial group before returning:
659          */
660         list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
661                 if (counter == partial_group)
662                         break;
663                 counter_sched_out(counter, cpuctx, ctx);
664         }
665         counter_sched_out(group_counter, cpuctx, ctx);
666
667         return -EAGAIN;
668 }
669
670 /*
671  * Return 1 for a group consisting entirely of software counters,
672  * 0 if the group contains any hardware counters.
673  */
674 static int is_software_only_group(struct perf_counter *leader)
675 {
676         struct perf_counter *counter;
677
678         if (!is_software_counter(leader))
679                 return 0;
680
681         list_for_each_entry(counter, &leader->sibling_list, list_entry)
682                 if (!is_software_counter(counter))
683                         return 0;
684
685         return 1;
686 }
687
688 /*
689  * Work out whether we can put this counter group on the CPU now.
690  */
691 static int group_can_go_on(struct perf_counter *counter,
692                            struct perf_cpu_context *cpuctx,
693                            int can_add_hw)
694 {
695         /*
696          * Groups consisting entirely of software counters can always go on.
697          */
698         if (is_software_only_group(counter))
699                 return 1;
700         /*
701          * If an exclusive group is already on, no other hardware
702          * counters can go on.
703          */
704         if (cpuctx->exclusive)
705                 return 0;
706         /*
707          * If this group is exclusive and there are already
708          * counters on the CPU, it can't go on.
709          */
710         if (counter->attr.exclusive && cpuctx->active_oncpu)
711                 return 0;
712         /*
713          * Otherwise, try to add it if all previous groups were able
714          * to go on.
715          */
716         return can_add_hw;
717 }
718
719 static void add_counter_to_ctx(struct perf_counter *counter,
720                                struct perf_counter_context *ctx)
721 {
722         list_add_counter(counter, ctx);
723         counter->tstamp_enabled = ctx->time;
724         counter->tstamp_running = ctx->time;
725         counter->tstamp_stopped = ctx->time;
726 }
727
728 /*
729  * Cross CPU call to install and enable a performance counter
730  *
731  * Must be called with ctx->mutex held
732  */
733 static void __perf_install_in_context(void *info)
734 {
735         struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
736         struct perf_counter *counter = info;
737         struct perf_counter_context *ctx = counter->ctx;
738         struct perf_counter *leader = counter->group_leader;
739         int cpu = smp_processor_id();
740         int err;
741
742         /*
743          * If this is a task context, we need to check whether it is
744          * the current task context of this cpu. If not it has been
745          * scheduled out before the smp call arrived.
746          * Or possibly this is the right context but it isn't
747          * on this cpu because it had no counters.
748          */
749         if (ctx->task && cpuctx->task_ctx != ctx) {
750                 if (cpuctx->task_ctx || ctx->task != current)
751                         return;
752                 cpuctx->task_ctx = ctx;
753         }
754
755         spin_lock(&ctx->lock);
756         ctx->is_active = 1;
757         update_context_time(ctx);
758
759         /*
760          * Protect the list operation against NMI by disabling the
761          * counters on a global level. NOP for non NMI based counters.
762          */
763         perf_disable();
764
765         add_counter_to_ctx(counter, ctx);
766
767         /*
768          * Don't put the counter on if it is disabled or if
769          * it is in a group and the group isn't on.
770          */
771         if (counter->state != PERF_COUNTER_STATE_INACTIVE ||
772             (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE))
773                 goto unlock;
774
775         /*
776          * An exclusive counter can't go on if there are already active
777          * hardware counters, and no hardware counter can go on if there
778          * is already an exclusive counter on.
779          */
780         if (!group_can_go_on(counter, cpuctx, 1))
781                 err = -EEXIST;
782         else
783                 err = counter_sched_in(counter, cpuctx, ctx, cpu);
784
785         if (err) {
786                 /*
787                  * This counter couldn't go on.  If it is in a group
788                  * then we have to pull the whole group off.
789                  * If the counter group is pinned then put it in error state.
790                  */
791                 if (leader != counter)
792                         group_sched_out(leader, cpuctx, ctx);
793                 if (leader->attr.pinned) {
794                         update_group_times(leader);
795                         leader->state = PERF_COUNTER_STATE_ERROR;
796                 }
797         }
798
799         if (!err && !ctx->task && cpuctx->max_pertask)
800                 cpuctx->max_pertask--;
801
802  unlock:
803         perf_enable();
804
805         spin_unlock(&ctx->lock);
806 }
807
808 /*
809  * Attach a performance counter to a context
810  *
811  * First we add the counter to the list with the hardware enable bit
812  * in counter->hw_config cleared.
813  *
814  * If the counter is attached to a task which is on a CPU we use a smp
815  * call to enable it in the task context. The task might have been
816  * scheduled away, but we check this in the smp call again.
817  *
818  * Must be called with ctx->mutex held.
819  */
820 static void
821 perf_install_in_context(struct perf_counter_context *ctx,
822                         struct perf_counter *counter,
823                         int cpu)
824 {
825         struct task_struct *task = ctx->task;
826
827         if (!task) {
828                 /*
829                  * Per cpu counters are installed via an smp call and
830                  * the install is always sucessful.
831                  */
832                 smp_call_function_single(cpu, __perf_install_in_context,
833                                          counter, 1);
834                 return;
835         }
836
837 retry:
838         task_oncpu_function_call(task, __perf_install_in_context,
839                                  counter);
840
841         spin_lock_irq(&ctx->lock);
842         /*
843          * we need to retry the smp call.
844          */
845         if (ctx->is_active && list_empty(&counter->list_entry)) {
846                 spin_unlock_irq(&ctx->lock);
847                 goto retry;
848         }
849
850         /*
851          * The lock prevents that this context is scheduled in so we
852          * can add the counter safely, if it the call above did not
853          * succeed.
854          */
855         if (list_empty(&counter->list_entry))
856                 add_counter_to_ctx(counter, ctx);
857         spin_unlock_irq(&ctx->lock);
858 }
859
860 /*
861  * Put a counter into inactive state and update time fields.
862  * Enabling the leader of a group effectively enables all
863  * the group members that aren't explicitly disabled, so we
864  * have to update their ->tstamp_enabled also.
865  * Note: this works for group members as well as group leaders
866  * since the non-leader members' sibling_lists will be empty.
867  */
868 static void __perf_counter_mark_enabled(struct perf_counter *counter,
869                                         struct perf_counter_context *ctx)
870 {
871         struct perf_counter *sub;
872
873         counter->state = PERF_COUNTER_STATE_INACTIVE;
874         counter->tstamp_enabled = ctx->time - counter->total_time_enabled;
875         list_for_each_entry(sub, &counter->sibling_list, list_entry)
876                 if (sub->state >= PERF_COUNTER_STATE_INACTIVE)
877                         sub->tstamp_enabled =
878                                 ctx->time - sub->total_time_enabled;
879 }
880
881 /*
882  * Cross CPU call to enable a performance counter
883  */
884 static void __perf_counter_enable(void *info)
885 {
886         struct perf_counter *counter = info;
887         struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
888         struct perf_counter_context *ctx = counter->ctx;
889         struct perf_counter *leader = counter->group_leader;
890         int err;
891
892         /*
893          * If this is a per-task counter, need to check whether this
894          * counter's task is the current task on this cpu.
895          */
896         if (ctx->task && cpuctx->task_ctx != ctx) {
897                 if (cpuctx->task_ctx || ctx->task != current)
898                         return;
899                 cpuctx->task_ctx = ctx;
900         }
901
902         spin_lock(&ctx->lock);
903         ctx->is_active = 1;
904         update_context_time(ctx);
905
906         if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
907                 goto unlock;
908         __perf_counter_mark_enabled(counter, ctx);
909
910         /*
911          * If the counter is in a group and isn't the group leader,
912          * then don't put it on unless the group is on.
913          */
914         if (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE)
915                 goto unlock;
916
917         if (!group_can_go_on(counter, cpuctx, 1)) {
918                 err = -EEXIST;
919         } else {
920                 perf_disable();
921                 if (counter == leader)
922                         err = group_sched_in(counter, cpuctx, ctx,
923                                              smp_processor_id());
924                 else
925                         err = counter_sched_in(counter, cpuctx, ctx,
926                                                smp_processor_id());
927                 perf_enable();
928         }
929
930         if (err) {
931                 /*
932                  * If this counter can't go on and it's part of a
933                  * group, then the whole group has to come off.
934                  */
935                 if (leader != counter)
936                         group_sched_out(leader, cpuctx, ctx);
937                 if (leader->attr.pinned) {
938                         update_group_times(leader);
939                         leader->state = PERF_COUNTER_STATE_ERROR;
940                 }
941         }
942
943  unlock:
944         spin_unlock(&ctx->lock);
945 }
946
947 /*
948  * Enable a counter.
949  *
950  * If counter->ctx is a cloned context, callers must make sure that
951  * every task struct that counter->ctx->task could possibly point to
952  * remains valid.  This condition is satisfied when called through
953  * perf_counter_for_each_child or perf_counter_for_each as described
954  * for perf_counter_disable.
955  */
956 static void perf_counter_enable(struct perf_counter *counter)
957 {
958         struct perf_counter_context *ctx = counter->ctx;
959         struct task_struct *task = ctx->task;
960
961         if (!task) {
962                 /*
963                  * Enable the counter on the cpu that it's on
964                  */
965                 smp_call_function_single(counter->cpu, __perf_counter_enable,
966                                          counter, 1);
967                 return;
968         }
969
970         spin_lock_irq(&ctx->lock);
971         if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
972                 goto out;
973
974         /*
975          * If the counter is in error state, clear that first.
976          * That way, if we see the counter in error state below, we
977          * know that it has gone back into error state, as distinct
978          * from the task having been scheduled away before the
979          * cross-call arrived.
980          */
981         if (counter->state == PERF_COUNTER_STATE_ERROR)
982                 counter->state = PERF_COUNTER_STATE_OFF;
983
984  retry:
985         spin_unlock_irq(&ctx->lock);
986         task_oncpu_function_call(task, __perf_counter_enable, counter);
987
988         spin_lock_irq(&ctx->lock);
989
990         /*
991          * If the context is active and the counter is still off,
992          * we need to retry the cross-call.
993          */
994         if (ctx->is_active && counter->state == PERF_COUNTER_STATE_OFF)
995                 goto retry;
996
997         /*
998          * Since we have the lock this context can't be scheduled
999          * in, so we can change the state safely.
1000          */
1001         if (counter->state == PERF_COUNTER_STATE_OFF)
1002                 __perf_counter_mark_enabled(counter, ctx);
1003
1004  out:
1005         spin_unlock_irq(&ctx->lock);
1006 }
1007
1008 static int perf_counter_refresh(struct perf_counter *counter, int refresh)
1009 {
1010         /*
1011          * not supported on inherited counters
1012          */
1013         if (counter->attr.inherit)
1014                 return -EINVAL;
1015
1016         atomic_add(refresh, &counter->event_limit);
1017         perf_counter_enable(counter);
1018
1019         return 0;
1020 }
1021
1022 void __perf_counter_sched_out(struct perf_counter_context *ctx,
1023                               struct perf_cpu_context *cpuctx)
1024 {
1025         struct perf_counter *counter;
1026
1027         spin_lock(&ctx->lock);
1028         ctx->is_active = 0;
1029         if (likely(!ctx->nr_counters))
1030                 goto out;
1031         update_context_time(ctx);
1032
1033         perf_disable();
1034         if (ctx->nr_active) {
1035                 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1036                         if (counter != counter->group_leader)
1037                                 counter_sched_out(counter, cpuctx, ctx);
1038                         else
1039                                 group_sched_out(counter, cpuctx, ctx);
1040                 }
1041         }
1042         perf_enable();
1043  out:
1044         spin_unlock(&ctx->lock);
1045 }
1046
1047 /*
1048  * Test whether two contexts are equivalent, i.e. whether they
1049  * have both been cloned from the same version of the same context
1050  * and they both have the same number of enabled counters.
1051  * If the number of enabled counters is the same, then the set
1052  * of enabled counters should be the same, because these are both
1053  * inherited contexts, therefore we can't access individual counters
1054  * in them directly with an fd; we can only enable/disable all
1055  * counters via prctl, or enable/disable all counters in a family
1056  * via ioctl, which will have the same effect on both contexts.
1057  */
1058 static int context_equiv(struct perf_counter_context *ctx1,
1059                          struct perf_counter_context *ctx2)
1060 {
1061         return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
1062                 && ctx1->parent_gen == ctx2->parent_gen
1063                 && !ctx1->pin_count && !ctx2->pin_count;
1064 }
1065
1066 static void __perf_counter_read(void *counter);
1067
1068 static void __perf_counter_sync_stat(struct perf_counter *counter,
1069                                      struct perf_counter *next_counter)
1070 {
1071         u64 value;
1072
1073         if (!counter->attr.inherit_stat)
1074                 return;
1075
1076         /*
1077          * Update the counter value, we cannot use perf_counter_read()
1078          * because we're in the middle of a context switch and have IRQs
1079          * disabled, which upsets smp_call_function_single(), however
1080          * we know the counter must be on the current CPU, therefore we
1081          * don't need to use it.
1082          */
1083         switch (counter->state) {
1084         case PERF_COUNTER_STATE_ACTIVE:
1085                 __perf_counter_read(counter);
1086                 break;
1087
1088         case PERF_COUNTER_STATE_INACTIVE:
1089                 update_counter_times(counter);
1090                 break;
1091
1092         default:
1093                 break;
1094         }
1095
1096         /*
1097          * In order to keep per-task stats reliable we need to flip the counter
1098          * values when we flip the contexts.
1099          */
1100         value = atomic64_read(&next_counter->count);
1101         value = atomic64_xchg(&counter->count, value);
1102         atomic64_set(&next_counter->count, value);
1103
1104         swap(counter->total_time_enabled, next_counter->total_time_enabled);
1105         swap(counter->total_time_running, next_counter->total_time_running);
1106
1107         /*
1108          * Since we swizzled the values, update the user visible data too.
1109          */
1110         perf_counter_update_userpage(counter);
1111         perf_counter_update_userpage(next_counter);
1112 }
1113
1114 #define list_next_entry(pos, member) \
1115         list_entry(pos->member.next, typeof(*pos), member)
1116
1117 static void perf_counter_sync_stat(struct perf_counter_context *ctx,
1118                                    struct perf_counter_context *next_ctx)
1119 {
1120         struct perf_counter *counter, *next_counter;
1121
1122         if (!ctx->nr_stat)
1123                 return;
1124
1125         counter = list_first_entry(&ctx->event_list,
1126                                    struct perf_counter, event_entry);
1127
1128         next_counter = list_first_entry(&next_ctx->event_list,
1129                                         struct perf_counter, event_entry);
1130
1131         while (&counter->event_entry != &ctx->event_list &&
1132                &next_counter->event_entry != &next_ctx->event_list) {
1133
1134                 __perf_counter_sync_stat(counter, next_counter);
1135
1136                 counter = list_next_entry(counter, event_entry);
1137                 next_counter = list_next_entry(next_counter, event_entry);
1138         }
1139 }
1140
1141 /*
1142  * Called from scheduler to remove the counters of the current task,
1143  * with interrupts disabled.
1144  *
1145  * We stop each counter and update the counter value in counter->count.
1146  *
1147  * This does not protect us against NMI, but disable()
1148  * sets the disabled bit in the control field of counter _before_
1149  * accessing the counter control register. If a NMI hits, then it will
1150  * not restart the counter.
1151  */
1152 void perf_counter_task_sched_out(struct task_struct *task,
1153                                  struct task_struct *next, int cpu)
1154 {
1155         struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
1156         struct perf_counter_context *ctx = task->perf_counter_ctxp;
1157         struct perf_counter_context *next_ctx;
1158         struct perf_counter_context *parent;
1159         struct pt_regs *regs;
1160         int do_switch = 1;
1161
1162         regs = task_pt_regs(task);
1163         perf_swcounter_event(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 1, regs, 0);
1164
1165         if (likely(!ctx || !cpuctx->task_ctx))
1166                 return;
1167
1168         update_context_time(ctx);
1169
1170         rcu_read_lock();
1171         parent = rcu_dereference(ctx->parent_ctx);
1172         next_ctx = next->perf_counter_ctxp;
1173         if (parent && next_ctx &&
1174             rcu_dereference(next_ctx->parent_ctx) == parent) {
1175                 /*
1176                  * Looks like the two contexts are clones, so we might be
1177                  * able to optimize the context switch.  We lock both
1178                  * contexts and check that they are clones under the
1179                  * lock (including re-checking that neither has been
1180                  * uncloned in the meantime).  It doesn't matter which
1181                  * order we take the locks because no other cpu could
1182                  * be trying to lock both of these tasks.
1183                  */
1184                 spin_lock(&ctx->lock);
1185                 spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
1186                 if (context_equiv(ctx, next_ctx)) {
1187                         /*
1188                          * XXX do we need a memory barrier of sorts
1189                          * wrt to rcu_dereference() of perf_counter_ctxp
1190                          */
1191                         task->perf_counter_ctxp = next_ctx;
1192                         next->perf_counter_ctxp = ctx;
1193                         ctx->task = next;
1194                         next_ctx->task = task;
1195                         do_switch = 0;
1196
1197                         perf_counter_sync_stat(ctx, next_ctx);
1198                 }
1199                 spin_unlock(&next_ctx->lock);
1200                 spin_unlock(&ctx->lock);
1201         }
1202         rcu_read_unlock();
1203
1204         if (do_switch) {
1205                 __perf_counter_sched_out(ctx, cpuctx);
1206                 cpuctx->task_ctx = NULL;
1207         }
1208 }
1209
1210 /*
1211  * Called with IRQs disabled
1212  */
1213 static void __perf_counter_task_sched_out(struct perf_counter_context *ctx)
1214 {
1215         struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1216
1217         if (!cpuctx->task_ctx)
1218                 return;
1219
1220         if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
1221                 return;
1222
1223         __perf_counter_sched_out(ctx, cpuctx);
1224         cpuctx->task_ctx = NULL;
1225 }
1226
1227 /*
1228  * Called with IRQs disabled
1229  */
1230 static void perf_counter_cpu_sched_out(struct perf_cpu_context *cpuctx)
1231 {
1232         __perf_counter_sched_out(&cpuctx->ctx, cpuctx);
1233 }
1234
1235 static void
1236 __perf_counter_sched_in(struct perf_counter_context *ctx,
1237                         struct perf_cpu_context *cpuctx, int cpu)
1238 {
1239         struct perf_counter *counter;
1240         int can_add_hw = 1;
1241
1242         spin_lock(&ctx->lock);
1243         ctx->is_active = 1;
1244         if (likely(!ctx->nr_counters))
1245                 goto out;
1246
1247         ctx->timestamp = perf_clock();
1248
1249         perf_disable();
1250
1251         /*
1252          * First go through the list and put on any pinned groups
1253          * in order to give them the best chance of going on.
1254          */
1255         list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1256                 if (counter->state <= PERF_COUNTER_STATE_OFF ||
1257                     !counter->attr.pinned)
1258                         continue;
1259                 if (counter->cpu != -1 && counter->cpu != cpu)
1260                         continue;
1261
1262                 if (counter != counter->group_leader)
1263                         counter_sched_in(counter, cpuctx, ctx, cpu);
1264                 else {
1265                         if (group_can_go_on(counter, cpuctx, 1))
1266                                 group_sched_in(counter, cpuctx, ctx, cpu);
1267                 }
1268
1269                 /*
1270                  * If this pinned group hasn't been scheduled,
1271                  * put it in error state.
1272                  */
1273                 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1274                         update_group_times(counter);
1275                         counter->state = PERF_COUNTER_STATE_ERROR;
1276                 }
1277         }
1278
1279         list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1280                 /*
1281                  * Ignore counters in OFF or ERROR state, and
1282                  * ignore pinned counters since we did them already.
1283                  */
1284                 if (counter->state <= PERF_COUNTER_STATE_OFF ||
1285                     counter->attr.pinned)
1286                         continue;
1287
1288                 /*
1289                  * Listen to the 'cpu' scheduling filter constraint
1290                  * of counters:
1291                  */
1292                 if (counter->cpu != -1 && counter->cpu != cpu)
1293                         continue;
1294
1295                 if (counter != counter->group_leader) {
1296                         if (counter_sched_in(counter, cpuctx, ctx, cpu))
1297                                 can_add_hw = 0;
1298                 } else {
1299                         if (group_can_go_on(counter, cpuctx, can_add_hw)) {
1300                                 if (group_sched_in(counter, cpuctx, ctx, cpu))
1301                                         can_add_hw = 0;
1302                         }
1303                 }
1304         }
1305         perf_enable();
1306  out:
1307         spin_unlock(&ctx->lock);
1308 }
1309
1310 /*
1311  * Called from scheduler to add the counters of the current task
1312  * with interrupts disabled.
1313  *
1314  * We restore the counter value and then enable it.
1315  *
1316  * This does not protect us against NMI, but enable()
1317  * sets the enabled bit in the control field of counter _before_
1318  * accessing the counter control register. If a NMI hits, then it will
1319  * keep the counter running.
1320  */
1321 void perf_counter_task_sched_in(struct task_struct *task, int cpu)
1322 {
1323         struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
1324         struct perf_counter_context *ctx = task->perf_counter_ctxp;
1325
1326         if (likely(!ctx))
1327                 return;
1328         if (cpuctx->task_ctx == ctx)
1329                 return;
1330         __perf_counter_sched_in(ctx, cpuctx, cpu);
1331         cpuctx->task_ctx = ctx;
1332 }
1333
1334 static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu)
1335 {
1336         struct perf_counter_context *ctx = &cpuctx->ctx;
1337
1338         __perf_counter_sched_in(ctx, cpuctx, cpu);
1339 }
1340
1341 #define MAX_INTERRUPTS (~0ULL)
1342
1343 static void perf_log_throttle(struct perf_counter *counter, int enable);
1344
1345 static void perf_adjust_period(struct perf_counter *counter, u64 events)
1346 {
1347         struct hw_perf_counter *hwc = &counter->hw;
1348         u64 period, sample_period;
1349         s64 delta;
1350
1351         events *= hwc->sample_period;
1352         period = div64_u64(events, counter->attr.sample_freq);
1353
1354         delta = (s64)(period - hwc->sample_period);
1355         delta = (delta + 7) / 8; /* low pass filter */
1356
1357         sample_period = hwc->sample_period + delta;
1358
1359         if (!sample_period)
1360                 sample_period = 1;
1361
1362         hwc->sample_period = sample_period;
1363 }
1364
1365 static void perf_ctx_adjust_freq(struct perf_counter_context *ctx)
1366 {
1367         struct perf_counter *counter;
1368         struct hw_perf_counter *hwc;
1369         u64 interrupts, freq;
1370
1371         spin_lock(&ctx->lock);
1372         list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1373                 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
1374                         continue;
1375
1376                 hwc = &counter->hw;
1377
1378                 interrupts = hwc->interrupts;
1379                 hwc->interrupts = 0;
1380
1381                 /*
1382                  * unthrottle counters on the tick
1383                  */
1384                 if (interrupts == MAX_INTERRUPTS) {
1385                         perf_log_throttle(counter, 1);
1386                         counter->pmu->unthrottle(counter);
1387                         interrupts = 2*sysctl_perf_counter_sample_rate/HZ;
1388                 }
1389
1390                 if (!counter->attr.freq || !counter->attr.sample_freq)
1391                         continue;
1392
1393                 /*
1394                  * if the specified freq < HZ then we need to skip ticks
1395                  */
1396                 if (counter->attr.sample_freq < HZ) {
1397                         freq = counter->attr.sample_freq;
1398
1399                         hwc->freq_count += freq;
1400                         hwc->freq_interrupts += interrupts;
1401
1402                         if (hwc->freq_count < HZ)
1403                                 continue;
1404
1405                         interrupts = hwc->freq_interrupts;
1406                         hwc->freq_interrupts = 0;
1407                         hwc->freq_count -= HZ;
1408                 } else
1409                         freq = HZ;
1410
1411                 perf_adjust_period(counter, freq * interrupts);
1412
1413                 /*
1414                  * In order to avoid being stalled by an (accidental) huge
1415                  * sample period, force reset the sample period if we didn't
1416                  * get any events in this freq period.
1417                  */
1418                 if (!interrupts) {
1419                         perf_disable();
1420                         counter->pmu->disable(counter);
1421                         atomic64_set(&hwc->period_left, 0);
1422                         counter->pmu->enable(counter);
1423                         perf_enable();
1424                 }
1425         }
1426         spin_unlock(&ctx->lock);
1427 }
1428
1429 /*
1430  * Round-robin a context's counters:
1431  */
1432 static void rotate_ctx(struct perf_counter_context *ctx)
1433 {
1434         struct perf_counter *counter;
1435
1436         if (!ctx->nr_counters)
1437                 return;
1438
1439         spin_lock(&ctx->lock);
1440         /*
1441          * Rotate the first entry last (works just fine for group counters too):
1442          */
1443         perf_disable();
1444         list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1445                 list_move_tail(&counter->list_entry, &ctx->counter_list);
1446                 break;
1447         }
1448         perf_enable();
1449
1450         spin_unlock(&ctx->lock);
1451 }
1452
1453 void perf_counter_task_tick(struct task_struct *curr, int cpu)
1454 {
1455         struct perf_cpu_context *cpuctx;
1456         struct perf_counter_context *ctx;
1457
1458         if (!atomic_read(&nr_counters))
1459                 return;
1460
1461         cpuctx = &per_cpu(perf_cpu_context, cpu);
1462         ctx = curr->perf_counter_ctxp;
1463
1464         perf_ctx_adjust_freq(&cpuctx->ctx);
1465         if (ctx)
1466                 perf_ctx_adjust_freq(ctx);
1467
1468         perf_counter_cpu_sched_out(cpuctx);
1469         if (ctx)
1470                 __perf_counter_task_sched_out(ctx);
1471
1472         rotate_ctx(&cpuctx->ctx);
1473         if (ctx)
1474                 rotate_ctx(ctx);
1475
1476         perf_counter_cpu_sched_in(cpuctx, cpu);
1477         if (ctx)
1478                 perf_counter_task_sched_in(curr, cpu);
1479 }
1480
1481 /*
1482  * Enable all of a task's counters that have been marked enable-on-exec.
1483  * This expects task == current.
1484  */
1485 static void perf_counter_enable_on_exec(struct task_struct *task)
1486 {
1487         struct perf_counter_context *ctx;
1488         struct perf_counter *counter;
1489         unsigned long flags;
1490         int enabled = 0;
1491
1492         local_irq_save(flags);
1493         ctx = task->perf_counter_ctxp;
1494         if (!ctx || !ctx->nr_counters)
1495                 goto out;
1496
1497         __perf_counter_task_sched_out(ctx);
1498
1499         spin_lock(&ctx->lock);
1500
1501         list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1502                 if (!counter->attr.enable_on_exec)
1503                         continue;
1504                 counter->attr.enable_on_exec = 0;
1505                 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
1506                         continue;
1507                 __perf_counter_mark_enabled(counter, ctx);
1508                 enabled = 1;
1509         }
1510
1511         /*
1512          * Unclone this context if we enabled any counter.
1513          */
1514         if (enabled)
1515                 unclone_ctx(ctx);
1516
1517         spin_unlock(&ctx->lock);
1518
1519         perf_counter_task_sched_in(task, smp_processor_id());
1520  out:
1521         local_irq_restore(flags);
1522 }
1523
1524 /*
1525  * Cross CPU call to read the hardware counter
1526  */
1527 static void __perf_counter_read(void *info)
1528 {
1529         struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1530         struct perf_counter *counter = info;
1531         struct perf_counter_context *ctx = counter->ctx;
1532         unsigned long flags;
1533
1534         /*
1535          * If this is a task context, we need to check whether it is
1536          * the current task context of this cpu.  If not it has been
1537          * scheduled out before the smp call arrived.  In that case
1538          * counter->count would have been updated to a recent sample
1539          * when the counter was scheduled out.
1540          */
1541         if (ctx->task && cpuctx->task_ctx != ctx)
1542                 return;
1543
1544         local_irq_save(flags);
1545         if (ctx->is_active)
1546                 update_context_time(ctx);
1547         counter->pmu->read(counter);
1548         update_counter_times(counter);
1549         local_irq_restore(flags);
1550 }
1551
1552 static u64 perf_counter_read(struct perf_counter *counter)
1553 {
1554         /*
1555          * If counter is enabled and currently active on a CPU, update the
1556          * value in the counter structure:
1557          */
1558         if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
1559                 smp_call_function_single(counter->oncpu,
1560                                          __perf_counter_read, counter, 1);
1561         } else if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1562                 update_counter_times(counter);
1563         }
1564
1565         return atomic64_read(&counter->count);
1566 }
1567
1568 /*
1569  * Initialize the perf_counter context in a task_struct:
1570  */
1571 static void
1572 __perf_counter_init_context(struct perf_counter_context *ctx,
1573                             struct task_struct *task)
1574 {
1575         memset(ctx, 0, sizeof(*ctx));
1576         spin_lock_init(&ctx->lock);
1577         mutex_init(&ctx->mutex);
1578         INIT_LIST_HEAD(&ctx->counter_list);
1579         INIT_LIST_HEAD(&ctx->event_list);
1580         atomic_set(&ctx->refcount, 1);
1581         ctx->task = task;
1582 }
1583
1584 static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
1585 {
1586         struct perf_counter_context *ctx;
1587         struct perf_cpu_context *cpuctx;
1588         struct task_struct *task;
1589         unsigned long flags;
1590         int err;
1591
1592         /*
1593          * If cpu is not a wildcard then this is a percpu counter:
1594          */
1595         if (cpu != -1) {
1596                 /* Must be root to operate on a CPU counter: */
1597                 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
1598                         return ERR_PTR(-EACCES);
1599
1600                 if (cpu < 0 || cpu > num_possible_cpus())
1601                         return ERR_PTR(-EINVAL);
1602
1603                 /*
1604                  * We could be clever and allow to attach a counter to an
1605                  * offline CPU and activate it when the CPU comes up, but
1606                  * that's for later.
1607                  */
1608                 if (!cpu_isset(cpu, cpu_online_map))
1609                         return ERR_PTR(-ENODEV);
1610
1611                 cpuctx = &per_cpu(perf_cpu_context, cpu);
1612                 ctx = &cpuctx->ctx;
1613                 get_ctx(ctx);
1614
1615                 return ctx;
1616         }
1617
1618         rcu_read_lock();
1619         if (!pid)
1620                 task = current;
1621         else
1622                 task = find_task_by_vpid(pid);
1623         if (task)
1624                 get_task_struct(task);
1625         rcu_read_unlock();
1626
1627         if (!task)
1628                 return ERR_PTR(-ESRCH);
1629
1630         /*
1631          * Can't attach counters to a dying task.
1632          */
1633         err = -ESRCH;
1634         if (task->flags & PF_EXITING)
1635                 goto errout;
1636
1637         /* Reuse ptrace permission checks for now. */
1638         err = -EACCES;
1639         if (!ptrace_may_access(task, PTRACE_MODE_READ))
1640                 goto errout;
1641
1642  retry:
1643         ctx = perf_lock_task_context(task, &flags);
1644         if (ctx) {
1645                 unclone_ctx(ctx);
1646                 spin_unlock_irqrestore(&ctx->lock, flags);
1647         }
1648
1649         if (!ctx) {
1650                 ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
1651                 err = -ENOMEM;
1652                 if (!ctx)
1653                         goto errout;
1654                 __perf_counter_init_context(ctx, task);
1655                 get_ctx(ctx);
1656                 if (cmpxchg(&task->perf_counter_ctxp, NULL, ctx)) {
1657                         /*
1658                          * We raced with some other task; use
1659                          * the context they set.
1660                          */
1661                         kfree(ctx);
1662                         goto retry;
1663                 }
1664                 get_task_struct(task);
1665         }
1666
1667         put_task_struct(task);
1668         return ctx;
1669
1670  errout:
1671         put_task_struct(task);
1672         return ERR_PTR(err);
1673 }
1674
1675 static void free_counter_rcu(struct rcu_head *head)
1676 {
1677         struct perf_counter *counter;
1678
1679         counter = container_of(head, struct perf_counter, rcu_head);
1680         if (counter->ns)
1681                 put_pid_ns(counter->ns);
1682         kfree(counter);
1683 }
1684
1685 static void perf_pending_sync(struct perf_counter *counter);
1686
1687 static void free_counter(struct perf_counter *counter)
1688 {
1689         perf_pending_sync(counter);
1690
1691         if (!counter->parent) {
1692                 atomic_dec(&nr_counters);
1693                 if (counter->attr.mmap)
1694                         atomic_dec(&nr_mmap_counters);
1695                 if (counter->attr.comm)
1696                         atomic_dec(&nr_comm_counters);
1697                 if (counter->attr.task)
1698                         atomic_dec(&nr_task_counters);
1699         }
1700
1701         if (counter->output) {
1702                 fput(counter->output->filp);
1703                 counter->output = NULL;
1704         }
1705
1706         if (counter->destroy)
1707                 counter->destroy(counter);
1708
1709         put_ctx(counter->ctx);
1710         call_rcu(&counter->rcu_head, free_counter_rcu);
1711 }
1712
1713 /*
1714  * Called when the last reference to the file is gone.
1715  */
1716 static int perf_release(struct inode *inode, struct file *file)
1717 {
1718         struct perf_counter *counter = file->private_data;
1719         struct perf_counter_context *ctx = counter->ctx;
1720
1721         file->private_data = NULL;
1722
1723         WARN_ON_ONCE(ctx->parent_ctx);
1724         mutex_lock(&ctx->mutex);
1725         perf_counter_remove_from_context(counter);
1726         mutex_unlock(&ctx->mutex);
1727
1728         mutex_lock(&counter->owner->perf_counter_mutex);
1729         list_del_init(&counter->owner_entry);
1730         mutex_unlock(&counter->owner->perf_counter_mutex);
1731         put_task_struct(counter->owner);
1732
1733         free_counter(counter);
1734
1735         return 0;
1736 }
1737
1738 static int perf_counter_read_size(struct perf_counter *counter)
1739 {
1740         int entry = sizeof(u64); /* value */
1741         int size = 0;
1742         int nr = 1;
1743
1744         if (counter->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1745                 size += sizeof(u64);
1746
1747         if (counter->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1748                 size += sizeof(u64);
1749
1750         if (counter->attr.read_format & PERF_FORMAT_ID)
1751                 entry += sizeof(u64);
1752
1753         if (counter->attr.read_format & PERF_FORMAT_GROUP) {
1754                 nr += counter->group_leader->nr_siblings;
1755                 size += sizeof(u64);
1756         }
1757
1758         size += entry * nr;
1759
1760         return size;
1761 }
1762
1763 static u64 perf_counter_read_value(struct perf_counter *counter)
1764 {
1765         struct perf_counter *child;
1766         u64 total = 0;
1767
1768         total += perf_counter_read(counter);
1769         list_for_each_entry(child, &counter->child_list, child_list)
1770                 total += perf_counter_read(child);
1771
1772         return total;
1773 }
1774
1775 static int perf_counter_read_entry(struct perf_counter *counter,
1776                                    u64 read_format, char __user *buf)
1777 {
1778         int n = 0, count = 0;
1779         u64 values[2];
1780
1781         values[n++] = perf_counter_read_value(counter);
1782         if (read_format & PERF_FORMAT_ID)
1783                 values[n++] = primary_counter_id(counter);
1784
1785         count = n * sizeof(u64);
1786
1787         if (copy_to_user(buf, values, count))
1788                 return -EFAULT;
1789
1790         return count;
1791 }
1792
1793 static int perf_counter_read_group(struct perf_counter *counter,
1794                                    u64 read_format, char __user *buf)
1795 {
1796         struct perf_counter *leader = counter->group_leader, *sub;
1797         int n = 0, size = 0, err = -EFAULT;
1798         u64 values[3];
1799
1800         values[n++] = 1 + leader->nr_siblings;
1801         if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
1802                 values[n++] = leader->total_time_enabled +
1803                         atomic64_read(&leader->child_total_time_enabled);
1804         }
1805         if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
1806                 values[n++] = leader->total_time_running +
1807                         atomic64_read(&leader->child_total_time_running);
1808         }
1809
1810         size = n * sizeof(u64);
1811
1812         if (copy_to_user(buf, values, size))
1813                 return -EFAULT;
1814
1815         err = perf_counter_read_entry(leader, read_format, buf + size);
1816         if (err < 0)
1817                 return err;
1818
1819         size += err;
1820
1821         list_for_each_entry(sub, &leader->sibling_list, list_entry) {
1822                 err = perf_counter_read_entry(sub, read_format,
1823                                 buf + size);
1824                 if (err < 0)
1825                         return err;
1826
1827                 size += err;
1828         }
1829
1830         return size;
1831 }
1832
1833 static int perf_counter_read_one(struct perf_counter *counter,
1834                                  u64 read_format, char __user *buf)
1835 {
1836         u64 values[4];
1837         int n = 0;
1838
1839         values[n++] = perf_counter_read_value(counter);
1840         if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
1841                 values[n++] = counter->total_time_enabled +
1842                         atomic64_read(&counter->child_total_time_enabled);
1843         }
1844         if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
1845                 values[n++] = counter->total_time_running +
1846                         atomic64_read(&counter->child_total_time_running);
1847         }
1848         if (read_format & PERF_FORMAT_ID)
1849                 values[n++] = primary_counter_id(counter);
1850
1851         if (copy_to_user(buf, values, n * sizeof(u64)))
1852                 return -EFAULT;
1853
1854         return n * sizeof(u64);
1855 }
1856
1857 /*
1858  * Read the performance counter - simple non blocking version for now
1859  */
1860 static ssize_t
1861 perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
1862 {
1863         u64 read_format = counter->attr.read_format;
1864         int ret;
1865
1866         /*
1867          * Return end-of-file for a read on a counter that is in
1868          * error state (i.e. because it was pinned but it couldn't be
1869          * scheduled on to the CPU at some point).
1870          */
1871         if (counter->state == PERF_COUNTER_STATE_ERROR)
1872                 return 0;
1873
1874         if (count < perf_counter_read_size(counter))
1875                 return -ENOSPC;
1876
1877         WARN_ON_ONCE(counter->ctx->parent_ctx);
1878         mutex_lock(&counter->child_mutex);
1879         if (read_format & PERF_FORMAT_GROUP)
1880                 ret = perf_counter_read_group(counter, read_format, buf);
1881         else
1882                 ret = perf_counter_read_one(counter, read_format, buf);
1883         mutex_unlock(&counter->child_mutex);
1884
1885         return ret;
1886 }
1887
1888 static ssize_t
1889 perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
1890 {
1891         struct perf_counter *counter = file->private_data;
1892
1893         return perf_read_hw(counter, buf, count);
1894 }
1895
1896 static unsigned int perf_poll(struct file *file, poll_table *wait)
1897 {
1898         struct perf_counter *counter = file->private_data;
1899         struct perf_mmap_data *data;
1900         unsigned int events = POLL_HUP;
1901
1902         rcu_read_lock();
1903         data = rcu_dereference(counter->data);
1904         if (data)
1905                 events = atomic_xchg(&data->poll, 0);
1906         rcu_read_unlock();
1907
1908         poll_wait(file, &counter->waitq, wait);
1909
1910         return events;
1911 }
1912
1913 static void perf_counter_reset(struct perf_counter *counter)
1914 {
1915         (void)perf_counter_read(counter);
1916         atomic64_set(&counter->count, 0);
1917         perf_counter_update_userpage(counter);
1918 }
1919
1920 /*
1921  * Holding the top-level counter's child_mutex means that any
1922  * descendant process that has inherited this counter will block
1923  * in sync_child_counter if it goes to exit, thus satisfying the
1924  * task existence requirements of perf_counter_enable/disable.
1925  */
1926 static void perf_counter_for_each_child(struct perf_counter *counter,
1927                                         void (*func)(struct perf_counter *))
1928 {
1929         struct perf_counter *child;
1930
1931         WARN_ON_ONCE(counter->ctx->parent_ctx);
1932         mutex_lock(&counter->child_mutex);
1933         func(counter);
1934         list_for_each_entry(child, &counter->child_list, child_list)
1935                 func(child);
1936         mutex_unlock(&counter->child_mutex);
1937 }
1938
1939 static void perf_counter_for_each(struct perf_counter *counter,
1940                                   void (*func)(struct perf_counter *))
1941 {
1942         struct perf_counter_context *ctx = counter->ctx;
1943         struct perf_counter *sibling;
1944
1945         WARN_ON_ONCE(ctx->parent_ctx);
1946         mutex_lock(&ctx->mutex);
1947         counter = counter->group_leader;
1948
1949         perf_counter_for_each_child(counter, func);
1950         func(counter);
1951         list_for_each_entry(sibling, &counter->sibling_list, list_entry)
1952                 perf_counter_for_each_child(counter, func);
1953         mutex_unlock(&ctx->mutex);
1954 }
1955
1956 static int perf_counter_period(struct perf_counter *counter, u64 __user *arg)
1957 {
1958         struct perf_counter_context *ctx = counter->ctx;
1959         unsigned long size;
1960         int ret = 0;
1961         u64 value;
1962
1963         if (!counter->attr.sample_period)
1964                 return -EINVAL;
1965
1966         size = copy_from_user(&value, arg, sizeof(value));
1967         if (size != sizeof(value))
1968                 return -EFAULT;
1969
1970         if (!value)
1971                 return -EINVAL;
1972
1973         spin_lock_irq(&ctx->lock);
1974         if (counter->attr.freq) {
1975                 if (value > sysctl_perf_counter_sample_rate) {
1976                         ret = -EINVAL;
1977                         goto unlock;
1978                 }
1979
1980                 counter->attr.sample_freq = value;
1981         } else {
1982                 counter->attr.sample_period = value;
1983                 counter->hw.sample_period = value;
1984         }
1985 unlock:
1986         spin_unlock_irq(&ctx->lock);
1987
1988         return ret;
1989 }
1990
1991 int perf_counter_set_output(struct perf_counter *counter, int output_fd);
1992
1993 static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1994 {
1995         struct perf_counter *counter = file->private_data;
1996         void (*func)(struct perf_counter *);
1997         u32 flags = arg;
1998
1999         switch (cmd) {
2000         case PERF_COUNTER_IOC_ENABLE:
2001                 func = perf_counter_enable;
2002                 break;
2003         case PERF_COUNTER_IOC_DISABLE:
2004                 func = perf_counter_disable;
2005                 break;
2006         case PERF_COUNTER_IOC_RESET:
2007                 func = perf_counter_reset;
2008                 break;
2009
2010         case PERF_COUNTER_IOC_REFRESH:
2011                 return perf_counter_refresh(counter, arg);
2012
2013         case PERF_COUNTER_IOC_PERIOD:
2014                 return perf_counter_period(counter, (u64 __user *)arg);
2015
2016         case PERF_COUNTER_IOC_SET_OUTPUT:
2017                 return perf_counter_set_output(counter, arg);
2018
2019         default:
2020                 return -ENOTTY;
2021         }
2022
2023         if (flags & PERF_IOC_FLAG_GROUP)
2024                 perf_counter_for_each(counter, func);
2025         else
2026                 perf_counter_for_each_child(counter, func);
2027
2028         return 0;
2029 }
2030
2031 int perf_counter_task_enable(void)
2032 {
2033         struct perf_counter *counter;
2034
2035         mutex_lock(&current->perf_counter_mutex);
2036         list_for_each_entry(counter, &current->perf_counter_list, owner_entry)
2037                 perf_counter_for_each_child(counter, perf_counter_enable);
2038         mutex_unlock(&current->perf_counter_mutex);
2039
2040         return 0;
2041 }
2042
2043 int perf_counter_task_disable(void)
2044 {
2045         struct perf_counter *counter;
2046
2047         mutex_lock(&current->perf_counter_mutex);
2048         list_for_each_entry(counter, &current->perf_counter_list, owner_entry)
2049                 perf_counter_for_each_child(counter, perf_counter_disable);
2050         mutex_unlock(&current->perf_counter_mutex);
2051
2052         return 0;
2053 }
2054
2055 #ifndef PERF_COUNTER_INDEX_OFFSET
2056 # define PERF_COUNTER_INDEX_OFFSET 0
2057 #endif
2058
2059 static int perf_counter_index(struct perf_counter *counter)
2060 {
2061         if (counter->state != PERF_COUNTER_STATE_ACTIVE)
2062                 return 0;
2063
2064         return counter->hw.idx + 1 - PERF_COUNTER_INDEX_OFFSET;
2065 }
2066
2067 /*
2068  * Callers need to ensure there can be no nesting of this function, otherwise
2069  * the seqlock logic goes bad. We can not serialize this because the arch
2070  * code calls this from NMI context.
2071  */
2072 void perf_counter_update_userpage(struct perf_counter *counter)
2073 {
2074         struct perf_counter_mmap_page *userpg;
2075         struct perf_mmap_data *data;
2076
2077         rcu_read_lock();
2078         data = rcu_dereference(counter->data);
2079         if (!data)
2080                 goto unlock;
2081
2082         userpg = data->user_page;
2083
2084         /*
2085          * Disable preemption so as to not let the corresponding user-space
2086          * spin too long if we get preempted.
2087          */
2088         preempt_disable();
2089         ++userpg->lock;
2090         barrier();
2091         userpg->index = perf_counter_index(counter);
2092         userpg->offset = atomic64_read(&counter->count);
2093         if (counter->state == PERF_COUNTER_STATE_ACTIVE)
2094                 userpg->offset -= atomic64_read(&counter->hw.prev_count);
2095
2096         userpg->time_enabled = counter->total_time_enabled +
2097                         atomic64_read(&counter->child_total_time_enabled);
2098
2099         userpg->time_running = counter->total_time_running +
2100                         atomic64_read(&counter->child_total_time_running);
2101
2102         barrier();
2103         ++userpg->lock;
2104         preempt_enable();
2105 unlock:
2106         rcu_read_unlock();
2107 }
2108
2109 static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
2110 {
2111         struct perf_counter *counter = vma->vm_file->private_data;
2112         struct perf_mmap_data *data;
2113         int ret = VM_FAULT_SIGBUS;
2114
2115         if (vmf->flags & FAULT_FLAG_MKWRITE) {
2116                 if (vmf->pgoff == 0)
2117                         ret = 0;
2118                 return ret;
2119         }
2120
2121         rcu_read_lock();
2122         data = rcu_dereference(counter->data);
2123         if (!data)
2124                 goto unlock;
2125
2126         if (vmf->pgoff == 0) {
2127                 vmf->page = virt_to_page(data->user_page);
2128         } else {
2129                 int nr = vmf->pgoff - 1;
2130
2131                 if ((unsigned)nr > data->nr_pages)
2132                         goto unlock;
2133
2134                 if (vmf->flags & FAULT_FLAG_WRITE)
2135                         goto unlock;
2136
2137                 vmf->page = virt_to_page(data->data_pages[nr]);
2138         }
2139
2140         get_page(vmf->page);
2141         vmf->page->mapping = vma->vm_file->f_mapping;
2142         vmf->page->index   = vmf->pgoff;
2143
2144         ret = 0;
2145 unlock:
2146         rcu_read_unlock();
2147
2148         return ret;
2149 }
2150
2151 static int perf_mmap_data_alloc(struct perf_counter *counter, int nr_pages)
2152 {
2153         struct perf_mmap_data *data;
2154         unsigned long size;
2155         int i;
2156
2157         WARN_ON(atomic_read(&counter->mmap_count));
2158
2159         size = sizeof(struct perf_mmap_data);
2160         size += nr_pages * sizeof(void *);
2161
2162         data = kzalloc(size, GFP_KERNEL);
2163         if (!data)
2164                 goto fail;
2165
2166         data->user_page = (void *)get_zeroed_page(GFP_KERNEL);
2167         if (!data->user_page)
2168                 goto fail_user_page;
2169
2170         for (i = 0; i < nr_pages; i++) {
2171                 data->data_pages[i] = (void *)get_zeroed_page(GFP_KERNEL);
2172                 if (!data->data_pages[i])
2173                         goto fail_data_pages;
2174         }
2175
2176         data->nr_pages = nr_pages;
2177         atomic_set(&data->lock, -1);
2178
2179         rcu_assign_pointer(counter->data, data);
2180
2181         return 0;
2182
2183 fail_data_pages:
2184         for (i--; i >= 0; i--)
2185                 free_page((unsigned long)data->data_pages[i]);
2186
2187         free_page((unsigned long)data->user_page);
2188
2189 fail_user_page:
2190         kfree(data);
2191
2192 fail:
2193         return -ENOMEM;
2194 }
2195
2196 static void perf_mmap_free_page(unsigned long addr)
2197 {
2198         struct page *page = virt_to_page((void *)addr);
2199
2200         page->mapping = NULL;
2201         __free_page(page);
2202 }
2203
2204 static void __perf_mmap_data_free(struct rcu_head *rcu_head)
2205 {
2206         struct perf_mmap_data *data;
2207         int i;
2208
2209         data = container_of(rcu_head, struct perf_mmap_data, rcu_head);
2210
2211         perf_mmap_free_page((unsigned long)data->user_page);
2212         for (i = 0; i < data->nr_pages; i++)
2213                 perf_mmap_free_page((unsigned long)data->data_pages[i]);
2214
2215         kfree(data);
2216 }
2217
2218 static void perf_mmap_data_free(struct perf_counter *counter)
2219 {
2220         struct perf_mmap_data *data = counter->data;
2221
2222         WARN_ON(atomic_read(&counter->mmap_count));
2223
2224         rcu_assign_pointer(counter->data, NULL);
2225         call_rcu(&data->rcu_head, __perf_mmap_data_free);
2226 }
2227
2228 static void perf_mmap_open(struct vm_area_struct *vma)
2229 {
2230         struct perf_counter *counter = vma->vm_file->private_data;
2231
2232         atomic_inc(&counter->mmap_count);
2233 }
2234
2235 static void perf_mmap_close(struct vm_area_struct *vma)
2236 {
2237         struct perf_counter *counter = vma->vm_file->private_data;
2238
2239         WARN_ON_ONCE(counter->ctx->parent_ctx);
2240         if (atomic_dec_and_mutex_lock(&counter->mmap_count, &counter->mmap_mutex)) {
2241                 struct user_struct *user = current_user();
2242
2243                 atomic_long_sub(counter->data->nr_pages + 1, &user->locked_vm);
2244                 vma->vm_mm->locked_vm -= counter->data->nr_locked;
2245                 perf_mmap_data_free(counter);
2246                 mutex_unlock(&counter->mmap_mutex);
2247         }
2248 }
2249
2250 static struct vm_operations_struct perf_mmap_vmops = {
2251         .open           = perf_mmap_open,
2252         .close          = perf_mmap_close,
2253         .fault          = perf_mmap_fault,
2254         .page_mkwrite   = perf_mmap_fault,
2255 };
2256
2257 static int perf_mmap(struct file *file, struct vm_area_struct *vma)
2258 {
2259         struct perf_counter *counter = file->private_data;
2260         unsigned long user_locked, user_lock_limit;
2261         struct user_struct *user = current_user();
2262         unsigned long locked, lock_limit;
2263         unsigned long vma_size;
2264         unsigned long nr_pages;
2265         long user_extra, extra;
2266         int ret = 0;
2267
2268         if (!(vma->vm_flags & VM_SHARED))
2269                 return -EINVAL;
2270
2271         vma_size = vma->vm_end - vma->vm_start;
2272         nr_pages = (vma_size / PAGE_SIZE) - 1;
2273
2274         /*
2275          * If we have data pages ensure they're a power-of-two number, so we
2276          * can do bitmasks instead of modulo.
2277          */
2278         if (nr_pages != 0 && !is_power_of_2(nr_pages))
2279                 return -EINVAL;
2280
2281         if (vma_size != PAGE_SIZE * (1 + nr_pages))
2282                 return -EINVAL;
2283
2284         if (vma->vm_pgoff != 0)
2285                 return -EINVAL;
2286
2287         WARN_ON_ONCE(counter->ctx->parent_ctx);
2288         mutex_lock(&counter->mmap_mutex);
2289         if (counter->output) {
2290                 ret = -EINVAL;
2291                 goto unlock;
2292         }
2293
2294         if (atomic_inc_not_zero(&counter->mmap_count)) {
2295                 if (nr_pages != counter->data->nr_pages)
2296                         ret = -EINVAL;
2297                 goto unlock;
2298         }
2299
2300         user_extra = nr_pages + 1;
2301         user_lock_limit = sysctl_perf_counter_mlock >> (PAGE_SHIFT - 10);
2302
2303         /*
2304          * Increase the limit linearly with more CPUs:
2305          */
2306         user_lock_limit *= num_online_cpus();
2307
2308         user_locked = atomic_long_read(&user->locked_vm) + user_extra;
2309
2310         extra = 0;
2311         if (user_locked > user_lock_limit)
2312                 extra = user_locked - user_lock_limit;
2313
2314         lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
2315         lock_limit >>= PAGE_SHIFT;
2316         locked = vma->vm_mm->locked_vm + extra;
2317
2318         if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
2319                 !capable(CAP_IPC_LOCK)) {
2320                 ret = -EPERM;
2321                 goto unlock;
2322         }
2323
2324         WARN_ON(counter->data);
2325         ret = perf_mmap_data_alloc(counter, nr_pages);
2326         if (ret)
2327                 goto unlock;
2328
2329         atomic_set(&counter->mmap_count, 1);
2330         atomic_long_add(user_extra, &user->locked_vm);
2331         vma->vm_mm->locked_vm += extra;
2332         counter->data->nr_locked = extra;
2333         if (vma->vm_flags & VM_WRITE)
2334                 counter->data->writable = 1;
2335
2336 unlock:
2337         mutex_unlock(&counter->mmap_mutex);
2338
2339         vma->vm_flags |= VM_RESERVED;
2340         vma->vm_ops = &perf_mmap_vmops;
2341
2342         return ret;
2343 }
2344
2345 static int perf_fasync(int fd, struct file *filp, int on)
2346 {
2347         struct inode *inode = filp->f_path.dentry->d_inode;
2348         struct perf_counter *counter = filp->private_data;
2349         int retval;
2350
2351         mutex_lock(&inode->i_mutex);
2352         retval = fasync_helper(fd, filp, on, &counter->fasync);
2353         mutex_unlock(&inode->i_mutex);
2354
2355         if (retval < 0)
2356                 return retval;
2357
2358         return 0;
2359 }
2360
2361 static const struct file_operations perf_fops = {
2362         .release                = perf_release,
2363         .read                   = perf_read,
2364         .poll                   = perf_poll,
2365         .unlocked_ioctl         = perf_ioctl,
2366         .compat_ioctl           = perf_ioctl,
2367         .mmap                   = perf_mmap,
2368         .fasync                 = perf_fasync,
2369 };
2370
2371 /*
2372  * Perf counter wakeup
2373  *
2374  * If there's data, ensure we set the poll() state and publish everything
2375  * to user-space before waking everybody up.
2376  */
2377
2378 void perf_counter_wakeup(struct perf_counter *counter)
2379 {
2380         wake_up_all(&counter->waitq);
2381
2382         if (counter->pending_kill) {
2383                 kill_fasync(&counter->fasync, SIGIO, counter->pending_kill);
2384                 counter->pending_kill = 0;
2385         }
2386 }
2387
2388 /*
2389  * Pending wakeups
2390  *
2391  * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
2392  *
2393  * The NMI bit means we cannot possibly take locks. Therefore, maintain a
2394  * single linked list and use cmpxchg() to add entries lockless.
2395  */
2396
2397 static void perf_pending_counter(struct perf_pending_entry *entry)
2398 {
2399         struct perf_counter *counter = container_of(entry,
2400                         struct perf_counter, pending);
2401
2402         if (counter->pending_disable) {
2403                 counter->pending_disable = 0;
2404                 __perf_counter_disable(counter);
2405         }
2406
2407         if (counter->pending_wakeup) {
2408                 counter->pending_wakeup = 0;
2409                 perf_counter_wakeup(counter);
2410         }
2411 }
2412
2413 #define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
2414
2415 static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
2416         PENDING_TAIL,
2417 };
2418
2419 static void perf_pending_queue(struct perf_pending_entry *entry,
2420                                void (*func)(struct perf_pending_entry *))
2421 {
2422         struct perf_pending_entry **head;
2423
2424         if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
2425                 return;
2426
2427         entry->func = func;
2428
2429         head = &get_cpu_var(perf_pending_head);
2430
2431         do {
2432                 entry->next = *head;
2433         } while (cmpxchg(head, entry->next, entry) != entry->next);
2434
2435         set_perf_counter_pending();
2436
2437         put_cpu_var(perf_pending_head);
2438 }
2439
2440 static int __perf_pending_run(void)
2441 {
2442         struct perf_pending_entry *list;
2443         int nr = 0;
2444
2445         list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
2446         while (list != PENDING_TAIL) {
2447                 void (*func)(struct perf_pending_entry *);
2448                 struct perf_pending_entry *entry = list;
2449
2450                 list = list->next;
2451
2452                 func = entry->func;
2453                 entry->next = NULL;
2454                 /*
2455                  * Ensure we observe the unqueue before we issue the wakeup,
2456                  * so that we won't be waiting forever.
2457                  * -- see perf_not_pending().
2458                  */
2459                 smp_wmb();
2460
2461                 func(entry);
2462                 nr++;
2463         }
2464
2465         return nr;
2466 }
2467
2468 static inline int perf_not_pending(struct perf_counter *counter)
2469 {
2470         /*
2471          * If we flush on whatever cpu we run, there is a chance we don't
2472          * need to wait.
2473          */
2474         get_cpu();
2475         __perf_pending_run();
2476         put_cpu();
2477
2478         /*
2479          * Ensure we see the proper queue state before going to sleep
2480          * so that we do not miss the wakeup. -- see perf_pending_handle()
2481          */
2482         smp_rmb();
2483         return counter->pending.next == NULL;
2484 }
2485
2486 static void perf_pending_sync(struct perf_counter *counter)
2487 {
2488         wait_event(counter->waitq, perf_not_pending(counter));
2489 }
2490
2491 void perf_counter_do_pending(void)
2492 {
2493         __perf_pending_run();
2494 }
2495
2496 /*
2497  * Callchain support -- arch specific
2498  */
2499
2500 __weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
2501 {
2502         return NULL;
2503 }
2504
2505 /*
2506  * Output
2507  */
2508
2509 struct perf_output_handle {
2510         struct perf_counter     *counter;
2511         struct perf_mmap_data   *data;
2512         unsigned long           head;
2513         unsigned long           offset;
2514         int                     nmi;
2515         int                     sample;
2516         int                     locked;
2517         unsigned long           flags;
2518 };
2519
2520 static bool perf_output_space(struct perf_mmap_data *data,
2521                               unsigned int offset, unsigned int head)
2522 {
2523         unsigned long tail;
2524         unsigned long mask;
2525
2526         if (!data->writable)
2527                 return true;
2528
2529         mask = (data->nr_pages << PAGE_SHIFT) - 1;
2530         /*
2531          * Userspace could choose to issue a mb() before updating the tail
2532          * pointer. So that all reads will be completed before the write is
2533          * issued.
2534          */
2535         tail = ACCESS_ONCE(data->user_page->data_tail);
2536         smp_rmb();
2537
2538         offset = (offset - tail) & mask;
2539         head   = (head   - tail) & mask;
2540
2541         if ((int)(head - offset) < 0)
2542                 return false;
2543
2544         return true;
2545 }
2546
2547 static void perf_output_wakeup(struct perf_output_handle *handle)
2548 {
2549         atomic_set(&handle->data->poll, POLL_IN);
2550
2551         if (handle->nmi) {
2552                 handle->counter->pending_wakeup = 1;
2553                 perf_pending_queue(&handle->counter->pending,
2554                                    perf_pending_counter);
2555         } else
2556                 perf_counter_wakeup(handle->counter);
2557 }
2558
2559 /*
2560  * Curious locking construct.
2561  *
2562  * We need to ensure a later event doesn't publish a head when a former
2563  * event isn't done writing. However since we need to deal with NMIs we
2564  * cannot fully serialize things.
2565  *
2566  * What we do is serialize between CPUs so we only have to deal with NMI
2567  * nesting on a single CPU.
2568  *
2569  * We only publish the head (and generate a wakeup) when the outer-most
2570  * event completes.
2571  */
2572 static void perf_output_lock(struct perf_output_handle *handle)
2573 {
2574         struct perf_mmap_data *data = handle->data;
2575         int cpu;
2576
2577         handle->locked = 0;
2578
2579         local_irq_save(handle->flags);
2580         cpu = smp_processor_id();
2581
2582         if (in_nmi() && atomic_read(&data->lock) == cpu)
2583                 return;
2584
2585         while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
2586                 cpu_relax();
2587
2588         handle->locked = 1;
2589 }
2590
2591 static void perf_output_unlock(struct perf_output_handle *handle)
2592 {
2593         struct perf_mmap_data *data = handle->data;
2594         unsigned long head;
2595         int cpu;
2596
2597         data->done_head = data->head;
2598
2599         if (!handle->locked)
2600                 goto out;
2601
2602 again:
2603         /*
2604          * The xchg implies a full barrier that ensures all writes are done
2605          * before we publish the new head, matched by a rmb() in userspace when
2606          * reading this position.
2607          */
2608         while ((head = atomic_long_xchg(&data->done_head, 0)))
2609                 data->user_page->data_head = head;
2610
2611         /*
2612          * NMI can happen here, which means we can miss a done_head update.
2613          */
2614
2615         cpu = atomic_xchg(&data->lock, -1);
2616         WARN_ON_ONCE(cpu != smp_processor_id());
2617
2618         /*
2619          * Therefore we have to validate we did not indeed do so.
2620          */
2621         if (unlikely(atomic_long_read(&data->done_head))) {
2622                 /*
2623                  * Since we had it locked, we can lock it again.
2624                  */
2625                 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
2626                         cpu_relax();
2627
2628                 goto again;
2629         }
2630
2631         if (atomic_xchg(&data->wakeup, 0))
2632                 perf_output_wakeup(handle);
2633 out:
2634         local_irq_restore(handle->flags);
2635 }
2636
2637 static void perf_output_copy(struct perf_output_handle *handle,
2638                              const void *buf, unsigned int len)
2639 {
2640         unsigned int pages_mask;
2641         unsigned int offset;
2642         unsigned int size;
2643         void **pages;
2644
2645         offset          = handle->offset;
2646         pages_mask      = handle->data->nr_pages - 1;
2647         pages           = handle->data->data_pages;
2648
2649         do {
2650                 unsigned int page_offset;
2651                 int nr;
2652
2653                 nr          = (offset >> PAGE_SHIFT) & pages_mask;
2654                 page_offset = offset & (PAGE_SIZE - 1);
2655                 size        = min_t(unsigned int, PAGE_SIZE - page_offset, len);
2656
2657                 memcpy(pages[nr] + page_offset, buf, size);
2658
2659                 len         -= size;
2660                 buf         += size;
2661                 offset      += size;
2662         } while (len);
2663
2664         handle->offset = offset;
2665
2666         /*
2667          * Check we didn't copy past our reservation window, taking the
2668          * possible unsigned int wrap into account.
2669          */
2670         WARN_ON_ONCE(((long)(handle->head - handle->offset)) < 0);
2671 }
2672
2673 #define perf_output_put(handle, x) \
2674         perf_output_copy((handle), &(x), sizeof(x))
2675
2676 static int perf_output_begin(struct perf_output_handle *handle,
2677                              struct perf_counter *counter, unsigned int size,
2678                              int nmi, int sample)
2679 {
2680         struct perf_counter *output_counter;
2681         struct perf_mmap_data *data;
2682         unsigned int offset, head;
2683         int have_lost;
2684         struct {
2685                 struct perf_event_header header;
2686                 u64                      id;
2687                 u64                      lost;
2688         } lost_event;
2689
2690         rcu_read_lock();
2691         /*
2692          * For inherited counters we send all the output towards the parent.
2693          */
2694         if (counter->parent)
2695                 counter = counter->parent;
2696
2697         output_counter = rcu_dereference(counter->output);
2698         if (output_counter)
2699                 counter = output_counter;
2700
2701         data = rcu_dereference(counter->data);
2702         if (!data)
2703                 goto out;
2704
2705         handle->data    = data;
2706         handle->counter = counter;
2707         handle->nmi     = nmi;
2708         handle->sample  = sample;
2709
2710         if (!data->nr_pages)
2711                 goto fail;
2712
2713         have_lost = atomic_read(&data->lost);
2714         if (have_lost)
2715                 size += sizeof(lost_event);
2716
2717         perf_output_lock(handle);
2718
2719         do {
2720                 offset = head = atomic_long_read(&data->head);
2721                 head += size;
2722                 if (unlikely(!perf_output_space(data, offset, head)))
2723                         goto fail;
2724         } while (atomic_long_cmpxchg(&data->head, offset, head) != offset);
2725
2726         handle->offset  = offset;
2727         handle->head    = head;
2728
2729         if ((offset >> PAGE_SHIFT) != (head >> PAGE_SHIFT))
2730                 atomic_set(&data->wakeup, 1);
2731
2732         if (have_lost) {
2733                 lost_event.header.type = PERF_EVENT_LOST;
2734                 lost_event.header.misc = 0;
2735                 lost_event.header.size = sizeof(lost_event);
2736                 lost_event.id          = counter->id;
2737                 lost_event.lost        = atomic_xchg(&data->lost, 0);
2738
2739                 perf_output_put(handle, lost_event);
2740         }
2741
2742         return 0;
2743
2744 fail:
2745         atomic_inc(&data->lost);
2746         perf_output_unlock(handle);
2747 out:
2748         rcu_read_unlock();
2749
2750         return -ENOSPC;
2751 }
2752
2753 static void perf_output_end(struct perf_output_handle *handle)
2754 {
2755         struct perf_counter *counter = handle->counter;
2756         struct perf_mmap_data *data = handle->data;
2757
2758         int wakeup_events = counter->attr.wakeup_events;
2759
2760         if (handle->sample && wakeup_events) {
2761                 int events = atomic_inc_return(&data->events);
2762                 if (events >= wakeup_events) {
2763                         atomic_sub(wakeup_events, &data->events);
2764                         atomic_set(&data->wakeup, 1);
2765                 }
2766         }
2767
2768         perf_output_unlock(handle);
2769         rcu_read_unlock();
2770 }
2771
2772 static u32 perf_counter_pid(struct perf_counter *counter, struct task_struct *p)
2773 {
2774         /*
2775          * only top level counters have the pid namespace they were created in
2776          */
2777         if (counter->parent)
2778                 counter = counter->parent;
2779
2780         return task_tgid_nr_ns(p, counter->ns);
2781 }
2782
2783 static u32 perf_counter_tid(struct perf_counter *counter, struct task_struct *p)
2784 {
2785         /*
2786          * only top level counters have the pid namespace they were created in
2787          */
2788         if (counter->parent)
2789                 counter = counter->parent;
2790
2791         return task_pid_nr_ns(p, counter->ns);
2792 }
2793
2794 static void perf_output_read_one(struct perf_output_handle *handle,
2795                                  struct perf_counter *counter)
2796 {
2797         u64 read_format = counter->attr.read_format;
2798         u64 values[4];
2799         int n = 0;
2800
2801         values[n++] = atomic64_read(&counter->count);
2802         if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
2803                 values[n++] = counter->total_time_enabled +
2804                         atomic64_read(&counter->child_total_time_enabled);
2805         }
2806         if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
2807                 values[n++] = counter->total_time_running +
2808                         atomic64_read(&counter->child_total_time_running);
2809         }
2810         if (read_format & PERF_FORMAT_ID)
2811                 values[n++] = primary_counter_id(counter);
2812
2813         perf_output_copy(handle, values, n * sizeof(u64));
2814 }
2815
2816 /*
2817  * XXX PERF_FORMAT_GROUP vs inherited counters seems difficult.
2818  */
2819 static void perf_output_read_group(struct perf_output_handle *handle,
2820                             struct perf_counter *counter)
2821 {
2822         struct perf_counter *leader = counter->group_leader, *sub;
2823         u64 read_format = counter->attr.read_format;
2824         u64 values[5];
2825         int n = 0;
2826
2827         values[n++] = 1 + leader->nr_siblings;
2828
2829         if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2830                 values[n++] = leader->total_time_enabled;
2831
2832         if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2833                 values[n++] = leader->total_time_running;
2834
2835         if (leader != counter)
2836                 leader->pmu->read(leader);
2837
2838         values[n++] = atomic64_read(&leader->count);
2839         if (read_format & PERF_FORMAT_ID)
2840                 values[n++] = primary_counter_id(leader);
2841
2842         perf_output_copy(handle, values, n * sizeof(u64));
2843
2844         list_for_each_entry(sub, &leader->sibling_list, list_entry) {
2845                 n = 0;
2846
2847                 if (sub != counter)
2848                         sub->pmu->read(sub);
2849
2850                 values[n++] = atomic64_read(&sub->count);
2851                 if (read_format & PERF_FORMAT_ID)
2852                         values[n++] = primary_counter_id(sub);
2853
2854                 perf_output_copy(handle, values, n * sizeof(u64));
2855         }
2856 }
2857
2858 static void perf_output_read(struct perf_output_handle *handle,
2859                              struct perf_counter *counter)
2860 {
2861         if (counter->attr.read_format & PERF_FORMAT_GROUP)
2862                 perf_output_read_group(handle, counter);
2863         else
2864                 perf_output_read_one(handle, counter);
2865 }
2866
2867 void perf_counter_output(struct perf_counter *counter, int nmi,
2868                                 struct perf_sample_data *data)
2869 {
2870         int ret;
2871         u64 sample_type = counter->attr.sample_type;
2872         struct perf_output_handle handle;
2873         struct perf_event_header header;
2874         u64 ip;
2875         struct {
2876                 u32 pid, tid;
2877         } tid_entry;
2878         struct perf_callchain_entry *callchain = NULL;
2879         int callchain_size = 0;
2880         u64 time;
2881         struct {
2882                 u32 cpu, reserved;
2883         } cpu_entry;
2884
2885         header.type = PERF_EVENT_SAMPLE;
2886         header.size = sizeof(header);
2887
2888         header.misc = 0;
2889         header.misc |= perf_misc_flags(data->regs);
2890
2891         if (sample_type & PERF_SAMPLE_IP) {
2892                 ip = perf_instruction_pointer(data->regs);
2893                 header.size += sizeof(ip);
2894         }
2895
2896         if (sample_type & PERF_SAMPLE_TID) {
2897                 /* namespace issues */
2898                 tid_entry.pid = perf_counter_pid(counter, current);
2899                 tid_entry.tid = perf_counter_tid(counter, current);
2900
2901                 header.size += sizeof(tid_entry);
2902         }
2903
2904         if (sample_type & PERF_SAMPLE_TIME) {
2905                 /*
2906                  * Maybe do better on x86 and provide cpu_clock_nmi()
2907                  */
2908                 time = sched_clock();
2909
2910                 header.size += sizeof(u64);
2911         }
2912
2913         if (sample_type & PERF_SAMPLE_ADDR)
2914                 header.size += sizeof(u64);
2915
2916         if (sample_type & PERF_SAMPLE_ID)
2917                 header.size += sizeof(u64);
2918
2919         if (sample_type & PERF_SAMPLE_STREAM_ID)
2920                 header.size += sizeof(u64);
2921
2922         if (sample_type & PERF_SAMPLE_CPU) {
2923                 header.size += sizeof(cpu_entry);
2924
2925                 cpu_entry.cpu = raw_smp_processor_id();
2926                 cpu_entry.reserved = 0;
2927         }
2928
2929         if (sample_type & PERF_SAMPLE_PERIOD)
2930                 header.size += sizeof(u64);
2931
2932         if (sample_type & PERF_SAMPLE_READ)
2933                 header.size += perf_counter_read_size(counter);
2934
2935         if (sample_type & PERF_SAMPLE_CALLCHAIN) {
2936                 callchain = perf_callchain(data->regs);
2937
2938                 if (callchain) {
2939                         callchain_size = (1 + callchain->nr) * sizeof(u64);
2940                         header.size += callchain_size;
2941                 } else
2942                         header.size += sizeof(u64);
2943         }
2944
2945         if (sample_type & PERF_SAMPLE_RAW) {
2946                 int size = sizeof(u32);
2947
2948                 if (data->raw)
2949                         size += data->raw->size;
2950                 else
2951                         size += sizeof(u32);
2952
2953                 WARN_ON_ONCE(size & (sizeof(u64)-1));
2954                 header.size += size;
2955         }
2956
2957         ret = perf_output_begin(&handle, counter, header.size, nmi, 1);
2958         if (ret)
2959                 return;
2960
2961         perf_output_put(&handle, header);
2962
2963         if (sample_type & PERF_SAMPLE_IP)
2964                 perf_output_put(&handle, ip);
2965
2966         if (sample_type & PERF_SAMPLE_TID)
2967                 perf_output_put(&handle, tid_entry);
2968
2969         if (sample_type & PERF_SAMPLE_TIME)
2970                 perf_output_put(&handle, time);
2971
2972         if (sample_type & PERF_SAMPLE_ADDR)
2973                 perf_output_put(&handle, data->addr);
2974
2975         if (sample_type & PERF_SAMPLE_ID) {
2976                 u64 id = primary_counter_id(counter);
2977
2978                 perf_output_put(&handle, id);
2979         }
2980
2981         if (sample_type & PERF_SAMPLE_STREAM_ID)
2982                 perf_output_put(&handle, counter->id);
2983
2984         if (sample_type & PERF_SAMPLE_CPU)
2985                 perf_output_put(&handle, cpu_entry);
2986
2987         if (sample_type & PERF_SAMPLE_PERIOD)
2988                 perf_output_put(&handle, data->period);
2989
2990         if (sample_type & PERF_SAMPLE_READ)
2991                 perf_output_read(&handle, counter);
2992
2993         if (sample_type & PERF_SAMPLE_CALLCHAIN) {
2994                 if (callchain)
2995                         perf_output_copy(&handle, callchain, callchain_size);
2996                 else {
2997                         u64 nr = 0;
2998                         perf_output_put(&handle, nr);
2999                 }
3000         }
3001
3002         if (sample_type & PERF_SAMPLE_RAW) {
3003                 if (data->raw) {
3004                         perf_output_put(&handle, data->raw->size);
3005                         perf_output_copy(&handle, data->raw->data, data->raw->size);
3006                 } else {
3007                         struct {
3008                                 u32     size;
3009                                 u32     data;
3010                         } raw = {
3011                                 .size = sizeof(u32),
3012                                 .data = 0,
3013                         };
3014                         perf_output_put(&handle, raw);
3015                 }
3016         }
3017
3018         perf_output_end(&handle);
3019 }
3020
3021 /*
3022  * read event
3023  */
3024
3025 struct perf_read_event {
3026         struct perf_event_header        header;
3027
3028         u32                             pid;
3029         u32                             tid;
3030 };
3031
3032 static void
3033 perf_counter_read_event(struct perf_counter *counter,
3034                         struct task_struct *task)
3035 {
3036         struct perf_output_handle handle;
3037         struct perf_read_event event = {
3038                 .header = {
3039                         .type = PERF_EVENT_READ,
3040                         .misc = 0,
3041                         .size = sizeof(event) + perf_counter_read_size(counter),
3042                 },
3043                 .pid = perf_counter_pid(counter, task),
3044                 .tid = perf_counter_tid(counter, task),
3045         };
3046         int ret;
3047
3048         ret = perf_output_begin(&handle, counter, event.header.size, 0, 0);
3049         if (ret)
3050                 return;
3051
3052         perf_output_put(&handle, event);
3053         perf_output_read(&handle, counter);
3054
3055         perf_output_end(&handle);
3056 }
3057
3058 /*
3059  * task tracking -- fork/exit
3060  *
3061  * enabled by: attr.comm | attr.mmap | attr.task
3062  */
3063
3064 struct perf_task_event {
3065         struct task_struct              *task;
3066         struct perf_counter_context     *task_ctx;
3067
3068         struct {
3069                 struct perf_event_header        header;
3070
3071                 u32                             pid;
3072                 u32                             ppid;
3073                 u32                             tid;
3074                 u32                             ptid;
3075         } event;
3076 };
3077
3078 static void perf_counter_task_output(struct perf_counter *counter,
3079                                      struct perf_task_event *task_event)
3080 {
3081         struct perf_output_handle handle;
3082         int size = task_event->event.header.size;
3083         struct task_struct *task = task_event->task;
3084         int ret = perf_output_begin(&handle, counter, size, 0, 0);
3085
3086         if (ret)
3087                 return;
3088
3089         task_event->event.pid = perf_counter_pid(counter, task);
3090         task_event->event.ppid = perf_counter_pid(counter, current);
3091
3092         task_event->event.tid = perf_counter_tid(counter, task);
3093         task_event->event.ptid = perf_counter_tid(counter, current);
3094
3095         perf_output_put(&handle, task_event->event);
3096         perf_output_end(&handle);
3097 }
3098
3099 static int perf_counter_task_match(struct perf_counter *counter)
3100 {
3101         if (counter->attr.comm || counter->attr.mmap || counter->attr.task)
3102                 return 1;
3103
3104         return 0;
3105 }
3106
3107 static void perf_counter_task_ctx(struct perf_counter_context *ctx,
3108                                   struct perf_task_event *task_event)
3109 {
3110         struct perf_counter *counter;
3111
3112         if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
3113                 return;
3114
3115         rcu_read_lock();
3116         list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
3117                 if (perf_counter_task_match(counter))
3118                         perf_counter_task_output(counter, task_event);
3119         }
3120         rcu_read_unlock();
3121 }
3122
3123 static void perf_counter_task_event(struct perf_task_event *task_event)
3124 {
3125         struct perf_cpu_context *cpuctx;
3126         struct perf_counter_context *ctx = task_event->task_ctx;
3127
3128         cpuctx = &get_cpu_var(perf_cpu_context);
3129         perf_counter_task_ctx(&cpuctx->ctx, task_event);
3130         put_cpu_var(perf_cpu_context);
3131
3132         rcu_read_lock();
3133         if (!ctx)
3134                 ctx = rcu_dereference(task_event->task->perf_counter_ctxp);
3135         if (ctx)
3136                 perf_counter_task_ctx(ctx, task_event);
3137         rcu_read_unlock();
3138 }
3139
3140 static void perf_counter_task(struct task_struct *task,
3141                               struct perf_counter_context *task_ctx,
3142                               int new)
3143 {
3144         struct perf_task_event task_event;
3145
3146         if (!atomic_read(&nr_comm_counters) &&
3147             !atomic_read(&nr_mmap_counters) &&
3148             !atomic_read(&nr_task_counters))
3149                 return;
3150
3151         task_event = (struct perf_task_event){
3152                 .task     = task,
3153                 .task_ctx = task_ctx,
3154                 .event    = {
3155                         .header = {
3156                                 .type = new ? PERF_EVENT_FORK : PERF_EVENT_EXIT,
3157                                 .misc = 0,
3158                                 .size = sizeof(task_event.event),
3159                         },
3160                         /* .pid  */
3161                         /* .ppid */
3162                         /* .tid  */
3163                         /* .ptid */
3164                 },
3165         };
3166
3167         perf_counter_task_event(&task_event);
3168 }
3169
3170 void perf_counter_fork(struct task_struct *task)
3171 {
3172         perf_counter_task(task, NULL, 1);
3173 }
3174
3175 /*
3176  * comm tracking
3177  */
3178
3179 struct perf_comm_event {
3180         struct task_struct      *task;
3181         char                    *comm;
3182         int                     comm_size;
3183
3184         struct {
3185                 struct perf_event_header        header;
3186
3187                 u32                             pid;
3188                 u32                             tid;
3189         } event;
3190 };
3191
3192 static void perf_counter_comm_output(struct perf_counter *counter,
3193                                      struct perf_comm_event *comm_event)
3194 {
3195         struct perf_output_handle handle;
3196         int size = comm_event->event.header.size;
3197         int ret = perf_output_begin(&handle, counter, size, 0, 0);
3198
3199         if (ret)
3200                 return;
3201
3202         comm_event->event.pid = perf_counter_pid(counter, comm_event->task);
3203         comm_event->event.tid = perf_counter_tid(counter, comm_event->task);
3204
3205         perf_output_put(&handle, comm_event->event);
3206         perf_output_copy(&handle, comm_event->comm,
3207                                    comm_event->comm_size);
3208         perf_output_end(&handle);
3209 }
3210
3211 static int perf_counter_comm_match(struct perf_counter *counter)
3212 {
3213         if (counter->attr.comm)
3214                 return 1;
3215
3216         return 0;
3217 }
3218
3219 static void perf_counter_comm_ctx(struct perf_counter_context *ctx,
3220                                   struct perf_comm_event *comm_event)
3221 {
3222         struct perf_counter *counter;
3223
3224         if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
3225                 return;
3226
3227         rcu_read_lock();
3228         list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
3229                 if (perf_counter_comm_match(counter))
3230                         perf_counter_comm_output(counter, comm_event);
3231         }
3232         rcu_read_unlock();
3233 }
3234
3235 static void perf_counter_comm_event(struct perf_comm_event *comm_event)
3236 {
3237         struct perf_cpu_context *cpuctx;
3238         struct perf_counter_context *ctx;
3239         unsigned int size;
3240         char comm[TASK_COMM_LEN];
3241
3242         memset(comm, 0, sizeof(comm));
3243         strncpy(comm, comm_event->task->comm, sizeof(comm));
3244         size = ALIGN(strlen(comm)+1, sizeof(u64));
3245
3246         comm_event->comm = comm;
3247         comm_event->comm_size = size;
3248
3249         comm_event->event.header.size = sizeof(comm_event->event) + size;
3250
3251         cpuctx = &get_cpu_var(perf_cpu_context);
3252         perf_counter_comm_ctx(&cpuctx->ctx, comm_event);
3253         put_cpu_var(perf_cpu_context);
3254
3255         rcu_read_lock();
3256         /*
3257          * doesn't really matter which of the child contexts the
3258          * events ends up in.
3259          */
3260         ctx = rcu_dereference(current->perf_counter_ctxp);
3261         if (ctx)
3262                 perf_counter_comm_ctx(ctx, comm_event);
3263         rcu_read_unlock();
3264 }
3265
3266 void perf_counter_comm(struct task_struct *task)
3267 {
3268         struct perf_comm_event comm_event;
3269
3270         if (task->perf_counter_ctxp)
3271                 perf_counter_enable_on_exec(task);
3272
3273         if (!atomic_read(&nr_comm_counters))
3274                 return;
3275
3276         comm_event = (struct perf_comm_event){
3277                 .task   = task,
3278                 /* .comm      */
3279                 /* .comm_size */
3280                 .event  = {
3281                         .header = {
3282                                 .type = PERF_EVENT_COMM,
3283                                 .misc = 0,
3284                                 /* .size */
3285                         },
3286                         /* .pid */
3287                         /* .tid */
3288                 },
3289         };
3290
3291         perf_counter_comm_event(&comm_event);
3292 }
3293
3294 /*
3295  * mmap tracking
3296  */
3297
3298 struct perf_mmap_event {
3299         struct vm_area_struct   *vma;
3300
3301         const char              *file_name;
3302         int                     file_size;
3303
3304         struct {
3305                 struct perf_event_header        header;
3306
3307                 u32                             pid;
3308                 u32                             tid;
3309                 u64                             start;
3310                 u64                             len;
3311                 u64                             pgoff;
3312         } event;
3313 };
3314
3315 static void perf_counter_mmap_output(struct perf_counter *counter,
3316                                      struct perf_mmap_event *mmap_event)
3317 {
3318         struct perf_output_handle handle;
3319         int size = mmap_event->event.header.size;
3320         int ret = perf_output_begin(&handle, counter, size, 0, 0);
3321
3322         if (ret)
3323                 return;
3324
3325         mmap_event->event.pid = perf_counter_pid(counter, current);
3326         mmap_event->event.tid = perf_counter_tid(counter, current);
3327
3328         perf_output_put(&handle, mmap_event->event);
3329         perf_output_copy(&handle, mmap_event->file_name,
3330                                    mmap_event->file_size);
3331         perf_output_end(&handle);
3332 }
3333
3334 static int perf_counter_mmap_match(struct perf_counter *counter,
3335                                    struct perf_mmap_event *mmap_event)
3336 {
3337         if (counter->attr.mmap)
3338                 return 1;
3339
3340         return 0;
3341 }
3342
3343 static void perf_counter_mmap_ctx(struct perf_counter_context *ctx,
3344                                   struct perf_mmap_event *mmap_event)
3345 {
3346         struct perf_counter *counter;
3347
3348         if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
3349                 return;
3350
3351         rcu_read_lock();
3352         list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
3353                 if (perf_counter_mmap_match(counter, mmap_event))
3354                         perf_counter_mmap_output(counter, mmap_event);
3355         }
3356         rcu_read_unlock();
3357 }
3358
3359 static void perf_counter_mmap_event(struct perf_mmap_event *mmap_event)
3360 {
3361         struct perf_cpu_context *cpuctx;
3362         struct perf_counter_context *ctx;
3363         struct vm_area_struct *vma = mmap_event->vma;
3364         struct file *file = vma->vm_file;
3365         unsigned int size;
3366         char tmp[16];
3367         char *buf = NULL;
3368         const char *name;
3369
3370         memset(tmp, 0, sizeof(tmp));
3371
3372         if (file) {
3373                 /*
3374                  * d_path works from the end of the buffer backwards, so we
3375                  * need to add enough zero bytes after the string to handle
3376                  * the 64bit alignment we do later.
3377                  */
3378                 buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
3379                 if (!buf) {
3380                         name = strncpy(tmp, "//enomem", sizeof(tmp));
3381                         goto got_name;
3382                 }
3383                 name = d_path(&file->f_path, buf, PATH_MAX);
3384                 if (IS_ERR(name)) {
3385                         name = strncpy(tmp, "//toolong", sizeof(tmp));
3386                         goto got_name;
3387                 }
3388         } else {
3389                 if (arch_vma_name(mmap_event->vma)) {
3390                         name = strncpy(tmp, arch_vma_name(mmap_event->vma),
3391                                        sizeof(tmp));
3392                         goto got_name;
3393                 }
3394
3395                 if (!vma->vm_mm) {
3396                         name = strncpy(tmp, "[vdso]", sizeof(tmp));
3397                         goto got_name;
3398                 }
3399
3400                 name = strncpy(tmp, "//anon", sizeof(tmp));
3401                 goto got_name;
3402         }
3403
3404 got_name:
3405         size = ALIGN(strlen(name)+1, sizeof(u64));
3406
3407         mmap_event->file_name = name;
3408         mmap_event->file_size = size;
3409
3410         mmap_event->event.header.size = sizeof(mmap_event->event) + size;
3411
3412         cpuctx = &get_cpu_var(perf_cpu_context);
3413         perf_counter_mmap_ctx(&cpuctx->ctx, mmap_event);
3414         put_cpu_var(perf_cpu_context);
3415
3416         rcu_read_lock();
3417         /*
3418          * doesn't really matter which of the child contexts the
3419          * events ends up in.
3420          */
3421         ctx = rcu_dereference(current->perf_counter_ctxp);
3422         if (ctx)
3423                 perf_counter_mmap_ctx(ctx, mmap_event);
3424         rcu_read_unlock();
3425
3426         kfree(buf);
3427 }
3428
3429 void __perf_counter_mmap(struct vm_area_struct *vma)
3430 {
3431         struct perf_mmap_event mmap_event;
3432
3433         if (!atomic_read(&nr_mmap_counters))
3434                 return;
3435
3436         mmap_event = (struct perf_mmap_event){
3437                 .vma    = vma,
3438                 /* .file_name */
3439                 /* .file_size */
3440                 .event  = {
3441                         .header = {
3442                                 .type = PERF_EVENT_MMAP,
3443                                 .misc = 0,
3444                                 /* .size */
3445                         },
3446                         /* .pid */
3447                         /* .tid */
3448                         .start  = vma->vm_start,
3449                         .len    = vma->vm_end - vma->vm_start,
3450                         .pgoff  = vma->vm_pgoff,
3451                 },
3452         };
3453
3454         perf_counter_mmap_event(&mmap_event);
3455 }
3456
3457 /*
3458  * IRQ throttle logging
3459  */
3460
3461 static void perf_log_throttle(struct perf_counter *counter, int enable)
3462 {
3463         struct perf_output_handle handle;
3464         int ret;
3465
3466         struct {
3467                 struct perf_event_header        header;
3468                 u64                             time;
3469                 u64                             id;
3470                 u64                             stream_id;
3471         } throttle_event = {
3472                 .header = {
3473                         .type = PERF_EVENT_THROTTLE,
3474                         .misc = 0,
3475                         .size = sizeof(throttle_event),
3476                 },
3477                 .time           = sched_clock(),
3478                 .id             = primary_counter_id(counter),
3479                 .stream_id      = counter->id,
3480         };
3481
3482         if (enable)
3483                 throttle_event.header.type = PERF_EVENT_UNTHROTTLE;
3484
3485         ret = perf_output_begin(&handle, counter, sizeof(throttle_event), 1, 0);
3486         if (ret)
3487                 return;
3488
3489         perf_output_put(&handle, throttle_event);
3490         perf_output_end(&handle);
3491 }
3492
3493 /*
3494  * Generic counter overflow handling, sampling.
3495  */
3496
3497 static int __perf_counter_overflow(struct perf_counter *counter, int nmi,
3498                                    int throttle, struct perf_sample_data *data)
3499 {
3500         int events = atomic_read(&counter->event_limit);
3501         struct hw_perf_counter *hwc = &counter->hw;
3502         int ret = 0;
3503
3504         throttle = (throttle && counter->pmu->unthrottle != NULL);
3505
3506         if (!throttle) {
3507                 hwc->interrupts++;
3508         } else {
3509                 if (hwc->interrupts != MAX_INTERRUPTS) {
3510                         hwc->interrupts++;
3511                         if (HZ * hwc->interrupts >
3512                                         (u64)sysctl_perf_counter_sample_rate) {
3513                                 hwc->interrupts = MAX_INTERRUPTS;
3514                                 perf_log_throttle(counter, 0);
3515                                 ret = 1;
3516                         }
3517                 } else {
3518                         /*
3519                          * Keep re-disabling counters even though on the previous
3520                          * pass we disabled it - just in case we raced with a
3521                          * sched-in and the counter got enabled again:
3522                          */
3523                         ret = 1;
3524                 }
3525         }
3526
3527         if (counter->attr.freq) {
3528                 u64 now = sched_clock();
3529                 s64 delta = now - hwc->freq_stamp;
3530
3531                 hwc->freq_stamp = now;
3532
3533                 if (delta > 0 && delta < TICK_NSEC)
3534                         perf_adjust_period(counter, NSEC_PER_SEC / (int)delta);
3535         }
3536
3537         /*
3538          * XXX event_limit might not quite work as expected on inherited
3539          * counters
3540          */
3541
3542         counter->pending_kill = POLL_IN;
3543         if (events && atomic_dec_and_test(&counter->event_limit)) {
3544                 ret = 1;
3545                 counter->pending_kill = POLL_HUP;
3546                 if (nmi) {
3547                         counter->pending_disable = 1;
3548                         perf_pending_queue(&counter->pending,
3549                                            perf_pending_counter);
3550                 } else
3551                         perf_counter_disable(counter);
3552         }
3553
3554         perf_counter_output(counter, nmi, data);
3555         return ret;
3556 }
3557
3558 int perf_counter_overflow(struct perf_counter *counter, int nmi,
3559                           struct perf_sample_data *data)
3560 {
3561         return __perf_counter_overflow(counter, nmi, 1, data);
3562 }
3563
3564 /*
3565  * Generic software counter infrastructure
3566  */
3567
3568 /*
3569  * We directly increment counter->count and keep a second value in
3570  * counter->hw.period_left to count intervals. This period counter
3571  * is kept in the range [-sample_period, 0] so that we can use the
3572  * sign as trigger.
3573  */
3574
3575 static u64 perf_swcounter_set_period(struct perf_counter *counter)
3576 {
3577         struct hw_perf_counter *hwc = &counter->hw;
3578         u64 period = hwc->last_period;
3579         u64 nr, offset;
3580         s64 old, val;
3581
3582         hwc->last_period = hwc->sample_period;
3583
3584 again:
3585         old = val = atomic64_read(&hwc->period_left);
3586         if (val < 0)
3587                 return 0;
3588
3589         nr = div64_u64(period + val, period);
3590         offset = nr * period;
3591         val -= offset;
3592         if (atomic64_cmpxchg(&hwc->period_left, old, val) != old)
3593                 goto again;
3594
3595         return nr;
3596 }
3597
3598 static void perf_swcounter_overflow(struct perf_counter *counter,
3599                                     int nmi, struct perf_sample_data *data)
3600 {
3601         struct hw_perf_counter *hwc = &counter->hw;
3602         int throttle = 0;
3603         u64 overflow;
3604
3605         data->period = counter->hw.last_period;
3606         overflow = perf_swcounter_set_period(counter);
3607
3608         if (hwc->interrupts == MAX_INTERRUPTS)
3609                 return;
3610
3611         for (; overflow; overflow--) {
3612                 if (__perf_counter_overflow(counter, nmi, throttle, data)) {
3613                         /*
3614                          * We inhibit the overflow from happening when
3615                          * hwc->interrupts == MAX_INTERRUPTS.
3616                          */
3617                         break;
3618                 }
3619                 throttle = 0;
3620         }
3621 }
3622
3623 static void perf_swcounter_unthrottle(struct perf_counter *counter)
3624 {
3625         /*
3626          * Nothing to do, we already reset hwc->interrupts.
3627          */
3628 }
3629
3630 static void perf_swcounter_add(struct perf_counter *counter, u64 nr,
3631                                int nmi, struct perf_sample_data *data)
3632 {
3633         struct hw_perf_counter *hwc = &counter->hw;
3634
3635         atomic64_add(nr, &counter->count);
3636
3637         if (!hwc->sample_period)
3638                 return;
3639
3640         if (!data->regs)
3641                 return;
3642
3643         if (!atomic64_add_negative(nr, &hwc->period_left))
3644                 perf_swcounter_overflow(counter, nmi, data);
3645 }
3646
3647 static int perf_swcounter_is_counting(struct perf_counter *counter)
3648 {
3649         /*
3650          * The counter is active, we're good!
3651          */
3652         if (counter->state == PERF_COUNTER_STATE_ACTIVE)
3653                 return 1;
3654
3655         /*
3656          * The counter is off/error, not counting.
3657          */
3658         if (counter->state != PERF_COUNTER_STATE_INACTIVE)
3659                 return 0;
3660
3661         /*
3662          * The counter is inactive, if the context is active
3663          * we're part of a group that didn't make it on the 'pmu',
3664          * not counting.
3665          */
3666         if (counter->ctx->is_active)
3667                 return 0;
3668
3669         /*
3670          * We're inactive and the context is too, this means the
3671          * task is scheduled out, we're counting events that happen
3672          * to us, like migration events.
3673          */
3674         return 1;
3675 }
3676
3677 static int perf_swcounter_match(struct perf_counter *counter,
3678                                 enum perf_type_id type,
3679                                 u32 event, struct pt_regs *regs)
3680 {
3681         if (!perf_swcounter_is_counting(counter))
3682                 return 0;
3683
3684         if (counter->attr.type != type)
3685                 return 0;
3686         if (counter->attr.config != event)
3687                 return 0;
3688
3689         if (regs) {
3690                 if (counter->attr.exclude_user && user_mode(regs))
3691                         return 0;
3692
3693                 if (counter->attr.exclude_kernel && !user_mode(regs))
3694                         return 0;
3695         }
3696
3697         return 1;
3698 }
3699
3700 static void perf_swcounter_ctx_event(struct perf_counter_context *ctx,
3701                                      enum perf_type_id type,
3702                                      u32 event, u64 nr, int nmi,
3703                                      struct perf_sample_data *data)
3704 {
3705         struct perf_counter *counter;
3706
3707         if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
3708                 return;
3709
3710         rcu_read_lock();
3711         list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
3712                 if (perf_swcounter_match(counter, type, event, data->regs))
3713                         perf_swcounter_add(counter, nr, nmi, data);
3714         }
3715         rcu_read_unlock();
3716 }
3717
3718 static int *perf_swcounter_recursion_context(struct perf_cpu_context *cpuctx)
3719 {
3720         if (in_nmi())
3721                 return &cpuctx->recursion[3];
3722
3723         if (in_irq())
3724                 return &cpuctx->recursion[2];
3725
3726         if (in_softirq())
3727                 return &cpuctx->recursion[1];
3728
3729         return &cpuctx->recursion[0];
3730 }
3731
3732 static void do_perf_swcounter_event(enum perf_type_id type, u32 event,
3733                                     u64 nr, int nmi,
3734                                     struct perf_sample_data *data)
3735 {
3736         struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
3737         int *recursion = perf_swcounter_recursion_context(cpuctx);
3738         struct perf_counter_context *ctx;
3739
3740         if (*recursion)
3741                 goto out;
3742
3743         (*recursion)++;
3744         barrier();
3745
3746         perf_swcounter_ctx_event(&cpuctx->ctx, type, event,
3747                                  nr, nmi, data);
3748         rcu_read_lock();
3749         /*
3750          * doesn't really matter which of the child contexts the
3751          * events ends up in.
3752          */
3753         ctx = rcu_dereference(current->perf_counter_ctxp);
3754         if (ctx)
3755                 perf_swcounter_ctx_event(ctx, type, event, nr, nmi, data);
3756         rcu_read_unlock();
3757
3758         barrier();
3759         (*recursion)--;
3760
3761 out:
3762         put_cpu_var(perf_cpu_context);
3763 }
3764
3765 void __perf_swcounter_event(u32 event, u64 nr, int nmi,
3766                             struct pt_regs *regs, u64 addr)
3767 {
3768         struct perf_sample_data data = {
3769                 .regs = regs,
3770                 .addr = addr,
3771         };
3772
3773         do_perf_swcounter_event(PERF_TYPE_SOFTWARE, event, nr, nmi, &data);
3774 }
3775
3776 static void perf_swcounter_read(struct perf_counter *counter)
3777 {
3778 }
3779
3780 static int perf_swcounter_enable(struct perf_counter *counter)
3781 {
3782         struct hw_perf_counter *hwc = &counter->hw;
3783
3784         if (hwc->sample_period) {
3785                 hwc->last_period = hwc->sample_period;
3786                 perf_swcounter_set_period(counter);
3787         }
3788         return 0;
3789 }
3790
3791 static void perf_swcounter_disable(struct perf_counter *counter)
3792 {
3793 }
3794
3795 static const struct pmu perf_ops_generic = {
3796         .enable         = perf_swcounter_enable,
3797         .disable        = perf_swcounter_disable,
3798         .read           = perf_swcounter_read,
3799         .unthrottle     = perf_swcounter_unthrottle,
3800 };
3801
3802 /*
3803  * hrtimer based swcounter callback
3804  */
3805
3806 static enum hrtimer_restart perf_swcounter_hrtimer(struct hrtimer *hrtimer)
3807 {
3808         enum hrtimer_restart ret = HRTIMER_RESTART;
3809         struct perf_sample_data data;
3810         struct perf_counter *counter;
3811         u64 period;
3812
3813         counter = container_of(hrtimer, struct perf_counter, hw.hrtimer);
3814         counter->pmu->read(counter);
3815
3816         data.addr = 0;
3817         data.regs = get_irq_regs();
3818         /*
3819          * In case we exclude kernel IPs or are somehow not in interrupt
3820          * context, provide the next best thing, the user IP.
3821          */
3822         if ((counter->attr.exclude_kernel || !data.regs) &&
3823                         !counter->attr.exclude_user)
3824                 data.regs = task_pt_regs(current);
3825
3826         if (data.regs) {
3827                 if (perf_counter_overflow(counter, 0, &data))
3828                         ret = HRTIMER_NORESTART;
3829         }
3830
3831         period = max_t(u64, 10000, counter->hw.sample_period);
3832         hrtimer_forward_now(hrtimer, ns_to_ktime(period));
3833
3834         return ret;
3835 }
3836
3837 /*
3838  * Software counter: cpu wall time clock
3839  */
3840
3841 static void cpu_clock_perf_counter_update(struct perf_counter *counter)
3842 {
3843         int cpu = raw_smp_processor_id();
3844         s64 prev;
3845         u64 now;
3846
3847         now = cpu_clock(cpu);
3848         prev = atomic64_read(&counter->hw.prev_count);
3849         atomic64_set(&counter->hw.prev_count, now);
3850         atomic64_add(now - prev, &counter->count);
3851 }
3852
3853 static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
3854 {
3855         struct hw_perf_counter *hwc = &counter->hw;
3856         int cpu = raw_smp_processor_id();
3857
3858         atomic64_set(&hwc->prev_count, cpu_clock(cpu));
3859         hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3860         hwc->hrtimer.function = perf_swcounter_hrtimer;
3861         if (hwc->sample_period) {
3862                 u64 period = max_t(u64, 10000, hwc->sample_period);
3863                 __hrtimer_start_range_ns(&hwc->hrtimer,
3864                                 ns_to_ktime(period), 0,
3865                                 HRTIMER_MODE_REL, 0);
3866         }
3867
3868         return 0;
3869 }
3870
3871 static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
3872 {
3873         if (counter->hw.sample_period)
3874                 hrtimer_cancel(&counter->hw.hrtimer);
3875         cpu_clock_perf_counter_update(counter);
3876 }
3877
3878 static void cpu_clock_perf_counter_read(struct perf_counter *counter)
3879 {
3880         cpu_clock_perf_counter_update(counter);
3881 }
3882
3883 static const struct pmu perf_ops_cpu_clock = {
3884         .enable         = cpu_clock_perf_counter_enable,
3885         .disable        = cpu_clock_perf_counter_disable,
3886         .read           = cpu_clock_perf_counter_read,
3887 };
3888
3889 /*
3890  * Software counter: task time clock
3891  */
3892
3893 static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
3894 {
3895         u64 prev;
3896         s64 delta;
3897
3898         prev = atomic64_xchg(&counter->hw.prev_count, now);
3899         delta = now - prev;
3900         atomic64_add(delta, &counter->count);
3901 }
3902
3903 static int task_clock_perf_counter_enable(struct perf_counter *counter)
3904 {
3905         struct hw_perf_counter *hwc = &counter->hw;
3906         u64 now;
3907
3908         now = counter->ctx->time;
3909
3910         atomic64_set(&hwc->prev_count, now);
3911         hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3912         hwc->hrtimer.function = perf_swcounter_hrtimer;
3913         if (hwc->sample_period) {
3914                 u64 period = max_t(u64, 10000, hwc->sample_period);
3915                 __hrtimer_start_range_ns(&hwc->hrtimer,
3916                                 ns_to_ktime(period), 0,
3917                                 HRTIMER_MODE_REL, 0);
3918         }
3919
3920         return 0;
3921 }
3922
3923 static void task_clock_perf_counter_disable(struct perf_counter *counter)
3924 {
3925         if (counter->hw.sample_period)
3926                 hrtimer_cancel(&counter->hw.hrtimer);
3927         task_clock_perf_counter_update(counter, counter->ctx->time);
3928
3929 }
3930
3931 static void task_clock_perf_counter_read(struct perf_counter *counter)
3932 {
3933         u64 time;
3934
3935         if (!in_nmi()) {
3936                 update_context_time(counter->ctx);
3937                 time = counter->ctx->time;
3938         } else {
3939                 u64 now = perf_clock();
3940                 u64 delta = now - counter->ctx->timestamp;
3941                 time = counter->ctx->time + delta;
3942         }
3943
3944         task_clock_perf_counter_update(counter, time);
3945 }
3946
3947 static const struct pmu perf_ops_task_clock = {
3948         .enable         = task_clock_perf_counter_enable,
3949         .disable        = task_clock_perf_counter_disable,
3950         .read           = task_clock_perf_counter_read,
3951 };
3952
3953 #ifdef CONFIG_EVENT_PROFILE
3954 void perf_tpcounter_event(int event_id, u64 addr, u64 count, void *record,
3955                           int entry_size)
3956 {
3957         struct perf_raw_record raw = {
3958                 .size = entry_size,
3959                 .data = record,
3960         };
3961
3962         struct perf_sample_data data = {
3963                 .regs = get_irq_regs(),
3964                 .addr = addr,
3965                 .raw = &raw,
3966         };
3967
3968         if (!data.regs)
3969                 data.regs = task_pt_regs(current);
3970
3971         do_perf_swcounter_event(PERF_TYPE_TRACEPOINT, event_id, count, 1, &data);
3972 }
3973 EXPORT_SYMBOL_GPL(perf_tpcounter_event);
3974
3975 extern int ftrace_profile_enable(int);
3976 extern void ftrace_profile_disable(int);
3977
3978 static void tp_perf_counter_destroy(struct perf_counter *counter)
3979 {
3980         ftrace_profile_disable(counter->attr.config);
3981 }
3982
3983 static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
3984 {
3985         /*
3986          * Raw tracepoint data is a severe data leak, only allow root to
3987          * have these.
3988          */
3989         if ((counter->attr.sample_type & PERF_SAMPLE_RAW) &&
3990                         perf_paranoid_tracepoint_raw() &&
3991                         !capable(CAP_SYS_ADMIN))
3992                 return ERR_PTR(-EPERM);
3993
3994         if (ftrace_profile_enable(counter->attr.config))
3995                 return NULL;
3996
3997         counter->destroy = tp_perf_counter_destroy;
3998
3999         return &perf_ops_generic;
4000 }
4001 #else
4002 static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
4003 {
4004         return NULL;
4005 }
4006 #endif
4007
4008 atomic_t perf_swcounter_enabled[PERF_COUNT_SW_MAX];
4009
4010 static void sw_perf_counter_destroy(struct perf_counter *counter)
4011 {
4012         u64 event = counter->attr.config;
4013
4014         WARN_ON(counter->parent);
4015
4016         atomic_dec(&perf_swcounter_enabled[event]);
4017 }
4018
4019 static const struct pmu *sw_perf_counter_init(struct perf_counter *counter)
4020 {
4021         const struct pmu *pmu = NULL;
4022         u64 event = counter->attr.config;
4023
4024         /*
4025          * Software counters (currently) can't in general distinguish
4026          * between user, kernel and hypervisor events.
4027          * However, context switches and cpu migrations are considered
4028          * to be kernel events, and page faults are never hypervisor
4029          * events.
4030          */
4031         switch (event) {
4032         case PERF_COUNT_SW_CPU_CLOCK:
4033                 pmu = &perf_ops_cpu_clock;
4034
4035                 break;
4036         case PERF_COUNT_SW_TASK_CLOCK:
4037                 /*
4038                  * If the user instantiates this as a per-cpu counter,
4039                  * use the cpu_clock counter instead.
4040                  */
4041                 if (counter->ctx->task)
4042                         pmu = &perf_ops_task_clock;
4043                 else
4044                         pmu = &perf_ops_cpu_clock;
4045
4046                 break;
4047         case PERF_COUNT_SW_PAGE_FAULTS:
4048         case PERF_COUNT_SW_PAGE_FAULTS_MIN:
4049         case PERF_COUNT_SW_PAGE_FAULTS_MAJ:
4050         case PERF_COUNT_SW_CONTEXT_SWITCHES:
4051         case PERF_COUNT_SW_CPU_MIGRATIONS:
4052                 if (!counter->parent) {
4053                         atomic_inc(&perf_swcounter_enabled[event]);
4054                         counter->destroy = sw_perf_counter_destroy;
4055                 }
4056                 pmu = &perf_ops_generic;
4057                 break;
4058         }
4059
4060         return pmu;
4061 }
4062
4063 /*
4064  * Allocate and initialize a counter structure
4065  */
4066 static struct perf_counter *
4067 perf_counter_alloc(struct perf_counter_attr *attr,
4068                    int cpu,
4069                    struct perf_counter_context *ctx,
4070                    struct perf_counter *group_leader,
4071                    struct perf_counter *parent_counter,
4072                    gfp_t gfpflags)
4073 {
4074         const struct pmu *pmu;
4075         struct perf_counter *counter;
4076         struct hw_perf_counter *hwc;
4077         long err;
4078
4079         counter = kzalloc(sizeof(*counter), gfpflags);
4080         if (!counter)
4081                 return ERR_PTR(-ENOMEM);
4082
4083         /*
4084          * Single counters are their own group leaders, with an
4085          * empty sibling list:
4086          */
4087         if (!group_leader)
4088                 group_leader = counter;
4089
4090         mutex_init(&counter->child_mutex);
4091         INIT_LIST_HEAD(&counter->child_list);
4092
4093         INIT_LIST_HEAD(&counter->list_entry);
4094         INIT_LIST_HEAD(&counter->event_entry);
4095         INIT_LIST_HEAD(&counter->sibling_list);
4096         init_waitqueue_head(&counter->waitq);
4097
4098         mutex_init(&counter->mmap_mutex);
4099
4100         counter->cpu            = cpu;
4101         counter->attr           = *attr;
4102         counter->group_leader   = group_leader;
4103         counter->pmu            = NULL;
4104         counter->ctx            = ctx;
4105         counter->oncpu          = -1;
4106
4107         counter->parent         = parent_counter;
4108
4109         counter->ns             = get_pid_ns(current->nsproxy->pid_ns);
4110         counter->id             = atomic64_inc_return(&perf_counter_id);
4111
4112         counter->state          = PERF_COUNTER_STATE_INACTIVE;
4113
4114         if (attr->disabled)
4115                 counter->state = PERF_COUNTER_STATE_OFF;
4116
4117         pmu = NULL;
4118
4119         hwc = &counter->hw;
4120         hwc->sample_period = attr->sample_period;
4121         if (attr->freq && attr->sample_freq)
4122                 hwc->sample_period = 1;
4123         hwc->last_period = hwc->sample_period;
4124
4125         atomic64_set(&hwc->period_left, hwc->sample_period);
4126
4127         /*
4128          * we currently do not support PERF_FORMAT_GROUP on inherited counters
4129          */
4130         if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
4131                 goto done;
4132
4133         switch (attr->type) {
4134         case PERF_TYPE_RAW:
4135         case PERF_TYPE_HARDWARE:
4136         case PERF_TYPE_HW_CACHE:
4137                 pmu = hw_perf_counter_init(counter);
4138                 break;
4139
4140         case PERF_TYPE_SOFTWARE:
4141                 pmu = sw_perf_counter_init(counter);
4142                 break;
4143
4144         case PERF_TYPE_TRACEPOINT:
4145                 pmu = tp_perf_counter_init(counter);
4146                 break;
4147
4148         default:
4149                 break;
4150         }
4151 done:
4152         err = 0;
4153         if (!pmu)
4154                 err = -EINVAL;
4155         else if (IS_ERR(pmu))
4156                 err = PTR_ERR(pmu);
4157
4158         if (err) {
4159                 if (counter->ns)
4160                         put_pid_ns(counter->ns);
4161                 kfree(counter);
4162                 return ERR_PTR(err);
4163         }
4164
4165         counter->pmu = pmu;
4166
4167         if (!counter->parent) {
4168                 atomic_inc(&nr_counters);
4169                 if (counter->attr.mmap)
4170                         atomic_inc(&nr_mmap_counters);
4171                 if (counter->attr.comm)
4172                         atomic_inc(&nr_comm_counters);
4173                 if (counter->attr.task)
4174                         atomic_inc(&nr_task_counters);
4175         }
4176
4177         return counter;
4178 }
4179
4180 static int perf_copy_attr(struct perf_counter_attr __user *uattr,
4181                           struct perf_counter_attr *attr)
4182 {
4183         int ret;
4184         u32 size;
4185
4186         if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
4187                 return -EFAULT;
4188
4189         /*
4190          * zero the full structure, so that a short copy will be nice.
4191          */
4192         memset(attr, 0, sizeof(*attr));
4193
4194         ret = get_user(size, &uattr->size);
4195         if (ret)
4196                 return ret;
4197
4198         if (size > PAGE_SIZE)   /* silly large */
4199                 goto err_size;
4200
4201         if (!size)              /* abi compat */
4202                 size = PERF_ATTR_SIZE_VER0;
4203
4204         if (size < PERF_ATTR_SIZE_VER0)
4205                 goto err_size;
4206
4207         /*
4208          * If we're handed a bigger struct than we know of,
4209          * ensure all the unknown bits are 0.
4210          */
4211         if (size > sizeof(*attr)) {
4212                 unsigned long val;
4213                 unsigned long __user *addr;
4214                 unsigned long __user *end;
4215
4216                 addr = PTR_ALIGN((void __user *)uattr + sizeof(*attr),
4217                                 sizeof(unsigned long));
4218                 end  = PTR_ALIGN((void __user *)uattr + size,
4219                                 sizeof(unsigned long));
4220
4221                 for (; addr < end; addr += sizeof(unsigned long)) {
4222                         ret = get_user(val, addr);
4223                         if (ret)
4224                                 return ret;
4225                         if (val)
4226                                 goto err_size;
4227                 }
4228         }
4229
4230         ret = copy_from_user(attr, uattr, size);
4231         if (ret)
4232                 return -EFAULT;
4233
4234         /*
4235          * If the type exists, the corresponding creation will verify
4236          * the attr->config.
4237          */
4238         if (attr->type >= PERF_TYPE_MAX)
4239                 return -EINVAL;
4240
4241         if (attr->__reserved_1 || attr->__reserved_2 || attr->__reserved_3)
4242                 return -EINVAL;
4243
4244         if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
4245                 return -EINVAL;
4246
4247         if (attr->read_format & ~(PERF_FORMAT_MAX-1))
4248                 return -EINVAL;
4249
4250 out:
4251         return ret;
4252
4253 err_size:
4254         put_user(sizeof(*attr), &uattr->size);
4255         ret = -E2BIG;
4256         goto out;
4257 }
4258
4259 int perf_counter_set_output(struct perf_counter *counter, int output_fd)
4260 {
4261         struct perf_counter *output_counter = NULL;
4262         struct file *output_file = NULL;
4263         struct perf_counter *old_output;
4264         int fput_needed = 0;
4265         int ret = -EINVAL;
4266
4267         if (!output_fd)
4268                 goto set;
4269
4270         output_file = fget_light(output_fd, &fput_needed);
4271         if (!output_file)
4272                 return -EBADF;
4273
4274         if (output_file->f_op != &perf_fops)
4275                 goto out;
4276
4277         output_counter = output_file->private_data;
4278
4279         /* Don't chain output fds */
4280         if (output_counter->output)
4281                 goto out;
4282
4283         /* Don't set an output fd when we already have an output channel */
4284         if (counter->data)
4285                 goto out;
4286
4287         atomic_long_inc(&output_file->f_count);
4288
4289 set:
4290         mutex_lock(&counter->mmap_mutex);
4291         old_output = counter->output;
4292         rcu_assign_pointer(counter->output, output_counter);
4293         mutex_unlock(&counter->mmap_mutex);
4294
4295         if (old_output) {
4296                 /*
4297                  * we need to make sure no existing perf_output_*()
4298                  * is still referencing this counter.
4299                  */
4300                 synchronize_rcu();
4301                 fput(old_output->filp);
4302         }
4303
4304         ret = 0;
4305 out:
4306         fput_light(output_file, fput_needed);
4307         return ret;
4308 }
4309
4310 /**
4311  * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
4312  *
4313  * @attr_uptr:  event type attributes for monitoring/sampling
4314  * @pid:                target pid
4315  * @cpu:                target cpu
4316  * @group_fd:           group leader counter fd
4317  */
4318 SYSCALL_DEFINE5(perf_counter_open,
4319                 struct perf_counter_attr __user *, attr_uptr,
4320                 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
4321 {
4322         struct perf_counter *counter, *group_leader;
4323         struct perf_counter_attr attr;
4324         struct perf_counter_context *ctx;
4325         struct file *counter_file = NULL;
4326         struct file *group_file = NULL;
4327         int fput_needed = 0;
4328         int fput_needed2 = 0;
4329         int err;
4330
4331         /* for future expandability... */
4332         if (flags & ~(PERF_FLAG_FD_NO_GROUP | PERF_FLAG_FD_OUTPUT))
4333                 return -EINVAL;
4334
4335         err = perf_copy_attr(attr_uptr, &attr);
4336         if (err)
4337                 return err;
4338
4339         if (!attr.exclude_kernel) {
4340                 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
4341                         return -EACCES;
4342         }
4343
4344         if (attr.freq) {
4345                 if (attr.sample_freq > sysctl_perf_counter_sample_rate)
4346                         return -EINVAL;
4347         }
4348
4349         /*
4350          * Get the target context (task or percpu):
4351          */
4352         ctx = find_get_context(pid, cpu);
4353         if (IS_ERR(ctx))
4354                 return PTR_ERR(ctx);
4355
4356         /*
4357          * Look up the group leader (we will attach this counter to it):
4358          */
4359         group_leader = NULL;
4360         if (group_fd != -1 && !(flags & PERF_FLAG_FD_NO_GROUP)) {
4361                 err = -EINVAL;
4362                 group_file = fget_light(group_fd, &fput_needed);
4363                 if (!group_file)
4364                         goto err_put_context;
4365                 if (group_file->f_op != &perf_fops)
4366                         goto err_put_context;
4367
4368                 group_leader = group_file->private_data;
4369                 /*
4370                  * Do not allow a recursive hierarchy (this new sibling
4371                  * becoming part of another group-sibling):
4372                  */
4373                 if (group_leader->group_leader != group_leader)
4374                         goto err_put_context;
4375                 /*
4376                  * Do not allow to attach to a group in a different
4377                  * task or CPU context:
4378                  */
4379                 if (group_leader->ctx != ctx)
4380                         goto err_put_context;
4381                 /*
4382                  * Only a group leader can be exclusive or pinned
4383                  */
4384                 if (attr.exclusive || attr.pinned)
4385                         goto err_put_context;
4386         }
4387
4388         counter = perf_counter_alloc(&attr, cpu, ctx, group_leader,
4389                                      NULL, GFP_KERNEL);
4390         err = PTR_ERR(counter);
4391         if (IS_ERR(counter))
4392                 goto err_put_context;
4393
4394         err = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
4395         if (err < 0)
4396                 goto err_free_put_context;
4397
4398         counter_file = fget_light(err, &fput_needed2);
4399         if (!counter_file)
4400                 goto err_free_put_context;
4401
4402         if (flags & PERF_FLAG_FD_OUTPUT) {
4403                 err = perf_counter_set_output(counter, group_fd);
4404                 if (err)
4405                         goto err_fput_free_put_context;
4406         }
4407
4408         counter->filp = counter_file;
4409         WARN_ON_ONCE(ctx->parent_ctx);
4410         mutex_lock(&ctx->mutex);
4411         perf_install_in_context(ctx, counter, cpu);
4412         ++ctx->generation;
4413         mutex_unlock(&ctx->mutex);
4414
4415         counter->owner = current;
4416         get_task_struct(current);
4417         mutex_lock(&current->perf_counter_mutex);
4418         list_add_tail(&counter->owner_entry, &current->perf_counter_list);
4419         mutex_unlock(&current->perf_counter_mutex);
4420
4421 err_fput_free_put_context:
4422         fput_light(counter_file, fput_needed2);
4423
4424 err_free_put_context:
4425         if (err < 0)
4426                 kfree(counter);
4427
4428 err_put_context:
4429         if (err < 0)
4430                 put_ctx(ctx);
4431
4432         fput_light(group_file, fput_needed);
4433
4434         return err;
4435 }
4436
4437 /*
4438  * inherit a counter from parent task to child task:
4439  */
4440 static struct perf_counter *
4441 inherit_counter(struct perf_counter *parent_counter,
4442               struct task_struct *parent,
4443               struct perf_counter_context *parent_ctx,
4444               struct task_struct *child,
4445               struct perf_counter *group_leader,
4446               struct perf_counter_context *child_ctx)
4447 {
4448         struct perf_counter *child_counter;
4449
4450         /*
4451          * Instead of creating recursive hierarchies of counters,
4452          * we link inherited counters back to the original parent,
4453          * which has a filp for sure, which we use as the reference
4454          * count:
4455          */
4456         if (parent_counter->parent)
4457                 parent_counter = parent_counter->parent;
4458
4459         child_counter = perf_counter_alloc(&parent_counter->attr,
4460                                            parent_counter->cpu, child_ctx,
4461                                            group_leader, parent_counter,
4462                                            GFP_KERNEL);
4463         if (IS_ERR(child_counter))
4464                 return child_counter;
4465         get_ctx(child_ctx);
4466
4467         /*
4468          * Make the child state follow the state of the parent counter,
4469          * not its attr.disabled bit.  We hold the parent's mutex,
4470          * so we won't race with perf_counter_{en, dis}able_family.
4471          */
4472         if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE)
4473                 child_counter->state = PERF_COUNTER_STATE_INACTIVE;
4474         else
4475                 child_counter->state = PERF_COUNTER_STATE_OFF;
4476
4477         if (parent_counter->attr.freq)
4478                 child_counter->hw.sample_period = parent_counter->hw.sample_period;
4479
4480         /*
4481          * Link it up in the child's context:
4482          */
4483         add_counter_to_ctx(child_counter, child_ctx);
4484
4485         /*
4486          * Get a reference to the parent filp - we will fput it
4487          * when the child counter exits. This is safe to do because
4488          * we are in the parent and we know that the filp still
4489          * exists and has a nonzero count:
4490          */
4491         atomic_long_inc(&parent_counter->filp->f_count);
4492
4493         /*
4494          * Link this into the parent counter's child list
4495          */
4496         WARN_ON_ONCE(parent_counter->ctx->parent_ctx);
4497         mutex_lock(&parent_counter->child_mutex);
4498         list_add_tail(&child_counter->child_list, &parent_counter->child_list);
4499         mutex_unlock(&parent_counter->child_mutex);
4500
4501         return child_counter;
4502 }
4503
4504 static int inherit_group(struct perf_counter *parent_counter,
4505               struct task_struct *parent,
4506               struct perf_counter_context *parent_ctx,
4507               struct task_struct *child,
4508               struct perf_counter_context *child_ctx)
4509 {
4510         struct perf_counter *leader;
4511         struct perf_counter *sub;
4512         struct perf_counter *child_ctr;
4513
4514         leader = inherit_counter(parent_counter, parent, parent_ctx,
4515                                  child, NULL, child_ctx);
4516         if (IS_ERR(leader))
4517                 return PTR_ERR(leader);
4518         list_for_each_entry(sub, &parent_counter->sibling_list, list_entry) {
4519                 child_ctr = inherit_counter(sub, parent, parent_ctx,
4520                                             child, leader, child_ctx);
4521                 if (IS_ERR(child_ctr))
4522                         return PTR_ERR(child_ctr);
4523         }
4524         return 0;
4525 }
4526
4527 static void sync_child_counter(struct perf_counter *child_counter,
4528                                struct task_struct *child)
4529 {
4530         struct perf_counter *parent_counter = child_counter->parent;
4531         u64 child_val;
4532
4533         if (child_counter->attr.inherit_stat)
4534                 perf_counter_read_event(child_counter, child);
4535
4536         child_val = atomic64_read(&child_counter->count);
4537
4538         /*
4539          * Add back the child's count to the parent's count:
4540          */
4541         atomic64_add(child_val, &parent_counter->count);
4542         atomic64_add(child_counter->total_time_enabled,
4543                      &parent_counter->child_total_time_enabled);
4544         atomic64_add(child_counter->total_time_running,
4545                      &parent_counter->child_total_time_running);
4546
4547         /*
4548          * Remove this counter from the parent's list
4549          */
4550         WARN_ON_ONCE(parent_counter->ctx->parent_ctx);
4551         mutex_lock(&parent_counter->child_mutex);
4552         list_del_init(&child_counter->child_list);
4553         mutex_unlock(&parent_counter->child_mutex);
4554
4555         /*
4556          * Release the parent counter, if this was the last
4557          * reference to it.
4558          */
4559         fput(parent_counter->filp);
4560 }
4561
4562 static void
4563 __perf_counter_exit_task(struct perf_counter *child_counter,
4564                          struct perf_counter_context *child_ctx,
4565                          struct task_struct *child)
4566 {
4567         struct perf_counter *parent_counter;
4568
4569         update_counter_times(child_counter);
4570         perf_counter_remove_from_context(child_counter);
4571
4572         parent_counter = child_counter->parent;
4573         /*
4574          * It can happen that parent exits first, and has counters
4575          * that are still around due to the child reference. These
4576          * counters need to be zapped - but otherwise linger.
4577          */
4578         if (parent_counter) {
4579                 sync_child_counter(child_counter, child);
4580                 free_counter(child_counter);
4581         }
4582 }
4583
4584 /*
4585  * When a child task exits, feed back counter values to parent counters.
4586  */
4587 void perf_counter_exit_task(struct task_struct *child)
4588 {
4589         struct perf_counter *child_counter, *tmp;
4590         struct perf_counter_context *child_ctx;
4591         unsigned long flags;
4592
4593         if (likely(!child->perf_counter_ctxp)) {
4594                 perf_counter_task(child, NULL, 0);
4595                 return;
4596         }
4597
4598         local_irq_save(flags);
4599         /*
4600          * We can't reschedule here because interrupts are disabled,
4601          * and either child is current or it is a task that can't be
4602          * scheduled, so we are now safe from rescheduling changing
4603          * our context.
4604          */
4605         child_ctx = child->perf_counter_ctxp;
4606         __perf_counter_task_sched_out(child_ctx);
4607
4608         /*
4609          * Take the context lock here so that if find_get_context is
4610          * reading child->perf_counter_ctxp, we wait until it has
4611          * incremented the context's refcount before we do put_ctx below.
4612          */
4613         spin_lock(&child_ctx->lock);
4614         child->perf_counter_ctxp = NULL;
4615         /*
4616          * If this context is a clone; unclone it so it can't get
4617          * swapped to another process while we're removing all
4618          * the counters from it.
4619          */
4620         unclone_ctx(child_ctx);
4621         spin_unlock_irqrestore(&child_ctx->lock, flags);
4622
4623         /*
4624          * Report the task dead after unscheduling the counters so that we
4625          * won't get any samples after PERF_EVENT_EXIT. We can however still
4626          * get a few PERF_EVENT_READ events.
4627          */
4628         perf_counter_task(child, child_ctx, 0);
4629
4630         /*
4631          * We can recurse on the same lock type through:
4632          *
4633          *   __perf_counter_exit_task()
4634          *     sync_child_counter()
4635          *       fput(parent_counter->filp)
4636          *         perf_release()
4637          *           mutex_lock(&ctx->mutex)
4638          *
4639          * But since its the parent context it won't be the same instance.
4640          */
4641         mutex_lock_nested(&child_ctx->mutex, SINGLE_DEPTH_NESTING);
4642
4643 again:
4644         list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
4645                                  list_entry)
4646                 __perf_counter_exit_task(child_counter, child_ctx, child);
4647
4648         /*
4649          * If the last counter was a group counter, it will have appended all
4650          * its siblings to the list, but we obtained 'tmp' before that which
4651          * will still point to the list head terminating the iteration.
4652          */
4653         if (!list_empty(&child_ctx->counter_list))
4654                 goto again;
4655
4656         mutex_unlock(&child_ctx->mutex);
4657
4658         put_ctx(child_ctx);
4659 }
4660
4661 /*
4662  * free an unexposed, unused context as created by inheritance by
4663  * init_task below, used by fork() in case of fail.
4664  */
4665 void perf_counter_free_task(struct task_struct *task)
4666 {
4667         struct perf_counter_context *ctx = task->perf_counter_ctxp;
4668         struct perf_counter *counter, *tmp;
4669
4670         if (!ctx)
4671                 return;
4672
4673         mutex_lock(&ctx->mutex);
4674 again:
4675         list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry) {
4676                 struct perf_counter *parent = counter->parent;
4677
4678                 if (WARN_ON_ONCE(!parent))
4679                         continue;
4680
4681                 mutex_lock(&parent->child_mutex);
4682                 list_del_init(&counter->child_list);
4683                 mutex_unlock(&parent->child_mutex);
4684
4685                 fput(parent->filp);
4686
4687                 list_del_counter(counter, ctx);
4688                 free_counter(counter);
4689         }
4690
4691         if (!list_empty(&ctx->counter_list))
4692                 goto again;
4693
4694         mutex_unlock(&ctx->mutex);
4695
4696         put_ctx(ctx);
4697 }
4698
4699 /*
4700  * Initialize the perf_counter context in task_struct
4701  */
4702 int perf_counter_init_task(struct task_struct *child)
4703 {
4704         struct perf_counter_context *child_ctx, *parent_ctx;
4705         struct perf_counter_context *cloned_ctx;
4706         struct perf_counter *counter;
4707         struct task_struct *parent = current;
4708         int inherited_all = 1;
4709         int ret = 0;
4710
4711         child->perf_counter_ctxp = NULL;
4712
4713         mutex_init(&child->perf_counter_mutex);
4714         INIT_LIST_HEAD(&child->perf_counter_list);
4715
4716         if (likely(!parent->perf_counter_ctxp))
4717                 return 0;
4718
4719         /*
4720          * This is executed from the parent task context, so inherit
4721          * counters that have been marked for cloning.
4722          * First allocate and initialize a context for the child.
4723          */
4724
4725         child_ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
4726         if (!child_ctx)
4727                 return -ENOMEM;
4728
4729         __perf_counter_init_context(child_ctx, child);
4730         child->perf_counter_ctxp = child_ctx;
4731         get_task_struct(child);
4732
4733         /*
4734          * If the parent's context is a clone, pin it so it won't get
4735          * swapped under us.
4736          */
4737         parent_ctx = perf_pin_task_context(parent);
4738
4739         /*
4740          * No need to check if parent_ctx != NULL here; since we saw
4741          * it non-NULL earlier, the only reason for it to become NULL
4742          * is if we exit, and since we're currently in the middle of
4743          * a fork we can't be exiting at the same time.
4744          */
4745
4746         /*
4747          * Lock the parent list. No need to lock the child - not PID
4748          * hashed yet and not running, so nobody can access it.
4749          */
4750         mutex_lock(&parent_ctx->mutex);
4751
4752         /*
4753          * We dont have to disable NMIs - we are only looking at
4754          * the list, not manipulating it:
4755          */
4756         list_for_each_entry_rcu(counter, &parent_ctx->event_list, event_entry) {
4757                 if (counter != counter->group_leader)
4758                         continue;
4759
4760                 if (!counter->attr.inherit) {
4761                         inherited_all = 0;
4762                         continue;
4763                 }
4764
4765                 ret = inherit_group(counter, parent, parent_ctx,
4766                                              child, child_ctx);
4767                 if (ret) {
4768                         inherited_all = 0;
4769                         break;
4770                 }
4771         }
4772
4773         if (inherited_all) {
4774                 /*
4775                  * Mark the child context as a clone of the parent
4776                  * context, or of whatever the parent is a clone of.
4777                  * Note that if the parent is a clone, it could get
4778                  * uncloned at any point, but that doesn't matter
4779                  * because the list of counters and the generation
4780                  * count can't have changed since we took the mutex.
4781                  */
4782                 cloned_ctx = rcu_dereference(parent_ctx->parent_ctx);
4783                 if (cloned_ctx) {
4784                         child_ctx->parent_ctx = cloned_ctx;
4785                         child_ctx->parent_gen = parent_ctx->parent_gen;
4786                 } else {
4787                         child_ctx->parent_ctx = parent_ctx;
4788                         child_ctx->parent_gen = parent_ctx->generation;
4789                 }
4790                 get_ctx(child_ctx->parent_ctx);
4791         }
4792
4793         mutex_unlock(&parent_ctx->mutex);
4794
4795         perf_unpin_context(parent_ctx);
4796
4797         return ret;
4798 }
4799
4800 static void __cpuinit perf_counter_init_cpu(int cpu)
4801 {
4802         struct perf_cpu_context *cpuctx;
4803
4804         cpuctx = &per_cpu(perf_cpu_context, cpu);
4805         __perf_counter_init_context(&cpuctx->ctx, NULL);
4806
4807         spin_lock(&perf_resource_lock);
4808         cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
4809         spin_unlock(&perf_resource_lock);
4810
4811         hw_perf_counter_setup(cpu);
4812 }
4813
4814 #ifdef CONFIG_HOTPLUG_CPU
4815 static void __perf_counter_exit_cpu(void *info)
4816 {
4817         struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
4818         struct perf_counter_context *ctx = &cpuctx->ctx;
4819         struct perf_counter *counter, *tmp;
4820
4821         list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
4822                 __perf_counter_remove_from_context(counter);
4823 }
4824 static void perf_counter_exit_cpu(int cpu)
4825 {
4826         struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
4827         struct perf_counter_context *ctx = &cpuctx->ctx;
4828
4829         mutex_lock(&ctx->mutex);
4830         smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
4831         mutex_unlock(&ctx->mutex);
4832 }
4833 #else
4834 static inline void perf_counter_exit_cpu(int cpu) { }
4835 #endif
4836
4837 static int __cpuinit
4838 perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
4839 {
4840         unsigned int cpu = (long)hcpu;
4841
4842         switch (action) {
4843
4844         case CPU_UP_PREPARE:
4845         case CPU_UP_PREPARE_FROZEN:
4846                 perf_counter_init_cpu(cpu);
4847                 break;
4848
4849         case CPU_ONLINE:
4850         case CPU_ONLINE_FROZEN:
4851                 hw_perf_counter_setup_online(cpu);
4852                 break;
4853
4854         case CPU_DOWN_PREPARE:
4855         case CPU_DOWN_PREPARE_FROZEN:
4856                 perf_counter_exit_cpu(cpu);
4857                 break;
4858
4859         default:
4860                 break;
4861         }
4862
4863         return NOTIFY_OK;
4864 }
4865
4866 /*
4867  * This has to have a higher priority than migration_notifier in sched.c.
4868  */
4869 static struct notifier_block __cpuinitdata perf_cpu_nb = {
4870         .notifier_call          = perf_cpu_notify,
4871         .priority               = 20,
4872 };
4873
4874 void __init perf_counter_init(void)
4875 {
4876         perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
4877                         (void *)(long)smp_processor_id());
4878         perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_ONLINE,
4879                         (void *)(long)smp_processor_id());
4880         register_cpu_notifier(&perf_cpu_nb);
4881 }
4882
4883 static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
4884 {
4885         return sprintf(buf, "%d\n", perf_reserved_percpu);
4886 }
4887
4888 static ssize_t
4889 perf_set_reserve_percpu(struct sysdev_class *class,
4890                         const char *buf,
4891                         size_t count)
4892 {
4893         struct perf_cpu_context *cpuctx;
4894         unsigned long val;
4895         int err, cpu, mpt;
4896
4897         err = strict_strtoul(buf, 10, &val);
4898         if (err)
4899                 return err;
4900         if (val > perf_max_counters)
4901                 return -EINVAL;
4902
4903         spin_lock(&perf_resource_lock);
4904         perf_reserved_percpu = val;
4905         for_each_online_cpu(cpu) {
4906                 cpuctx = &per_cpu(perf_cpu_context, cpu);
4907                 spin_lock_irq(&cpuctx->ctx.lock);
4908                 mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
4909                           perf_max_counters - perf_reserved_percpu);
4910                 cpuctx->max_pertask = mpt;
4911                 spin_unlock_irq(&cpuctx->ctx.lock);
4912         }
4913         spin_unlock(&perf_resource_lock);
4914
4915         return count;
4916 }
4917
4918 static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
4919 {
4920         return sprintf(buf, "%d\n", perf_overcommit);
4921 }
4922
4923 static ssize_t
4924 perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
4925 {
4926         unsigned long val;
4927         int err;
4928
4929         err = strict_strtoul(buf, 10, &val);
4930         if (err)
4931                 return err;
4932         if (val > 1)
4933                 return -EINVAL;
4934
4935         spin_lock(&perf_resource_lock);
4936         perf_overcommit = val;
4937         spin_unlock(&perf_resource_lock);
4938
4939         return count;
4940 }
4941
4942 static SYSDEV_CLASS_ATTR(
4943                                 reserve_percpu,
4944                                 0644,
4945                                 perf_show_reserve_percpu,
4946                                 perf_set_reserve_percpu
4947                         );
4948
4949 static SYSDEV_CLASS_ATTR(
4950                                 overcommit,
4951                                 0644,
4952                                 perf_show_overcommit,
4953                                 perf_set_overcommit
4954                         );
4955
4956 static struct attribute *perfclass_attrs[] = {
4957         &attr_reserve_percpu.attr,
4958         &attr_overcommit.attr,
4959         NULL
4960 };
4961
4962 static struct attribute_group perfclass_attr_group = {
4963         .attrs                  = perfclass_attrs,
4964         .name                   = "perf_counters",
4965 };
4966
4967 static int __init perf_counter_sysfs_init(void)
4968 {
4969         return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
4970                                   &perfclass_attr_group);
4971 }
4972 device_initcall(perf_counter_sysfs_init);