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