perfcounters: fix task clock counter
[linux-2.6.git] / kernel / perf_counter.c
1 /*
2  * Performance counter core code
3  *
4  *  Copyright(C) 2008 Thomas Gleixner <tglx@linutronix.de>
5  *  Copyright(C) 2008 Red Hat, Inc., Ingo Molnar
6  *
7  *  For licencing details see kernel-base/COPYING
8  */
9
10 #include <linux/fs.h>
11 #include <linux/cpu.h>
12 #include <linux/smp.h>
13 #include <linux/file.h>
14 #include <linux/poll.h>
15 #include <linux/sysfs.h>
16 #include <linux/ptrace.h>
17 #include <linux/percpu.h>
18 #include <linux/uaccess.h>
19 #include <linux/syscalls.h>
20 #include <linux/anon_inodes.h>
21 #include <linux/kernel_stat.h>
22 #include <linux/perf_counter.h>
23
24 /*
25  * Each CPU has a list of per CPU counters:
26  */
27 DEFINE_PER_CPU(struct perf_cpu_context, perf_cpu_context);
28
29 int perf_max_counters __read_mostly = 1;
30 static int perf_reserved_percpu __read_mostly;
31 static int perf_overcommit __read_mostly = 1;
32
33 /*
34  * Mutex for (sysadmin-configurable) counter reservations:
35  */
36 static DEFINE_MUTEX(perf_resource_mutex);
37
38 /*
39  * Architecture provided APIs - weak aliases:
40  */
41 extern __weak const struct hw_perf_counter_ops *
42 hw_perf_counter_init(struct perf_counter *counter)
43 {
44         return ERR_PTR(-EINVAL);
45 }
46
47 u64 __weak hw_perf_save_disable(void)           { return 0; }
48 void __weak hw_perf_restore(u64 ctrl)           { }
49 void __weak hw_perf_counter_setup(void)         { }
50
51 static void
52 list_add_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
53 {
54         struct perf_counter *group_leader = counter->group_leader;
55
56         /*
57          * Depending on whether it is a standalone or sibling counter,
58          * add it straight to the context's counter list, or to the group
59          * leader's sibling list:
60          */
61         if (counter->group_leader == counter)
62                 list_add_tail(&counter->list_entry, &ctx->counter_list);
63         else
64                 list_add_tail(&counter->list_entry, &group_leader->sibling_list);
65 }
66
67 static void
68 list_del_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
69 {
70         struct perf_counter *sibling, *tmp;
71
72         list_del_init(&counter->list_entry);
73
74         /*
75          * If this was a group counter with sibling counters then
76          * upgrade the siblings to singleton counters by adding them
77          * to the context list directly:
78          */
79         list_for_each_entry_safe(sibling, tmp,
80                                  &counter->sibling_list, list_entry) {
81
82                 list_del_init(&sibling->list_entry);
83                 list_add_tail(&sibling->list_entry, &ctx->counter_list);
84                 sibling->group_leader = sibling;
85         }
86 }
87
88 /*
89  * Cross CPU call to remove a performance counter
90  *
91  * We disable the counter on the hardware level first. After that we
92  * remove it from the context list.
93  */
94 static void __perf_counter_remove_from_context(void *info)
95 {
96         struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
97         struct perf_counter *counter = info;
98         struct perf_counter_context *ctx = counter->ctx;
99         unsigned long flags;
100         u64 perf_flags;
101
102         /*
103          * If this is a task context, we need to check whether it is
104          * the current task context of this cpu. If not it has been
105          * scheduled out before the smp call arrived.
106          */
107         if (ctx->task && cpuctx->task_ctx != ctx)
108                 return;
109
110         curr_rq_lock_irq_save(&flags);
111         spin_lock(&ctx->lock);
112
113         if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
114                 counter->hw_ops->disable(counter);
115                 counter->state = PERF_COUNTER_STATE_INACTIVE;
116                 ctx->nr_active--;
117                 cpuctx->active_oncpu--;
118                 counter->task = NULL;
119         }
120         ctx->nr_counters--;
121
122         /*
123          * Protect the list operation against NMI by disabling the
124          * counters on a global level. NOP for non NMI based counters.
125          */
126         perf_flags = hw_perf_save_disable();
127         list_del_counter(counter, ctx);
128         hw_perf_restore(perf_flags);
129
130         if (!ctx->task) {
131                 /*
132                  * Allow more per task counters with respect to the
133                  * reservation:
134                  */
135                 cpuctx->max_pertask =
136                         min(perf_max_counters - ctx->nr_counters,
137                             perf_max_counters - perf_reserved_percpu);
138         }
139
140         spin_unlock(&ctx->lock);
141         curr_rq_unlock_irq_restore(&flags);
142 }
143
144
145 /*
146  * Remove the counter from a task's (or a CPU's) list of counters.
147  *
148  * Must be called with counter->mutex held.
149  *
150  * CPU counters are removed with a smp call. For task counters we only
151  * call when the task is on a CPU.
152  */
153 static void perf_counter_remove_from_context(struct perf_counter *counter)
154 {
155         struct perf_counter_context *ctx = counter->ctx;
156         struct task_struct *task = ctx->task;
157
158         if (!task) {
159                 /*
160                  * Per cpu counters are removed via an smp call and
161                  * the removal is always sucessful.
162                  */
163                 smp_call_function_single(counter->cpu,
164                                          __perf_counter_remove_from_context,
165                                          counter, 1);
166                 return;
167         }
168
169 retry:
170         task_oncpu_function_call(task, __perf_counter_remove_from_context,
171                                  counter);
172
173         spin_lock_irq(&ctx->lock);
174         /*
175          * If the context is active we need to retry the smp call.
176          */
177         if (ctx->nr_active && !list_empty(&counter->list_entry)) {
178                 spin_unlock_irq(&ctx->lock);
179                 goto retry;
180         }
181
182         /*
183          * The lock prevents that this context is scheduled in so we
184          * can remove the counter safely, if the call above did not
185          * succeed.
186          */
187         if (!list_empty(&counter->list_entry)) {
188                 ctx->nr_counters--;
189                 list_del_counter(counter, ctx);
190                 counter->task = NULL;
191         }
192         spin_unlock_irq(&ctx->lock);
193 }
194
195 /*
196  * Cross CPU call to install and enable a preformance counter
197  */
198 static void __perf_install_in_context(void *info)
199 {
200         struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
201         struct perf_counter *counter = info;
202         struct perf_counter_context *ctx = counter->ctx;
203         int cpu = smp_processor_id();
204         unsigned long flags;
205         u64 perf_flags;
206
207         /*
208          * If this is a task context, we need to check whether it is
209          * the current task context of this cpu. If not it has been
210          * scheduled out before the smp call arrived.
211          */
212         if (ctx->task && cpuctx->task_ctx != ctx)
213                 return;
214
215         curr_rq_lock_irq_save(&flags);
216         spin_lock(&ctx->lock);
217
218         /*
219          * Protect the list operation against NMI by disabling the
220          * counters on a global level. NOP for non NMI based counters.
221          */
222         perf_flags = hw_perf_save_disable();
223         list_add_counter(counter, ctx);
224         hw_perf_restore(perf_flags);
225
226         ctx->nr_counters++;
227
228         if (cpuctx->active_oncpu < perf_max_counters) {
229                 counter->state = PERF_COUNTER_STATE_ACTIVE;
230                 counter->oncpu = cpu;
231                 ctx->nr_active++;
232                 cpuctx->active_oncpu++;
233                 counter->hw_ops->enable(counter);
234         }
235
236         if (!ctx->task && cpuctx->max_pertask)
237                 cpuctx->max_pertask--;
238
239         spin_unlock(&ctx->lock);
240         curr_rq_unlock_irq_restore(&flags);
241 }
242
243 /*
244  * Attach a performance counter to a context
245  *
246  * First we add the counter to the list with the hardware enable bit
247  * in counter->hw_config cleared.
248  *
249  * If the counter is attached to a task which is on a CPU we use a smp
250  * call to enable it in the task context. The task might have been
251  * scheduled away, but we check this in the smp call again.
252  */
253 static void
254 perf_install_in_context(struct perf_counter_context *ctx,
255                         struct perf_counter *counter,
256                         int cpu)
257 {
258         struct task_struct *task = ctx->task;
259
260         counter->ctx = ctx;
261         if (!task) {
262                 /*
263                  * Per cpu counters are installed via an smp call and
264                  * the install is always sucessful.
265                  */
266                 smp_call_function_single(cpu, __perf_install_in_context,
267                                          counter, 1);
268                 return;
269         }
270
271         counter->task = task;
272 retry:
273         task_oncpu_function_call(task, __perf_install_in_context,
274                                  counter);
275
276         spin_lock_irq(&ctx->lock);
277         /*
278          * we need to retry the smp call.
279          */
280         if (ctx->nr_active && list_empty(&counter->list_entry)) {
281                 spin_unlock_irq(&ctx->lock);
282                 goto retry;
283         }
284
285         /*
286          * The lock prevents that this context is scheduled in so we
287          * can add the counter safely, if it the call above did not
288          * succeed.
289          */
290         if (list_empty(&counter->list_entry)) {
291                 list_add_counter(counter, ctx);
292                 ctx->nr_counters++;
293         }
294         spin_unlock_irq(&ctx->lock);
295 }
296
297 static void
298 counter_sched_out(struct perf_counter *counter,
299                   struct perf_cpu_context *cpuctx,
300                   struct perf_counter_context *ctx)
301 {
302         if (counter->state != PERF_COUNTER_STATE_ACTIVE)
303                 return;
304
305         counter->hw_ops->disable(counter);
306         counter->state = PERF_COUNTER_STATE_INACTIVE;
307         counter->oncpu = -1;
308
309         cpuctx->active_oncpu--;
310         ctx->nr_active--;
311 }
312
313 static void
314 group_sched_out(struct perf_counter *group_counter,
315                 struct perf_cpu_context *cpuctx,
316                 struct perf_counter_context *ctx)
317 {
318         struct perf_counter *counter;
319
320         counter_sched_out(group_counter, cpuctx, ctx);
321
322         /*
323          * Schedule out siblings (if any):
324          */
325         list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
326                 counter_sched_out(counter, cpuctx, ctx);
327 }
328
329 /*
330  * Called from scheduler to remove the counters of the current task,
331  * with interrupts disabled.
332  *
333  * We stop each counter and update the counter value in counter->count.
334  *
335  * This does not protect us against NMI, but disable()
336  * sets the disabled bit in the control field of counter _before_
337  * accessing the counter control register. If a NMI hits, then it will
338  * not restart the counter.
339  */
340 void perf_counter_task_sched_out(struct task_struct *task, int cpu)
341 {
342         struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
343         struct perf_counter_context *ctx = &task->perf_counter_ctx;
344         struct perf_counter *counter;
345
346         if (likely(!cpuctx->task_ctx))
347                 return;
348
349         spin_lock(&ctx->lock);
350         if (ctx->nr_active) {
351                 list_for_each_entry(counter, &ctx->counter_list, list_entry)
352                         group_sched_out(counter, cpuctx, ctx);
353         }
354         spin_unlock(&ctx->lock);
355         cpuctx->task_ctx = NULL;
356 }
357
358 static void
359 counter_sched_in(struct perf_counter *counter,
360                  struct perf_cpu_context *cpuctx,
361                  struct perf_counter_context *ctx,
362                  int cpu)
363 {
364         if (counter->state == PERF_COUNTER_STATE_OFF)
365                 return;
366
367         counter->hw_ops->enable(counter);
368         counter->state = PERF_COUNTER_STATE_ACTIVE;
369         counter->oncpu = cpu;   /* TODO: put 'cpu' into cpuctx->cpu */
370
371         cpuctx->active_oncpu++;
372         ctx->nr_active++;
373 }
374
375 static int
376 group_sched_in(struct perf_counter *group_counter,
377                struct perf_cpu_context *cpuctx,
378                struct perf_counter_context *ctx,
379                int cpu)
380 {
381         struct perf_counter *counter;
382         int was_group = 0;
383
384         counter_sched_in(group_counter, cpuctx, ctx, cpu);
385
386         /*
387          * Schedule in siblings as one group (if any):
388          */
389         list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
390                 counter_sched_in(counter, cpuctx, ctx, cpu);
391                 was_group = 1;
392         }
393
394         return was_group;
395 }
396
397 /*
398  * Called from scheduler to add the counters of the current task
399  * with interrupts disabled.
400  *
401  * We restore the counter value and then enable it.
402  *
403  * This does not protect us against NMI, but enable()
404  * sets the enabled bit in the control field of counter _before_
405  * accessing the counter control register. If a NMI hits, then it will
406  * keep the counter running.
407  */
408 void perf_counter_task_sched_in(struct task_struct *task, int cpu)
409 {
410         struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
411         struct perf_counter_context *ctx = &task->perf_counter_ctx;
412         struct perf_counter *counter;
413
414         if (likely(!ctx->nr_counters))
415                 return;
416
417         spin_lock(&ctx->lock);
418         list_for_each_entry(counter, &ctx->counter_list, list_entry) {
419                 if (ctx->nr_active == cpuctx->max_pertask)
420                         break;
421
422                 /*
423                  * Listen to the 'cpu' scheduling filter constraint
424                  * of counters:
425                  */
426                 if (counter->cpu != -1 && counter->cpu != cpu)
427                         continue;
428
429                 /*
430                  * If we scheduled in a group atomically and
431                  * exclusively, break out:
432                  */
433                 if (group_sched_in(counter, cpuctx, ctx, cpu))
434                         break;
435         }
436         spin_unlock(&ctx->lock);
437
438         cpuctx->task_ctx = ctx;
439 }
440
441 int perf_counter_task_disable(void)
442 {
443         struct task_struct *curr = current;
444         struct perf_counter_context *ctx = &curr->perf_counter_ctx;
445         struct perf_counter *counter;
446         unsigned long flags;
447         u64 perf_flags;
448         int cpu;
449
450         if (likely(!ctx->nr_counters))
451                 return 0;
452
453         curr_rq_lock_irq_save(&flags);
454         cpu = smp_processor_id();
455
456         /* force the update of the task clock: */
457         __task_delta_exec(curr, 1);
458
459         perf_counter_task_sched_out(curr, cpu);
460
461         spin_lock(&ctx->lock);
462
463         /*
464          * Disable all the counters:
465          */
466         perf_flags = hw_perf_save_disable();
467
468         list_for_each_entry(counter, &ctx->counter_list, list_entry)
469                 counter->state = PERF_COUNTER_STATE_OFF;
470
471         hw_perf_restore(perf_flags);
472
473         spin_unlock(&ctx->lock);
474
475         curr_rq_unlock_irq_restore(&flags);
476
477         return 0;
478 }
479
480 int perf_counter_task_enable(void)
481 {
482         struct task_struct *curr = current;
483         struct perf_counter_context *ctx = &curr->perf_counter_ctx;
484         struct perf_counter *counter;
485         unsigned long flags;
486         u64 perf_flags;
487         int cpu;
488
489         if (likely(!ctx->nr_counters))
490                 return 0;
491
492         curr_rq_lock_irq_save(&flags);
493         cpu = smp_processor_id();
494
495         /* force the update of the task clock: */
496         __task_delta_exec(curr, 1);
497
498         spin_lock(&ctx->lock);
499
500         /*
501          * Disable all the counters:
502          */
503         perf_flags = hw_perf_save_disable();
504
505         list_for_each_entry(counter, &ctx->counter_list, list_entry) {
506                 if (counter->state != PERF_COUNTER_STATE_OFF)
507                         continue;
508                 counter->state = PERF_COUNTER_STATE_INACTIVE;
509                 counter->hw_event.disabled = 0;
510         }
511         hw_perf_restore(perf_flags);
512
513         spin_unlock(&ctx->lock);
514
515         perf_counter_task_sched_in(curr, cpu);
516
517         curr_rq_unlock_irq_restore(&flags);
518
519         return 0;
520 }
521
522 void perf_counter_task_tick(struct task_struct *curr, int cpu)
523 {
524         struct perf_counter_context *ctx = &curr->perf_counter_ctx;
525         struct perf_counter *counter;
526         u64 perf_flags;
527
528         if (likely(!ctx->nr_counters))
529                 return;
530
531         perf_counter_task_sched_out(curr, cpu);
532
533         spin_lock(&ctx->lock);
534
535         /*
536          * Rotate the first entry last (works just fine for group counters too):
537          */
538         perf_flags = hw_perf_save_disable();
539         list_for_each_entry(counter, &ctx->counter_list, list_entry) {
540                 list_del(&counter->list_entry);
541                 list_add_tail(&counter->list_entry, &ctx->counter_list);
542                 break;
543         }
544         hw_perf_restore(perf_flags);
545
546         spin_unlock(&ctx->lock);
547
548         perf_counter_task_sched_in(curr, cpu);
549 }
550
551 /*
552  * Cross CPU call to read the hardware counter
553  */
554 static void __read(void *info)
555 {
556         struct perf_counter *counter = info;
557         unsigned long flags;
558
559         curr_rq_lock_irq_save(&flags);
560         counter->hw_ops->read(counter);
561         curr_rq_unlock_irq_restore(&flags);
562 }
563
564 static u64 perf_counter_read(struct perf_counter *counter)
565 {
566         /*
567          * If counter is enabled and currently active on a CPU, update the
568          * value in the counter structure:
569          */
570         if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
571                 smp_call_function_single(counter->oncpu,
572                                          __read, counter, 1);
573         }
574
575         return atomic64_read(&counter->count);
576 }
577
578 /*
579  * Cross CPU call to switch performance data pointers
580  */
581 static void __perf_switch_irq_data(void *info)
582 {
583         struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
584         struct perf_counter *counter = info;
585         struct perf_counter_context *ctx = counter->ctx;
586         struct perf_data *oldirqdata = counter->irqdata;
587
588         /*
589          * If this is a task context, we need to check whether it is
590          * the current task context of this cpu. If not it has been
591          * scheduled out before the smp call arrived.
592          */
593         if (ctx->task) {
594                 if (cpuctx->task_ctx != ctx)
595                         return;
596                 spin_lock(&ctx->lock);
597         }
598
599         /* Change the pointer NMI safe */
600         atomic_long_set((atomic_long_t *)&counter->irqdata,
601                         (unsigned long) counter->usrdata);
602         counter->usrdata = oldirqdata;
603
604         if (ctx->task)
605                 spin_unlock(&ctx->lock);
606 }
607
608 static struct perf_data *perf_switch_irq_data(struct perf_counter *counter)
609 {
610         struct perf_counter_context *ctx = counter->ctx;
611         struct perf_data *oldirqdata = counter->irqdata;
612         struct task_struct *task = ctx->task;
613
614         if (!task) {
615                 smp_call_function_single(counter->cpu,
616                                          __perf_switch_irq_data,
617                                          counter, 1);
618                 return counter->usrdata;
619         }
620
621 retry:
622         spin_lock_irq(&ctx->lock);
623         if (counter->state != PERF_COUNTER_STATE_ACTIVE) {
624                 counter->irqdata = counter->usrdata;
625                 counter->usrdata = oldirqdata;
626                 spin_unlock_irq(&ctx->lock);
627                 return oldirqdata;
628         }
629         spin_unlock_irq(&ctx->lock);
630         task_oncpu_function_call(task, __perf_switch_irq_data, counter);
631         /* Might have failed, because task was scheduled out */
632         if (counter->irqdata == oldirqdata)
633                 goto retry;
634
635         return counter->usrdata;
636 }
637
638 static void put_context(struct perf_counter_context *ctx)
639 {
640         if (ctx->task)
641                 put_task_struct(ctx->task);
642 }
643
644 static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
645 {
646         struct perf_cpu_context *cpuctx;
647         struct perf_counter_context *ctx;
648         struct task_struct *task;
649
650         /*
651          * If cpu is not a wildcard then this is a percpu counter:
652          */
653         if (cpu != -1) {
654                 /* Must be root to operate on a CPU counter: */
655                 if (!capable(CAP_SYS_ADMIN))
656                         return ERR_PTR(-EACCES);
657
658                 if (cpu < 0 || cpu > num_possible_cpus())
659                         return ERR_PTR(-EINVAL);
660
661                 /*
662                  * We could be clever and allow to attach a counter to an
663                  * offline CPU and activate it when the CPU comes up, but
664                  * that's for later.
665                  */
666                 if (!cpu_isset(cpu, cpu_online_map))
667                         return ERR_PTR(-ENODEV);
668
669                 cpuctx = &per_cpu(perf_cpu_context, cpu);
670                 ctx = &cpuctx->ctx;
671
672                 return ctx;
673         }
674
675         rcu_read_lock();
676         if (!pid)
677                 task = current;
678         else
679                 task = find_task_by_vpid(pid);
680         if (task)
681                 get_task_struct(task);
682         rcu_read_unlock();
683
684         if (!task)
685                 return ERR_PTR(-ESRCH);
686
687         ctx = &task->perf_counter_ctx;
688         ctx->task = task;
689
690         /* Reuse ptrace permission checks for now. */
691         if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
692                 put_context(ctx);
693                 return ERR_PTR(-EACCES);
694         }
695
696         return ctx;
697 }
698
699 /*
700  * Called when the last reference to the file is gone.
701  */
702 static int perf_release(struct inode *inode, struct file *file)
703 {
704         struct perf_counter *counter = file->private_data;
705         struct perf_counter_context *ctx = counter->ctx;
706
707         file->private_data = NULL;
708
709         mutex_lock(&counter->mutex);
710
711         perf_counter_remove_from_context(counter);
712         put_context(ctx);
713
714         mutex_unlock(&counter->mutex);
715
716         kfree(counter);
717
718         return 0;
719 }
720
721 /*
722  * Read the performance counter - simple non blocking version for now
723  */
724 static ssize_t
725 perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
726 {
727         u64 cntval;
728
729         if (count != sizeof(cntval))
730                 return -EINVAL;
731
732         mutex_lock(&counter->mutex);
733         cntval = perf_counter_read(counter);
734         mutex_unlock(&counter->mutex);
735
736         return put_user(cntval, (u64 __user *) buf) ? -EFAULT : sizeof(cntval);
737 }
738
739 static ssize_t
740 perf_copy_usrdata(struct perf_data *usrdata, char __user *buf, size_t count)
741 {
742         if (!usrdata->len)
743                 return 0;
744
745         count = min(count, (size_t)usrdata->len);
746         if (copy_to_user(buf, usrdata->data + usrdata->rd_idx, count))
747                 return -EFAULT;
748
749         /* Adjust the counters */
750         usrdata->len -= count;
751         if (!usrdata->len)
752                 usrdata->rd_idx = 0;
753         else
754                 usrdata->rd_idx += count;
755
756         return count;
757 }
758
759 static ssize_t
760 perf_read_irq_data(struct perf_counter  *counter,
761                    char __user          *buf,
762                    size_t               count,
763                    int                  nonblocking)
764 {
765         struct perf_data *irqdata, *usrdata;
766         DECLARE_WAITQUEUE(wait, current);
767         ssize_t res;
768
769         irqdata = counter->irqdata;
770         usrdata = counter->usrdata;
771
772         if (usrdata->len + irqdata->len >= count)
773                 goto read_pending;
774
775         if (nonblocking)
776                 return -EAGAIN;
777
778         spin_lock_irq(&counter->waitq.lock);
779         __add_wait_queue(&counter->waitq, &wait);
780         for (;;) {
781                 set_current_state(TASK_INTERRUPTIBLE);
782                 if (usrdata->len + irqdata->len >= count)
783                         break;
784
785                 if (signal_pending(current))
786                         break;
787
788                 spin_unlock_irq(&counter->waitq.lock);
789                 schedule();
790                 spin_lock_irq(&counter->waitq.lock);
791         }
792         __remove_wait_queue(&counter->waitq, &wait);
793         __set_current_state(TASK_RUNNING);
794         spin_unlock_irq(&counter->waitq.lock);
795
796         if (usrdata->len + irqdata->len < count)
797                 return -ERESTARTSYS;
798 read_pending:
799         mutex_lock(&counter->mutex);
800
801         /* Drain pending data first: */
802         res = perf_copy_usrdata(usrdata, buf, count);
803         if (res < 0 || res == count)
804                 goto out;
805
806         /* Switch irq buffer: */
807         usrdata = perf_switch_irq_data(counter);
808         if (perf_copy_usrdata(usrdata, buf + res, count - res) < 0) {
809                 if (!res)
810                         res = -EFAULT;
811         } else {
812                 res = count;
813         }
814 out:
815         mutex_unlock(&counter->mutex);
816
817         return res;
818 }
819
820 static ssize_t
821 perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
822 {
823         struct perf_counter *counter = file->private_data;
824
825         switch (counter->hw_event.record_type) {
826         case PERF_RECORD_SIMPLE:
827                 return perf_read_hw(counter, buf, count);
828
829         case PERF_RECORD_IRQ:
830         case PERF_RECORD_GROUP:
831                 return perf_read_irq_data(counter, buf, count,
832                                           file->f_flags & O_NONBLOCK);
833         }
834         return -EINVAL;
835 }
836
837 static unsigned int perf_poll(struct file *file, poll_table *wait)
838 {
839         struct perf_counter *counter = file->private_data;
840         unsigned int events = 0;
841         unsigned long flags;
842
843         poll_wait(file, &counter->waitq, wait);
844
845         spin_lock_irqsave(&counter->waitq.lock, flags);
846         if (counter->usrdata->len || counter->irqdata->len)
847                 events |= POLLIN;
848         spin_unlock_irqrestore(&counter->waitq.lock, flags);
849
850         return events;
851 }
852
853 static const struct file_operations perf_fops = {
854         .release                = perf_release,
855         .read                   = perf_read,
856         .poll                   = perf_poll,
857 };
858
859 static void cpu_clock_perf_counter_enable(struct perf_counter *counter)
860 {
861 }
862
863 static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
864 {
865 }
866
867 static void cpu_clock_perf_counter_read(struct perf_counter *counter)
868 {
869         int cpu = raw_smp_processor_id();
870
871         atomic64_set(&counter->count, cpu_clock(cpu));
872 }
873
874 static const struct hw_perf_counter_ops perf_ops_cpu_clock = {
875         .enable         = cpu_clock_perf_counter_enable,
876         .disable        = cpu_clock_perf_counter_disable,
877         .read           = cpu_clock_perf_counter_read,
878 };
879
880 /*
881  * Called from within the scheduler:
882  */
883 static u64 task_clock_perf_counter_val(struct perf_counter *counter, int update)
884 {
885         struct task_struct *curr = counter->task;
886         u64 delta;
887
888         WARN_ON_ONCE(counter->task != current);
889
890         delta = __task_delta_exec(curr, update);
891
892         return curr->se.sum_exec_runtime + delta;
893 }
894
895 static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
896 {
897         u64 prev;
898         s64 delta;
899
900         prev = atomic64_read(&counter->hw.prev_count);
901
902         atomic64_set(&counter->hw.prev_count, now);
903
904         delta = now - prev;
905
906         atomic64_add(delta, &counter->count);
907 }
908
909 static void task_clock_perf_counter_read(struct perf_counter *counter)
910 {
911         u64 now = task_clock_perf_counter_val(counter, 1);
912
913         task_clock_perf_counter_update(counter, now);
914 }
915
916 static void task_clock_perf_counter_enable(struct perf_counter *counter)
917 {
918         u64 now = task_clock_perf_counter_val(counter, 0);
919
920         atomic64_set(&counter->hw.prev_count, now);
921 }
922
923 static void task_clock_perf_counter_disable(struct perf_counter *counter)
924 {
925         u64 now = task_clock_perf_counter_val(counter, 0);
926
927         task_clock_perf_counter_update(counter, now);
928 }
929
930 static const struct hw_perf_counter_ops perf_ops_task_clock = {
931         .enable         = task_clock_perf_counter_enable,
932         .disable        = task_clock_perf_counter_disable,
933         .read           = task_clock_perf_counter_read,
934 };
935
936 static u64 get_page_faults(void)
937 {
938         struct task_struct *curr = current;
939
940         return curr->maj_flt + curr->min_flt;
941 }
942
943 static void page_faults_perf_counter_update(struct perf_counter *counter)
944 {
945         u64 prev, now;
946         s64 delta;
947
948         prev = atomic64_read(&counter->hw.prev_count);
949         now = get_page_faults();
950
951         atomic64_set(&counter->hw.prev_count, now);
952
953         delta = now - prev;
954
955         atomic64_add(delta, &counter->count);
956 }
957
958 static void page_faults_perf_counter_read(struct perf_counter *counter)
959 {
960         page_faults_perf_counter_update(counter);
961 }
962
963 static void page_faults_perf_counter_enable(struct perf_counter *counter)
964 {
965         /*
966          * page-faults is a per-task value already,
967          * so we dont have to clear it on switch-in.
968          */
969 }
970
971 static void page_faults_perf_counter_disable(struct perf_counter *counter)
972 {
973         page_faults_perf_counter_update(counter);
974 }
975
976 static const struct hw_perf_counter_ops perf_ops_page_faults = {
977         .enable         = page_faults_perf_counter_enable,
978         .disable        = page_faults_perf_counter_disable,
979         .read           = page_faults_perf_counter_read,
980 };
981
982 static u64 get_context_switches(void)
983 {
984         struct task_struct *curr = current;
985
986         return curr->nvcsw + curr->nivcsw;
987 }
988
989 static void context_switches_perf_counter_update(struct perf_counter *counter)
990 {
991         u64 prev, now;
992         s64 delta;
993
994         prev = atomic64_read(&counter->hw.prev_count);
995         now = get_context_switches();
996
997         atomic64_set(&counter->hw.prev_count, now);
998
999         delta = now - prev;
1000
1001         atomic64_add(delta, &counter->count);
1002 }
1003
1004 static void context_switches_perf_counter_read(struct perf_counter *counter)
1005 {
1006         context_switches_perf_counter_update(counter);
1007 }
1008
1009 static void context_switches_perf_counter_enable(struct perf_counter *counter)
1010 {
1011         /*
1012          * ->nvcsw + curr->nivcsw is a per-task value already,
1013          * so we dont have to clear it on switch-in.
1014          */
1015 }
1016
1017 static void context_switches_perf_counter_disable(struct perf_counter *counter)
1018 {
1019         context_switches_perf_counter_update(counter);
1020 }
1021
1022 static const struct hw_perf_counter_ops perf_ops_context_switches = {
1023         .enable         = context_switches_perf_counter_enable,
1024         .disable        = context_switches_perf_counter_disable,
1025         .read           = context_switches_perf_counter_read,
1026 };
1027
1028 static inline u64 get_cpu_migrations(void)
1029 {
1030         return current->se.nr_migrations;
1031 }
1032
1033 static void cpu_migrations_perf_counter_update(struct perf_counter *counter)
1034 {
1035         u64 prev, now;
1036         s64 delta;
1037
1038         prev = atomic64_read(&counter->hw.prev_count);
1039         now = get_cpu_migrations();
1040
1041         atomic64_set(&counter->hw.prev_count, now);
1042
1043         delta = now - prev;
1044
1045         atomic64_add(delta, &counter->count);
1046 }
1047
1048 static void cpu_migrations_perf_counter_read(struct perf_counter *counter)
1049 {
1050         cpu_migrations_perf_counter_update(counter);
1051 }
1052
1053 static void cpu_migrations_perf_counter_enable(struct perf_counter *counter)
1054 {
1055         /*
1056          * se.nr_migrations is a per-task value already,
1057          * so we dont have to clear it on switch-in.
1058          */
1059 }
1060
1061 static void cpu_migrations_perf_counter_disable(struct perf_counter *counter)
1062 {
1063         cpu_migrations_perf_counter_update(counter);
1064 }
1065
1066 static const struct hw_perf_counter_ops perf_ops_cpu_migrations = {
1067         .enable         = cpu_migrations_perf_counter_enable,
1068         .disable        = cpu_migrations_perf_counter_disable,
1069         .read           = cpu_migrations_perf_counter_read,
1070 };
1071
1072 static const struct hw_perf_counter_ops *
1073 sw_perf_counter_init(struct perf_counter *counter)
1074 {
1075         const struct hw_perf_counter_ops *hw_ops = NULL;
1076
1077         switch (counter->hw_event.type) {
1078         case PERF_COUNT_CPU_CLOCK:
1079                 hw_ops = &perf_ops_cpu_clock;
1080                 break;
1081         case PERF_COUNT_TASK_CLOCK:
1082                 hw_ops = &perf_ops_task_clock;
1083                 break;
1084         case PERF_COUNT_PAGE_FAULTS:
1085                 hw_ops = &perf_ops_page_faults;
1086                 break;
1087         case PERF_COUNT_CONTEXT_SWITCHES:
1088                 hw_ops = &perf_ops_context_switches;
1089                 break;
1090         case PERF_COUNT_CPU_MIGRATIONS:
1091                 hw_ops = &perf_ops_cpu_migrations;
1092                 break;
1093         default:
1094                 break;
1095         }
1096         return hw_ops;
1097 }
1098
1099 /*
1100  * Allocate and initialize a counter structure
1101  */
1102 static struct perf_counter *
1103 perf_counter_alloc(struct perf_counter_hw_event *hw_event,
1104                    int cpu,
1105                    struct perf_counter *group_leader,
1106                    gfp_t gfpflags)
1107 {
1108         const struct hw_perf_counter_ops *hw_ops;
1109         struct perf_counter *counter;
1110
1111         counter = kzalloc(sizeof(*counter), gfpflags);
1112         if (!counter)
1113                 return NULL;
1114
1115         /*
1116          * Single counters are their own group leaders, with an
1117          * empty sibling list:
1118          */
1119         if (!group_leader)
1120                 group_leader = counter;
1121
1122         mutex_init(&counter->mutex);
1123         INIT_LIST_HEAD(&counter->list_entry);
1124         INIT_LIST_HEAD(&counter->sibling_list);
1125         init_waitqueue_head(&counter->waitq);
1126
1127         counter->irqdata                = &counter->data[0];
1128         counter->usrdata                = &counter->data[1];
1129         counter->cpu                    = cpu;
1130         counter->hw_event               = *hw_event;
1131         counter->wakeup_pending         = 0;
1132         counter->group_leader           = group_leader;
1133         counter->hw_ops                 = NULL;
1134
1135         if (hw_event->disabled)
1136                 counter->state = PERF_COUNTER_STATE_OFF;
1137
1138         hw_ops = NULL;
1139         if (!hw_event->raw && hw_event->type < 0)
1140                 hw_ops = sw_perf_counter_init(counter);
1141         if (!hw_ops)
1142                 hw_ops = hw_perf_counter_init(counter);
1143
1144         if (!hw_ops) {
1145                 kfree(counter);
1146                 return NULL;
1147         }
1148         counter->hw_ops = hw_ops;
1149
1150         return counter;
1151 }
1152
1153 /**
1154  * sys_perf_task_open - open a performance counter, associate it to a task/cpu
1155  *
1156  * @hw_event_uptr:      event type attributes for monitoring/sampling
1157  * @pid:                target pid
1158  * @cpu:                target cpu
1159  * @group_fd:           group leader counter fd
1160  */
1161 asmlinkage int
1162 sys_perf_counter_open(struct perf_counter_hw_event *hw_event_uptr __user,
1163                       pid_t pid, int cpu, int group_fd)
1164 {
1165         struct perf_counter *counter, *group_leader;
1166         struct perf_counter_hw_event hw_event;
1167         struct perf_counter_context *ctx;
1168         struct file *counter_file = NULL;
1169         struct file *group_file = NULL;
1170         int fput_needed = 0;
1171         int fput_needed2 = 0;
1172         int ret;
1173
1174         if (copy_from_user(&hw_event, hw_event_uptr, sizeof(hw_event)) != 0)
1175                 return -EFAULT;
1176
1177         /*
1178          * Get the target context (task or percpu):
1179          */
1180         ctx = find_get_context(pid, cpu);
1181         if (IS_ERR(ctx))
1182                 return PTR_ERR(ctx);
1183
1184         /*
1185          * Look up the group leader (we will attach this counter to it):
1186          */
1187         group_leader = NULL;
1188         if (group_fd != -1) {
1189                 ret = -EINVAL;
1190                 group_file = fget_light(group_fd, &fput_needed);
1191                 if (!group_file)
1192                         goto err_put_context;
1193                 if (group_file->f_op != &perf_fops)
1194                         goto err_put_context;
1195
1196                 group_leader = group_file->private_data;
1197                 /*
1198                  * Do not allow a recursive hierarchy (this new sibling
1199                  * becoming part of another group-sibling):
1200                  */
1201                 if (group_leader->group_leader != group_leader)
1202                         goto err_put_context;
1203                 /*
1204                  * Do not allow to attach to a group in a different
1205                  * task or CPU context:
1206                  */
1207                 if (group_leader->ctx != ctx)
1208                         goto err_put_context;
1209         }
1210
1211         ret = -EINVAL;
1212         counter = perf_counter_alloc(&hw_event, cpu, group_leader, GFP_KERNEL);
1213         if (!counter)
1214                 goto err_put_context;
1215
1216         ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
1217         if (ret < 0)
1218                 goto err_free_put_context;
1219
1220         counter_file = fget_light(ret, &fput_needed2);
1221         if (!counter_file)
1222                 goto err_free_put_context;
1223
1224         counter->filp = counter_file;
1225         perf_install_in_context(ctx, counter, cpu);
1226
1227         fput_light(counter_file, fput_needed2);
1228
1229 out_fput:
1230         fput_light(group_file, fput_needed);
1231
1232         return ret;
1233
1234 err_free_put_context:
1235         kfree(counter);
1236
1237 err_put_context:
1238         put_context(ctx);
1239
1240         goto out_fput;
1241 }
1242
1243 /*
1244  * Initialize the perf_counter context in a task_struct:
1245  */
1246 static void
1247 __perf_counter_init_context(struct perf_counter_context *ctx,
1248                             struct task_struct *task)
1249 {
1250         memset(ctx, 0, sizeof(*ctx));
1251         spin_lock_init(&ctx->lock);
1252         INIT_LIST_HEAD(&ctx->counter_list);
1253         ctx->task = task;
1254 }
1255
1256 /*
1257  * inherit a counter from parent task to child task:
1258  */
1259 static int
1260 inherit_counter(struct perf_counter *parent_counter,
1261               struct task_struct *parent,
1262               struct perf_counter_context *parent_ctx,
1263               struct task_struct *child,
1264               struct perf_counter_context *child_ctx)
1265 {
1266         struct perf_counter *child_counter;
1267
1268         child_counter = perf_counter_alloc(&parent_counter->hw_event,
1269                                             parent_counter->cpu, NULL,
1270                                             GFP_ATOMIC);
1271         if (!child_counter)
1272                 return -ENOMEM;
1273
1274         /*
1275          * Link it up in the child's context:
1276          */
1277         child_counter->ctx = child_ctx;
1278         child_counter->task = child;
1279         list_add_counter(child_counter, child_ctx);
1280         child_ctx->nr_counters++;
1281
1282         child_counter->parent = parent_counter;
1283         parent_counter->nr_inherited++;
1284         /*
1285          * inherit into child's child as well:
1286          */
1287         child_counter->hw_event.inherit = 1;
1288
1289         /*
1290          * Get a reference to the parent filp - we will fput it
1291          * when the child counter exits. This is safe to do because
1292          * we are in the parent and we know that the filp still
1293          * exists and has a nonzero count:
1294          */
1295         atomic_long_inc(&parent_counter->filp->f_count);
1296
1297         return 0;
1298 }
1299
1300 static void
1301 __perf_counter_exit_task(struct task_struct *child,
1302                          struct perf_counter *child_counter,
1303                          struct perf_counter_context *child_ctx)
1304 {
1305         struct perf_counter *parent_counter;
1306         u64 parent_val, child_val;
1307         unsigned long flags;
1308         u64 perf_flags;
1309
1310         /*
1311          * Disable and unlink this counter.
1312          *
1313          * Be careful about zapping the list - IRQ/NMI context
1314          * could still be processing it:
1315          */
1316         curr_rq_lock_irq_save(&flags);
1317         perf_flags = hw_perf_save_disable();
1318
1319         if (child_counter->state == PERF_COUNTER_STATE_ACTIVE) {
1320                 struct perf_cpu_context *cpuctx;
1321
1322                 cpuctx = &__get_cpu_var(perf_cpu_context);
1323
1324                 child_counter->hw_ops->disable(child_counter);
1325                 child_counter->state = PERF_COUNTER_STATE_INACTIVE;
1326                 child_counter->oncpu = -1;
1327
1328                 cpuctx->active_oncpu--;
1329                 child_ctx->nr_active--;
1330         }
1331
1332         list_del_init(&child_counter->list_entry);
1333
1334         hw_perf_restore(perf_flags);
1335         curr_rq_unlock_irq_restore(&flags);
1336
1337         parent_counter = child_counter->parent;
1338         /*
1339          * It can happen that parent exits first, and has counters
1340          * that are still around due to the child reference. These
1341          * counters need to be zapped - but otherwise linger.
1342          */
1343         if (!parent_counter)
1344                 return;
1345
1346         parent_val = atomic64_read(&parent_counter->count);
1347         child_val = atomic64_read(&child_counter->count);
1348
1349         /*
1350          * Add back the child's count to the parent's count:
1351          */
1352         atomic64_add(child_val, &parent_counter->count);
1353
1354         fput(parent_counter->filp);
1355
1356         kfree(child_counter);
1357 }
1358
1359 /*
1360  * When a child task exist, feed back counter values to parent counters.
1361  *
1362  * Note: we are running in child context, but the PID is not hashed
1363  * anymore so new counters will not be added.
1364  */
1365 void perf_counter_exit_task(struct task_struct *child)
1366 {
1367         struct perf_counter *child_counter, *tmp;
1368         struct perf_counter_context *child_ctx;
1369
1370         child_ctx = &child->perf_counter_ctx;
1371
1372         if (likely(!child_ctx->nr_counters))
1373                 return;
1374
1375         list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
1376                                  list_entry)
1377                 __perf_counter_exit_task(child, child_counter, child_ctx);
1378 }
1379
1380 /*
1381  * Initialize the perf_counter context in task_struct
1382  */
1383 void perf_counter_init_task(struct task_struct *child)
1384 {
1385         struct perf_counter_context *child_ctx, *parent_ctx;
1386         struct perf_counter *counter, *parent_counter;
1387         struct task_struct *parent = current;
1388         unsigned long flags;
1389
1390         child_ctx  =  &child->perf_counter_ctx;
1391         parent_ctx = &parent->perf_counter_ctx;
1392
1393         __perf_counter_init_context(child_ctx, child);
1394
1395         /*
1396          * This is executed from the parent task context, so inherit
1397          * counters that have been marked for cloning:
1398          */
1399
1400         if (likely(!parent_ctx->nr_counters))
1401                 return;
1402
1403         /*
1404          * Lock the parent list. No need to lock the child - not PID
1405          * hashed yet and not running, so nobody can access it.
1406          */
1407         spin_lock_irqsave(&parent_ctx->lock, flags);
1408
1409         /*
1410          * We dont have to disable NMIs - we are only looking at
1411          * the list, not manipulating it:
1412          */
1413         list_for_each_entry(counter, &parent_ctx->counter_list, list_entry) {
1414                 if (!counter->hw_event.inherit || counter->group_leader != counter)
1415                         continue;
1416
1417                 /*
1418                  * Instead of creating recursive hierarchies of counters,
1419                  * we link inheritd counters back to the original parent,
1420                  * which has a filp for sure, which we use as the reference
1421                  * count:
1422                  */
1423                 parent_counter = counter;
1424                 if (counter->parent)
1425                         parent_counter = counter->parent;
1426
1427                 if (inherit_counter(parent_counter, parent,
1428                                   parent_ctx, child, child_ctx))
1429                         break;
1430         }
1431
1432         spin_unlock_irqrestore(&parent_ctx->lock, flags);
1433 }
1434
1435 static void __cpuinit perf_counter_init_cpu(int cpu)
1436 {
1437         struct perf_cpu_context *cpuctx;
1438
1439         cpuctx = &per_cpu(perf_cpu_context, cpu);
1440         __perf_counter_init_context(&cpuctx->ctx, NULL);
1441
1442         mutex_lock(&perf_resource_mutex);
1443         cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
1444         mutex_unlock(&perf_resource_mutex);
1445
1446         hw_perf_counter_setup();
1447 }
1448
1449 #ifdef CONFIG_HOTPLUG_CPU
1450 static void __perf_counter_exit_cpu(void *info)
1451 {
1452         struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1453         struct perf_counter_context *ctx = &cpuctx->ctx;
1454         struct perf_counter *counter, *tmp;
1455
1456         list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
1457                 __perf_counter_remove_from_context(counter);
1458
1459 }
1460 static void perf_counter_exit_cpu(int cpu)
1461 {
1462         smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
1463 }
1464 #else
1465 static inline void perf_counter_exit_cpu(int cpu) { }
1466 #endif
1467
1468 static int __cpuinit
1469 perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
1470 {
1471         unsigned int cpu = (long)hcpu;
1472
1473         switch (action) {
1474
1475         case CPU_UP_PREPARE:
1476         case CPU_UP_PREPARE_FROZEN:
1477                 perf_counter_init_cpu(cpu);
1478                 break;
1479
1480         case CPU_DOWN_PREPARE:
1481         case CPU_DOWN_PREPARE_FROZEN:
1482                 perf_counter_exit_cpu(cpu);
1483                 break;
1484
1485         default:
1486                 break;
1487         }
1488
1489         return NOTIFY_OK;
1490 }
1491
1492 static struct notifier_block __cpuinitdata perf_cpu_nb = {
1493         .notifier_call          = perf_cpu_notify,
1494 };
1495
1496 static int __init perf_counter_init(void)
1497 {
1498         perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
1499                         (void *)(long)smp_processor_id());
1500         register_cpu_notifier(&perf_cpu_nb);
1501
1502         return 0;
1503 }
1504 early_initcall(perf_counter_init);
1505
1506 static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
1507 {
1508         return sprintf(buf, "%d\n", perf_reserved_percpu);
1509 }
1510
1511 static ssize_t
1512 perf_set_reserve_percpu(struct sysdev_class *class,
1513                         const char *buf,
1514                         size_t count)
1515 {
1516         struct perf_cpu_context *cpuctx;
1517         unsigned long val;
1518         int err, cpu, mpt;
1519
1520         err = strict_strtoul(buf, 10, &val);
1521         if (err)
1522                 return err;
1523         if (val > perf_max_counters)
1524                 return -EINVAL;
1525
1526         mutex_lock(&perf_resource_mutex);
1527         perf_reserved_percpu = val;
1528         for_each_online_cpu(cpu) {
1529                 cpuctx = &per_cpu(perf_cpu_context, cpu);
1530                 spin_lock_irq(&cpuctx->ctx.lock);
1531                 mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
1532                           perf_max_counters - perf_reserved_percpu);
1533                 cpuctx->max_pertask = mpt;
1534                 spin_unlock_irq(&cpuctx->ctx.lock);
1535         }
1536         mutex_unlock(&perf_resource_mutex);
1537
1538         return count;
1539 }
1540
1541 static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
1542 {
1543         return sprintf(buf, "%d\n", perf_overcommit);
1544 }
1545
1546 static ssize_t
1547 perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
1548 {
1549         unsigned long val;
1550         int err;
1551
1552         err = strict_strtoul(buf, 10, &val);
1553         if (err)
1554                 return err;
1555         if (val > 1)
1556                 return -EINVAL;
1557
1558         mutex_lock(&perf_resource_mutex);
1559         perf_overcommit = val;
1560         mutex_unlock(&perf_resource_mutex);
1561
1562         return count;
1563 }
1564
1565 static SYSDEV_CLASS_ATTR(
1566                                 reserve_percpu,
1567                                 0644,
1568                                 perf_show_reserve_percpu,
1569                                 perf_set_reserve_percpu
1570                         );
1571
1572 static SYSDEV_CLASS_ATTR(
1573                                 overcommit,
1574                                 0644,
1575                                 perf_show_overcommit,
1576                                 perf_set_overcommit
1577                         );
1578
1579 static struct attribute *perfclass_attrs[] = {
1580         &attr_reserve_percpu.attr,
1581         &attr_overcommit.attr,
1582         NULL
1583 };
1584
1585 static struct attribute_group perfclass_attr_group = {
1586         .attrs                  = perfclass_attrs,
1587         .name                   = "perf_counters",
1588 };
1589
1590 static int __init perf_counter_sysfs_init(void)
1591 {
1592         return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
1593                                   &perfclass_attr_group);
1594 }
1595 device_initcall(perf_counter_sysfs_init);