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