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