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