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