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