perf_counters: account NMI interrupts
[GitHub/mt8127/android_kernel_alcatel_ttab.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>
aa9c4c0f 21#include <linux/kernel_stat.h>
0793a61d 22#include <linux/perf_counter.h>
23a185ca
PM
23#include <linux/mm.h>
24#include <linux/vmstat.h>
0793a61d
TG
25
26/*
27 * Each CPU has a list of per CPU counters:
28 */
29DEFINE_PER_CPU(struct perf_cpu_context, perf_cpu_context);
30
088e2852 31int perf_max_counters __read_mostly = 1;
0793a61d
TG
32static int perf_reserved_percpu __read_mostly;
33static int perf_overcommit __read_mostly = 1;
34
35/*
36 * Mutex for (sysadmin-configurable) counter reservations:
37 */
38static DEFINE_MUTEX(perf_resource_mutex);
39
40/*
41 * Architecture provided APIs - weak aliases:
42 */
5c92d124 43extern __weak const struct hw_perf_counter_ops *
621a01ea 44hw_perf_counter_init(struct perf_counter *counter)
0793a61d 45{
ff6f0541 46 return NULL;
0793a61d
TG
47}
48
01b2838c 49u64 __weak hw_perf_save_disable(void) { return 0; }
01ea1cca 50void __weak hw_perf_restore(u64 ctrl) { barrier(); }
01d0287f 51void __weak hw_perf_counter_setup(int cpu) { barrier(); }
3cbed429
PM
52int __weak hw_perf_group_sched_in(struct perf_counter *group_leader,
53 struct perf_cpu_context *cpuctx,
54 struct perf_counter_context *ctx, int cpu)
55{
56 return 0;
57}
0793a61d 58
4eb96fcf
PM
59void __weak perf_counter_print_debug(void) { }
60
04289bb9
IM
61static void
62list_add_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
63{
64 struct perf_counter *group_leader = counter->group_leader;
65
66 /*
67 * Depending on whether it is a standalone or sibling counter,
68 * add it straight to the context's counter list, or to the group
69 * leader's sibling list:
70 */
71 if (counter->group_leader == counter)
72 list_add_tail(&counter->list_entry, &ctx->counter_list);
73 else
74 list_add_tail(&counter->list_entry, &group_leader->sibling_list);
75}
76
77static void
78list_del_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
79{
80 struct perf_counter *sibling, *tmp;
81
82 list_del_init(&counter->list_entry);
83
04289bb9
IM
84 /*
85 * If this was a group counter with sibling counters then
86 * upgrade the siblings to singleton counters by adding them
87 * to the context list directly:
88 */
89 list_for_each_entry_safe(sibling, tmp,
90 &counter->sibling_list, list_entry) {
91
92 list_del_init(&sibling->list_entry);
93 list_add_tail(&sibling->list_entry, &ctx->counter_list);
04289bb9
IM
94 sibling->group_leader = sibling;
95 }
96}
97
3b6f9e5c
PM
98static void
99counter_sched_out(struct perf_counter *counter,
100 struct perf_cpu_context *cpuctx,
101 struct perf_counter_context *ctx)
102{
103 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
104 return;
105
106 counter->state = PERF_COUNTER_STATE_INACTIVE;
107 counter->hw_ops->disable(counter);
108 counter->oncpu = -1;
109
110 if (!is_software_counter(counter))
111 cpuctx->active_oncpu--;
112 ctx->nr_active--;
113 if (counter->hw_event.exclusive || !cpuctx->active_oncpu)
114 cpuctx->exclusive = 0;
115}
116
d859e29f
PM
117static void
118group_sched_out(struct perf_counter *group_counter,
119 struct perf_cpu_context *cpuctx,
120 struct perf_counter_context *ctx)
121{
122 struct perf_counter *counter;
123
124 if (group_counter->state != PERF_COUNTER_STATE_ACTIVE)
125 return;
126
127 counter_sched_out(group_counter, cpuctx, ctx);
128
129 /*
130 * Schedule out siblings (if any):
131 */
132 list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
133 counter_sched_out(counter, cpuctx, ctx);
134
135 if (group_counter->hw_event.exclusive)
136 cpuctx->exclusive = 0;
137}
138
0793a61d
TG
139/*
140 * Cross CPU call to remove a performance counter
141 *
142 * We disable the counter on the hardware level first. After that we
143 * remove it from the context list.
144 */
04289bb9 145static void __perf_counter_remove_from_context(void *info)
0793a61d
TG
146{
147 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
148 struct perf_counter *counter = info;
149 struct perf_counter_context *ctx = counter->ctx;
9b51f66d 150 unsigned long flags;
5c92d124 151 u64 perf_flags;
0793a61d
TG
152
153 /*
154 * If this is a task context, we need to check whether it is
155 * the current task context of this cpu. If not it has been
156 * scheduled out before the smp call arrived.
157 */
158 if (ctx->task && cpuctx->task_ctx != ctx)
159 return;
160
aa9c4c0f
IM
161 curr_rq_lock_irq_save(&flags);
162 spin_lock(&ctx->lock);
0793a61d 163
3b6f9e5c
PM
164 counter_sched_out(counter, cpuctx, ctx);
165
166 counter->task = NULL;
0793a61d
TG
167 ctx->nr_counters--;
168
169 /*
170 * Protect the list operation against NMI by disabling the
171 * counters on a global level. NOP for non NMI based counters.
172 */
01b2838c 173 perf_flags = hw_perf_save_disable();
04289bb9 174 list_del_counter(counter, ctx);
01b2838c 175 hw_perf_restore(perf_flags);
0793a61d
TG
176
177 if (!ctx->task) {
178 /*
179 * Allow more per task counters with respect to the
180 * reservation:
181 */
182 cpuctx->max_pertask =
183 min(perf_max_counters - ctx->nr_counters,
184 perf_max_counters - perf_reserved_percpu);
185 }
186
aa9c4c0f
IM
187 spin_unlock(&ctx->lock);
188 curr_rq_unlock_irq_restore(&flags);
0793a61d
TG
189}
190
191
192/*
193 * Remove the counter from a task's (or a CPU's) list of counters.
194 *
d859e29f 195 * Must be called with counter->mutex and ctx->mutex held.
0793a61d
TG
196 *
197 * CPU counters are removed with a smp call. For task counters we only
198 * call when the task is on a CPU.
199 */
04289bb9 200static void perf_counter_remove_from_context(struct perf_counter *counter)
0793a61d
TG
201{
202 struct perf_counter_context *ctx = counter->ctx;
203 struct task_struct *task = ctx->task;
204
205 if (!task) {
206 /*
207 * Per cpu counters are removed via an smp call and
208 * the removal is always sucessful.
209 */
210 smp_call_function_single(counter->cpu,
04289bb9 211 __perf_counter_remove_from_context,
0793a61d
TG
212 counter, 1);
213 return;
214 }
215
216retry:
04289bb9 217 task_oncpu_function_call(task, __perf_counter_remove_from_context,
0793a61d
TG
218 counter);
219
220 spin_lock_irq(&ctx->lock);
221 /*
222 * If the context is active we need to retry the smp call.
223 */
04289bb9 224 if (ctx->nr_active && !list_empty(&counter->list_entry)) {
0793a61d
TG
225 spin_unlock_irq(&ctx->lock);
226 goto retry;
227 }
228
229 /*
230 * The lock prevents that this context is scheduled in so we
04289bb9 231 * can remove the counter safely, if the call above did not
0793a61d
TG
232 * succeed.
233 */
04289bb9 234 if (!list_empty(&counter->list_entry)) {
0793a61d 235 ctx->nr_counters--;
04289bb9 236 list_del_counter(counter, ctx);
0793a61d
TG
237 counter->task = NULL;
238 }
239 spin_unlock_irq(&ctx->lock);
240}
241
d859e29f
PM
242/*
243 * Cross CPU call to disable a performance counter
244 */
245static void __perf_counter_disable(void *info)
246{
247 struct perf_counter *counter = info;
248 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
249 struct perf_counter_context *ctx = counter->ctx;
250 unsigned long flags;
251
252 /*
253 * If this is a per-task counter, need to check whether this
254 * counter's task is the current task on this cpu.
255 */
256 if (ctx->task && cpuctx->task_ctx != ctx)
257 return;
258
259 curr_rq_lock_irq_save(&flags);
260 spin_lock(&ctx->lock);
261
262 /*
263 * If the counter is on, turn it off.
264 * If it is in error state, leave it in error state.
265 */
266 if (counter->state >= PERF_COUNTER_STATE_INACTIVE) {
267 if (counter == counter->group_leader)
268 group_sched_out(counter, cpuctx, ctx);
269 else
270 counter_sched_out(counter, cpuctx, ctx);
271 counter->state = PERF_COUNTER_STATE_OFF;
272 }
273
274 spin_unlock(&ctx->lock);
275 curr_rq_unlock_irq_restore(&flags);
276}
277
278/*
279 * Disable a counter.
280 */
281static void perf_counter_disable(struct perf_counter *counter)
282{
283 struct perf_counter_context *ctx = counter->ctx;
284 struct task_struct *task = ctx->task;
285
286 if (!task) {
287 /*
288 * Disable the counter on the cpu that it's on
289 */
290 smp_call_function_single(counter->cpu, __perf_counter_disable,
291 counter, 1);
292 return;
293 }
294
295 retry:
296 task_oncpu_function_call(task, __perf_counter_disable, counter);
297
298 spin_lock_irq(&ctx->lock);
299 /*
300 * If the counter is still active, we need to retry the cross-call.
301 */
302 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
303 spin_unlock_irq(&ctx->lock);
304 goto retry;
305 }
306
307 /*
308 * Since we have the lock this context can't be scheduled
309 * in, so we can change the state safely.
310 */
311 if (counter->state == PERF_COUNTER_STATE_INACTIVE)
312 counter->state = PERF_COUNTER_STATE_OFF;
313
314 spin_unlock_irq(&ctx->lock);
315}
316
317/*
318 * Disable a counter and all its children.
319 */
320static void perf_counter_disable_family(struct perf_counter *counter)
321{
322 struct perf_counter *child;
323
324 perf_counter_disable(counter);
325
326 /*
327 * Lock the mutex to protect the list of children
328 */
329 mutex_lock(&counter->mutex);
330 list_for_each_entry(child, &counter->child_list, child_list)
331 perf_counter_disable(child);
332 mutex_unlock(&counter->mutex);
333}
334
235c7fc7
IM
335static int
336counter_sched_in(struct perf_counter *counter,
337 struct perf_cpu_context *cpuctx,
338 struct perf_counter_context *ctx,
339 int cpu)
340{
3b6f9e5c 341 if (counter->state <= PERF_COUNTER_STATE_OFF)
235c7fc7
IM
342 return 0;
343
344 counter->state = PERF_COUNTER_STATE_ACTIVE;
345 counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
346 /*
347 * The new state must be visible before we turn it on in the hardware:
348 */
349 smp_wmb();
350
351 if (counter->hw_ops->enable(counter)) {
352 counter->state = PERF_COUNTER_STATE_INACTIVE;
353 counter->oncpu = -1;
354 return -EAGAIN;
355 }
356
3b6f9e5c
PM
357 if (!is_software_counter(counter))
358 cpuctx->active_oncpu++;
235c7fc7
IM
359 ctx->nr_active++;
360
3b6f9e5c
PM
361 if (counter->hw_event.exclusive)
362 cpuctx->exclusive = 1;
363
235c7fc7
IM
364 return 0;
365}
366
3b6f9e5c
PM
367/*
368 * Return 1 for a group consisting entirely of software counters,
369 * 0 if the group contains any hardware counters.
370 */
371static int is_software_only_group(struct perf_counter *leader)
372{
373 struct perf_counter *counter;
374
375 if (!is_software_counter(leader))
376 return 0;
377 list_for_each_entry(counter, &leader->sibling_list, list_entry)
378 if (!is_software_counter(counter))
379 return 0;
380 return 1;
381}
382
383/*
384 * Work out whether we can put this counter group on the CPU now.
385 */
386static int group_can_go_on(struct perf_counter *counter,
387 struct perf_cpu_context *cpuctx,
388 int can_add_hw)
389{
390 /*
391 * Groups consisting entirely of software counters can always go on.
392 */
393 if (is_software_only_group(counter))
394 return 1;
395 /*
396 * If an exclusive group is already on, no other hardware
397 * counters can go on.
398 */
399 if (cpuctx->exclusive)
400 return 0;
401 /*
402 * If this group is exclusive and there are already
403 * counters on the CPU, it can't go on.
404 */
405 if (counter->hw_event.exclusive && cpuctx->active_oncpu)
406 return 0;
407 /*
408 * Otherwise, try to add it if all previous groups were able
409 * to go on.
410 */
411 return can_add_hw;
412}
413
0793a61d 414/*
235c7fc7 415 * Cross CPU call to install and enable a performance counter
0793a61d
TG
416 */
417static void __perf_install_in_context(void *info)
418{
419 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
420 struct perf_counter *counter = info;
421 struct perf_counter_context *ctx = counter->ctx;
d859e29f 422 struct perf_counter *leader = counter->group_leader;
0793a61d 423 int cpu = smp_processor_id();
9b51f66d 424 unsigned long flags;
5c92d124 425 u64 perf_flags;
3b6f9e5c 426 int err;
0793a61d
TG
427
428 /*
429 * If this is a task context, we need to check whether it is
430 * the current task context of this cpu. If not it has been
431 * scheduled out before the smp call arrived.
432 */
433 if (ctx->task && cpuctx->task_ctx != ctx)
434 return;
435
aa9c4c0f
IM
436 curr_rq_lock_irq_save(&flags);
437 spin_lock(&ctx->lock);
0793a61d
TG
438
439 /*
440 * Protect the list operation against NMI by disabling the
441 * counters on a global level. NOP for non NMI based counters.
442 */
01b2838c 443 perf_flags = hw_perf_save_disable();
0793a61d 444
235c7fc7 445 list_add_counter(counter, ctx);
0793a61d
TG
446 ctx->nr_counters++;
447
d859e29f
PM
448 /*
449 * Don't put the counter on if it is disabled or if
450 * it is in a group and the group isn't on.
451 */
452 if (counter->state != PERF_COUNTER_STATE_INACTIVE ||
453 (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE))
454 goto unlock;
455
3b6f9e5c
PM
456 /*
457 * An exclusive counter can't go on if there are already active
458 * hardware counters, and no hardware counter can go on if there
459 * is already an exclusive counter on.
460 */
d859e29f 461 if (!group_can_go_on(counter, cpuctx, 1))
3b6f9e5c
PM
462 err = -EEXIST;
463 else
464 err = counter_sched_in(counter, cpuctx, ctx, cpu);
465
d859e29f
PM
466 if (err) {
467 /*
468 * This counter couldn't go on. If it is in a group
469 * then we have to pull the whole group off.
470 * If the counter group is pinned then put it in error state.
471 */
472 if (leader != counter)
473 group_sched_out(leader, cpuctx, ctx);
474 if (leader->hw_event.pinned)
475 leader->state = PERF_COUNTER_STATE_ERROR;
476 }
0793a61d 477
3b6f9e5c 478 if (!err && !ctx->task && cpuctx->max_pertask)
0793a61d
TG
479 cpuctx->max_pertask--;
480
d859e29f 481 unlock:
235c7fc7
IM
482 hw_perf_restore(perf_flags);
483
aa9c4c0f
IM
484 spin_unlock(&ctx->lock);
485 curr_rq_unlock_irq_restore(&flags);
0793a61d
TG
486}
487
488/*
489 * Attach a performance counter to a context
490 *
491 * First we add the counter to the list with the hardware enable bit
492 * in counter->hw_config cleared.
493 *
494 * If the counter is attached to a task which is on a CPU we use a smp
495 * call to enable it in the task context. The task might have been
496 * scheduled away, but we check this in the smp call again.
d859e29f
PM
497 *
498 * Must be called with ctx->mutex held.
0793a61d
TG
499 */
500static void
501perf_install_in_context(struct perf_counter_context *ctx,
502 struct perf_counter *counter,
503 int cpu)
504{
505 struct task_struct *task = ctx->task;
506
0793a61d
TG
507 if (!task) {
508 /*
509 * Per cpu counters are installed via an smp call and
510 * the install is always sucessful.
511 */
512 smp_call_function_single(cpu, __perf_install_in_context,
513 counter, 1);
514 return;
515 }
516
517 counter->task = task;
518retry:
519 task_oncpu_function_call(task, __perf_install_in_context,
520 counter);
521
522 spin_lock_irq(&ctx->lock);
523 /*
0793a61d
TG
524 * we need to retry the smp call.
525 */
d859e29f 526 if (ctx->is_active && list_empty(&counter->list_entry)) {
0793a61d
TG
527 spin_unlock_irq(&ctx->lock);
528 goto retry;
529 }
530
531 /*
532 * The lock prevents that this context is scheduled in so we
533 * can add the counter safely, if it the call above did not
534 * succeed.
535 */
04289bb9
IM
536 if (list_empty(&counter->list_entry)) {
537 list_add_counter(counter, ctx);
0793a61d
TG
538 ctx->nr_counters++;
539 }
540 spin_unlock_irq(&ctx->lock);
541}
542
d859e29f
PM
543/*
544 * Cross CPU call to enable a performance counter
545 */
546static void __perf_counter_enable(void *info)
04289bb9 547{
d859e29f
PM
548 struct perf_counter *counter = info;
549 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
550 struct perf_counter_context *ctx = counter->ctx;
551 struct perf_counter *leader = counter->group_leader;
552 unsigned long flags;
553 int err;
04289bb9 554
d859e29f
PM
555 /*
556 * If this is a per-task counter, need to check whether this
557 * counter's task is the current task on this cpu.
558 */
559 if (ctx->task && cpuctx->task_ctx != ctx)
3cbed429
PM
560 return;
561
d859e29f
PM
562 curr_rq_lock_irq_save(&flags);
563 spin_lock(&ctx->lock);
564
565 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
566 goto unlock;
567 counter->state = PERF_COUNTER_STATE_INACTIVE;
04289bb9
IM
568
569 /*
d859e29f
PM
570 * If the counter is in a group and isn't the group leader,
571 * then don't put it on unless the group is on.
04289bb9 572 */
d859e29f
PM
573 if (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE)
574 goto unlock;
3b6f9e5c 575
d859e29f
PM
576 if (!group_can_go_on(counter, cpuctx, 1))
577 err = -EEXIST;
578 else
579 err = counter_sched_in(counter, cpuctx, ctx,
580 smp_processor_id());
581
582 if (err) {
583 /*
584 * If this counter can't go on and it's part of a
585 * group, then the whole group has to come off.
586 */
587 if (leader != counter)
588 group_sched_out(leader, cpuctx, ctx);
589 if (leader->hw_event.pinned)
590 leader->state = PERF_COUNTER_STATE_ERROR;
591 }
592
593 unlock:
594 spin_unlock(&ctx->lock);
595 curr_rq_unlock_irq_restore(&flags);
596}
597
598/*
599 * Enable a counter.
600 */
601static void perf_counter_enable(struct perf_counter *counter)
602{
603 struct perf_counter_context *ctx = counter->ctx;
604 struct task_struct *task = ctx->task;
605
606 if (!task) {
607 /*
608 * Enable the counter on the cpu that it's on
609 */
610 smp_call_function_single(counter->cpu, __perf_counter_enable,
611 counter, 1);
612 return;
613 }
614
615 spin_lock_irq(&ctx->lock);
616 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
617 goto out;
618
619 /*
620 * If the counter is in error state, clear that first.
621 * That way, if we see the counter in error state below, we
622 * know that it has gone back into error state, as distinct
623 * from the task having been scheduled away before the
624 * cross-call arrived.
625 */
626 if (counter->state == PERF_COUNTER_STATE_ERROR)
627 counter->state = PERF_COUNTER_STATE_OFF;
628
629 retry:
630 spin_unlock_irq(&ctx->lock);
631 task_oncpu_function_call(task, __perf_counter_enable, counter);
632
633 spin_lock_irq(&ctx->lock);
634
635 /*
636 * If the context is active and the counter is still off,
637 * we need to retry the cross-call.
638 */
639 if (ctx->is_active && counter->state == PERF_COUNTER_STATE_OFF)
640 goto retry;
641
642 /*
643 * Since we have the lock this context can't be scheduled
644 * in, so we can change the state safely.
645 */
646 if (counter->state == PERF_COUNTER_STATE_OFF)
647 counter->state = PERF_COUNTER_STATE_INACTIVE;
648 out:
649 spin_unlock_irq(&ctx->lock);
650}
651
652/*
653 * Enable a counter and all its children.
654 */
655static void perf_counter_enable_family(struct perf_counter *counter)
656{
657 struct perf_counter *child;
658
659 perf_counter_enable(counter);
660
661 /*
662 * Lock the mutex to protect the list of children
663 */
664 mutex_lock(&counter->mutex);
665 list_for_each_entry(child, &counter->child_list, child_list)
666 perf_counter_enable(child);
667 mutex_unlock(&counter->mutex);
04289bb9
IM
668}
669
235c7fc7
IM
670void __perf_counter_sched_out(struct perf_counter_context *ctx,
671 struct perf_cpu_context *cpuctx)
672{
673 struct perf_counter *counter;
3cbed429 674 u64 flags;
235c7fc7 675
d859e29f
PM
676 spin_lock(&ctx->lock);
677 ctx->is_active = 0;
235c7fc7 678 if (likely(!ctx->nr_counters))
d859e29f 679 goto out;
235c7fc7 680
3cbed429 681 flags = hw_perf_save_disable();
235c7fc7
IM
682 if (ctx->nr_active) {
683 list_for_each_entry(counter, &ctx->counter_list, list_entry)
684 group_sched_out(counter, cpuctx, ctx);
685 }
3cbed429 686 hw_perf_restore(flags);
d859e29f 687 out:
235c7fc7
IM
688 spin_unlock(&ctx->lock);
689}
690
0793a61d
TG
691/*
692 * Called from scheduler to remove the counters of the current task,
693 * with interrupts disabled.
694 *
695 * We stop each counter and update the counter value in counter->count.
696 *
7671581f 697 * This does not protect us against NMI, but disable()
0793a61d
TG
698 * sets the disabled bit in the control field of counter _before_
699 * accessing the counter control register. If a NMI hits, then it will
700 * not restart the counter.
701 */
702void perf_counter_task_sched_out(struct task_struct *task, int cpu)
703{
704 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
705 struct perf_counter_context *ctx = &task->perf_counter_ctx;
0793a61d
TG
706
707 if (likely(!cpuctx->task_ctx))
708 return;
709
235c7fc7
IM
710 __perf_counter_sched_out(ctx, cpuctx);
711
0793a61d
TG
712 cpuctx->task_ctx = NULL;
713}
714
235c7fc7 715static void perf_counter_cpu_sched_out(struct perf_cpu_context *cpuctx)
04289bb9 716{
235c7fc7 717 __perf_counter_sched_out(&cpuctx->ctx, cpuctx);
04289bb9
IM
718}
719
7995888f 720static int
04289bb9
IM
721group_sched_in(struct perf_counter *group_counter,
722 struct perf_cpu_context *cpuctx,
723 struct perf_counter_context *ctx,
724 int cpu)
725{
95cdd2e7 726 struct perf_counter *counter, *partial_group;
3cbed429
PM
727 int ret;
728
729 if (group_counter->state == PERF_COUNTER_STATE_OFF)
730 return 0;
731
732 ret = hw_perf_group_sched_in(group_counter, cpuctx, ctx, cpu);
733 if (ret)
734 return ret < 0 ? ret : 0;
04289bb9 735
95cdd2e7
IM
736 if (counter_sched_in(group_counter, cpuctx, ctx, cpu))
737 return -EAGAIN;
04289bb9
IM
738
739 /*
740 * Schedule in siblings as one group (if any):
741 */
7995888f 742 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
95cdd2e7
IM
743 if (counter_sched_in(counter, cpuctx, ctx, cpu)) {
744 partial_group = counter;
745 goto group_error;
746 }
95cdd2e7
IM
747 }
748
3cbed429 749 return 0;
95cdd2e7
IM
750
751group_error:
752 /*
753 * Groups can be scheduled in as one unit only, so undo any
754 * partial group before returning:
755 */
756 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
757 if (counter == partial_group)
758 break;
759 counter_sched_out(counter, cpuctx, ctx);
7995888f 760 }
95cdd2e7 761 counter_sched_out(group_counter, cpuctx, ctx);
7995888f 762
95cdd2e7 763 return -EAGAIN;
04289bb9
IM
764}
765
235c7fc7
IM
766static void
767__perf_counter_sched_in(struct perf_counter_context *ctx,
768 struct perf_cpu_context *cpuctx, int cpu)
0793a61d 769{
0793a61d 770 struct perf_counter *counter;
3cbed429 771 u64 flags;
dd0e6ba2 772 int can_add_hw = 1;
0793a61d 773
d859e29f
PM
774 spin_lock(&ctx->lock);
775 ctx->is_active = 1;
0793a61d 776 if (likely(!ctx->nr_counters))
d859e29f 777 goto out;
0793a61d 778
3cbed429 779 flags = hw_perf_save_disable();
3b6f9e5c
PM
780
781 /*
782 * First go through the list and put on any pinned groups
783 * in order to give them the best chance of going on.
784 */
785 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
786 if (counter->state <= PERF_COUNTER_STATE_OFF ||
787 !counter->hw_event.pinned)
788 continue;
789 if (counter->cpu != -1 && counter->cpu != cpu)
790 continue;
791
792 if (group_can_go_on(counter, cpuctx, 1))
793 group_sched_in(counter, cpuctx, ctx, cpu);
794
795 /*
796 * If this pinned group hasn't been scheduled,
797 * put it in error state.
798 */
799 if (counter->state == PERF_COUNTER_STATE_INACTIVE)
800 counter->state = PERF_COUNTER_STATE_ERROR;
801 }
802
04289bb9 803 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
3b6f9e5c
PM
804 /*
805 * Ignore counters in OFF or ERROR state, and
806 * ignore pinned counters since we did them already.
807 */
808 if (counter->state <= PERF_COUNTER_STATE_OFF ||
809 counter->hw_event.pinned)
810 continue;
811
04289bb9
IM
812 /*
813 * Listen to the 'cpu' scheduling filter constraint
814 * of counters:
815 */
0793a61d
TG
816 if (counter->cpu != -1 && counter->cpu != cpu)
817 continue;
818
3b6f9e5c 819 if (group_can_go_on(counter, cpuctx, can_add_hw)) {
dd0e6ba2
PM
820 if (group_sched_in(counter, cpuctx, ctx, cpu))
821 can_add_hw = 0;
3b6f9e5c 822 }
0793a61d 823 }
3cbed429 824 hw_perf_restore(flags);
d859e29f 825 out:
0793a61d 826 spin_unlock(&ctx->lock);
235c7fc7
IM
827}
828
829/*
830 * Called from scheduler to add the counters of the current task
831 * with interrupts disabled.
832 *
833 * We restore the counter value and then enable it.
834 *
835 * This does not protect us against NMI, but enable()
836 * sets the enabled bit in the control field of counter _before_
837 * accessing the counter control register. If a NMI hits, then it will
838 * keep the counter running.
839 */
840void perf_counter_task_sched_in(struct task_struct *task, int cpu)
841{
842 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
843 struct perf_counter_context *ctx = &task->perf_counter_ctx;
04289bb9 844
235c7fc7 845 __perf_counter_sched_in(ctx, cpuctx, cpu);
0793a61d
TG
846 cpuctx->task_ctx = ctx;
847}
848
235c7fc7
IM
849static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu)
850{
851 struct perf_counter_context *ctx = &cpuctx->ctx;
852
853 __perf_counter_sched_in(ctx, cpuctx, cpu);
854}
855
1d1c7ddb
IM
856int perf_counter_task_disable(void)
857{
858 struct task_struct *curr = current;
859 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
860 struct perf_counter *counter;
aa9c4c0f 861 unsigned long flags;
1d1c7ddb
IM
862 u64 perf_flags;
863 int cpu;
864
865 if (likely(!ctx->nr_counters))
866 return 0;
867
aa9c4c0f 868 curr_rq_lock_irq_save(&flags);
1d1c7ddb
IM
869 cpu = smp_processor_id();
870
aa9c4c0f
IM
871 /* force the update of the task clock: */
872 __task_delta_exec(curr, 1);
873
1d1c7ddb
IM
874 perf_counter_task_sched_out(curr, cpu);
875
876 spin_lock(&ctx->lock);
877
878 /*
879 * Disable all the counters:
880 */
881 perf_flags = hw_perf_save_disable();
882
3b6f9e5c
PM
883 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
884 if (counter->state != PERF_COUNTER_STATE_ERROR)
885 counter->state = PERF_COUNTER_STATE_OFF;
886 }
9b51f66d 887
1d1c7ddb
IM
888 hw_perf_restore(perf_flags);
889
890 spin_unlock(&ctx->lock);
891
aa9c4c0f 892 curr_rq_unlock_irq_restore(&flags);
1d1c7ddb
IM
893
894 return 0;
895}
896
897int perf_counter_task_enable(void)
898{
899 struct task_struct *curr = current;
900 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
901 struct perf_counter *counter;
aa9c4c0f 902 unsigned long flags;
1d1c7ddb
IM
903 u64 perf_flags;
904 int cpu;
905
906 if (likely(!ctx->nr_counters))
907 return 0;
908
aa9c4c0f 909 curr_rq_lock_irq_save(&flags);
1d1c7ddb
IM
910 cpu = smp_processor_id();
911
aa9c4c0f
IM
912 /* force the update of the task clock: */
913 __task_delta_exec(curr, 1);
914
235c7fc7
IM
915 perf_counter_task_sched_out(curr, cpu);
916
1d1c7ddb
IM
917 spin_lock(&ctx->lock);
918
919 /*
920 * Disable all the counters:
921 */
922 perf_flags = hw_perf_save_disable();
923
924 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
3b6f9e5c 925 if (counter->state > PERF_COUNTER_STATE_OFF)
1d1c7ddb 926 continue;
6a930700 927 counter->state = PERF_COUNTER_STATE_INACTIVE;
aa9c4c0f 928 counter->hw_event.disabled = 0;
1d1c7ddb
IM
929 }
930 hw_perf_restore(perf_flags);
931
932 spin_unlock(&ctx->lock);
933
934 perf_counter_task_sched_in(curr, cpu);
935
aa9c4c0f 936 curr_rq_unlock_irq_restore(&flags);
1d1c7ddb
IM
937
938 return 0;
939}
940
235c7fc7
IM
941/*
942 * Round-robin a context's counters:
943 */
944static void rotate_ctx(struct perf_counter_context *ctx)
0793a61d 945{
0793a61d 946 struct perf_counter *counter;
5c92d124 947 u64 perf_flags;
0793a61d 948
235c7fc7 949 if (!ctx->nr_counters)
0793a61d
TG
950 return;
951
0793a61d 952 spin_lock(&ctx->lock);
0793a61d 953 /*
04289bb9 954 * Rotate the first entry last (works just fine for group counters too):
0793a61d 955 */
01b2838c 956 perf_flags = hw_perf_save_disable();
04289bb9
IM
957 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
958 list_del(&counter->list_entry);
959 list_add_tail(&counter->list_entry, &ctx->counter_list);
0793a61d
TG
960 break;
961 }
01b2838c 962 hw_perf_restore(perf_flags);
0793a61d
TG
963
964 spin_unlock(&ctx->lock);
235c7fc7
IM
965}
966
967void perf_counter_task_tick(struct task_struct *curr, int cpu)
968{
969 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
970 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
971 const int rotate_percpu = 0;
972
973 if (rotate_percpu)
974 perf_counter_cpu_sched_out(cpuctx);
975 perf_counter_task_sched_out(curr, cpu);
0793a61d 976
235c7fc7
IM
977 if (rotate_percpu)
978 rotate_ctx(&cpuctx->ctx);
979 rotate_ctx(ctx);
980
981 if (rotate_percpu)
982 perf_counter_cpu_sched_in(cpuctx, cpu);
0793a61d
TG
983 perf_counter_task_sched_in(curr, cpu);
984}
985
0793a61d
TG
986/*
987 * Cross CPU call to read the hardware counter
988 */
7671581f 989static void __read(void *info)
0793a61d 990{
621a01ea 991 struct perf_counter *counter = info;
aa9c4c0f 992 unsigned long flags;
621a01ea 993
aa9c4c0f 994 curr_rq_lock_irq_save(&flags);
7671581f 995 counter->hw_ops->read(counter);
aa9c4c0f 996 curr_rq_unlock_irq_restore(&flags);
0793a61d
TG
997}
998
04289bb9 999static u64 perf_counter_read(struct perf_counter *counter)
0793a61d
TG
1000{
1001 /*
1002 * If counter is enabled and currently active on a CPU, update the
1003 * value in the counter structure:
1004 */
6a930700 1005 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
0793a61d 1006 smp_call_function_single(counter->oncpu,
7671581f 1007 __read, counter, 1);
0793a61d
TG
1008 }
1009
ee06094f 1010 return atomic64_read(&counter->count);
0793a61d
TG
1011}
1012
1013/*
1014 * Cross CPU call to switch performance data pointers
1015 */
1016static void __perf_switch_irq_data(void *info)
1017{
1018 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1019 struct perf_counter *counter = info;
1020 struct perf_counter_context *ctx = counter->ctx;
1021 struct perf_data *oldirqdata = counter->irqdata;
1022
1023 /*
1024 * If this is a task context, we need to check whether it is
1025 * the current task context of this cpu. If not it has been
1026 * scheduled out before the smp call arrived.
1027 */
1028 if (ctx->task) {
1029 if (cpuctx->task_ctx != ctx)
1030 return;
1031 spin_lock(&ctx->lock);
1032 }
1033
1034 /* Change the pointer NMI safe */
1035 atomic_long_set((atomic_long_t *)&counter->irqdata,
1036 (unsigned long) counter->usrdata);
1037 counter->usrdata = oldirqdata;
1038
1039 if (ctx->task)
1040 spin_unlock(&ctx->lock);
1041}
1042
1043static struct perf_data *perf_switch_irq_data(struct perf_counter *counter)
1044{
1045 struct perf_counter_context *ctx = counter->ctx;
1046 struct perf_data *oldirqdata = counter->irqdata;
1047 struct task_struct *task = ctx->task;
1048
1049 if (!task) {
1050 smp_call_function_single(counter->cpu,
1051 __perf_switch_irq_data,
1052 counter, 1);
1053 return counter->usrdata;
1054 }
1055
1056retry:
1057 spin_lock_irq(&ctx->lock);
6a930700 1058 if (counter->state != PERF_COUNTER_STATE_ACTIVE) {
0793a61d
TG
1059 counter->irqdata = counter->usrdata;
1060 counter->usrdata = oldirqdata;
1061 spin_unlock_irq(&ctx->lock);
1062 return oldirqdata;
1063 }
1064 spin_unlock_irq(&ctx->lock);
1065 task_oncpu_function_call(task, __perf_switch_irq_data, counter);
1066 /* Might have failed, because task was scheduled out */
1067 if (counter->irqdata == oldirqdata)
1068 goto retry;
1069
1070 return counter->usrdata;
1071}
1072
1073static void put_context(struct perf_counter_context *ctx)
1074{
1075 if (ctx->task)
1076 put_task_struct(ctx->task);
1077}
1078
1079static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
1080{
1081 struct perf_cpu_context *cpuctx;
1082 struct perf_counter_context *ctx;
1083 struct task_struct *task;
1084
1085 /*
1086 * If cpu is not a wildcard then this is a percpu counter:
1087 */
1088 if (cpu != -1) {
1089 /* Must be root to operate on a CPU counter: */
1090 if (!capable(CAP_SYS_ADMIN))
1091 return ERR_PTR(-EACCES);
1092
1093 if (cpu < 0 || cpu > num_possible_cpus())
1094 return ERR_PTR(-EINVAL);
1095
1096 /*
1097 * We could be clever and allow to attach a counter to an
1098 * offline CPU and activate it when the CPU comes up, but
1099 * that's for later.
1100 */
1101 if (!cpu_isset(cpu, cpu_online_map))
1102 return ERR_PTR(-ENODEV);
1103
1104 cpuctx = &per_cpu(perf_cpu_context, cpu);
1105 ctx = &cpuctx->ctx;
1106
0793a61d
TG
1107 return ctx;
1108 }
1109
1110 rcu_read_lock();
1111 if (!pid)
1112 task = current;
1113 else
1114 task = find_task_by_vpid(pid);
1115 if (task)
1116 get_task_struct(task);
1117 rcu_read_unlock();
1118
1119 if (!task)
1120 return ERR_PTR(-ESRCH);
1121
1122 ctx = &task->perf_counter_ctx;
1123 ctx->task = task;
1124
1125 /* Reuse ptrace permission checks for now. */
1126 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
1127 put_context(ctx);
1128 return ERR_PTR(-EACCES);
1129 }
1130
1131 return ctx;
1132}
1133
1134/*
1135 * Called when the last reference to the file is gone.
1136 */
1137static int perf_release(struct inode *inode, struct file *file)
1138{
1139 struct perf_counter *counter = file->private_data;
1140 struct perf_counter_context *ctx = counter->ctx;
1141
1142 file->private_data = NULL;
1143
d859e29f 1144 mutex_lock(&ctx->mutex);
0793a61d
TG
1145 mutex_lock(&counter->mutex);
1146
04289bb9 1147 perf_counter_remove_from_context(counter);
0793a61d
TG
1148 put_context(ctx);
1149
1150 mutex_unlock(&counter->mutex);
d859e29f 1151 mutex_unlock(&ctx->mutex);
0793a61d
TG
1152
1153 kfree(counter);
1154
1155 return 0;
1156}
1157
1158/*
1159 * Read the performance counter - simple non blocking version for now
1160 */
1161static ssize_t
1162perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
1163{
1164 u64 cntval;
1165
1166 if (count != sizeof(cntval))
1167 return -EINVAL;
1168
3b6f9e5c
PM
1169 /*
1170 * Return end-of-file for a read on a counter that is in
1171 * error state (i.e. because it was pinned but it couldn't be
1172 * scheduled on to the CPU at some point).
1173 */
1174 if (counter->state == PERF_COUNTER_STATE_ERROR)
1175 return 0;
1176
0793a61d 1177 mutex_lock(&counter->mutex);
04289bb9 1178 cntval = perf_counter_read(counter);
0793a61d
TG
1179 mutex_unlock(&counter->mutex);
1180
1181 return put_user(cntval, (u64 __user *) buf) ? -EFAULT : sizeof(cntval);
1182}
1183
1184static ssize_t
1185perf_copy_usrdata(struct perf_data *usrdata, char __user *buf, size_t count)
1186{
1187 if (!usrdata->len)
1188 return 0;
1189
1190 count = min(count, (size_t)usrdata->len);
1191 if (copy_to_user(buf, usrdata->data + usrdata->rd_idx, count))
1192 return -EFAULT;
1193
1194 /* Adjust the counters */
1195 usrdata->len -= count;
1196 if (!usrdata->len)
1197 usrdata->rd_idx = 0;
1198 else
1199 usrdata->rd_idx += count;
1200
1201 return count;
1202}
1203
1204static ssize_t
1205perf_read_irq_data(struct perf_counter *counter,
1206 char __user *buf,
1207 size_t count,
1208 int nonblocking)
1209{
1210 struct perf_data *irqdata, *usrdata;
1211 DECLARE_WAITQUEUE(wait, current);
3b6f9e5c 1212 ssize_t res, res2;
0793a61d
TG
1213
1214 irqdata = counter->irqdata;
1215 usrdata = counter->usrdata;
1216
1217 if (usrdata->len + irqdata->len >= count)
1218 goto read_pending;
1219
1220 if (nonblocking)
1221 return -EAGAIN;
1222
1223 spin_lock_irq(&counter->waitq.lock);
1224 __add_wait_queue(&counter->waitq, &wait);
1225 for (;;) {
1226 set_current_state(TASK_INTERRUPTIBLE);
1227 if (usrdata->len + irqdata->len >= count)
1228 break;
1229
1230 if (signal_pending(current))
1231 break;
1232
3b6f9e5c
PM
1233 if (counter->state == PERF_COUNTER_STATE_ERROR)
1234 break;
1235
0793a61d
TG
1236 spin_unlock_irq(&counter->waitq.lock);
1237 schedule();
1238 spin_lock_irq(&counter->waitq.lock);
1239 }
1240 __remove_wait_queue(&counter->waitq, &wait);
1241 __set_current_state(TASK_RUNNING);
1242 spin_unlock_irq(&counter->waitq.lock);
1243
3b6f9e5c
PM
1244 if (usrdata->len + irqdata->len < count &&
1245 counter->state != PERF_COUNTER_STATE_ERROR)
0793a61d
TG
1246 return -ERESTARTSYS;
1247read_pending:
1248 mutex_lock(&counter->mutex);
1249
1250 /* Drain pending data first: */
1251 res = perf_copy_usrdata(usrdata, buf, count);
1252 if (res < 0 || res == count)
1253 goto out;
1254
1255 /* Switch irq buffer: */
1256 usrdata = perf_switch_irq_data(counter);
3b6f9e5c
PM
1257 res2 = perf_copy_usrdata(usrdata, buf + res, count - res);
1258 if (res2 < 0) {
0793a61d
TG
1259 if (!res)
1260 res = -EFAULT;
1261 } else {
3b6f9e5c 1262 res += res2;
0793a61d
TG
1263 }
1264out:
1265 mutex_unlock(&counter->mutex);
1266
1267 return res;
1268}
1269
1270static ssize_t
1271perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
1272{
1273 struct perf_counter *counter = file->private_data;
1274
9f66a381 1275 switch (counter->hw_event.record_type) {
0793a61d
TG
1276 case PERF_RECORD_SIMPLE:
1277 return perf_read_hw(counter, buf, count);
1278
1279 case PERF_RECORD_IRQ:
1280 case PERF_RECORD_GROUP:
1281 return perf_read_irq_data(counter, buf, count,
1282 file->f_flags & O_NONBLOCK);
1283 }
1284 return -EINVAL;
1285}
1286
1287static unsigned int perf_poll(struct file *file, poll_table *wait)
1288{
1289 struct perf_counter *counter = file->private_data;
1290 unsigned int events = 0;
1291 unsigned long flags;
1292
1293 poll_wait(file, &counter->waitq, wait);
1294
1295 spin_lock_irqsave(&counter->waitq.lock, flags);
1296 if (counter->usrdata->len || counter->irqdata->len)
1297 events |= POLLIN;
1298 spin_unlock_irqrestore(&counter->waitq.lock, flags);
1299
1300 return events;
1301}
1302
d859e29f
PM
1303static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1304{
1305 struct perf_counter *counter = file->private_data;
1306 int err = 0;
1307
1308 switch (cmd) {
1309 case PERF_COUNTER_IOC_ENABLE:
1310 perf_counter_enable_family(counter);
1311 break;
1312 case PERF_COUNTER_IOC_DISABLE:
1313 perf_counter_disable_family(counter);
1314 break;
1315 default:
1316 err = -ENOTTY;
1317 }
1318 return err;
1319}
1320
0793a61d
TG
1321static const struct file_operations perf_fops = {
1322 .release = perf_release,
1323 .read = perf_read,
1324 .poll = perf_poll,
d859e29f
PM
1325 .unlocked_ioctl = perf_ioctl,
1326 .compat_ioctl = perf_ioctl,
0793a61d
TG
1327};
1328
95cdd2e7 1329static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
5c92d124 1330{
9abf8a08
PM
1331 int cpu = raw_smp_processor_id();
1332
1333 atomic64_set(&counter->hw.prev_count, cpu_clock(cpu));
95cdd2e7 1334 return 0;
5c92d124
IM
1335}
1336
9abf8a08
PM
1337static void cpu_clock_perf_counter_update(struct perf_counter *counter)
1338{
1339 int cpu = raw_smp_processor_id();
1340 s64 prev;
1341 u64 now;
1342
1343 now = cpu_clock(cpu);
1344 prev = atomic64_read(&counter->hw.prev_count);
1345 atomic64_set(&counter->hw.prev_count, now);
1346 atomic64_add(now - prev, &counter->count);
1347}
1348
5c92d124
IM
1349static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
1350{
9abf8a08 1351 cpu_clock_perf_counter_update(counter);
5c92d124
IM
1352}
1353
1354static void cpu_clock_perf_counter_read(struct perf_counter *counter)
1355{
9abf8a08 1356 cpu_clock_perf_counter_update(counter);
5c92d124
IM
1357}
1358
1359static const struct hw_perf_counter_ops perf_ops_cpu_clock = {
7671581f
IM
1360 .enable = cpu_clock_perf_counter_enable,
1361 .disable = cpu_clock_perf_counter_disable,
1362 .read = cpu_clock_perf_counter_read,
5c92d124
IM
1363};
1364
aa9c4c0f
IM
1365/*
1366 * Called from within the scheduler:
1367 */
1368static u64 task_clock_perf_counter_val(struct perf_counter *counter, int update)
bae43c99 1369{
aa9c4c0f
IM
1370 struct task_struct *curr = counter->task;
1371 u64 delta;
1372
aa9c4c0f
IM
1373 delta = __task_delta_exec(curr, update);
1374
1375 return curr->se.sum_exec_runtime + delta;
1376}
1377
1378static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
1379{
1380 u64 prev;
8cb391e8
IM
1381 s64 delta;
1382
1383 prev = atomic64_read(&counter->hw.prev_count);
8cb391e8
IM
1384
1385 atomic64_set(&counter->hw.prev_count, now);
1386
1387 delta = now - prev;
8cb391e8
IM
1388
1389 atomic64_add(delta, &counter->count);
bae43c99
IM
1390}
1391
8cb391e8 1392static void task_clock_perf_counter_read(struct perf_counter *counter)
bae43c99 1393{
aa9c4c0f
IM
1394 u64 now = task_clock_perf_counter_val(counter, 1);
1395
1396 task_clock_perf_counter_update(counter, now);
bae43c99
IM
1397}
1398
95cdd2e7 1399static int task_clock_perf_counter_enable(struct perf_counter *counter)
8cb391e8 1400{
aa9c4c0f
IM
1401 u64 now = task_clock_perf_counter_val(counter, 0);
1402
1403 atomic64_set(&counter->hw.prev_count, now);
95cdd2e7
IM
1404
1405 return 0;
8cb391e8
IM
1406}
1407
1408static void task_clock_perf_counter_disable(struct perf_counter *counter)
bae43c99 1409{
aa9c4c0f
IM
1410 u64 now = task_clock_perf_counter_val(counter, 0);
1411
1412 task_clock_perf_counter_update(counter, now);
bae43c99
IM
1413}
1414
1415static const struct hw_perf_counter_ops perf_ops_task_clock = {
7671581f
IM
1416 .enable = task_clock_perf_counter_enable,
1417 .disable = task_clock_perf_counter_disable,
1418 .read = task_clock_perf_counter_read,
bae43c99
IM
1419};
1420
23a185ca
PM
1421#ifdef CONFIG_VM_EVENT_COUNTERS
1422#define cpu_page_faults() __get_cpu_var(vm_event_states).event[PGFAULT]
1423#else
1424#define cpu_page_faults() 0
1425#endif
1426
1427static u64 get_page_faults(struct perf_counter *counter)
e06c61a8 1428{
23a185ca 1429 struct task_struct *curr = counter->ctx->task;
e06c61a8 1430
23a185ca
PM
1431 if (curr)
1432 return curr->maj_flt + curr->min_flt;
1433 return cpu_page_faults();
e06c61a8
IM
1434}
1435
1436static void page_faults_perf_counter_update(struct perf_counter *counter)
1437{
1438 u64 prev, now;
1439 s64 delta;
1440
1441 prev = atomic64_read(&counter->hw.prev_count);
23a185ca 1442 now = get_page_faults(counter);
e06c61a8
IM
1443
1444 atomic64_set(&counter->hw.prev_count, now);
1445
1446 delta = now - prev;
e06c61a8
IM
1447
1448 atomic64_add(delta, &counter->count);
1449}
1450
1451static void page_faults_perf_counter_read(struct perf_counter *counter)
1452{
1453 page_faults_perf_counter_update(counter);
1454}
1455
95cdd2e7 1456static int page_faults_perf_counter_enable(struct perf_counter *counter)
e06c61a8 1457{
23a185ca 1458 atomic64_set(&counter->hw.prev_count, get_page_faults(counter));
95cdd2e7 1459 return 0;
e06c61a8
IM
1460}
1461
1462static void page_faults_perf_counter_disable(struct perf_counter *counter)
1463{
1464 page_faults_perf_counter_update(counter);
1465}
1466
1467static const struct hw_perf_counter_ops perf_ops_page_faults = {
7671581f
IM
1468 .enable = page_faults_perf_counter_enable,
1469 .disable = page_faults_perf_counter_disable,
1470 .read = page_faults_perf_counter_read,
e06c61a8
IM
1471};
1472
23a185ca 1473static u64 get_context_switches(struct perf_counter *counter)
5d6a27d8 1474{
23a185ca 1475 struct task_struct *curr = counter->ctx->task;
5d6a27d8 1476
23a185ca
PM
1477 if (curr)
1478 return curr->nvcsw + curr->nivcsw;
1479 return cpu_nr_switches(smp_processor_id());
5d6a27d8
IM
1480}
1481
1482static void context_switches_perf_counter_update(struct perf_counter *counter)
1483{
1484 u64 prev, now;
1485 s64 delta;
1486
1487 prev = atomic64_read(&counter->hw.prev_count);
23a185ca 1488 now = get_context_switches(counter);
5d6a27d8
IM
1489
1490 atomic64_set(&counter->hw.prev_count, now);
1491
1492 delta = now - prev;
5d6a27d8
IM
1493
1494 atomic64_add(delta, &counter->count);
1495}
1496
1497static void context_switches_perf_counter_read(struct perf_counter *counter)
1498{
1499 context_switches_perf_counter_update(counter);
1500}
1501
95cdd2e7 1502static int context_switches_perf_counter_enable(struct perf_counter *counter)
5d6a27d8 1503{
23a185ca 1504 atomic64_set(&counter->hw.prev_count, get_context_switches(counter));
95cdd2e7 1505 return 0;
5d6a27d8
IM
1506}
1507
1508static void context_switches_perf_counter_disable(struct perf_counter *counter)
1509{
1510 context_switches_perf_counter_update(counter);
1511}
1512
1513static const struct hw_perf_counter_ops perf_ops_context_switches = {
7671581f
IM
1514 .enable = context_switches_perf_counter_enable,
1515 .disable = context_switches_perf_counter_disable,
1516 .read = context_switches_perf_counter_read,
5d6a27d8
IM
1517};
1518
23a185ca 1519static inline u64 get_cpu_migrations(struct perf_counter *counter)
6c594c21 1520{
23a185ca
PM
1521 struct task_struct *curr = counter->ctx->task;
1522
1523 if (curr)
1524 return curr->se.nr_migrations;
1525 return cpu_nr_migrations(smp_processor_id());
6c594c21
IM
1526}
1527
1528static void cpu_migrations_perf_counter_update(struct perf_counter *counter)
1529{
1530 u64 prev, now;
1531 s64 delta;
1532
1533 prev = atomic64_read(&counter->hw.prev_count);
23a185ca 1534 now = get_cpu_migrations(counter);
6c594c21
IM
1535
1536 atomic64_set(&counter->hw.prev_count, now);
1537
1538 delta = now - prev;
6c594c21
IM
1539
1540 atomic64_add(delta, &counter->count);
1541}
1542
1543static void cpu_migrations_perf_counter_read(struct perf_counter *counter)
1544{
1545 cpu_migrations_perf_counter_update(counter);
1546}
1547
95cdd2e7 1548static int cpu_migrations_perf_counter_enable(struct perf_counter *counter)
6c594c21 1549{
23a185ca 1550 atomic64_set(&counter->hw.prev_count, get_cpu_migrations(counter));
95cdd2e7 1551 return 0;
6c594c21
IM
1552}
1553
1554static void cpu_migrations_perf_counter_disable(struct perf_counter *counter)
1555{
1556 cpu_migrations_perf_counter_update(counter);
1557}
1558
1559static const struct hw_perf_counter_ops perf_ops_cpu_migrations = {
7671581f
IM
1560 .enable = cpu_migrations_perf_counter_enable,
1561 .disable = cpu_migrations_perf_counter_disable,
1562 .read = cpu_migrations_perf_counter_read,
6c594c21
IM
1563};
1564
5c92d124
IM
1565static const struct hw_perf_counter_ops *
1566sw_perf_counter_init(struct perf_counter *counter)
1567{
1568 const struct hw_perf_counter_ops *hw_ops = NULL;
1569
1570 switch (counter->hw_event.type) {
1571 case PERF_COUNT_CPU_CLOCK:
1572 hw_ops = &perf_ops_cpu_clock;
1573 break;
bae43c99 1574 case PERF_COUNT_TASK_CLOCK:
23a185ca
PM
1575 /*
1576 * If the user instantiates this as a per-cpu counter,
1577 * use the cpu_clock counter instead.
1578 */
1579 if (counter->ctx->task)
1580 hw_ops = &perf_ops_task_clock;
1581 else
1582 hw_ops = &perf_ops_cpu_clock;
bae43c99 1583 break;
e06c61a8
IM
1584 case PERF_COUNT_PAGE_FAULTS:
1585 hw_ops = &perf_ops_page_faults;
1586 break;
5d6a27d8
IM
1587 case PERF_COUNT_CONTEXT_SWITCHES:
1588 hw_ops = &perf_ops_context_switches;
1589 break;
6c594c21
IM
1590 case PERF_COUNT_CPU_MIGRATIONS:
1591 hw_ops = &perf_ops_cpu_migrations;
1592 break;
5c92d124
IM
1593 default:
1594 break;
1595 }
1596 return hw_ops;
1597}
1598
0793a61d
TG
1599/*
1600 * Allocate and initialize a counter structure
1601 */
1602static struct perf_counter *
04289bb9
IM
1603perf_counter_alloc(struct perf_counter_hw_event *hw_event,
1604 int cpu,
23a185ca 1605 struct perf_counter_context *ctx,
9b51f66d
IM
1606 struct perf_counter *group_leader,
1607 gfp_t gfpflags)
0793a61d 1608{
5c92d124 1609 const struct hw_perf_counter_ops *hw_ops;
621a01ea 1610 struct perf_counter *counter;
0793a61d 1611
9b51f66d 1612 counter = kzalloc(sizeof(*counter), gfpflags);
0793a61d
TG
1613 if (!counter)
1614 return NULL;
1615
04289bb9
IM
1616 /*
1617 * Single counters are their own group leaders, with an
1618 * empty sibling list:
1619 */
1620 if (!group_leader)
1621 group_leader = counter;
1622
0793a61d 1623 mutex_init(&counter->mutex);
04289bb9
IM
1624 INIT_LIST_HEAD(&counter->list_entry);
1625 INIT_LIST_HEAD(&counter->sibling_list);
0793a61d
TG
1626 init_waitqueue_head(&counter->waitq);
1627
d859e29f
PM
1628 INIT_LIST_HEAD(&counter->child_list);
1629
9f66a381
IM
1630 counter->irqdata = &counter->data[0];
1631 counter->usrdata = &counter->data[1];
1632 counter->cpu = cpu;
1633 counter->hw_event = *hw_event;
1634 counter->wakeup_pending = 0;
04289bb9 1635 counter->group_leader = group_leader;
621a01ea 1636 counter->hw_ops = NULL;
23a185ca 1637 counter->ctx = ctx;
621a01ea 1638
235c7fc7 1639 counter->state = PERF_COUNTER_STATE_INACTIVE;
a86ed508
IM
1640 if (hw_event->disabled)
1641 counter->state = PERF_COUNTER_STATE_OFF;
1642
5c92d124
IM
1643 hw_ops = NULL;
1644 if (!hw_event->raw && hw_event->type < 0)
1645 hw_ops = sw_perf_counter_init(counter);
23a185ca 1646 else
5c92d124 1647 hw_ops = hw_perf_counter_init(counter);
5c92d124 1648
621a01ea
IM
1649 if (!hw_ops) {
1650 kfree(counter);
1651 return NULL;
1652 }
1653 counter->hw_ops = hw_ops;
0793a61d
TG
1654
1655 return counter;
1656}
1657
1658/**
9f66a381
IM
1659 * sys_perf_task_open - open a performance counter, associate it to a task/cpu
1660 *
1661 * @hw_event_uptr: event type attributes for monitoring/sampling
0793a61d 1662 * @pid: target pid
9f66a381
IM
1663 * @cpu: target cpu
1664 * @group_fd: group leader counter fd
0793a61d 1665 */
1d1c7ddb
IM
1666asmlinkage int
1667sys_perf_counter_open(struct perf_counter_hw_event *hw_event_uptr __user,
1668 pid_t pid, int cpu, int group_fd)
0793a61d 1669{
04289bb9 1670 struct perf_counter *counter, *group_leader;
9f66a381 1671 struct perf_counter_hw_event hw_event;
04289bb9 1672 struct perf_counter_context *ctx;
9b51f66d 1673 struct file *counter_file = NULL;
04289bb9
IM
1674 struct file *group_file = NULL;
1675 int fput_needed = 0;
9b51f66d 1676 int fput_needed2 = 0;
0793a61d
TG
1677 int ret;
1678
9f66a381 1679 if (copy_from_user(&hw_event, hw_event_uptr, sizeof(hw_event)) != 0)
eab656ae
TG
1680 return -EFAULT;
1681
04289bb9 1682 /*
ccff286d
IM
1683 * Get the target context (task or percpu):
1684 */
1685 ctx = find_get_context(pid, cpu);
1686 if (IS_ERR(ctx))
1687 return PTR_ERR(ctx);
1688
1689 /*
1690 * Look up the group leader (we will attach this counter to it):
04289bb9
IM
1691 */
1692 group_leader = NULL;
1693 if (group_fd != -1) {
1694 ret = -EINVAL;
1695 group_file = fget_light(group_fd, &fput_needed);
1696 if (!group_file)
ccff286d 1697 goto err_put_context;
04289bb9 1698 if (group_file->f_op != &perf_fops)
ccff286d 1699 goto err_put_context;
04289bb9
IM
1700
1701 group_leader = group_file->private_data;
1702 /*
ccff286d
IM
1703 * Do not allow a recursive hierarchy (this new sibling
1704 * becoming part of another group-sibling):
1705 */
1706 if (group_leader->group_leader != group_leader)
1707 goto err_put_context;
1708 /*
1709 * Do not allow to attach to a group in a different
1710 * task or CPU context:
04289bb9 1711 */
ccff286d
IM
1712 if (group_leader->ctx != ctx)
1713 goto err_put_context;
3b6f9e5c
PM
1714 /*
1715 * Only a group leader can be exclusive or pinned
1716 */
1717 if (hw_event.exclusive || hw_event.pinned)
1718 goto err_put_context;
04289bb9
IM
1719 }
1720
5c92d124 1721 ret = -EINVAL;
23a185ca
PM
1722 counter = perf_counter_alloc(&hw_event, cpu, ctx, group_leader,
1723 GFP_KERNEL);
0793a61d
TG
1724 if (!counter)
1725 goto err_put_context;
1726
0793a61d
TG
1727 ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
1728 if (ret < 0)
9b51f66d
IM
1729 goto err_free_put_context;
1730
1731 counter_file = fget_light(ret, &fput_needed2);
1732 if (!counter_file)
1733 goto err_free_put_context;
1734
1735 counter->filp = counter_file;
d859e29f 1736 mutex_lock(&ctx->mutex);
9b51f66d 1737 perf_install_in_context(ctx, counter, cpu);
d859e29f 1738 mutex_unlock(&ctx->mutex);
9b51f66d
IM
1739
1740 fput_light(counter_file, fput_needed2);
0793a61d 1741
04289bb9
IM
1742out_fput:
1743 fput_light(group_file, fput_needed);
1744
0793a61d
TG
1745 return ret;
1746
9b51f66d 1747err_free_put_context:
0793a61d
TG
1748 kfree(counter);
1749
1750err_put_context:
1751 put_context(ctx);
1752
04289bb9 1753 goto out_fput;
0793a61d
TG
1754}
1755
9b51f66d
IM
1756/*
1757 * Initialize the perf_counter context in a task_struct:
1758 */
1759static void
1760__perf_counter_init_context(struct perf_counter_context *ctx,
1761 struct task_struct *task)
1762{
1763 memset(ctx, 0, sizeof(*ctx));
1764 spin_lock_init(&ctx->lock);
d859e29f 1765 mutex_init(&ctx->mutex);
9b51f66d
IM
1766 INIT_LIST_HEAD(&ctx->counter_list);
1767 ctx->task = task;
1768}
1769
1770/*
1771 * inherit a counter from parent task to child task:
1772 */
d859e29f 1773static struct perf_counter *
9b51f66d
IM
1774inherit_counter(struct perf_counter *parent_counter,
1775 struct task_struct *parent,
1776 struct perf_counter_context *parent_ctx,
1777 struct task_struct *child,
d859e29f 1778 struct perf_counter *group_leader,
9b51f66d
IM
1779 struct perf_counter_context *child_ctx)
1780{
1781 struct perf_counter *child_counter;
1782
d859e29f
PM
1783 /*
1784 * Instead of creating recursive hierarchies of counters,
1785 * we link inherited counters back to the original parent,
1786 * which has a filp for sure, which we use as the reference
1787 * count:
1788 */
1789 if (parent_counter->parent)
1790 parent_counter = parent_counter->parent;
1791
9b51f66d 1792 child_counter = perf_counter_alloc(&parent_counter->hw_event,
23a185ca
PM
1793 parent_counter->cpu, child_ctx,
1794 group_leader, GFP_KERNEL);
9b51f66d 1795 if (!child_counter)
d859e29f 1796 return NULL;
9b51f66d
IM
1797
1798 /*
1799 * Link it up in the child's context:
1800 */
9b51f66d
IM
1801 child_counter->task = child;
1802 list_add_counter(child_counter, child_ctx);
1803 child_ctx->nr_counters++;
1804
1805 child_counter->parent = parent_counter;
9b51f66d
IM
1806 /*
1807 * inherit into child's child as well:
1808 */
1809 child_counter->hw_event.inherit = 1;
1810
1811 /*
1812 * Get a reference to the parent filp - we will fput it
1813 * when the child counter exits. This is safe to do because
1814 * we are in the parent and we know that the filp still
1815 * exists and has a nonzero count:
1816 */
1817 atomic_long_inc(&parent_counter->filp->f_count);
1818
d859e29f
PM
1819 /*
1820 * Link this into the parent counter's child list
1821 */
1822 mutex_lock(&parent_counter->mutex);
1823 list_add_tail(&child_counter->child_list, &parent_counter->child_list);
1824
1825 /*
1826 * Make the child state follow the state of the parent counter,
1827 * not its hw_event.disabled bit. We hold the parent's mutex,
1828 * so we won't race with perf_counter_{en,dis}able_family.
1829 */
1830 if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE)
1831 child_counter->state = PERF_COUNTER_STATE_INACTIVE;
1832 else
1833 child_counter->state = PERF_COUNTER_STATE_OFF;
1834
1835 mutex_unlock(&parent_counter->mutex);
1836
1837 return child_counter;
1838}
1839
1840static int inherit_group(struct perf_counter *parent_counter,
1841 struct task_struct *parent,
1842 struct perf_counter_context *parent_ctx,
1843 struct task_struct *child,
1844 struct perf_counter_context *child_ctx)
1845{
1846 struct perf_counter *leader;
1847 struct perf_counter *sub;
1848
1849 leader = inherit_counter(parent_counter, parent, parent_ctx,
1850 child, NULL, child_ctx);
1851 if (!leader)
1852 return -ENOMEM;
1853 list_for_each_entry(sub, &parent_counter->sibling_list, list_entry) {
1854 if (!inherit_counter(sub, parent, parent_ctx,
1855 child, leader, child_ctx))
1856 return -ENOMEM;
1857 }
9b51f66d
IM
1858 return 0;
1859}
1860
d859e29f
PM
1861static void sync_child_counter(struct perf_counter *child_counter,
1862 struct perf_counter *parent_counter)
1863{
1864 u64 parent_val, child_val;
1865
1866 parent_val = atomic64_read(&parent_counter->count);
1867 child_val = atomic64_read(&child_counter->count);
1868
1869 /*
1870 * Add back the child's count to the parent's count:
1871 */
1872 atomic64_add(child_val, &parent_counter->count);
1873
1874 /*
1875 * Remove this counter from the parent's list
1876 */
1877 mutex_lock(&parent_counter->mutex);
1878 list_del_init(&child_counter->child_list);
1879 mutex_unlock(&parent_counter->mutex);
1880
1881 /*
1882 * Release the parent counter, if this was the last
1883 * reference to it.
1884 */
1885 fput(parent_counter->filp);
1886}
1887
9b51f66d
IM
1888static void
1889__perf_counter_exit_task(struct task_struct *child,
1890 struct perf_counter *child_counter,
1891 struct perf_counter_context *child_ctx)
1892{
1893 struct perf_counter *parent_counter;
d859e29f 1894 struct perf_counter *sub, *tmp;
9b51f66d
IM
1895
1896 /*
235c7fc7
IM
1897 * If we do not self-reap then we have to wait for the
1898 * child task to unschedule (it will happen for sure),
1899 * so that its counter is at its final count. (This
1900 * condition triggers rarely - child tasks usually get
1901 * off their CPU before the parent has a chance to
1902 * get this far into the reaping action)
9b51f66d 1903 */
235c7fc7
IM
1904 if (child != current) {
1905 wait_task_inactive(child, 0);
1906 list_del_init(&child_counter->list_entry);
1907 } else {
0cc0c027 1908 struct perf_cpu_context *cpuctx;
235c7fc7
IM
1909 unsigned long flags;
1910 u64 perf_flags;
1911
1912 /*
1913 * Disable and unlink this counter.
1914 *
1915 * Be careful about zapping the list - IRQ/NMI context
1916 * could still be processing it:
1917 */
1918 curr_rq_lock_irq_save(&flags);
1919 perf_flags = hw_perf_save_disable();
0cc0c027
IM
1920
1921 cpuctx = &__get_cpu_var(perf_cpu_context);
1922
d859e29f 1923 group_sched_out(child_counter, cpuctx, child_ctx);
0cc0c027 1924
235c7fc7 1925 list_del_init(&child_counter->list_entry);
0cc0c027 1926
235c7fc7 1927 child_ctx->nr_counters--;
9b51f66d 1928
235c7fc7
IM
1929 hw_perf_restore(perf_flags);
1930 curr_rq_unlock_irq_restore(&flags);
1931 }
9b51f66d
IM
1932
1933 parent_counter = child_counter->parent;
1934 /*
1935 * It can happen that parent exits first, and has counters
1936 * that are still around due to the child reference. These
1937 * counters need to be zapped - but otherwise linger.
1938 */
d859e29f
PM
1939 if (parent_counter) {
1940 sync_child_counter(child_counter, parent_counter);
1941 list_for_each_entry_safe(sub, tmp, &child_counter->sibling_list,
1942 list_entry) {
1943 if (sub->parent)
1944 sync_child_counter(sub, sub->parent);
1945 kfree(sub);
1946 }
1947 }
9b51f66d 1948
65d37086
MG
1949 if (!child_counter->filp || !atomic_long_read(&child_counter->filp->f_count))
1950 kfree(child_counter);
9b51f66d
IM
1951}
1952
1953/*
d859e29f 1954 * When a child task exits, feed back counter values to parent counters.
9b51f66d 1955 *
d859e29f 1956 * Note: we may be running in child context, but the PID is not hashed
9b51f66d
IM
1957 * anymore so new counters will not be added.
1958 */
1959void perf_counter_exit_task(struct task_struct *child)
1960{
1961 struct perf_counter *child_counter, *tmp;
1962 struct perf_counter_context *child_ctx;
1963
1964 child_ctx = &child->perf_counter_ctx;
1965
1966 if (likely(!child_ctx->nr_counters))
1967 return;
1968
1969 list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
1970 list_entry)
1971 __perf_counter_exit_task(child, child_counter, child_ctx);
1972}
1973
1974/*
1975 * Initialize the perf_counter context in task_struct
1976 */
1977void perf_counter_init_task(struct task_struct *child)
1978{
1979 struct perf_counter_context *child_ctx, *parent_ctx;
d859e29f 1980 struct perf_counter *counter;
9b51f66d 1981 struct task_struct *parent = current;
9b51f66d
IM
1982
1983 child_ctx = &child->perf_counter_ctx;
1984 parent_ctx = &parent->perf_counter_ctx;
1985
1986 __perf_counter_init_context(child_ctx, child);
1987
1988 /*
1989 * This is executed from the parent task context, so inherit
1990 * counters that have been marked for cloning:
1991 */
1992
1993 if (likely(!parent_ctx->nr_counters))
1994 return;
1995
1996 /*
1997 * Lock the parent list. No need to lock the child - not PID
1998 * hashed yet and not running, so nobody can access it.
1999 */
d859e29f 2000 mutex_lock(&parent_ctx->mutex);
9b51f66d
IM
2001
2002 /*
2003 * We dont have to disable NMIs - we are only looking at
2004 * the list, not manipulating it:
2005 */
2006 list_for_each_entry(counter, &parent_ctx->counter_list, list_entry) {
d859e29f 2007 if (!counter->hw_event.inherit)
9b51f66d
IM
2008 continue;
2009
d859e29f 2010 if (inherit_group(counter, parent,
9b51f66d
IM
2011 parent_ctx, child, child_ctx))
2012 break;
2013 }
2014
d859e29f 2015 mutex_unlock(&parent_ctx->mutex);
9b51f66d
IM
2016}
2017
04289bb9 2018static void __cpuinit perf_counter_init_cpu(int cpu)
0793a61d 2019{
04289bb9 2020 struct perf_cpu_context *cpuctx;
0793a61d 2021
04289bb9
IM
2022 cpuctx = &per_cpu(perf_cpu_context, cpu);
2023 __perf_counter_init_context(&cpuctx->ctx, NULL);
0793a61d
TG
2024
2025 mutex_lock(&perf_resource_mutex);
04289bb9 2026 cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
0793a61d 2027 mutex_unlock(&perf_resource_mutex);
04289bb9 2028
01d0287f 2029 hw_perf_counter_setup(cpu);
0793a61d
TG
2030}
2031
2032#ifdef CONFIG_HOTPLUG_CPU
04289bb9 2033static void __perf_counter_exit_cpu(void *info)
0793a61d
TG
2034{
2035 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
2036 struct perf_counter_context *ctx = &cpuctx->ctx;
2037 struct perf_counter *counter, *tmp;
2038
04289bb9
IM
2039 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
2040 __perf_counter_remove_from_context(counter);
0793a61d 2041}
04289bb9 2042static void perf_counter_exit_cpu(int cpu)
0793a61d 2043{
d859e29f
PM
2044 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
2045 struct perf_counter_context *ctx = &cpuctx->ctx;
2046
2047 mutex_lock(&ctx->mutex);
04289bb9 2048 smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
d859e29f 2049 mutex_unlock(&ctx->mutex);
0793a61d
TG
2050}
2051#else
04289bb9 2052static inline void perf_counter_exit_cpu(int cpu) { }
0793a61d
TG
2053#endif
2054
2055static int __cpuinit
2056perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
2057{
2058 unsigned int cpu = (long)hcpu;
2059
2060 switch (action) {
2061
2062 case CPU_UP_PREPARE:
2063 case CPU_UP_PREPARE_FROZEN:
04289bb9 2064 perf_counter_init_cpu(cpu);
0793a61d
TG
2065 break;
2066
2067 case CPU_DOWN_PREPARE:
2068 case CPU_DOWN_PREPARE_FROZEN:
04289bb9 2069 perf_counter_exit_cpu(cpu);
0793a61d
TG
2070 break;
2071
2072 default:
2073 break;
2074 }
2075
2076 return NOTIFY_OK;
2077}
2078
2079static struct notifier_block __cpuinitdata perf_cpu_nb = {
2080 .notifier_call = perf_cpu_notify,
2081};
2082
2083static int __init perf_counter_init(void)
2084{
2085 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
2086 (void *)(long)smp_processor_id());
2087 register_cpu_notifier(&perf_cpu_nb);
2088
2089 return 0;
2090}
2091early_initcall(perf_counter_init);
2092
2093static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
2094{
2095 return sprintf(buf, "%d\n", perf_reserved_percpu);
2096}
2097
2098static ssize_t
2099perf_set_reserve_percpu(struct sysdev_class *class,
2100 const char *buf,
2101 size_t count)
2102{
2103 struct perf_cpu_context *cpuctx;
2104 unsigned long val;
2105 int err, cpu, mpt;
2106
2107 err = strict_strtoul(buf, 10, &val);
2108 if (err)
2109 return err;
2110 if (val > perf_max_counters)
2111 return -EINVAL;
2112
2113 mutex_lock(&perf_resource_mutex);
2114 perf_reserved_percpu = val;
2115 for_each_online_cpu(cpu) {
2116 cpuctx = &per_cpu(perf_cpu_context, cpu);
2117 spin_lock_irq(&cpuctx->ctx.lock);
2118 mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
2119 perf_max_counters - perf_reserved_percpu);
2120 cpuctx->max_pertask = mpt;
2121 spin_unlock_irq(&cpuctx->ctx.lock);
2122 }
2123 mutex_unlock(&perf_resource_mutex);
2124
2125 return count;
2126}
2127
2128static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
2129{
2130 return sprintf(buf, "%d\n", perf_overcommit);
2131}
2132
2133static ssize_t
2134perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
2135{
2136 unsigned long val;
2137 int err;
2138
2139 err = strict_strtoul(buf, 10, &val);
2140 if (err)
2141 return err;
2142 if (val > 1)
2143 return -EINVAL;
2144
2145 mutex_lock(&perf_resource_mutex);
2146 perf_overcommit = val;
2147 mutex_unlock(&perf_resource_mutex);
2148
2149 return count;
2150}
2151
2152static SYSDEV_CLASS_ATTR(
2153 reserve_percpu,
2154 0644,
2155 perf_show_reserve_percpu,
2156 perf_set_reserve_percpu
2157 );
2158
2159static SYSDEV_CLASS_ATTR(
2160 overcommit,
2161 0644,
2162 perf_show_overcommit,
2163 perf_set_overcommit
2164 );
2165
2166static struct attribute *perfclass_attrs[] = {
2167 &attr_reserve_percpu.attr,
2168 &attr_overcommit.attr,
2169 NULL
2170};
2171
2172static struct attribute_group perfclass_attr_group = {
2173 .attrs = perfclass_attrs,
2174 .name = "perf_counters",
2175};
2176
2177static int __init perf_counter_sysfs_init(void)
2178{
2179 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
2180 &perfclass_attr_group);
2181}
2182device_initcall(perf_counter_sysfs_init);