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